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-17 21:23:07 +00:00
|
|
|
type BlockChain struct {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockChain *BlockChain) GetHeaderByNumber(blockNumber int64) (core.Header, error) {
|
|
|
|
return blockChain.headers[blockNumber], nil
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) 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-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) CallContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return blockChain.ContractReturnValue, nil
|
2017-12-07 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) 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-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) GetLogs(contract core.Contract, startingBlock *big.Int, endingBlock *big.Int) ([]core.Log, error) {
|
|
|
|
return blockChain.logs[contract.Hash], nil
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) Node() core.Node {
|
|
|
|
return blockChain.node
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func NewBlockchain(err error) *BlockChain {
|
|
|
|
return &BlockChain{
|
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-17 21:23:07 +00:00
|
|
|
func NewBlockchainWithBlocks(blocks []core.Block) *BlockChain {
|
2017-11-06 20:36:12 +00:00
|
|
|
blockNumberToBlocks := make(map[int64]core.Block)
|
|
|
|
for _, block := range blocks {
|
|
|
|
blockNumberToBlocks[block.Number] = block
|
|
|
|
}
|
2018-07-17 21:23:07 +00:00
|
|
|
return &BlockChain{
|
2017-11-06 20:36:12 +00:00
|
|
|
blocks: blockNumberToBlocks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func NewBlockChainWithHeaders(headers []core.Header) *BlockChain {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
return &BlockChain{
|
|
|
|
blocks: memoryBlocks,
|
|
|
|
headers: memoryHeaders,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockChain *BlockChain) GetBlockByNumber(blockNumber int64) (core.Block, error) {
|
|
|
|
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-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) AddBlock(block core.Block) {
|
|
|
|
blockChain.blocks[block.Number] = block
|
|
|
|
blockChain.blocksChannel <- block
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|