6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/config"
9 "github.com/vapor/errors"
10 "github.com/vapor/protocol/bc"
11 "github.com/vapor/protocol/bc/types"
12 "github.com/vapor/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
27 Authoritys map[string]string
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) SetAuthoritys(authoritys map[string]string) {
61 c.Authoritys = authoritys
64 func (c *Chain) SetPosition(position uint64) {
68 func (c *Chain) initChainStatus() error {
69 genesisBlock := config.GenesisBlock()
70 txStatus := bc.NewTransactionStatus()
71 for i := range genesisBlock.Transactions {
72 if err := txStatus.SetStatus(i, false); err != nil {
77 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
81 utxoView := state.NewUtxoViewpoint()
82 bcBlock := types.MapBlock(genesisBlock)
83 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
87 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
91 return c.store.SaveChainStatus(node, utxoView)
94 // BestBlockHeight returns the current height of the blockchain.
95 func (c *Chain) BestBlockHeight() uint64 {
97 defer c.cond.L.Unlock()
98 return c.bestNode.Height
101 // BestBlockHash return the hash of the chain tail block
102 func (c *Chain) BestBlockHash() *bc.Hash {
104 defer c.cond.L.Unlock()
105 return &c.bestNode.Hash
108 // BestBlockHeader returns the chain tail block
109 func (c *Chain) BestBlockHeader() *types.BlockHeader {
110 node := c.index.BestNode()
111 return node.BlockHeader()
114 // InMainChain checks wheather a block is in the main chain
115 func (c *Chain) InMainChain(hash bc.Hash) bool {
116 return c.index.InMainchain(hash)
119 // CalcNextSeed return the seed for the given block
120 func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
121 node := c.index.GetNode(preBlock)
123 return nil, errors.New("can't find preblock in the blockindex")
125 return node.CalcNextSeed(), nil
128 // CalcNextBits return the seed for the given block
129 func (c *Chain) CalcNextBits(preBlock *bc.Hash) (uint64, error) {
130 node := c.index.GetNode(preBlock)
132 return 0, errors.New("can't find preblock in the blockindex")
134 return node.CalcNextBits(), nil
137 // This function must be called with mu lock in above level
138 func (c *Chain) setState(node *state.BlockNode, view *state.UtxoViewpoint) error {
139 if err := c.store.SaveChainStatus(node, view); err != nil {
144 defer c.cond.L.Unlock()
146 c.index.SetMainChain(node)
149 log.WithFields(log.Fields{"height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
154 // BlockWaiter returns a channel that waits for the block at the given height.
155 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
156 ch := make(chan struct{}, 1)
159 defer c.cond.L.Unlock()
160 for c.bestNode.Height < height {
169 // GetTxPool return chain txpool.
170 func (c *Chain) GetTxPool() *TxPool {