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
28 Authoritys map[string]string
33 // NewChain returns a new Chain using store as the underlying storage.
34 func NewChain(store Store, txPool *TxPool) (*Chain, error) {
36 orphanManage: NewOrphanManage(),
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) SetAuthoritys(authoritys map[string]string) {
63 c.Authoritys = authoritys
66 func (c *Chain) SetPosition(position uint64) {
70 func (c *Chain) GetAuthoritys(key string) string {
71 return c.Authoritys[key]
74 func (c *Chain) SetConsensusEngine(engine engine.Engine) {
78 func (c *Chain) initChainStatus() error {
79 genesisBlock := config.GenesisBlock()
80 txStatus := bc.NewTransactionStatus()
81 for i := range genesisBlock.Transactions {
82 if err := txStatus.SetStatus(i, false); err != nil {
87 if err := c.store.SaveBlock(genesisBlock, txStatus); err != nil {
91 utxoView := state.NewUtxoViewpoint()
92 bcBlock := types.MapBlock(genesisBlock)
93 if err := utxoView.ApplyBlock(bcBlock, txStatus); err != nil {
97 node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
101 return c.store.SaveChainStatus(node, utxoView)
104 // BestBlockHeight returns the current height of the blockchain.
105 func (c *Chain) BestBlockHeight() uint64 {
107 defer c.cond.L.Unlock()
108 return c.bestNode.Height
111 // BestBlockHash return the hash of the chain tail block
112 func (c *Chain) BestBlockHash() *bc.Hash {
114 defer c.cond.L.Unlock()
115 return &c.bestNode.Hash
118 // BestBlockHeader returns the chain tail block
119 func (c *Chain) BestBlockHeader() *types.BlockHeader {
120 node := c.index.BestNode()
121 return node.BlockHeader()
124 // InMainChain checks wheather a block is in the main chain
125 func (c *Chain) InMainChain(hash bc.Hash) bool {
126 return c.index.InMainchain(hash)
129 // CalcNextSeed return the seed for the given block
130 func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
131 node := c.index.GetNode(preBlock)
133 return nil, errors.New("can't find preblock in the blockindex")
135 return node.CalcNextSeed(), nil
138 // CalcNextBits return the seed for the given block
139 func (c *Chain) CalcNextBits(preBlock *bc.Hash) (uint64, error) {
140 node := c.index.GetNode(preBlock)
142 return 0, errors.New("can't find preblock in the blockindex")
144 return node.CalcNextBits(), nil
147 // This function must be called with mu lock in above level
148 func (c *Chain) setState(node *state.BlockNode, view *state.UtxoViewpoint) error {
149 if err := c.store.SaveChainStatus(node, view); err != nil {
154 defer c.cond.L.Unlock()
156 c.index.SetMainChain(node)
159 log.WithFields(log.Fields{"height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
164 // BlockWaiter returns a channel that waits for the block at the given height.
165 func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
166 ch := make(chan struct{}, 1)
169 defer c.cond.L.Unlock()
170 for c.bestNode.Height < height {
179 // GetTxPool return chain txpool.
180 func (c *Chain) GetTxPool() *TxPool {