OSDN Git Service

Merge branch 'v0.1' into edit
[bytom/vapor.git] / protocol / protocol.go
index 9eac945..73156e9 100644 (file)
@@ -38,6 +38,7 @@ func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*
        }
        c.cond.L = new(sync.Mutex)
 
+       c.bbft = newBbft(store, nil, c.orphanManage, eventDispatcher)
        storeStatus := store.GetStoreStatus()
        if storeStatus == nil {
                if err := c.initChainStatus(); err != nil {
@@ -52,8 +53,9 @@ func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*
        }
 
        c.bestNode = c.index.GetNode(storeStatus.Hash)
+       c.bestIrreversibleNode = c.index.GetNode(storeStatus.IrreversibleHash)
        c.index.SetMainChain(c.bestNode)
-       c.bbft = newBbft(store, c.index, c.orphanManage, eventDispatcher)
+       c.bbft.SetBlockIndex(c.index)
        go c.blockProcesser()
        return c, nil
 }
@@ -77,11 +79,17 @@ func (c *Chain) initChainStatus() error {
                return err
        }
 
+       voteResultMap := make(map[uint64]*state.VoteResult)
+       if err := c.bbft.ApplyBlock(voteResultMap, genesisBlock); err != nil {
+               return err
+       }
+
        node, err := state.NewBlockNode(&genesisBlock.BlockHeader, nil)
        if err != nil {
                return err
        }
-       return c.store.SaveChainStatus(node, node, utxoView, map[uint64]*state.VoteResult{})
+
+       return c.store.SaveChainStatus(node, node, utxoView, voteResultMap)
 }
 
 // BestBlockHeight returns the current height of the blockchain.
@@ -109,6 +117,12 @@ func (c *Chain) InMainChain(hash bc.Hash) bool {
        return c.index.InMainchain(hash)
 }
 
+func (c *Chain) IrreversibleBlockHeight() uint64 {
+       c.cond.L.Lock()
+       defer c.cond.L.Unlock()
+       return c.bestIrreversibleNode.Height
+}
+
 // This function must be called with mu lock in above level
 func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteMap map[uint64]*state.VoteResult) error {
        if err := c.store.SaveChainStatus(node, irreversibleNode, view, voteMap); err != nil {