2017-10-23 15:56:29 +00:00
|
|
|
package fakes
|
|
|
|
|
2017-11-28 16:45:21 +00:00
|
|
|
import (
|
2017-12-04 18:54:33 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-08-07 15:51:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-07-24 16:38:49 +00:00
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2017-11-28 16:45:21 +00:00
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
type MockBlockChain struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
fetchContractDataErr error
|
|
|
|
fetchContractDataPassedAbi string
|
|
|
|
fetchContractDataPassedAddress string
|
|
|
|
fetchContractDataPassedMethod string
|
|
|
|
fetchContractDataPassedMethodArg interface{}
|
|
|
|
fetchContractDataPassedResult interface{}
|
|
|
|
fetchContractDataPassedBlockNumber int64
|
2018-07-24 16:38:49 +00:00
|
|
|
getLogsPassedContract core.Contract
|
|
|
|
getLogsPassedStartingBlockNumber *big.Int
|
|
|
|
getLogsPassedEndingBlockNumber *big.Int
|
|
|
|
getLogsReturnLogs []core.Log
|
|
|
|
getLogsReturnErr error
|
2018-07-20 16:37:46 +00:00
|
|
|
getBlockByNumberErr error
|
2018-08-07 15:51:34 +00:00
|
|
|
logQuery ethereum.FilterQuery
|
|
|
|
logQueryErr error
|
2018-07-24 16:38:49 +00:00
|
|
|
logQueryReturnLogs []types.Log
|
2018-07-20 16:37:46 +00:00
|
|
|
lastBlock *big.Int
|
|
|
|
node core.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockBlockChain() *MockBlockChain {
|
|
|
|
return &MockBlockChain{
|
|
|
|
node: core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "Geth"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetFetchContractDataErr(err error) {
|
|
|
|
chain.fetchContractDataErr = err
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetLastBlock(blockNumber *big.Int) {
|
|
|
|
chain.lastBlock = blockNumber
|
2018-07-20 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetBlockByNumberErr(err error) {
|
|
|
|
chain.getBlockByNumberErr = err
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetEthLogsWithCustomQueryErr(err error) {
|
|
|
|
chain.logQueryErr = err
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetEthLogsWithCustomQueryReturnLogs(logs []types.Log) {
|
|
|
|
chain.logQueryReturnLogs = logs
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetLogsReturnErr(err error) {
|
|
|
|
chain.getLogsReturnErr = err
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) SetGetLogsReturnLogs(logs []core.Log) {
|
|
|
|
chain.getLogsReturnLogs = logs
|
2018-03-12 19:09:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
|
|
|
|
chain.fetchContractDataPassedAbi = abiJSON
|
|
|
|
chain.fetchContractDataPassedAddress = address
|
|
|
|
chain.fetchContractDataPassedMethod = method
|
|
|
|
chain.fetchContractDataPassedMethodArg = methodArg
|
|
|
|
chain.fetchContractDataPassedResult = result
|
|
|
|
chain.fetchContractDataPassedBlockNumber = blockNumber
|
|
|
|
return chain.fetchContractDataErr
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) GetBlockByNumber(blockNumber int64) (core.Block, error) {
|
|
|
|
return core.Block{Number: blockNumber}, chain.getBlockByNumberErr
|
2017-12-20 20:06:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (blockChain *MockBlockChain) GetEthLogsWithCustomQuery(query ethereum.FilterQuery) ([]types.Log, error) {
|
|
|
|
blockChain.logQuery = query
|
|
|
|
return blockChain.logQueryReturnLogs, blockChain.logQueryErr
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) GetHeaderByNumber(blockNumber int64) (core.Header, error) {
|
|
|
|
return core.Header{BlockNumber: blockNumber}, nil
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) GetLogs(contract core.Contract, startingBlockNumber, endingBlockNumber *big.Int) ([]core.Log, error) {
|
|
|
|
chain.getLogsPassedContract = contract
|
|
|
|
chain.getLogsPassedStartingBlockNumber = startingBlockNumber
|
|
|
|
chain.getLogsPassedEndingBlockNumber = endingBlockNumber
|
|
|
|
return chain.getLogsReturnLogs, chain.getLogsReturnErr
|
2017-11-06 20:36:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
func (chain *MockBlockChain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain *MockBlockChain) LastBlock() *big.Int {
|
|
|
|
return chain.lastBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain *MockBlockChain) Node() core.Node {
|
|
|
|
return chain.node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain *MockBlockChain) AssertFetchContractDataCalledWith(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) {
|
|
|
|
Expect(chain.fetchContractDataPassedAbi).To(Equal(abiJSON))
|
|
|
|
Expect(chain.fetchContractDataPassedAddress).To(Equal(address))
|
|
|
|
Expect(chain.fetchContractDataPassedMethod).To(Equal(method))
|
|
|
|
if methodArg != nil {
|
|
|
|
Expect(chain.fetchContractDataPassedMethodArg).To(Equal(methodArg))
|
|
|
|
}
|
|
|
|
Expect(chain.fetchContractDataPassedResult).To(Equal(result))
|
|
|
|
Expect(chain.fetchContractDataPassedBlockNumber).To(Equal(blockNumber))
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
func (blockChain *MockBlockChain) AssertGetEthLogsWithCustomQueryCalledWith(query ethereum.FilterQuery) {
|
|
|
|
Expect(blockChain.logQuery).To(Equal(query))
|
|
|
|
}
|