X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=protocol%2Fprotocol.go;h=cb31fe39b7fc1039b3857f497e5445de0b3c6c6a;hp=3782aee303ea900453d03e4536dbace8c788dee5;hb=4607cea3c6d3b1501ae1edb2c078b9ae0ee4bc79;hpb=db158dcf09436b003defd333f1a665e7e051d820 diff --git a/protocol/protocol.go b/protocol/protocol.go index 3782aee3..cb31fe39 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -5,8 +5,9 @@ import ( log "github.com/sirupsen/logrus" + "github.com/vapor/common" "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" @@ -22,17 +23,24 @@ type Chain struct { store Store processBlockCh chan *processBlockMsg - cond sync.Cond - bestNode *state.BlockNode + consensusNodeManager *consensusNodeManager + signatureCache *common.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: common.NewCache(maxSignatureCacheSize), + eventDispatcher: eventDispatcher, + processBlockCh: make(chan *processBlockMsg, maxProcessBlockChSize), } c.cond.L = new(sync.Mutex) @@ -50,6 +58,8 @@ func NewChain(store Store, txPool *TxPool) (*Chain, error) { } 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) go c.blockProcesser() return c, nil @@ -74,11 +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, utxoView) + + return c.store.SaveChainStatus(node, node, utxoView, voteResults) } // BestBlockHeight returns the current height of the blockchain. @@ -95,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() @@ -106,35 +127,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 -} - // 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, 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 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()