7 "github.com/golang/groupcache/lru"
8 log "github.com/sirupsen/logrus"
10 "github.com/vapor/config"
11 "github.com/vapor/crypto/ed25519"
12 "github.com/vapor/crypto/ed25519/chainkd"
13 "github.com/vapor/errors"
14 "github.com/vapor/event"
15 "github.com/vapor/protocol/bc"
16 "github.com/vapor/protocol/bc/types"
17 "github.com/vapor/protocol/state"
21 maxSignatureCacheSize = 10000
25 errVotingOperationOverFlow = errors.New("voting operation result overflow")
26 errDoubleSignBlock = errors.New("the consensus is double sign in same height of different block")
27 errInvalidSignature = errors.New("the signature of block is invalid")
31 consensusNodeManager *consensusNodeManager
32 orphanManage *OrphanManage
33 signatureCache *lru.Cache
34 eventDispatcher *event.Dispatcher
37 func newBbft(store Store, blockIndex *state.BlockIndex, orphanManage *OrphanManage, eventDispatcher *event.Dispatcher) *bbft {
39 orphanManage: orphanManage,
40 consensusNodeManager: newConsensusNodeManager(store, blockIndex),
41 signatureCache: lru.New(maxSignatureCacheSize),
42 eventDispatcher: eventDispatcher,
46 func (b *bbft) isIrreversible(block *types.Block) bool {
47 consensusNodes, err := b.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash)
52 signNum, err := b.validateSign(block)
57 return signNum > (uint64(len(consensusNodes)) * 2 / 3)
60 // NextLeaderTime returns the start time of the specified public key as the next leader node
61 func (b *bbft) NextLeaderTimeRange(pubkey []byte, prevBlockHash *bc.Hash) (uint64, uint64, error) {
62 return b.consensusNodeManager.nextLeaderTimeRange(pubkey, prevBlockHash)
65 func (b *bbft) ApplyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
66 return b.consensusNodeManager.applyBlock(voteResultMap, block)
69 func (b *bbft) DetachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
70 return b.consensusNodeManager.detachBlock(voteResultMap, block)
73 // ProcessBlockSignature process the received block signature messages
74 // return whether a block become irreversible, if so, the chain module must update status
75 func (b *bbft) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) (bool, error) {
76 block, err := b.consensusNodeManager.store.GetBlock(blockHash)
78 // block is not exist, save the signature
79 key := fmt.Sprintf("%s:%s", blockHash.String(), hex.EncodeToString(xPub[:]))
80 b.signatureCache.Add(key, signature)
84 consensusNode, err := b.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xPub[:]))
89 if chainkd.XPub(xPub).Verify(blockHash.Bytes(), signature) {
90 return false, errInvalidSignature
93 isDoubleSign, err := b.checkDoubleSign(consensusNode.order, blockHeight, *blockHash)
99 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")
100 return false, errDoubleSignBlock
103 orphanBlock, ok := b.orphanManage.Get(blockHash)
105 orphanBlock.Witness[consensusNode.order] = signature
109 if err := b.updateBlockSignature(block, consensusNode.order, signature); err != nil {
113 return b.isIrreversible(block), nil
116 // ValidateBlock verify whether the block is valid
117 func (b *bbft) ValidateBlock(block *types.Block) error {
118 signNum, err := b.validateSign(block)
124 return errors.New("no valid signature")
129 // validateSign verify the signatures of block, and return the number of correct signature
130 // if some signature is invalid, they will be reset to nil
131 // if the block has not the signature of blocker, it will return error
132 func (b *bbft) validateSign(block *types.Block) (uint64, error) {
133 var correctSignNum uint64
134 consensusNodeMap, err := b.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash)
139 hasBlockerSign := false
140 for pubKey, node := range consensusNodeMap {
141 if len(block.Witness) <= int(node.order) {
145 blockHash := block.Hash()
146 if block.Witness[node.order] == nil {
147 key := fmt.Sprintf("%s:%s", blockHash.String(), pubKey)
148 signature, ok := b.signatureCache.Get(key)
150 block.Witness[node.order] = signature.([]byte)
154 pubKeyBytes, err := hex.DecodeString(pubKey)
159 if ed25519.Verify(ed25519.PublicKey(pubKeyBytes[:32]), blockHash.Bytes(), block.Witness[node.order]) {
160 isDoubleSign, err := b.checkDoubleSign(node.order, block.Height, block.Hash())
166 log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String(), "pubKey": pubKey}).Warn("the consensus node double sign the same height of different block")
167 // Consensus node is signed twice with the same block height, discard the signature
168 block.Witness[node.order] = nil
171 isBlocker, err := b.consensusNodeManager.isBlocker(block, pubKey)
176 hasBlockerSign = true
180 // discard the invalid signature
181 block.Witness[node.order] = nil
185 return 0, errors.New("the block has no signature of the blocker")
187 return correctSignNum, nil
190 func (b *bbft) checkDoubleSign(nodeOrder, blockHeight uint64, blockHash bc.Hash) (bool, error) {
191 blockNodes := b.consensusNodeManager.blockIndex.NodesByHeight(blockHeight)
192 for _, blockNode := range blockNodes {
193 if blockNode.Hash == blockHash {
196 if ok, err := blockNode.BlockWitness.Test(uint32(nodeOrder)); err != nil && ok {
197 block, err := b.consensusNodeManager.store.GetBlock(&blockHash)
202 // reset nil to discard signature
203 if err := b.updateBlockSignature(block, nodeOrder, nil); err != nil {
213 // SignBlock signing the block if current node is consensus node
214 func (b *bbft) SignBlock(block *types.Block) ([]byte, error) {
215 xprv := config.CommonConfig.PrivateKey()
216 xpub := [64]byte(xprv.XPub())
217 node, err := b.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xpub[:]))
218 if err != nil && err != errNotFoundConsensusNode {
226 blockNodes := b.consensusNodeManager.blockIndex.NodesByHeight(block.Height)
227 for _, blockNode := range blockNodes {
228 // Has already signed the same height block
229 if ok, err := blockNode.BlockWitness.Test(uint32(node.order)); err != nil && ok {
234 signature := block.Witness[node.order]
235 if len(signature) == 0 {
236 signature = xprv.Sign(block.Hash().Bytes())
237 block.Witness[node.order] = signature
239 return signature, nil
242 func (b *bbft) updateBlockSignature(block *types.Block, nodeOrder uint64, signature []byte) error {
243 blockHash := block.Hash()
244 blockNode := b.consensusNodeManager.blockIndex.GetNode(&blockHash)
246 if len(signature) != 0 {
247 if err := blockNode.BlockWitness.Set(uint32(nodeOrder)); err != nil {
251 if err := blockNode.BlockWitness.Clean(uint32(nodeOrder)); err != nil {
256 block.Witness[nodeOrder] = signature
257 txStatus, err := b.consensusNodeManager.store.GetTransactionStatus(&blockHash)
262 return b.consensusNodeManager.store.SaveBlock(block, txStatus)
265 // SetBlockIndex set the block index field
266 func (b *bbft) SetBlockIndex(blockIndex *state.BlockIndex) {
267 b.consensusNodeManager.blockIndex = blockIndex