From: Paladz Date: Mon, 3 Jun 2019 09:15:40 +0000 (+0800) Subject: init push for easy code review (#121) X-Git-Tag: v1.0.5~208^2~73 X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=6d8346d7d81d94c93871d87d0c97c16efa67e742 init push for easy code review (#121) --- diff --git a/proposal/blockproposer/blockproposer.go b/proposal/blockproposer/blockproposer.go index afdc8d26..cd84e708 100644 --- a/proposal/blockproposer/blockproposer.go +++ b/proposal/blockproposer/blockproposer.go @@ -57,7 +57,7 @@ func (b *BlockProposer) generateBlocks() { nextBlockTime = minNextBlockTime } - if isBlocker, err := b.chain.GetBBFT().IsBlocker(&bestBlockHash, xpubStr, nextBlockTime); !isBlocker { + if isBlocker, err := b.chain.IsBlocker(&bestBlockHash, xpubStr, nextBlockTime); !isBlocker { log.WithFields(log.Fields{"module": logModule, "error": err, "pubKey": xpubStr}).Debug("fail on check is next blocker") continue } diff --git a/proposal/proposal.go b/proposal/proposal.go index 6e5387ba..46382e2c 100644 --- a/proposal/proposal.go +++ b/proposal/proposal.go @@ -154,7 +154,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager b.BlockHeader.BlockCommitment.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus) - _, err = c.GetBBFT().SignBlock(b) + _, err = c.SignBlock(b) return b, err } diff --git a/protocol/bbft.go b/protocol/bbft.go index d0049b5b..679af2ba 100644 --- a/protocol/bbft.go +++ b/protocol/bbft.go @@ -4,14 +4,12 @@ import ( "encoding/hex" "fmt" - "github.com/golang/groupcache/lru" log "github.com/sirupsen/logrus" "github.com/vapor/config" "github.com/vapor/crypto/ed25519" "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/errors" - "github.com/vapor/event" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" "github.com/vapor/protocol/state" @@ -27,29 +25,13 @@ var ( errInvalidSignature = errors.New("the signature of block is invalid") ) -type bbft struct { - consensusNodeManager *consensusNodeManager - orphanManage *OrphanManage - signatureCache *lru.Cache - eventDispatcher *event.Dispatcher -} - -func newBbft(store Store, blockIndex *state.BlockIndex, orphanManage *OrphanManage, eventDispatcher *event.Dispatcher) *bbft { - return &bbft{ - orphanManage: orphanManage, - consensusNodeManager: newConsensusNodeManager(store, blockIndex), - signatureCache: lru.New(maxSignatureCacheSize), - eventDispatcher: eventDispatcher, - } -} - -func (b *bbft) isIrreversible(block *types.Block) bool { - consensusNodes, err := b.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash) +func (c *Chain) isIrreversible(block *types.Block) bool { + consensusNodes, err := c.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash) if err != nil { return false } - signNum, err := b.validateSign(block) + signNum, err := c.validateSign(block) if err != nil { return false } @@ -58,64 +40,72 @@ func (b *bbft) isIrreversible(block *types.Block) bool { } // NextLeaderTime returns the start time of the specified public key as the next leader node -func (b *bbft) IsBlocker(prevBlockHash *bc.Hash, pubkey string, timeStamp uint64) (bool, error) { - return b.consensusNodeManager.isBlocker(prevBlockHash, pubkey, timeStamp) +func (c *Chain) IsBlocker(prevBlockHash *bc.Hash, pubkey string, timeStamp uint64) (bool, error) { + return c.consensusNodeManager.isBlocker(prevBlockHash, pubkey, timeStamp) } -func (b *bbft) ApplyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) { - return b.consensusNodeManager.applyBlock(voteResultMap, block) +func (c *Chain) ApplyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) { + return c.consensusNodeManager.applyBlock(voteResultMap, block) } -func (b *bbft) DetachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error { - return b.consensusNodeManager.detachBlock(voteResultMap, block) +func (c *Chain) DetachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error { + return c.consensusNodeManager.detachBlock(voteResultMap, block) } // ProcessBlockSignature process the received block signature messages // return whether a block become irreversible, if so, the chain module must update status -func (b *bbft) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) (bool, error) { - block, err := b.consensusNodeManager.store.GetBlock(blockHash) +func (c *Chain) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) error { + block, err := c.consensusNodeManager.store.GetBlock(blockHash) if err != nil { // block is not exist, save the signature key := fmt.Sprintf("%s:%s", blockHash.String(), hex.EncodeToString(xPub[:])) - b.signatureCache.Add(key, signature) - return false, err + c.signatureCache.Add(key, signature) + return err } - consensusNode, err := b.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xPub[:])) + consensusNode, err := c.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xPub[:])) if err != nil { - return false, err + return err } if chainkd.XPub(xPub).Verify(blockHash.Bytes(), signature) { - return false, errInvalidSignature + return errInvalidSignature } - isDoubleSign, err := b.checkDoubleSign(consensusNode.order, blockHeight, *blockHash) + isDoubleSign, err := c.checkDoubleSign(consensusNode.order, blockHeight, *blockHash) if err != nil { - return false, err + return err } if isDoubleSign { log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String(), "xPub": hex.EncodeToString(xPub[:])}).Warn("the consensus node double sign the same height of different block") - return false, errDoubleSignBlock + return errDoubleSignBlock } - orphanBlock, ok := b.orphanManage.Get(blockHash) + orphanBlock, ok := c.orphanManage.Get(blockHash) if ok { orphanBlock.Witness[consensusNode.order] = signature - return false, nil + return nil } - if err := b.updateBlockSignature(block, consensusNode.order, signature); err != nil { - return false, err + if err := c.updateBlockSignature(block, consensusNode.order, signature); err != nil { + return err } - return b.isIrreversible(block), nil + if c.isIrreversible(block) && blockHeight > c.bestIrreversibleNode.Height { + bestIrreversibleNode := c.index.GetNode(blockHash) + if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil { + return err + } + + c.bestIrreversibleNode = bestIrreversibleNode + } + return nil } // ValidateBlock verify whether the block is valid -func (b *bbft) ValidateBlock(block *types.Block) error { - signNum, err := b.validateSign(block) +func (c *Chain) ValidateBlock(block *types.Block) error { + signNum, err := c.validateSign(block) if err != nil { return err } @@ -129,9 +119,9 @@ func (b *bbft) ValidateBlock(block *types.Block) error { // validateSign verify the signatures of block, and return the number of correct signature // if some signature is invalid, they will be reset to nil // if the block has not the signature of blocker, it will return error -func (b *bbft) validateSign(block *types.Block) (uint64, error) { +func (c *Chain) validateSign(block *types.Block) (uint64, error) { var correctSignNum uint64 - consensusNodeMap, err := b.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash) + consensusNodeMap, err := c.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash) if err != nil { return 0, err } @@ -145,7 +135,7 @@ func (b *bbft) validateSign(block *types.Block) (uint64, error) { blockHash := block.Hash() if block.Witness[node.order] == nil { key := fmt.Sprintf("%s:%s", blockHash.String(), pubKey) - signature, ok := b.signatureCache.Get(key) + signature, ok := c.signatureCache.Get(key) if ok { block.Witness[node.order] = signature.([]byte) } @@ -157,7 +147,7 @@ func (b *bbft) validateSign(block *types.Block) (uint64, error) { } if ed25519.Verify(ed25519.PublicKey(pubKeyBytes[:32]), blockHash.Bytes(), block.Witness[node.order]) { - isDoubleSign, err := b.checkDoubleSign(node.order, block.Height, block.Hash()) + isDoubleSign, err := c.checkDoubleSign(node.order, block.Height, block.Hash()) if err != nil { return 0, err } @@ -168,7 +158,7 @@ func (b *bbft) validateSign(block *types.Block) (uint64, error) { block.Witness[node.order] = nil } else { correctSignNum++ - isBlocker, err := b.consensusNodeManager.isBlocker(&block.PreviousBlockHash, pubKey, block.Timestamp) + isBlocker, err := c.consensusNodeManager.isBlocker(&block.PreviousBlockHash, pubKey, block.Timestamp) if err != nil { return 0, err } @@ -187,20 +177,20 @@ func (b *bbft) validateSign(block *types.Block) (uint64, error) { return correctSignNum, nil } -func (b *bbft) checkDoubleSign(nodeOrder, blockHeight uint64, blockHash bc.Hash) (bool, error) { - blockNodes := b.consensusNodeManager.blockIndex.NodesByHeight(blockHeight) +func (c *Chain) checkDoubleSign(nodeOrder, blockHeight uint64, blockHash bc.Hash) (bool, error) { + blockNodes := c.consensusNodeManager.blockIndex.NodesByHeight(blockHeight) for _, blockNode := range blockNodes { if blockNode.Hash == blockHash { continue } if ok, err := blockNode.BlockWitness.Test(uint32(nodeOrder)); err != nil && ok { - block, err := b.consensusNodeManager.store.GetBlock(&blockHash) + block, err := c.consensusNodeManager.store.GetBlock(&blockHash) if err != nil { return false, err } // reset nil to discard signature - if err := b.updateBlockSignature(block, nodeOrder, nil); err != nil { + if err := c.updateBlockSignature(block, nodeOrder, nil); err != nil { return false, err } @@ -211,10 +201,10 @@ func (b *bbft) checkDoubleSign(nodeOrder, blockHeight uint64, blockHash bc.Hash) } // SignBlock signing the block if current node is consensus node -func (b *bbft) SignBlock(block *types.Block) ([]byte, error) { +func (c *Chain) SignBlock(block *types.Block) ([]byte, error) { xprv := config.CommonConfig.PrivateKey() xpub := [64]byte(xprv.XPub()) - node, err := b.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xpub[:])) + node, err := c.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xpub[:])) if err != nil && err != errNotFoundConsensusNode { return nil, err } @@ -223,7 +213,7 @@ func (b *bbft) SignBlock(block *types.Block) ([]byte, error) { return nil, nil } - blockNodes := b.consensusNodeManager.blockIndex.NodesByHeight(block.Height) + blockNodes := c.consensusNodeManager.blockIndex.NodesByHeight(block.Height) for _, blockNode := range blockNodes { // Has already signed the same height block if ok, err := blockNode.BlockWitness.Test(uint32(node.order)); err != nil && ok { @@ -239,9 +229,9 @@ func (b *bbft) SignBlock(block *types.Block) ([]byte, error) { return signature, nil } -func (b *bbft) updateBlockSignature(block *types.Block, nodeOrder uint64, signature []byte) error { +func (c *Chain) updateBlockSignature(block *types.Block, nodeOrder uint64, signature []byte) error { blockHash := block.Hash() - blockNode := b.consensusNodeManager.blockIndex.GetNode(&blockHash) + blockNode := c.consensusNodeManager.blockIndex.GetNode(&blockHash) if len(signature) != 0 { if err := blockNode.BlockWitness.Set(uint32(nodeOrder)); err != nil { @@ -254,15 +244,10 @@ func (b *bbft) updateBlockSignature(block *types.Block, nodeOrder uint64, signat } block.Witness[nodeOrder] = signature - txStatus, err := b.consensusNodeManager.store.GetTransactionStatus(&blockHash) + txStatus, err := c.consensusNodeManager.store.GetTransactionStatus(&blockHash) if err != nil { return err } - return b.consensusNodeManager.store.SaveBlock(block, txStatus) -} - -// SetBlockIndex set the block index field -func (b *bbft) SetBlockIndex(blockIndex *state.BlockIndex) { - b.consensusNodeManager.blockIndex = blockIndex + return c.consensusNodeManager.store.SaveBlock(block, txStatus) } diff --git a/protocol/block.go b/protocol/block.go index 8e5523b1..29dc3c72 100644 --- a/protocol/block.go +++ b/protocol/block.go @@ -3,9 +3,9 @@ package protocol import ( log "github.com/sirupsen/logrus" + "github.com/vapor/config" "github.com/vapor/errors" "github.com/vapor/event" - "github.com/vapor/config" "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" "github.com/vapor/protocol/state" @@ -91,12 +91,12 @@ func (c *Chain) connectBlock(block *types.Block) (err error) { } voteResultMap := make(map[uint64]*state.VoteResult) - if err := c.bbft.ApplyBlock(voteResultMap, block); err != nil { + if err := c.ApplyBlock(voteResultMap, block); err != nil { return err } node := c.index.GetNode(&bcBlock.ID) - if c.bbft.isIrreversible(block) && block.Height > irreversibleNode.Height { + if c.isIrreversible(block) && block.Height > irreversibleNode.Height { irreversibleNode = node } @@ -140,7 +140,7 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error { return err } - if err := c.bbft.DetachBlock(voteResultMap, b); err != nil { + if err := c.DetachBlock(voteResultMap, b); err != nil { return err } @@ -167,11 +167,11 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error { return err } - if err := c.bbft.ApplyBlock(voteResultMap, b); err != nil { + if err := c.ApplyBlock(voteResultMap, b); err != nil { return err } - if c.bbft.isIrreversible(b) && b.Height > irreversibleNode.Height { + if c.isIrreversible(b) && b.Height > irreversibleNode.Height { irreversibleNode = attachNode } @@ -183,7 +183,7 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error { // SaveBlock will validate and save block into storage func (c *Chain) saveBlock(block *types.Block) error { - if err := c.bbft.ValidateBlock(block); err != nil { + if err := c.ValidateBlock(block); err != nil { return errors.Sub(ErrBadBlock, err) } @@ -193,7 +193,7 @@ func (c *Chain) saveBlock(block *types.Block) error { return errors.Sub(ErrBadBlock, err) } - signature, err := c.bbft.SignBlock(block) + signature, err := c.SignBlock(block) if err != nil { return errors.Sub(ErrBadBlock, err) } @@ -212,7 +212,7 @@ func (c *Chain) saveBlock(block *types.Block) error { if len(signature) != 0 { xPub := config.CommonConfig.PrivateKey().XPub() - if err := c.bbft.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature, XPub: xPub}); err != nil { + if err := c.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature, XPub: xPub}); err != nil { return err } } @@ -315,20 +315,3 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) { } return false, nil } - -func (c *Chain) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) error { - isIrreversible, err := c.bbft.ProcessBlockSignature(signature, xPub, blockHeight, blockHash) - if err != nil { - return err - } - - if isIrreversible && blockHeight > c.bestIrreversibleNode.Height { - bestIrreversibleNode := c.index.GetNode(blockHash) - if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil { - return err - } - - c.bestIrreversibleNode = bestIrreversibleNode - } - return nil -} diff --git a/protocol/protocol.go b/protocol/protocol.go index 1331d7f9..384c54b5 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -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,14 +35,16 @@ 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) - c.bbft = newBbft(store, nil, c.orphanManage, eventDispatcher) storeStatus := store.GetStoreStatus() if storeStatus == nil { if err := c.initChainStatus(); err != nil { @@ -55,7 +61,7 @@ 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.SetBlockIndex(c.index) + c.consensusNodeManager.blockIndex = c.index go c.blockProcesser() return c, nil } @@ -80,7 +86,7 @@ func (c *Chain) initChainStatus() error { } voteResultMap := make(map[uint64]*state.VoteResult) - if err := c.bbft.ApplyBlock(voteResultMap, genesisBlock); err != nil { + if err := c.ApplyBlock(voteResultMap, genesisBlock); err != nil { return err } @@ -154,8 +160,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 -}