OSDN Git Service

add fast sync func (#204)
[bytom/vapor.git] / protocol / protocol.go
index 183f95a..cb31fe3 100644 (file)
@@ -5,6 +5,7 @@ import (
 
        log "github.com/sirupsen/logrus"
 
+       "github.com/vapor/common"
        "github.com/vapor/config"
        "github.com/vapor/event"
        "github.com/vapor/protocol/bc"
@@ -20,9 +21,12 @@ type Chain struct {
        orphanManage   *OrphanManage
        txPool         *TxPool
        store          Store
-       bbft           *bbft
        processBlockCh chan *processBlockMsg
 
+       consensusNodeManager *consensusNodeManager
+       signatureCache       *common.Cache
+       eventDispatcher      *event.Dispatcher
+
        cond                 sync.Cond
        bestNode             *state.BlockNode
        bestIrreversibleNode *state.BlockNode
@@ -31,10 +35,12 @@ type Chain struct {
 // NewChain returns a new Chain using store as the underlying storage.
 func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*Chain, error) {
        c := &Chain{
-               orphanManage:   NewOrphanManage(),
-               txPool:         txPool,
-               store:          store,
-               processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize),
+               orphanManage:    NewOrphanManage(),
+               txPool:          txPool,
+               store:           store,
+               signatureCache:  common.NewCache(maxSignatureCacheSize),
+               eventDispatcher: eventDispatcher,
+               processBlockCh:  make(chan *processBlockMsg, maxProcessBlockChSize),
        }
        c.cond.L = new(sync.Mutex)
 
@@ -52,8 +58,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.consensusNodeManager = newConsensusNodeManager(store, c.index)
        c.index.SetMainChain(c.bestNode)
-       c.bbft = newBbft(store, c.index, c.orphanManage, eventDispatcher)
        go c.blockProcesser()
        return c, nil
 }
@@ -77,12 +84,18 @@ func (c *Chain) initChainStatus() error {
                return err
        }
 
+       voteResults := []*state.VoteResult{&state.VoteResult{
+               Seq:         0,
+               NumOfVote:   map[string]uint64{},
+               BlockHash:   genesisBlock.Hash(),
+               BlockHeight: 0,
+       }}
        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, voteResults)
 }
 
 // BestBlockHeight returns the current height of the blockchain.
@@ -99,7 +112,11 @@ func (c *Chain) BestBlockHash() *bc.Hash {
        return &c.bestNode.Hash
 }
 
-// BestBlockHeader returns the chain tail block
+// BestIrreversibleHeader returns the chain best irreversible block
+func (c *Chain) BestIrreversibleHeader() *types.BlockHeader {
+       return c.bestIrreversibleNode.BlockHeader()
+}
+
 func (c *Chain) BestBlockHeader() *types.BlockHeader {
        node := c.index.BestNode()
        return node.BlockHeader()
@@ -111,14 +128,11 @@ func (c *Chain) InMainChain(hash bc.Hash) bool {
 }
 
 // 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 {
+func (c *Chain) setState(node, irreversibleNode *state.BlockNode, view *state.UtxoViewpoint, voteResults []*state.VoteResult) error {
+       if err := c.store.SaveChainStatus(node, irreversibleNode, view, voteResults); err != nil {
                return err
        }
 
-       c.cond.L.Lock()
-       defer c.cond.L.Unlock()
-
        c.index.SetMainChain(node)
        c.bestNode = node
        c.bestIrreversibleNode = irreversibleNode
@@ -147,8 +161,3 @@ 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
-}