OSDN Git Service

validate vote pubkey (#128)
[bytom/vapor.git] / protocol / protocol.go
index f3f4fc2..b893e8a 100644 (file)
@@ -3,6 +3,7 @@ package protocol
 import (
        "sync"
 
+       "github.com/golang/groupcache/lru"
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/config"
@@ -20,9 +21,12 @@ type Chain struct {
        orphanManage   *OrphanManage
        txPool         *TxPool
        store          Store
-       bbft           *bbft
        processBlockCh chan *processBlockMsg
 
+       consensusNodeManager *consensusNodeManager
+       signatureCache       *lru.Cache
+       eventDispatcher      *event.Dispatcher
+
        cond                 sync.Cond
        bestNode             *state.BlockNode
        bestIrreversibleNode *state.BlockNode
@@ -31,10 +35,13 @@ 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:       lru.New(maxSignatureCacheSize),
+               consensusNodeManager: newConsensusNodeManager(store, nil),
+               eventDispatcher:      eventDispatcher,
+               processBlockCh:       make(chan *processBlockMsg, maxProcessBlockChSize),
        }
        c.cond.L = new(sync.Mutex)
 
@@ -52,8 +59,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.consensusNodeManager.blockIndex = c.index
        go c.blockProcesser()
        return c, nil
 }
@@ -77,11 +85,17 @@ func (c *Chain) initChainStatus() error {
                return err
        }
 
+       voteResultMap := make(map[uint64]*state.VoteResult)
+       if err := c.consensusNodeManager.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.
@@ -118,10 +132,6 @@ func (c *Chain) setState(node *state.BlockNode, irreversibleNode *state.BlockNod
        c.cond.L.Lock()
        defer c.cond.L.Unlock()
 
-       if err := c.bbft.UpdateConsensusNodes(node.Height); err != nil {
-               return err
-       }
-
        c.index.SetMainChain(node)
        c.bestNode = node
        c.bestIrreversibleNode = irreversibleNode