6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/config"
9 "github.com/vapor/protocol/bc"
10 "github.com/vapor/protocol/bc/types"
11 "github.com/vapor/protocol/state"
14 const maxProcessBlockChSize = 1024
16 // Chain provides functions for working with the Bytom block chain.
18 index *state.BlockIndex
19 orphanManage *OrphanManage
23 processBlockCh chan *processBlockMsg
26 bestNode *state.BlockNode
27 bestIrreversibleNode *state.BlockNode
30 // NewChain returns a new Chain using store as the underlying storage.
31 func NewChain(store Store, txPool *TxPool) (*Chain, error) {
33 orphanManage: NewOrphanManage(),
36 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
38 c.cond.L = new(sync.Mutex)
40 storeStatus := store.GetStoreStatus()
41 if storeStatus == nil {
42 if err := c.initChainStatus(); err != nil {
45 storeStatus = store.GetStoreStatus()
49 if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
53 c.bestNode = c.index.GetNode(storeStatus.Hash)
54 c.index.SetMainChain(c.bestNode)
55 c.bbft = newBbft(store, c.index)
60 func (c *Chain) initChainStatus() error {
61 genesisBlock := config.GenesisBlock()
62 txStatus := bc.NewTransactionStatus()
63 for i := range genesisBlock.Transactions {
64 if err := txStatus.SetStatus(i, false); err != nil {
69 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
73 utxoView := state.NewUtxoViewpoint()
74 bcBlock := types.MapBlock(genesisBlock)
75 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
79 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
83 return c.store.SaveChainStatus(node, node, utxoView, map[uint64]*state.VoteResult{})
86 // BestBlockHeight returns the current height of the blockchain.
87 func (c *Chain) BestBlockHeight() uint64 {
89 defer c.cond.L.Unlock()
90 return c.bestNode.Height
93 // BestBlockHash return the hash of the chain tail block
94 func (c *Chain) BestBlockHash() *bc.Hash {
96 defer c.cond.L.Unlock()
97 return &c.bestNode.Hash
100 // BestBlockHeader returns the chain tail block
101 func (c *Chain) BestBlockHeader() *types.BlockHeader {
102 node := c.index.BestNode()
103 return node.BlockHeader()
106 // InMainChain checks wheather a block is in the main chain
107 func (c *Chain) InMainChain(hash bc.Hash) bool {
108 return c.index.InMainchain(hash)
111 // This function must be called with mu lock in above level
112 func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteMap map[uint64]*state.VoteResult) error {
113 if err := c.store.SaveChainStatus(node, irreversibleNode, view, voteMap); err != nil {
118 defer c.cond.L.Unlock()
120 if err := c.bbft.UpdateConsensusNodes(node.Height); err != nil {
124 c.index.SetMainChain(node)
126 c.bestIrreversibleNode = irreversibleNode
128 log.WithFields(log.Fields{"module": logModule, "height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
133 // BlockWaiter returns a channel that waits for the block at the given height.
134 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
135 ch := make(chan struct{}, 1)
138 defer c.cond.L.Unlock()
139 for c.bestNode.Height < height {
148 // GetTxPool return chain txpool.
149 func (c *Chain) GetTxPool() *TxPool {