6 "github.com/golang/groupcache/lru"
7 log "github.com/sirupsen/logrus"
9 "github.com/vapor/config"
10 "github.com/vapor/event"
11 "github.com/vapor/protocol/bc"
12 "github.com/vapor/protocol/bc/types"
13 "github.com/vapor/protocol/state"
16 const maxProcessBlockChSize = 1024
18 // Chain provides functions for working with the Bytom block chain.
20 index *state.BlockIndex
21 orphanManage *OrphanManage
24 processBlockCh chan *processBlockMsg
26 consensusNodeManager *consensusNodeManager
27 signatureCache *lru.Cache
28 eventDispatcher *event.Dispatcher
31 bestNode *state.BlockNode
32 bestIrreversibleNode *state.BlockNode
35 // NewChain returns a new Chain using store as the underlying storage.
36 func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*Chain, error) {
38 orphanManage: NewOrphanManage(),
41 signatureCache: lru.New(maxSignatureCacheSize),
42 consensusNodeManager: newConsensusNodeManager(store, nil),
43 eventDispatcher: eventDispatcher,
44 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
46 c.cond.L = new(sync.Mutex)
48 storeStatus := store.GetStoreStatus()
49 if storeStatus == nil {
50 if err := c.initChainStatus(); err != nil {
53 storeStatus = store.GetStoreStatus()
57 if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
61 c.bestNode = c.index.GetNode(storeStatus.Hash)
62 c.bestIrreversibleNode = c.index.GetNode(storeStatus.IrreversibleHash)
63 c.index.SetMainChain(c.bestNode)
64 c.consensusNodeManager.blockIndex = c.index
69 func (c *Chain) initChainStatus() error {
70 genesisBlock := config.GenesisBlock()
71 txStatus := bc.NewTransactionStatus()
72 for i := range genesisBlock.Transactions {
73 if err := txStatus.SetStatus(i, false); err != nil {
78 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
82 utxoView := state.NewUtxoViewpoint()
83 bcBlock := types.MapBlock(genesisBlock)
84 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
88 voteResultMap := make(map[uint64]*state.VoteResult)
89 if err := c.consensusNodeManager.applyBlock(voteResultMap, genesisBlock); err != nil {
93 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
98 return c.store.SaveChainStatus(node, node, utxoView, voteResultMap)
101 // BestBlockHeight returns the current height of the blockchain.
102 func (c *Chain) BestBlockHeight() uint64 {
104 defer c.cond.L.Unlock()
105 return c.bestNode.Height
108 // BestBlockHash return the hash of the chain tail block
109 func (c *Chain) BestBlockHash() *bc.Hash {
111 defer c.cond.L.Unlock()
112 return &c.bestNode.Hash
115 // BestBlockHeader returns the chain tail block
116 func (c *Chain) BestBlockHeader() *types.BlockHeader {
117 node := c.index.BestNode()
118 return node.BlockHeader()
121 // InMainChain checks wheather a block is in the main chain
122 func (c *Chain) InMainChain(hash bc.Hash) bool {
123 return c.index.InMainchain(hash)
126 // This function must be called with mu lock in above level
127 func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteMap map[uint64]*state.VoteResult) error {
128 if err := c.store.SaveChainStatus(node, irreversibleNode, view, voteMap); err != nil {
133 defer c.cond.L.Unlock()
135 c.index.SetMainChain(node)
137 c.bestIrreversibleNode = irreversibleNode
139 log.WithFields(log.Fields{"module": logModule, "height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
144 // BlockWaiter returns a channel that waits for the block at the given height.
145 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
146 ch := make(chan struct{}, 1)
149 defer c.cond.L.Unlock()
150 for c.bestNode.Height < height {
159 // GetTxPool return chain txpool.
160 func (c *Chain) GetTxPool() *TxPool {