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-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-17 21:23:07 +00:00
|
|
|
ContractReturnValue []byte
|
|
|
|
WasToldToStop bool
|
2018-02-13 16:31:57 +00:00
|
|
|
blocks map[int64]core.Block
|
|
|
|
blocksChannel chan core.Block
|
2018-07-17 21:23:07 +00:00
|
|
|
contractAttributes map[string]map[string]string
|
2018-03-27 21:06:12 +00:00
|
|
|
err error
|
2018-07-17 21:23:07 +00:00
|
|
|
headers map[int64]core.Header
|
|
|
|
logs map[string][]core.Log
|
|
|
|
node core.Node
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) GetHeaderByNumber(blockNumber int64) (core.Header, error) {
|
2018-07-17 21:23:07 +00:00
|
|
|
return blockChain.headers[blockNumber], nil
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
|
2018-03-12 19:09:20 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
|
2018-07-17 21:23:07 +00:00
|
|
|
return blockChain.ContractReturnValue, nil
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) LastBlock() *big.Int {
|
2017-12-20 20:06:22 +00:00
|
|
|
var max int64
|
2018-07-17 21:23:07 +00:00
|
|
|
for blockNumber := range blockChain.blocks {
|
2017-12-20 20:06:22 +00:00
|
|
|
if blockNumber > max {
|
|
|
|
max = blockNumber
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return big.NewInt(max)
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) GetLogs(contract core.Contract, startingBlock *big.Int, endingBlock *big.Int) ([]core.Log, error) {
|
2018-07-17 21:23:07 +00:00
|
|
|
return blockChain.logs[contract.Hash], nil
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) Node() core.Node {
|
2018-07-17 21:23:07 +00:00
|
|
|
return blockChain.node
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func NewMockBlockChain(err error) *MockBlockChain {
|
|
|
|
return &MockBlockChain{
|
2017-11-27 15:39:53 +00:00
|
|
|
blocks: make(map[int64]core.Block),
|
2017-12-11 21:08:00 +00:00
|
|
|
logs: make(map[string][]core.Log),
|
2017-11-27 15:39:53 +00:00
|
|
|
contractAttributes: make(map[string]map[string]string),
|
2018-02-13 16:31:57 +00:00
|
|
|
node: core.Node{GenesisBlock: "GENESIS", NetworkID: 1, ID: "x123", ClientName: "Geth"},
|
2018-03-27 21:06:12 +00:00
|
|
|
err: err,
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
2017-11-06 20:36:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func NewMockBlockChainWithBlocks(blocks []core.Block) *MockBlockChain {
|
2017-11-06 20:36:12 +00:00
|
|
|
blockNumberToBlocks := make(map[int64]core.Block)
|
|
|
|
for _, block := range blocks {
|
|
|
|
blockNumberToBlocks[block.Number] = block
|
|
|
|
}
|
2018-07-18 20:59:40 +00:00
|
|
|
return &MockBlockChain{
|
2017-11-06 20:36:12 +00:00
|
|
|
blocks: blockNumberToBlocks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func NewMockBlockChainWithHeaders(headers []core.Header) *MockBlockChain {
|
2018-07-17 21:23:07 +00:00
|
|
|
// need to create blocks and headers so that LastBlock() will work in the mock
|
|
|
|
// no reason to implement LastBlock() separately for headers since it checks
|
|
|
|
// the last header in the Node's DB already
|
|
|
|
memoryBlocks := make(map[int64]core.Block)
|
|
|
|
memoryHeaders := make(map[int64]core.Header)
|
|
|
|
for _, header := range headers {
|
|
|
|
memoryBlocks[header.BlockNumber] = core.Block{Number: header.BlockNumber}
|
|
|
|
memoryHeaders[header.BlockNumber] = header
|
|
|
|
}
|
2018-07-18 20:59:40 +00:00
|
|
|
return &MockBlockChain{
|
2018-07-17 21:23:07 +00:00
|
|
|
blocks: memoryBlocks,
|
|
|
|
headers: memoryHeaders,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) GetBlockByNumber(blockNumber int64) (core.Block, error) {
|
2018-07-17 21:23:07 +00:00
|
|
|
if blockChain.err != nil {
|
|
|
|
return core.Block{}, blockChain.err
|
2018-03-27 21:06:12 +00:00
|
|
|
}
|
2018-07-17 21:23:07 +00:00
|
|
|
return blockChain.blocks[blockNumber], nil
|
2017-11-06 20:36:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
func (blockChain *MockBlockChain) AddBlock(block core.Block) {
|
2018-07-17 21:23:07 +00:00
|
|
|
blockChain.blocks[block.Number] = block
|
|
|
|
blockChain.blocksChannel <- block
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|