7 log "github.com/sirupsen/logrus"
9 "github.com/vapor/config"
10 "github.com/vapor/crypto/ed25519"
11 "github.com/vapor/crypto/ed25519/chainkd"
12 "github.com/vapor/errors"
13 "github.com/vapor/protocol/bc"
14 "github.com/vapor/protocol/bc/types"
15 "github.com/vapor/protocol/state"
19 maxSignatureCacheSize = 10000
23 errVotingOperationOverFlow = errors.New("voting operation result overflow")
24 errDoubleSignBlock = errors.New("the consensus is double sign in same height of different block")
25 errInvalidSignature = errors.New("the signature of block is invalid")
28 func (c *Chain) isIrreversible(block *types.Block) bool {
29 consensusNodes, err := c.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash)
34 signNum, err := c.validateSign(block)
39 return signNum > (uint64(len(consensusNodes)) * 2 / 3)
42 // NextLeaderTime returns the start time of the specified public key as the next leader node
43 func (c *Chain) IsBlocker(prevBlockHash *bc.Hash, pubkey string, timeStamp uint64) (bool, error) {
44 return c.consensusNodeManager.isBlocker(prevBlockHash, pubkey, timeStamp)
47 func (c *Chain) ApplyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
48 return c.consensusNodeManager.applyBlock(voteResultMap, block)
51 func (c *Chain) DetachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
52 return c.consensusNodeManager.detachBlock(voteResultMap, block)
55 // ProcessBlockSignature process the received block signature messages
56 // return whether a block become irreversible, if so, the chain module must update status
57 func (c *Chain) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) error {
58 block, err := c.consensusNodeManager.store.GetBlock(blockHash)
60 // block is not exist, save the signature
61 key := fmt.Sprintf("%s:%s", blockHash.String(), hex.EncodeToString(xPub[:]))
62 c.signatureCache.Add(key, signature)
66 consensusNode, err := c.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xPub[:]))
71 if chainkd.XPub(xPub).Verify(blockHash.Bytes(), signature) {
72 return errInvalidSignature
75 isDoubleSign, err := c.checkDoubleSign(consensusNode.order, blockHeight, *blockHash)
81 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")
82 return errDoubleSignBlock
85 orphanBlock, ok := c.orphanManage.Get(blockHash)
87 orphanBlock.Witness[consensusNode.order] = signature
91 if err := c.updateBlockSignature(block, consensusNode.order, signature); err != nil {
95 if c.isIrreversible(block) && blockHeight > c.bestIrreversibleNode.Height {
96 bestIrreversibleNode := c.index.GetNode(blockHash)
97 if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil {
101 c.bestIrreversibleNode = bestIrreversibleNode
106 // ValidateBlock verify whether the block is valid
107 func (c *Chain) ValidateBlock(block *types.Block) error {
108 signNum, err := c.validateSign(block)
114 return errors.New("no valid signature")
119 // validateSign verify the signatures of block, and return the number of correct signature
120 // if some signature is invalid, they will be reset to nil
121 // if the block has not the signature of blocker, it will return error
122 func (c *Chain) validateSign(block *types.Block) (uint64, error) {
123 var correctSignNum uint64
124 consensusNodeMap, err := c.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash)
129 hasBlockerSign := false
130 for pubKey, node := range consensusNodeMap {
131 if len(block.Witness) <= int(node.order) {
135 blockHash := block.Hash()
136 if block.Witness[node.order] == nil {
137 key := fmt.Sprintf("%s:%s", blockHash.String(), pubKey)
138 signature, ok := c.signatureCache.Get(key)
140 block.Witness[node.order] = signature.([]byte)
144 pubKeyBytes, err := hex.DecodeString(pubKey)
149 if ed25519.Verify(ed25519.PublicKey(pubKeyBytes[:32]), blockHash.Bytes(), block.Witness[node.order]) {
150 isDoubleSign, err := c.checkDoubleSign(node.order, block.Height, block.Hash())
156 log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String(), "pubKey": pubKey}).Warn("the consensus node double sign the same height of different block")
157 // Consensus node is signed twice with the same block height, discard the signature
158 block.Witness[node.order] = nil
161 isBlocker, err := c.consensusNodeManager.isBlocker(&block.PreviousBlockHash, pubKey, block.Timestamp)
166 hasBlockerSign = true
170 // discard the invalid signature
171 block.Witness[node.order] = nil
175 return 0, errors.New("the block has no signature of the blocker")
177 return correctSignNum, nil
180 func (c *Chain) checkDoubleSign(nodeOrder, blockHeight uint64, blockHash bc.Hash) (bool, error) {
181 blockNodes := c.consensusNodeManager.blockIndex.NodesByHeight(blockHeight)
182 for _, blockNode := range blockNodes {
183 if blockNode.Hash == blockHash {
186 if ok, err := blockNode.BlockWitness.Test(uint32(nodeOrder)); err != nil && ok {
187 block, err := c.consensusNodeManager.store.GetBlock(&blockHash)
192 // reset nil to discard signature
193 if err := c.updateBlockSignature(block, nodeOrder, nil); err != nil {
203 // SignBlock signing the block if current node is consensus node
204 func (c *Chain) SignBlock(block *types.Block) ([]byte, error) {
205 xprv := config.CommonConfig.PrivateKey()
206 xpub := [64]byte(xprv.XPub())
207 node, err := c.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xpub[:]))
208 if err != nil && err != errNotFoundConsensusNode {
216 blockNodes := c.consensusNodeManager.blockIndex.NodesByHeight(block.Height)
217 for _, blockNode := range blockNodes {
218 // Has already signed the same height block
219 if ok, err := blockNode.BlockWitness.Test(uint32(node.order)); err != nil && ok {
224 signature := block.Witness[node.order]
225 if len(signature) == 0 {
226 signature = xprv.Sign(block.Hash().Bytes())
227 block.Witness[node.order] = signature
229 return signature, nil
232 func (c *Chain) updateBlockSignature(block *types.Block, nodeOrder uint64, signature []byte) error {
233 blockHash := block.Hash()
234 blockNode := c.consensusNodeManager.blockIndex.GetNode(&blockHash)
236 if len(signature) != 0 {
237 if err := blockNode.BlockWitness.Set(uint32(nodeOrder)); err != nil {
241 if err := blockNode.BlockWitness.Clean(uint32(nodeOrder)); err != nil {
246 block.Witness[nodeOrder] = signature
247 txStatus, err := c.consensusNodeManager.store.GetTransactionStatus(&blockHash)
252 return c.consensusNodeManager.store.SaveBlock(block, txStatus)