From 90ae869a1a8609bc0cec53b2ce9fd52cea40d7af Mon Sep 17 00:00:00 2001 From: Meng-Ying Yang Date: Sat, 29 Dec 2018 20:56:27 +0800 Subject: indexer: ReadOnlyBlockChain returns related configs (#110) For restoring mock componenets (such as: VM, Backend, ...etc), blockchain related configs should be exposed. --- indexer/blockchain.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ indexer/blockhain.go | 85 ------------------------------------------------ 2 files changed, 89 insertions(+), 85 deletions(-) create mode 100644 indexer/blockchain.go delete mode 100644 indexer/blockhain.go diff --git a/indexer/blockchain.go b/indexer/blockchain.go new file mode 100644 index 000000000..a762ef67d --- /dev/null +++ b/indexer/blockchain.go @@ -0,0 +1,89 @@ +package indexer + +import ( + "math/big" + + coreCommon "github.com/dexon-foundation/dexon-consensus/common" + coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" + + "github.com/dexon-foundation/dexon/common" + "github.com/dexon-foundation/dexon/core" + "github.com/dexon-foundation/dexon/core/state" + "github.com/dexon-foundation/dexon/core/types" + "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/event" + "github.com/dexon-foundation/dexon/params" + "github.com/dexon-foundation/dexon/rlp" +) + +// ReadOnlyBlockChain defines safe reading blockchain interface by removing write +// methods of core.BlockChain struct. +type ReadOnlyBlockChain interface { + BadBlocks() []*types.Block + Config() *params.ChainConfig + CurrentBlock() *types.Block + CurrentFastBlock() *types.Block + CurrentHeader() *types.Header + GasLimit() uint64 + Genesis() *types.Block + GetAncestor(common.Hash, uint64, uint64, *uint64) (common.Hash, uint64) + GetAddressInfo(uint32, common.Address) ( + info struct { + Nonce uint64 + Cost *big.Int + Counter uint64 + }) + GetBlock(common.Hash, uint64) *types.Block + GetBlockByHash(common.Hash) *types.Block + GetBlockByNumber(uint64) *types.Block + GetBlockHashesFromHash(common.Hash, uint64) []common.Hash + GetBlocksFromHash(common.Hash, int) (blocks []*types.Block) + GetBody(common.Hash) *types.Body + GetBodyRLP(common.Hash) rlp.RawValue + GetChainLastConfirmedHeight(uint32) uint64 + GetConfirmedBlockByHash(uint32, coreCommon.Hash) (*coreTypes.Block, types.Transactions) + GetCostInConfirmedBlocks(uint32, common.Address) (*big.Int, bool) + GetGovStateByHash(common.Hash) (*types.GovState, error) + GetGovStateByNumber(uint64) (*types.GovState, error) + GetHeader(common.Hash, uint64) *types.Header + GetHeaderByHash(common.Hash) *types.Header + GetHeaderByNumber(number uint64) *types.Header + GetLastNonceInConfirmedBlocks(uint32, common.Address) (uint64, bool) + GetPending() (*types.Block, *state.StateDB) + GetPendingBlockByNumber(uint64) *types.Block + GetPendingHeight() uint64 + GetReceiptsByHash(common.Hash) types.Receipts + GetRoundHeight(uint64) (uint64, bool) + GetTd(common.Hash, uint64) *big.Int + GetTdByHash(common.Hash) *big.Int + GetUnclesInChain(*types.Block, int) []*types.Header + GetVMConfig() *vm.Config + HasBlock(common.Hash, uint64) bool + HasBlockAndState(common.Hash, uint64) bool + HasHeader(common.Hash, uint64) bool + HasState(common.Hash) bool + PendingBlock() *types.Block + State() (*state.StateDB, error) + StateAt(root common.Hash) (*state.StateDB, error) + SubscribeBlockConfirmedEvent(chan<- core.BlockConfirmedEvent) event.Subscription + SubscribeChainEvent(chan<- core.ChainEvent) event.Subscription + SubscribeChainHeadEvent(chan<- core.ChainHeadEvent) event.Subscription + SubscribeChainSideEvent(chan<- core.ChainSideEvent) event.Subscription + SubscribeLogsEvent(chan<- []*types.Log) event.Subscription + SubscribeRemovedLogsEvent(chan<- core.RemovedLogsEvent) event.Subscription +} + +// access protection +type ro interface { + ReadOnlyBlockChain +} + +// ROBlockChain struct for safe read. +type ROBlockChain struct { + ro +} + +// NewROBlockChain converts original block chain to readonly interface. +func NewROBlockChain(bc *core.BlockChain) ReadOnlyBlockChain { + return &ROBlockChain{ro: bc} +} diff --git a/indexer/blockhain.go b/indexer/blockhain.go deleted file mode 100644 index bf9a180ce..000000000 --- a/indexer/blockhain.go +++ /dev/null @@ -1,85 +0,0 @@ -package indexer - -import ( - "math/big" - - coreCommon "github.com/dexon-foundation/dexon-consensus/common" - coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" - - "github.com/dexon-foundation/dexon/common" - "github.com/dexon-foundation/dexon/core" - "github.com/dexon-foundation/dexon/core/state" - "github.com/dexon-foundation/dexon/core/types" - "github.com/dexon-foundation/dexon/event" - "github.com/dexon-foundation/dexon/rlp" -) - -// ReadOnlyBlockChain defines safe reading blockchain interface by removing write -// methods of core.BlockChain struct. -type ReadOnlyBlockChain interface { - BadBlocks() []*types.Block - CurrentBlock() *types.Block - CurrentFastBlock() *types.Block - CurrentHeader() *types.Header - GasLimit() uint64 - Genesis() *types.Block - GetAncestor(common.Hash, uint64, uint64, *uint64) (common.Hash, uint64) - GetAddressInfo(uint32, common.Address) ( - info struct { - Nonce uint64 - Cost *big.Int - Counter uint64 - }) - GetBlock(common.Hash, uint64) *types.Block - GetBlockByHash(common.Hash) *types.Block - GetBlockByNumber(uint64) *types.Block - GetBlockHashesFromHash(common.Hash, uint64) []common.Hash - GetBlocksFromHash(common.Hash, int) (blocks []*types.Block) - GetBody(common.Hash) *types.Body - GetBodyRLP(common.Hash) rlp.RawValue - GetChainLastConfirmedHeight(uint32) uint64 - GetConfirmedBlockByHash(uint32, coreCommon.Hash) (*coreTypes.Block, types.Transactions) - GetCostInConfirmedBlocks(uint32, common.Address) (*big.Int, bool) - GetGovStateByHash(common.Hash) (*types.GovState, error) - GetGovStateByNumber(uint64) (*types.GovState, error) - GetHeader(common.Hash, uint64) *types.Header - GetHeaderByHash(common.Hash) *types.Header - GetHeaderByNumber(number uint64) *types.Header - GetLastNonceInConfirmedBlocks(uint32, common.Address) (uint64, bool) - GetPending() (*types.Block, *state.StateDB) - GetPendingBlockByNumber(uint64) *types.Block - GetPendingHeight() uint64 - GetReceiptsByHash(common.Hash) types.Receipts - GetRoundHeight(uint64) (uint64, bool) - GetTd(common.Hash, uint64) *big.Int - GetTdByHash(common.Hash) *big.Int - GetUnclesInChain(*types.Block, int) []*types.Header - HasBlock(common.Hash, uint64) bool - HasBlockAndState(common.Hash, uint64) bool - HasHeader(common.Hash, uint64) bool - HasState(common.Hash) bool - PendingBlock() *types.Block - State() (*state.StateDB, error) - StateAt(root common.Hash) (*state.StateDB, error) - SubscribeBlockConfirmedEvent(chan<- core.BlockConfirmedEvent) event.Subscription - SubscribeChainEvent(chan<- core.ChainEvent) event.Subscription - SubscribeChainHeadEvent(chan<- core.ChainHeadEvent) event.Subscription - SubscribeChainSideEvent(chan<- core.ChainSideEvent) event.Subscription - SubscribeLogsEvent(chan<- []*types.Log) event.Subscription - SubscribeRemovedLogsEvent(chan<- core.RemovedLogsEvent) event.Subscription -} - -// access protection -type ro interface { - ReadOnlyBlockChain -} - -// ROBlockChain struct for safe read. -type ROBlockChain struct { - ro -} - -// NewROBlockChain converts original block chain to readonly interface. -func NewROBlockChain(bc *core.BlockChain) ReadOnlyBlockChain { - return &ROBlockChain{ro: bc} -} -- cgit v1.2.3