2018-07-18 20:59:40 +00:00
|
|
|
package fakes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-12-12 16:01:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2018-07-18 20:59:40 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
type MockEthClient struct {
|
2018-07-18 20:59:40 +00:00
|
|
|
callContractErr error
|
|
|
|
callContractPassedContext context.Context
|
|
|
|
callContractPassedMsg ethereum.CallMsg
|
|
|
|
callContractPassedNumber *big.Int
|
|
|
|
callContractReturnBytes []byte
|
|
|
|
blockByNumberErr error
|
|
|
|
blockByNumberPassedContext context.Context
|
|
|
|
blockByNumberPassedNumber *big.Int
|
|
|
|
blockByNumberReturnBlock *types.Block
|
|
|
|
headerByNumberErr error
|
|
|
|
headerByNumberPassedContext context.Context
|
|
|
|
headerByNumberPassedNumber *big.Int
|
|
|
|
headerByNumberReturnHeader *types.Header
|
2018-12-12 16:01:50 +00:00
|
|
|
headerByNumbersReturnHeader []*types.Header
|
|
|
|
headerByNumbersPassedNumber []*big.Int
|
2018-07-18 20:59:40 +00:00
|
|
|
filterLogsErr error
|
|
|
|
filterLogsPassedContext context.Context
|
|
|
|
filterLogsPassedQuery ethereum.FilterQuery
|
|
|
|
filterLogsReturnLogs []types.Log
|
2018-07-20 16:37:46 +00:00
|
|
|
transactionReceipts map[string]*types.Receipt
|
|
|
|
err error
|
2018-12-12 16:01:50 +00:00
|
|
|
passedBatch []rpc.BatchElem
|
|
|
|
passedMethod string
|
2018-07-20 16:37:46 +00:00
|
|
|
transactionSenderErr error
|
|
|
|
transactionReceiptErr error
|
2018-07-18 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func NewMockEthClient() *MockEthClient {
|
|
|
|
return &MockEthClient{
|
2018-07-18 20:59:40 +00:00
|
|
|
callContractErr: nil,
|
|
|
|
callContractPassedContext: nil,
|
|
|
|
callContractPassedMsg: ethereum.CallMsg{},
|
|
|
|
callContractPassedNumber: nil,
|
|
|
|
callContractReturnBytes: nil,
|
|
|
|
blockByNumberErr: nil,
|
|
|
|
blockByNumberPassedContext: nil,
|
|
|
|
blockByNumberPassedNumber: nil,
|
|
|
|
blockByNumberReturnBlock: nil,
|
|
|
|
headerByNumberErr: nil,
|
|
|
|
headerByNumberPassedContext: nil,
|
|
|
|
headerByNumberPassedNumber: nil,
|
|
|
|
headerByNumberReturnHeader: nil,
|
|
|
|
filterLogsErr: nil,
|
|
|
|
filterLogsPassedContext: nil,
|
|
|
|
filterLogsPassedQuery: ethereum.FilterQuery{},
|
|
|
|
filterLogsReturnLogs: nil,
|
2018-07-20 16:37:46 +00:00
|
|
|
transactionReceipts: make(map[string]*types.Receipt),
|
|
|
|
err: nil,
|
2018-12-12 16:01:50 +00:00
|
|
|
passedBatch: nil,
|
|
|
|
passedMethod: "123",
|
2018-07-18 20:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetCallContractErr(err error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.callContractErr = err
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetCallContractReturnBytes(returnBytes []byte) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.callContractReturnBytes = returnBytes
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetBlockByNumberErr(err error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.blockByNumberErr = err
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetBlockByNumberReturnBlock(block *types.Block) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.blockByNumberReturnBlock = block
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetHeaderByNumberErr(err error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.headerByNumberErr = err
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetHeaderByNumberReturnHeader(header *types.Header) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.headerByNumberReturnHeader = header
|
|
|
|
}
|
|
|
|
|
2018-12-12 16:01:50 +00:00
|
|
|
func (client *MockEthClient) SetHeaderByNumbersReturnHeader(headers []*types.Header) {
|
|
|
|
client.headerByNumbersReturnHeader = headers
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetFilterLogsErr(err error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.filterLogsErr = err
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetFilterLogsReturnLogs(logs []types.Log) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.filterLogsReturnLogs = logs
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) SetTransactionReceiptErr(err error) {
|
|
|
|
client.transactionReceiptErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *MockEthClient) SetTransactionReceipts(receipts []*types.Receipt) {
|
|
|
|
for _, receipt := range receipts {
|
|
|
|
client.transactionReceipts[receipt.TxHash.Hex()] = receipt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *MockEthClient) SetTransactionSenderErr(err error) {
|
|
|
|
client.transactionSenderErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *MockEthClient) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.callContractPassedContext = ctx
|
|
|
|
client.callContractPassedMsg = msg
|
|
|
|
client.callContractPassedNumber = blockNumber
|
|
|
|
return client.callContractReturnBytes, client.callContractErr
|
|
|
|
}
|
|
|
|
|
2018-12-12 16:01:50 +00:00
|
|
|
func (client *MockEthClient) BatchCall(batch []rpc.BatchElem) error {
|
|
|
|
client.passedBatch = batch
|
|
|
|
client.passedMethod = batch[0].Method
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.blockByNumberPassedContext = ctx
|
|
|
|
client.blockByNumberPassedNumber = number
|
|
|
|
return client.blockByNumberReturnBlock, client.blockByNumberErr
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.headerByNumberPassedContext = ctx
|
|
|
|
client.headerByNumberPassedNumber = number
|
|
|
|
return client.headerByNumberReturnHeader, client.headerByNumberErr
|
|
|
|
}
|
|
|
|
|
2018-12-12 16:01:50 +00:00
|
|
|
func (client *MockEthClient) HeaderByNumbers(numbers []*big.Int) ([]*types.Header, error) {
|
|
|
|
client.headerByNumbersPassedNumber = numbers
|
|
|
|
return client.headerByNumbersReturnHeader, client.headerByNumberErr
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
|
2018-07-18 20:59:40 +00:00
|
|
|
client.filterLogsPassedContext = ctx
|
|
|
|
client.filterLogsPassedQuery = q
|
|
|
|
return client.filterLogsReturnLogs, client.filterLogsErr
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error) {
|
|
|
|
return common.HexToAddress("0x123"), client.transactionSenderErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *MockEthClient) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
|
|
|
|
if gasUsed, ok := client.transactionReceipts[txHash.Hex()]; ok {
|
|
|
|
return gasUsed, client.transactionReceiptErr
|
|
|
|
}
|
|
|
|
return &types.Receipt{GasUsed: uint64(0)}, client.transactionReceiptErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *MockEthClient) AssertCallContractCalledWith(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) {
|
2018-07-18 20:59:40 +00:00
|
|
|
Expect(client.callContractPassedContext).To(Equal(ctx))
|
|
|
|
Expect(client.callContractPassedMsg).To(Equal(msg))
|
|
|
|
Expect(client.callContractPassedNumber).To(Equal(blockNumber))
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) AssertBlockByNumberCalledWith(ctx context.Context, number *big.Int) {
|
2018-07-18 20:59:40 +00:00
|
|
|
Expect(client.blockByNumberPassedContext).To(Equal(ctx))
|
|
|
|
Expect(client.blockByNumberPassedNumber).To(Equal(number))
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) AssertHeaderByNumberCalledWith(ctx context.Context, number *big.Int) {
|
2018-07-18 20:59:40 +00:00
|
|
|
Expect(client.headerByNumberPassedContext).To(Equal(ctx))
|
|
|
|
Expect(client.headerByNumberPassedNumber).To(Equal(number))
|
|
|
|
}
|
|
|
|
|
2018-12-12 16:01:50 +00:00
|
|
|
func (client *MockEthClient) AssertHeaderByNumbersCalledWith(number []*big.Int) {
|
|
|
|
Expect(client.headerByNumbersPassedNumber).To(Equal(number))
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func (client *MockEthClient) AssertFilterLogsCalledWith(ctx context.Context, q ethereum.FilterQuery) {
|
2018-07-18 20:59:40 +00:00
|
|
|
Expect(client.filterLogsPassedContext).To(Equal(ctx))
|
|
|
|
Expect(client.filterLogsPassedQuery).To(Equal(q))
|
|
|
|
}
|
2018-12-12 16:01:50 +00:00
|
|
|
|
|
|
|
func (client *MockEthClient) AssertBatchCalledWith(method string) {
|
|
|
|
Expect(client.passedMethod).To(Equal(method))
|
|
|
|
}
|