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