OSDN Git Service

Merge branch 'v0.1' into edit
[bytom/vapor.git] / protocol / protocol.go
index 45cefc8..73156e9 100644 (file)
@@ -6,7 +6,7 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/config"
-       "github.com/vapor/errors"
+       "github.com/vapor/event"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
@@ -20,16 +20,16 @@ type Chain struct {
        orphanManage   *OrphanManage
        txPool         *TxPool
        store          Store
+       bbft           *bbft
        processBlockCh chan *processBlockMsg
 
-       cond       sync.Cond
-       bestNode   *state.BlockNode
-       Authoritys map[string]string
-       position   uint64
+       cond                 sync.Cond
+       bestNode             *state.BlockNode
+       bestIrreversibleNode *state.BlockNode
 }
 
 // NewChain returns a new Chain using store as the underlying storage.
-func NewChain(store Store, txPool *TxPool) (*Chain, error) {
+func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*Chain, error) {
        c := &Chain{
                orphanManage:   NewOrphanManage(),
                txPool:         txPool,
@@ -38,6 +38,7 @@ func NewChain(store Store, txPool *TxPool) (*Chain, error) {
        }
        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,19 +53,13 @@ func NewChain(store Store, txPool *TxPool) (*Chain, error) {
        }
 
        c.bestNode = c.index.GetNode(storeStatus.Hash)
+       c.bestIrreversibleNode = c.index.GetNode(storeStatus.IrreversibleHash)
        c.index.SetMainChain(c.bestNode)
+       c.bbft.SetBlockIndex(c.index)
        go c.blockProcesser()
        return c, nil
 }
 
-func (c *Chain) SetAuthoritys(authoritys map[string]string) {
-       c.Authoritys = authoritys
-}
-
-func (c *Chain) SetPosition(position uint64) {
-       c.position = position
-}
-
 func (c *Chain) initChainStatus() error {
        genesisBlock := config.GenesisBlock()
        txStatus := bc.NewTransactionStatus()
@@ -84,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, utxoView)
+
+       return c.store.SaveChainStatus(node, node, utxoView, voteResultMap)
 }
 
 // BestBlockHeight returns the current height of the blockchain.
@@ -116,27 +117,15 @@ func (c *Chain) InMainChain(hash bc.Hash) bool {
        return c.index.InMainchain(hash)
 }
 
-// CalcNextSeed return the seed for the given block
-func (c *Chain) CalcNextSeed(preBlock *bc.Hash) (*bc.Hash, error) {
-       node := c.index.GetNode(preBlock)
-       if node == nil {
-               return nil, errors.New("can't find preblock in the blockindex")
-       }
-       return node.CalcNextSeed(), nil
-}
-
-// CalcNextBits return the seed for the given block
-func (c *Chain) CalcNextBits(preBlock *bc.Hash) (uint64, error) {
-       node := c.index.GetNode(preBlock)
-       if node == nil {
-               return 0, errors.New("can't find preblock in the blockindex")
-       }
-       return node.CalcNextBits(), nil
+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, view *state.UtxoViewpoint) error {
-       if err := c.store.SaveChainStatus(node, view); err != nil {
+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 {
                return err
        }
 
@@ -145,8 +134,9 @@ func (c *Chain) setState(node *state.BlockNode, view *state.UtxoViewpoint) error
 
        c.index.SetMainChain(node)
        c.bestNode = node
+       c.bestIrreversibleNode = irreversibleNode
 
-       log.WithFields(log.Fields{"height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
+       log.WithFields(log.Fields{"module": logModule, "height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
        c.cond.Broadcast()
        return nil
 }
@@ -170,3 +160,8 @@ func (c *Chain) BlockWaiter(height uint64) <-chan struct{} {
 func (c *Chain) GetTxPool() *TxPool {
        return c.txPool
 }
+
+// GetBBFT return chain bbft
+func (c *Chain) GetBBFT() *bbft {
+       return c.bbft
+}