6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/config"
9 engine "github.com/vapor/consensus/consensus"
10 dpos "github.com/vapor/consensus/consensus/dpos"
11 "github.com/vapor/errors"
12 "github.com/vapor/protocol/bc"
13 "github.com/vapor/protocol/bc/types"
14 "github.com/vapor/protocol/state"
17 const maxProcessBlockChSize = 1024
19 // Chain provides functions for working with the Bytom block chain.
21 index *state.BlockIndex
22 orphanManage *OrphanManage
25 processBlockCh chan *processBlockMsg
28 bestNode *state.BlockNode
32 // NewChain returns a new Chain using store as the underlying storage.
33 func NewChain(store Store, txPool *TxPool) (*Chain, error) {
35 var engine engine.Engine
36 switch config.CommonConfig.Consensus.Type {
42 orphanManage: NewOrphanManage(),
45 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
48 c.cond.L = new(sync.Mutex)
50 storeStatus := store.GetStoreStatus()
51 if storeStatus == nil {
52 if err := c.initChainStatus(); err != nil {
55 storeStatus = store.GetStoreStatus()
59 if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
63 c.bestNode = c.index.GetNode(storeStatus.Hash)
64 c.index.SetMainChain(c.bestNode)
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 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
92 return c.store.SaveChainStatus(node, utxoView)
95 // BestBlockHeight returns the current height of the blockchain.
96 func (c *Chain) BestBlockHeight() uint64 {
98 defer c.cond.L.Unlock()
99 return c.bestNode.Height
102 // BestBlockHash return the hash of the chain tail block
103 func (c *Chain) BestBlockHash() *bc.Hash {
105 defer c.cond.L.Unlock()
106 return &c.bestNode.Hash
109 // BestBlockHeader returns the chain tail block
110 func (c *Chain) BestBlockHeader() *types.BlockHeader {
111 node := c.index.BestNode()
112 return node.BlockHeader()
115 // InMainChain checks wheather a block is in the main chain
116 func (c *Chain) InMainChain(hash bc.Hash) bool {
117 return c.index.InMainchain(hash)
120 // CalcNextSeed return the seed for the given block
121 func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
122 node := c.index.GetNode(preBlock)
124 return nil, errors.New("can't find preblock in the blockindex")
126 return node.CalcNextSeed(), nil
129 // CalcNextBits return the seed for the given block
130 func (c *Chain) CalcNextBits(preBlock *bc.Hash) (uint64, error) {
131 node := c.index.GetNode(preBlock)
133 return 0, errors.New("can't find preblock in the blockindex")
135 return node.CalcNextBits(), nil
138 // This function must be called with mu lock in above level
139 func (c *Chain) setState(node *state.BlockNode, view *state.UtxoViewpoint) error {
140 if err := c.store.SaveChainStatus(node, view); err != nil {
145 defer c.cond.L.Unlock()
147 c.index.SetMainChain(node)
150 log.WithFields(log.Fields{"height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
155 // BlockWaiter returns a channel that waits for the block at the given height.
156 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
157 ch := make(chan struct{}, 1)
160 defer c.cond.L.Unlock()
161 for c.bestNode.Height < height {
170 // GetTxPool return chain txpool.
171 func (c *Chain) GetTxPool() *TxPool {