Commit d619f5c0 authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

move transaction service to its own package (#812)

parent 0107aa44
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mock
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/crypto/eip712"
)
type signerMock struct {
signTx func(transaction *types.Transaction) (*types.Transaction, error)
signTypedData func(*eip712.TypedData) ([]byte, error)
}
func (*signerMock) EthereumAddress() (common.Address, error) {
return common.Address{}, nil
}
func (*signerMock) Sign(data []byte) ([]byte, error) {
return nil, nil
}
func (m *signerMock) SignTx(transaction *types.Transaction) (*types.Transaction, error) {
return m.signTx(transaction)
}
func (*signerMock) PublicKey() (*ecdsa.PublicKey, error) {
return nil, nil
}
func (m *signerMock) SignTypedData(d *eip712.TypedData) ([]byte, error) {
return m.signTypedData(d)
}
func New(opts ...Option) crypto.Signer {
mock := new(signerMock)
for _, o := range opts {
o.apply(mock)
}
return mock
}
// Option is the option passed to the mock Chequebook service
type Option interface {
apply(*signerMock)
}
type optionFunc func(*signerMock)
func (f optionFunc) apply(r *signerMock) { f(r) }
func WithSignTxFunc(f func(transaction *types.Transaction) (*types.Transaction, error)) Option {
return optionFunc(func(s *signerMock) {
s.signTx = f
})
}
func WithSignTypedDataFunc(f func(*eip712.TypedData) ([]byte, error)) Option {
return optionFunc(func(s *signerMock) {
s.signTypedData = f
})
}
......@@ -48,6 +48,7 @@ import (
"github.com/ethersphere/bee/pkg/settlement/swap"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/swapprotocol"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/soc"
"github.com/ethersphere/bee/pkg/statestore/leveldb"
mockinmem "github.com/ethersphere/bee/pkg/statestore/mock"
......@@ -161,7 +162,7 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
if err != nil {
return nil, err
}
transactionService, err := chequebook.NewTransactionService(logger, swapBackend, signer)
transactionService, err := transaction.NewService(logger, swapBackend, signer)
if err != nil {
return nil, err
}
......
......@@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/sw3-bindings/v2/simpleswapfactory"
)
......@@ -34,8 +35,8 @@ type CashoutService interface {
type cashoutService struct {
store storage.StateStorer
simpleSwapBindingFunc SimpleSwapBindingFunc
backend Backend
transactionService TransactionService
backend transaction.Backend
transactionService transaction.Service
chequebookABI abi.ABI
chequeStore ChequeStore
}
......@@ -69,8 +70,8 @@ type cashoutAction struct {
func NewCashoutService(
store storage.StateStorer,
simpleSwapBindingFunc SimpleSwapBindingFunc,
backend Backend,
transactionService TransactionService,
backend transaction.Backend,
transactionService transaction.Service,
chequeStore ChequeStore,
) (CashoutService, error) {
chequebookABI, err := abi.JSON(strings.NewReader(simpleswapfactory.ERC20SimpleSwapABI))
......@@ -105,7 +106,7 @@ func (s *cashoutService) CashCheque(ctx context.Context, chequebook common.Addre
return common.Hash{}, err
}
request := &TxRequest{
request := &transaction.TxRequest{
To: chequebook,
Data: callData,
GasPrice: nil,
......
......@@ -15,7 +15,9 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
chequestoremock "github.com/ethersphere/bee/pkg/settlement/swap/chequestore/mock"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction/backendmock"
transactionmock "github.com/ethersphere/bee/pkg/settlement/swap/transaction/mock"
storemock "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/sw3-bindings/v2/simpleswapfactory"
)
......@@ -79,8 +81,8 @@ func TestCashout(t *testing.T) {
}, nil
}),
),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) {
if request.To != chequebookAddress {
t.Fatalf("sending to wrong contract. wanted %x, got %x", chequebookAddress, request.To)
}
......@@ -88,8 +90,8 @@ func TestCashout(t *testing.T) {
t.Fatal("sending ether to chequebook contract")
}
return txHash, nil
},
},
}),
),
chequestoremock.NewChequeStore(
chequestoremock.WithLastChequeFunc(func(c common.Address) (*chequebook.SignedCheque, error) {
if c != chequebookAddress {
......@@ -218,8 +220,8 @@ func TestCashoutBounced(t *testing.T) {
}, nil
}),
),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) {
if request.To != chequebookAddress {
t.Fatalf("sending to wrong contract. wanted %x, got %x", chequebookAddress, request.To)
}
......@@ -227,8 +229,8 @@ func TestCashoutBounced(t *testing.T) {
t.Fatal("sending ether to chequebook contract")
}
return txHash, nil
},
},
}),
),
chequestoremock.NewChequeStore(
chequestoremock.WithLastChequeFunc(func(c common.Address) (*chequebook.SignedCheque, error) {
if c != chequebookAddress {
......@@ -324,11 +326,11 @@ func TestCashoutStatusReverted(t *testing.T) {
}, nil
}),
),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest) (common.Hash, error) {
return txHash, nil
},
},
}),
),
chequestoremock.NewChequeStore(
chequestoremock.WithLastChequeFunc(func(c common.Address) (*chequebook.SignedCheque, error) {
if c != chequebookAddress {
......@@ -398,11 +400,11 @@ func TestCashoutStatusPending(t *testing.T) {
return nil, true, nil
}),
),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) {
return txHash, nil
},
},
}),
),
chequestoremock.NewChequeStore(
chequestoremock.WithLastChequeFunc(func(c common.Address) (*chequebook.SignedCheque, error) {
if c != chequebookAddress {
......
......@@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/crypto/eip712"
signermock "github.com/ethersphere/bee/pkg/crypto/mock"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
)
......@@ -29,8 +30,8 @@ func TestSignCheque(t *testing.T) {
CumulativePayout: cumulativePayout,
}
signer := &signerMock{
signTypedData: func(data *eip712.TypedData) ([]byte, error) {
signer := signermock.New(
signermock.WithSignTypedDataFunc(func(data *eip712.TypedData) ([]byte, error) {
if data.Message["beneficiary"].(string) != beneficiaryAddress.Hex() {
t.Fatal("signing cheque with wrong beneficiary")
......@@ -45,8 +46,8 @@ func TestSignCheque(t *testing.T) {
}
return signature, nil
},
}
}),
)
chequeSigner := chequebook.NewChequeSigner(signer, chainId)
......
......@@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/sw3-bindings/v2/simpleswapfactory"
)
......@@ -58,8 +59,8 @@ type Service interface {
type service struct {
lock sync.Mutex
backend Backend
transactionService TransactionService
backend transaction.Backend
transactionService transaction.Service
address common.Address
chequebookABI abi.ABI
......@@ -75,7 +76,7 @@ type service struct {
}
// New creates a new chequebook service for the provided chequebook contract.
func New(backend Backend, transactionService TransactionService, address, erc20Address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, simpleSwapBindingFunc SimpleSwapBindingFunc, erc20BindingFunc ERC20BindingFunc) (Service, error) {
func New(backend transaction.Backend, transactionService transaction.Service, address, erc20Address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, simpleSwapBindingFunc SimpleSwapBindingFunc, erc20BindingFunc ERC20BindingFunc) (Service, error) {
chequebookABI, err := abi.JSON(strings.NewReader(simpleswapfactory.ERC20SimpleSwapABI))
if err != nil {
return nil, err
......@@ -135,7 +136,7 @@ func (s *service) Deposit(ctx context.Context, amount *big.Int) (hash common.Has
return common.Hash{}, err
}
request := &TxRequest{
request := &transaction.TxRequest{
To: s.erc20Address,
Data: callData,
GasPrice: nil,
......@@ -191,7 +192,7 @@ func (s *service) WaitForDeposit(ctx context.Context, txHash common.Hash) error
return err
}
if receipt.Status != 1 {
return ErrTransactionReverted
return transaction.ErrTransactionReverted
}
return nil
}
......@@ -346,7 +347,7 @@ func (s *service) Withdraw(ctx context.Context, amount *big.Int) (hash common.Ha
return common.Hash{}, err
}
request := &TxRequest{
request := &transaction.TxRequest{
To: s.address,
Data: callData,
GasPrice: nil,
......
......@@ -14,15 +14,17 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction/backendmock"
transactionmock "github.com/ethersphere/bee/pkg/settlement/swap/transaction/mock"
storemock "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/storage"
)
func newTestChequebook(
t *testing.T,
backend chequebook.Backend,
transactionService chequebook.TransactionService,
backend transaction.Backend,
transactionService transaction.Service,
address,
erc20address,
ownerAdress common.Address,
......@@ -66,7 +68,7 @@ func TestChequebookAddress(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
address,
erc20address,
ownerAdress,
......@@ -91,7 +93,7 @@ func TestChequebookBalance(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
address,
erc20address,
ownerAdress,
......@@ -127,8 +129,8 @@ func TestChequebookDeposit(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) {
if request.To != erc20address {
t.Fatalf("sending to wrong contract. wanted %x, got %x", erc20address, request.To)
}
......@@ -136,8 +138,8 @@ func TestChequebookDeposit(t *testing.T) {
t.Fatal("sending ether to token contract")
}
return txHash, nil
},
},
}),
),
address,
erc20address,
ownerAdress,
......@@ -174,16 +176,16 @@ func TestChequebookWaitForDeposit(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{
waitForReceipt: func(ctx context.Context, tx common.Hash) (*types.Receipt, error) {
transactionmock.New(
transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, tx common.Hash) (*types.Receipt, error) {
if tx != txHash {
t.Fatalf("waiting for wrong transaction. wanted %x, got %x", txHash, tx)
}
return &types.Receipt{
Status: 1,
}, nil
},
},
}),
),
address,
erc20address,
ownerAdress,
......@@ -209,16 +211,16 @@ func TestChequebookWaitForDepositReverted(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{
waitForReceipt: func(ctx context.Context, tx common.Hash) (*types.Receipt, error) {
transactionmock.New(
transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, tx common.Hash) (*types.Receipt, error) {
if tx != txHash {
t.Fatalf("waiting for wrong transaction. wanted %x, got %x", txHash, tx)
}
return &types.Receipt{
Status: 0,
}, nil
},
},
}),
),
address,
erc20address,
ownerAdress,
......@@ -234,8 +236,8 @@ func TestChequebookWaitForDepositReverted(t *testing.T) {
if err == nil {
t.Fatal("expected reverted error")
}
if !errors.Is(err, chequebook.ErrTransactionReverted) {
t.Fatalf("wrong error. wanted %v, got %v", chequebook.ErrTransactionReverted, err)
if !errors.Is(err, transaction.ErrTransactionReverted) {
t.Fatalf("wrong error. wanted %v, got %v", transaction.ErrTransactionReverted, err)
}
}
......@@ -254,7 +256,7 @@ func TestChequebookIssue(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
address,
erc20address,
ownerAdress,
......@@ -405,7 +407,7 @@ func TestChequebookIssueErrorSend(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
address,
erc20address,
ownerAdress,
......@@ -453,7 +455,7 @@ func TestChequebookIssueOutOfFunds(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
address,
erc20address,
ownerAdress,
......@@ -498,8 +500,8 @@ func TestChequebookWithdraw(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) {
if request.To != address {
t.Fatalf("sending to wrong contract. wanted %x, got %x", address, request.To)
}
......@@ -507,8 +509,8 @@ func TestChequebookWithdraw(t *testing.T) {
t.Fatal("sending ether to token contract")
}
return txHash, nil
},
},
}),
),
address,
erc20address,
ownerAdress,
......@@ -555,8 +557,8 @@ func TestChequebookWithdrawInsufficientFunds(t *testing.T) {
chequebookService, err := newTestChequebook(
t,
backendmock.New(),
&transactionServiceMock{
send: func(c context.Context, request *chequebook.TxRequest) (common.Hash, error) {
transactionmock.New(
transactionmock.WithSendFunc(func(c context.Context, request *transaction.TxRequest) (common.Hash, error) {
if request.To != address {
t.Fatalf("sending to wrong contract. wanted %x, got %x", address, request.To)
}
......@@ -564,8 +566,8 @@ func TestChequebookWithdrawInsufficientFunds(t *testing.T) {
t.Fatal("sending ether to token contract")
}
return txHash, nil
},
},
}),
),
address,
erc20address,
ownerAdress,
......
......@@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/storage"
)
......@@ -48,7 +49,7 @@ type chequeStore struct {
factory Factory
chaindID int64
simpleSwapBindingFunc SimpleSwapBindingFunc
backend Backend
backend transaction.Backend
beneficiary common.Address // the beneficiary we expect in cheques sent to us
recoverChequeFunc RecoverChequeFunc
}
......@@ -58,7 +59,7 @@ type RecoverChequeFunc func(cheque *SignedCheque, chainID int64) (common.Address
// NewChequeStore creates new ChequeStore
func NewChequeStore(
store storage.StateStorer,
backend Backend,
backend transaction.Backend,
factory Factory,
chainID int64,
beneficiary common.Address,
......
......@@ -6,30 +6,15 @@ package chequebook_test
import (
"context"
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto/eip712"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/sw3-bindings/v2/simpleswapfactory"
)
type transactionServiceMock struct {
send func(ctx context.Context, request *chequebook.TxRequest) (txHash common.Hash, err error)
waitForReceipt func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error)
}
func (m *transactionServiceMock) Send(ctx context.Context, request *chequebook.TxRequest) (txHash common.Hash, err error) {
return m.send(ctx, request)
}
func (m *transactionServiceMock) WaitForReceipt(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
return m.waitForReceipt(ctx, txHash)
}
type simpleSwapFactoryBindingMock struct {
erc20Address func(*bind.CallOpts) (common.Address, error)
deployedContracts func(*bind.CallOpts, common.Address) (bool, error)
......@@ -88,31 +73,6 @@ func (m *erc20BindingMock) BalanceOf(o *bind.CallOpts, a common.Address) (*big.I
return m.balanceOf(o, a)
}
type signerMock struct {
signTx func(transaction *types.Transaction) (*types.Transaction, error)
signTypedData func(*eip712.TypedData) ([]byte, error)
}
func (*signerMock) EthereumAddress() (common.Address, error) {
return common.Address{}, nil
}
func (*signerMock) Sign(data []byte) ([]byte, error) {
return nil, nil
}
func (m *signerMock) SignTx(transaction *types.Transaction) (*types.Transaction, error) {
return m.signTx(transaction)
}
func (*signerMock) PublicKey() (*ecdsa.PublicKey, error) {
return nil, nil
}
func (m *signerMock) SignTypedData(d *eip712.TypedData) ([]byte, error) {
return m.signTypedData(d)
}
type chequeSignerMock struct {
sign func(cheque *chequebook.Cheque) ([]byte, error)
}
......
......@@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/sw3-bindings/v2/simpleswapfactory"
"golang.org/x/net/context"
)
......@@ -38,8 +39,8 @@ type Factory interface {
}
type factory struct {
backend Backend
transactionService TransactionService
backend transaction.Backend
transactionService transaction.Service
address common.Address
ABI abi.ABI
......@@ -47,7 +48,7 @@ type factory struct {
}
// NewFactory creates a new factory service for the provided factory contract.
func NewFactory(backend Backend, transactionService TransactionService, address common.Address, simpleSwapFactoryBindingFunc SimpleSwapFactoryBindingFunc) (Factory, error) {
func NewFactory(backend transaction.Backend, transactionService transaction.Service, address common.Address, simpleSwapFactoryBindingFunc SimpleSwapFactoryBindingFunc) (Factory, error) {
ABI, err := abi.JSON(strings.NewReader(simpleswapfactory.SimpleSwapFactoryABI))
if err != nil {
return nil, err
......@@ -74,7 +75,7 @@ func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHard
return common.Hash{}, err
}
request := &TxRequest{
request := &transaction.TxRequest{
To: c.address,
Data: callData,
GasPrice: nil,
......@@ -108,7 +109,7 @@ func (c *factory) WaitDeployed(ctx context.Context, txHash common.Hash) (common.
// parseDeployReceipt parses the address of the deployed chequebook from the receipt.
func (c *factory) parseDeployReceipt(receipt *types.Receipt) (address common.Address, err error) {
if receipt.Status != 1 {
return common.Address{}, ErrTransactionReverted
return common.Address{}, transaction.ErrTransactionReverted
}
for _, log := range receipt.Logs {
if log.Address != c.address {
......
......@@ -15,11 +15,13 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction/backendmock"
transactionmock "github.com/ethersphere/bee/pkg/settlement/swap/transaction/mock"
"github.com/ethersphere/sw3-bindings/v2/simpleswapfactory"
)
func newTestFactory(t *testing.T, factoryAddress common.Address, backend chequebook.Backend, transactionService chequebook.TransactionService, simpleSwapFactoryBinding chequebook.SimpleSwapFactoryBinding) (chequebook.Factory, error) {
func newTestFactory(t *testing.T, factoryAddress common.Address, backend transaction.Backend, transactionService transaction.Service, simpleSwapFactoryBinding chequebook.SimpleSwapFactoryBinding) (chequebook.Factory, error) {
return chequebook.NewFactory(backend, transactionService, factoryAddress,
func(addr common.Address, b bind.ContractBackend) (chequebook.SimpleSwapFactoryBinding, error) {
if addr != factoryAddress {
......@@ -39,7 +41,7 @@ func TestFactoryERC20Address(t *testing.T) {
t,
factoryAddress,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
&simpleSwapFactoryBindingMock{
erc20Address: func(*bind.CallOpts) (common.Address, error) {
return erc20Address, nil
......@@ -75,7 +77,7 @@ func TestFactoryVerifySelf(t *testing.T) {
return common.FromHex(simpleswapfactory.SimpleSwapFactoryDeployedCode), nil
}),
),
&transactionServiceMock{},
transactionmock.New(),
&simpleSwapFactoryBindingMock{})
if err != nil {
t.Fatal(err)
......@@ -103,7 +105,7 @@ func TestFactoryVerifySelfInvalidCode(t *testing.T) {
return common.FromHex(simpleswapfactory.AddressBin), nil
}),
),
&transactionServiceMock{},
transactionmock.New(),
&simpleSwapFactoryBindingMock{})
if err != nil {
t.Fatal(err)
......@@ -125,7 +127,7 @@ func TestFactoryVerifyChequebook(t *testing.T) {
t,
factoryAddress,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
&simpleSwapFactoryBindingMock{
deployedContracts: func(o *bind.CallOpts, address common.Address) (bool, error) {
if address != chequebookAddress {
......@@ -151,7 +153,7 @@ func TestFactoryVerifyChequebookInvalid(t *testing.T) {
t,
factoryAddress,
backendmock.New(),
&transactionServiceMock{},
transactionmock.New(),
&simpleSwapFactoryBindingMock{
deployedContracts: func(o *bind.CallOpts, address common.Address) (bool, error) {
if address != chequebookAddress {
......@@ -184,8 +186,8 @@ func TestFactoryDeploy(t *testing.T) {
t,
factoryAddress,
backendmock.New(),
&transactionServiceMock{
send: func(ctx context.Context, request *chequebook.TxRequest) (txHash common.Hash, err error) {
transactionmock.New(
transactionmock.WithSendFunc(func(ctx context.Context, request *transaction.TxRequest) (txHash common.Hash, err error) {
if request.To != factoryAddress {
t.Fatalf("sending to wrong address. wanted %x, got %x", factoryAddress, request.To)
}
......@@ -193,8 +195,8 @@ func TestFactoryDeploy(t *testing.T) {
t.Fatal("trying to send ether")
}
return deployTransactionHash, nil
},
waitForReceipt: func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
}),
transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
if txHash != deployTransactionHash {
t.Fatalf("waiting for wrong transaction. wanted %x, got %x", deployTransactionHash, txHash)
}
......@@ -210,8 +212,8 @@ func TestFactoryDeploy(t *testing.T) {
},
},
}, nil
},
},
}),
),
&simpleSwapFactoryBindingMock{
parseSimpleSwapDeployed: func(log types.Log) (*simpleswapfactory.SimpleSwapFactorySimpleSwapDeployed, error) {
if !bytes.Equal(log.Data, logData) {
......@@ -252,17 +254,18 @@ func TestFactoryDeployReverted(t *testing.T) {
t,
factoryAddress,
backendmock.New(),
&transactionServiceMock{
waitForReceipt: func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
transactionmock.New(
transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
if txHash != deployTransactionHash {
t.Fatalf("waiting for wrong transaction. wanted %x, got %x", deployTransactionHash, txHash)
}
return &types.Receipt{
Status: 0,
}, nil
},
},
&simpleSwapFactoryBindingMock{})
}),
),
&simpleSwapFactoryBindingMock{},
)
if err != nil {
t.Fatal(err)
}
......@@ -271,7 +274,7 @@ func TestFactoryDeployReverted(t *testing.T) {
if err == nil {
t.Fatal("returned failed chequebook deployment")
}
if !errors.Is(err, chequebook.ErrTransactionReverted) {
t.Fatalf("wrong error. wanted %v, got %v", chequebook.ErrTransactionReverted, err)
if !errors.Is(err, transaction.ErrTransactionReverted) {
t.Fatalf("wrong error. wanted %v, got %v", transaction.ErrTransactionReverted, err)
}
}
......@@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/storage"
)
......@@ -24,8 +25,8 @@ func Init(
stateStore storage.StateStorer,
logger logging.Logger,
swapInitialDeposit uint64,
transactionService TransactionService,
swapBackend Backend,
transactionService transaction.Service,
swapBackend transaction.Backend,
chainId int64,
overlayEthAddress common.Address,
chequeSigner ChequeSigner,
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package transaction
import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
// Backend is the minimum of blockchain backend functions we need.
type Backend interface {
bind.ContractBackend
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
}
......@@ -12,7 +12,7 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
)
type backendMock struct {
......@@ -29,68 +29,68 @@ func (m *backendMock) CodeAt(ctx context.Context, contract common.Address, block
if m.codeAt != nil {
return m.codeAt(ctx, contract, blockNumber)
}
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (*backendMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (*backendMock) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (m *backendMock) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
if m.pendingNonceAt != nil {
return m.pendingNonceAt(ctx, account)
}
return 0, errors.New("Error")
return 0, errors.New("not implemented")
}
func (m *backendMock) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
if m.suggestGasPrice != nil {
return m.suggestGasPrice(ctx)
}
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (m *backendMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
if m.estimateGas != nil {
return m.estimateGas(ctx, call)
}
return 0, errors.New("Error")
return 0, errors.New("not implemented")
}
func (m *backendMock) SendTransaction(ctx context.Context, tx *types.Transaction) error {
if m.sendTransaction != nil {
return m.sendTransaction(ctx, tx)
}
return errors.New("Error")
return errors.New("not implemented")
}
func (*backendMock) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (*backendMock) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (m *backendMock) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
if m.transactionReceipt != nil {
return m.transactionReceipt(ctx, txHash)
}
return nil, errors.New("Error")
return nil, errors.New("not implemented")
}
func (m *backendMock) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
if m.transactionByHash != nil {
return m.transactionByHash(ctx, hash)
}
return nil, false, errors.New("Error")
return nil, false, errors.New("not implemented")
}
func New(opts ...Option) chequebook.Backend {
func New(opts ...Option) transaction.Backend {
mock := new(backendMock)
for _, o := range opts {
o.apply(mock)
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mock
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
)
type transactionServiceMock struct {
send func(ctx context.Context, request *transaction.TxRequest) (txHash common.Hash, err error)
waitForReceipt func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error)
}
func (m *transactionServiceMock) Send(ctx context.Context, request *transaction.TxRequest) (txHash common.Hash, err error) {
if m.send != nil {
return m.send(ctx, request)
}
return common.Hash{}, errors.New("not implemented")
}
func (m *transactionServiceMock) WaitForReceipt(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) {
if m.waitForReceipt != nil {
return m.waitForReceipt(ctx, txHash)
}
return nil, errors.New("not implemented")
}
// Option is the option passed to the mock Chequebook service
type Option interface {
apply(*transactionServiceMock)
}
type optionFunc func(*transactionServiceMock)
func (f optionFunc) apply(r *transactionServiceMock) { f(r) }
func WithSendFunc(f func(ctx context.Context, request *transaction.TxRequest) (txHash common.Hash, err error)) Option {
return optionFunc(func(s *transactionServiceMock) {
s.send = f
})
}
func WithWaitForReceiptFunc(f func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error)) Option {
return optionFunc(func(s *transactionServiceMock) {
s.waitForReceipt = f
})
}
func New(opts ...Option) transaction.Service {
mock := new(transactionServiceMock)
for _, o := range opts {
o.apply(mock)
}
return mock
}
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chequebook
package transaction
import (
"errors"
......@@ -10,7 +10,6 @@ import (
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto"
......@@ -22,13 +21,6 @@ var (
ErrTransactionReverted = errors.New("transaction reverted")
)
// Backend is the minimum of blockchain backend functions we need.
type Backend interface {
bind.ContractBackend
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
}
// TxRequest describes a request for a transaction that can be executed.
type TxRequest struct {
To common.Address // recipient of the transaction
......@@ -38,8 +30,8 @@ type TxRequest struct {
Value *big.Int // amount of wei to send
}
// TransactionService is the service to send transactions. It takes care of gas price, gas limit and nonce management.
type TransactionService interface {
// Service is the service to send transactions. It takes care of gas price, gas limit and nonce management.
type Service interface {
// Send creates a transaction based on the request and sends it.
Send(ctx context.Context, request *TxRequest) (txHash common.Hash, err error)
// WaitForReceipt waits until either the transaction with the given hash has been mined or the context is cancelled.
......@@ -53,8 +45,8 @@ type transactionService struct {
sender common.Address
}
// NewTransactionService creates a new transaction service.
func NewTransactionService(logger logging.Logger, backend Backend, signer crypto.Signer) (TransactionService, error) {
// NewService creates a new transaction service.
func NewService(logger logging.Logger, backend Backend, signer crypto.Signer) (Service, error) {
senderAddress, err := signer.EthereumAddress()
if err != nil {
return nil, err
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chequebook_test
package transaction_test
import (
"bytes"
......@@ -14,8 +14,9 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
signermock "github.com/ethersphere/bee/pkg/crypto/mock"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction"
"github.com/ethersphere/bee/pkg/settlement/swap/transaction/backendmock"
)
......@@ -29,13 +30,13 @@ func TestTransactionSend(t *testing.T) {
estimatedGasLimit := uint64(3)
nonce := uint64(2)
request := &chequebook.TxRequest{
request := &transaction.TxRequest{
To: recipient,
Data: txData,
Value: value,
}
transactionService, err := chequebook.NewTransactionService(logger,
transactionService, err := transaction.NewService(logger,
backendmock.New(
backendmock.WithSendTransactionFunc(func(ctx context.Context, tx *types.Transaction) error {
if tx != signedTx {
......@@ -59,8 +60,8 @@ func TestTransactionSend(t *testing.T) {
return nonce, nil
}),
),
&signerMock{
signTx: func(transaction *types.Transaction) (*types.Transaction, error) {
signermock.New(
signermock.WithSignTxFunc(func(transaction *types.Transaction) (*types.Transaction, error) {
if !bytes.Equal(transaction.To().Bytes(), recipient.Bytes()) {
t.Fatalf("signing transaction with wrong recipient. wanted %x, got %x", recipient, transaction.To())
}
......@@ -82,8 +83,9 @@ func TestTransactionSend(t *testing.T) {
}
return signedTx, nil
},
})
}),
),
)
if err != nil {
t.Fatal(err)
}
......@@ -102,7 +104,7 @@ func TestTransactionWaitForReceipt(t *testing.T) {
logger := logging.New(ioutil.Discard, 0)
txHash := common.HexToHash("0xabcdee")
transactionService, err := chequebook.NewTransactionService(logger,
transactionService, err := transaction.NewService(logger,
backendmock.New(
backendmock.WithTransactionReceiptFunc(func(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
return &types.Receipt{
......@@ -110,7 +112,8 @@ func TestTransactionWaitForReceipt(t *testing.T) {
}, nil
}),
),
&signerMock{})
signermock.New(),
)
if err != nil {
t.Fatal(err)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment