6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/config"
9 engine "github.com/vapor/consensus/consensus"
10 "github.com/vapor/errors"
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
27 bestNode *state.BlockNode
31 // NewChain returns a new Chain using store as the underlying storage.
32 func NewChain(store Store, txPool *TxPool) (*Chain, error) {
34 orphanManage: NewOrphanManage(),
37 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
39 c.cond.L = new(sync.Mutex)
41 storeStatus := store.GetStoreStatus()
42 if storeStatus == nil {
43 if err := c.initChainStatus(); err != nil {
46 storeStatus = store.GetStoreStatus()
50 if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
54 c.bestNode = c.index.GetNode(storeStatus.Hash)
55 c.index.SetMainChain(c.bestNode)
60 func (c *Chain) SetConsensusEngine(engine engine.Engine) {
64 func (c *Chain) initChainStatus() error {
65 genesisBlock := config.GenesisBlock()
66 txStatus := bc.NewTransactionStatus()
67 for i := range genesisBlock.Transactions {
68 if err := txStatus.SetStatus(i, false); err != nil {
73 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
77 utxoView := state.NewUtxoViewpoint()
78 bcBlock := types.MapBlock(genesisBlock)
79 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
83 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
87 return c.store.SaveChainStatus(node, utxoView)
90 // BestBlockHeight returns the current height of the blockchain.
91 func (c *Chain) BestBlockHeight() uint64 {
93 defer c.cond.L.Unlock()
94 return c.bestNode.Height
97 // BestBlockHash return the hash of the chain tail block
98 func (c *Chain) BestBlockHash() *bc.Hash {
100 defer c.cond.L.Unlock()
101 return &c.bestNode.Hash
104 // BestBlockHeader returns the chain tail block
105 func (c *Chain) BestBlockHeader() *types.BlockHeader {
106 node := c.index.BestNode()
107 return node.BlockHeader()
110 // InMainChain checks wheather a block is in the main chain
111 func (c *Chain) InMainChain(hash bc.Hash) bool {
112 return c.index.InMainchain(hash)
115 // CalcNextSeed return the seed for the given block
116 func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
117 node := c.index.GetNode(preBlock)
119 return nil, errors.New("can't find preblock in the blockindex")
121 return node.CalcNextSeed(), nil
124 // CalcNextBits return the seed for the given block
125 func (c *Chain) CalcNextBits(preBlock *bc.Hash) (uint64, error) {
126 node := c.index.GetNode(preBlock)
128 return 0, errors.New("can't find preblock in the blockindex")
130 return node.CalcNextBits(), nil
133 // This function must be called with mu lock in above level
134 func (c *Chain) setState(node *state.BlockNode, view *state.UtxoViewpoint) error {
135 if err := c.store.SaveChainStatus(node, view); err != nil {
140 defer c.cond.L.Unlock()
142 c.index.SetMainChain(node)
145 log.WithFields(log.Fields{"height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
150 // BlockWaiter returns a channel that waits for the block at the given height.
151 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
152 ch := make(chan struct{}, 1)
155 defer c.cond.L.Unlock()
156 for c.bestNode.Height < height {
165 // GetTxPool return chain txpool.
166 func (c *Chain) GetTxPool() *TxPool {