6 log "github.com/sirupsen/logrus"
8 "github.com/bytom/bytom/config"
9 "github.com/bytom/bytom/errors"
10 "github.com/bytom/bytom/protocol/bc"
11 "github.com/bytom/bytom/protocol/bc/types"
12 "github.com/bytom/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) {
31 return NewChainWithOrphanManage(store, txPool, NewOrphanManage())
34 func NewChainWithOrphanManage(store Store, txPool *TxPool, manage *OrphanManage) (*Chain, error) {
39 processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
41 c.cond.L = new(sync.Mutex)
43 storeStatus := store.GetStoreStatus()
44 if storeStatus == nil {
45 if err := c.initChainStatus(); err != nil {
48 storeStatus = store.GetStoreStatus()
52 if c.index, err = store.LoadBlockIndex(storeStatus.Height); err != nil {
56 c.bestNode = c.index.GetNode(storeStatus.Hash)
57 c.index.SetMainChain(c.bestNode)
62 func (c *Chain) initChainStatus() error {
63 genesisBlock := config.GenesisBlock()
64 txStatus := bc.NewTransactionStatus()
65 for i := range genesisBlock.Transactions {
66 if err := txStatus.SetStatus(i, false); err != nil {
71 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
75 utxoView := state.NewUtxoViewpoint()
76 bcBlock := types.MapBlock(genesisBlock)
77 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
81 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
85 return c.store.SaveChainStatus(node, utxoView)
88 // BestBlockHeight returns the last irreversible block header of the blockchain
89 func (c *Chain) LastIrreversibleHeader() *types.BlockHeader {
90 // TODO: LastIrreversibleHeader
94 // ProcessBlockSignature process blockchain signature
95 func (c *Chain) ProcessBlockSignature(signature, pubkey []byte, blockHash *bc.Hash) error {
96 // TODO: ProcessBlockSignature
100 // BestBlockHeight returns the current height of the blockchain.
101 func (c *Chain) BestBlockHeight() uint64 {
103 defer c.cond.L.Unlock()
104 return c.bestNode.Height
107 // BestBlockHash return the hash of the chain tail block
108 func (c *Chain) BestBlockHash() *bc.Hash {
110 defer c.cond.L.Unlock()
111 return &c.bestNode.Hash
114 // BestBlockHeader returns the chain tail block
115 func (c *Chain) BestBlockHeader() *types.BlockHeader {
116 node := c.index.BestNode()
117 return node.BlockHeader()
120 // InMainChain checks wheather a block is in the main chain
121 func (c *Chain) InMainChain(hash bc.Hash) bool {
122 return c.index.InMainchain(hash)
125 // CalcNextSeed return the seed for the given block
126 func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
127 node := c.index.GetNode(preBlock)
129 return nil, errors.New("can't find preblock in the blockindex")
131 return node.CalcNextSeed(), nil
134 func (c *Chain) GetBlockIndex() *state.BlockIndex {
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{"module": logModule, "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 {