OSDN Git Service

validate vote pubkey (#128)
[bytom/vapor.git] / protocol / protocol.go
index 9fb2b3d..b893e8a 100644 (file)
@@ -3,9 +3,11 @@ package protocol
 import (
        "sync"
 
+       "github.com/golang/groupcache/lru"
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/config"
+       "github.com/vapor/event"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
@@ -21,17 +23,25 @@ type Chain struct {
        store          Store
        processBlockCh chan *processBlockMsg
 
-       cond     sync.Cond
-       bestNode *state.BlockNode
+       consensusNodeManager *consensusNodeManager
+       signatureCache       *lru.Cache
+       eventDispatcher      *event.Dispatcher
+
+       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,
-               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)
 
@@ -49,7 +59,9 @@ 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.consensusNodeManager.blockIndex = c.index
        go c.blockProcesser()
        return c, nil
 }
@@ -73,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, utxoView)
+
+       return c.store.SaveChainStatus(node, node, utxoView, voteResultMap)
 }
 
 // BestBlockHeight returns the current height of the blockchain.
@@ -106,8 +124,8 @@ 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, 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
        }
 
@@ -116,6 +134,7 @@ 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{"module": logModule, "height": c.bestNode.Height, "hash": c.bestNode.Hash.String()}).Debug("chain best status has been update")
        c.cond.Broadcast()