6 log "github.com/sirupsen/logrus"
8 "github.com/bytom/config"
9 "github.com/bytom/errors"
10 "github.com/bytom/protocol/bc"
11 "github.com/bytom/protocol/bc/types"
12 "github.com/bytom/protocol/state"
15 const maxProcessBlockChSize = 1024
17 // Chain provides functions for working with the Bytom block chain.
19 index *state.BlockIndex
20 orphanManage *OrphanManage
23 processBlockCh chan *processBlockMsg
26 bestNode *state.BlockNode
29 // NewChain returns a new Chain using store as the underlying storage.
30 func NewChain(store Store, txPool *TxPool) (*Chain, error) {
32 orphanManage: NewOrphanManage(),
35 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
37 c.cond.L = new(sync.Mutex)
39 storeStatus := store.GetStoreStatus()
40 if storeStatus == nil {
41 if err := c.initChainStatus(); err != nil {
44 storeStatus = store.GetStoreStatus()
48 if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
52 c.bestNode = c.index.GetNode(storeStatus.Hash)
53 c.index.SetMainChain(c.bestNode)
58 func (c *Chain) initChainStatus() error {
59 genesisBlock := config.GenesisBlock()
60 txStatus := bc.NewTransactionStatus()
61 for i := range genesisBlock.Transactions {
62 if err := txStatus.SetStatus(i, false); err != nil {
67 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
71 utxoView := state.NewUtxoViewpoint()
72 bcBlock := types.MapBlock(genesisBlock)
73 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
77 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
81 return c.store.SaveChainStatus(node, utxoView)
84 // BestBlockHeight returns the current height of the blockchain.
85 func (c *Chain) BestBlockHeight() uint64 {
87 defer c.cond.L.Unlock()
88 return c.bestNode.Height
91 // BestBlockHash return the hash of the chain tail block
92 func (c *Chain) BestBlockHash() *bc.Hash {
94 defer c.cond.L.Unlock()
95 return &c.bestNode.Hash
98 // BestBlockHeader returns the chain tail block
99 func (c *Chain) BestBlockHeader() *types.BlockHeader {
100 node := c.index.BestNode()
101 return node.BlockHeader()
104 // InMainChain checks wheather a block is in the main chain
105 func (c *Chain) InMainChain(hash bc.Hash) bool {
106 return c.index.InMainchain(hash)
109 // CalcNextSeed return the seed for the given block
110 func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
111 node := c.index.GetNode(preBlock)
113 return nil, errors.New("can't find preblock in the blockindex")
115 return node.CalcNextSeed(), nil
118 // CalcNextBits return the seed for the given block
119 func (c *Chain) CalcNextBits(preBlock *bc.Hash) (uint64, error) {
120 node := c.index.GetNode(preBlock)
122 return 0, errors.New("can't find preblock in the blockindex")
124 return node.CalcNextBits(), nil
127 // This function must be called with mu lock in above level
128 func (c *Chain) setState(node *state.BlockNode, view *state.UtxoViewpoint) error {
129 if err := c.store.SaveChainStatus(node, view); err != nil {
134 defer c.cond.L.Unlock()
136 c.index.SetMainChain(node)
139 log.WithFields(log.Fields{"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 {