From f7d76d2342f2bf409ee5cc22720d4c41d9a19c5f Mon Sep 17 00:00:00 2001 From: Stefan Liu Date: Tue, 18 Jul 2023 18:28:06 +0800 Subject: [PATCH] Add endorsement verify step --- consensus/vbft/service.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/consensus/vbft/service.go b/consensus/vbft/service.go index 9672c3b5..07e5f793 100644 --- a/consensus/vbft/service.go +++ b/consensus/vbft/service.go @@ -1303,6 +1303,11 @@ func (self *Server) processMsgEvent() error { self.fetchProposal(msgBlkNum, pMsg.EndorsedProposer) } + if err := self.verifyProposalEndorsementMessage(msgBlkNum, pMsg.EndorsedProposer, pMsg.EndorsedBlockHash, pMsg.EndorseForEmpty); err != nil { + log.Errorf("failed to verify endorsement msg (%d): %s", msgBlkNum, err) + return nil + } + // add endorse to block-pool if err := self.blockPool.newBlockEndorsement(pMsg); err != nil { log.Errorf("failed to add endorsement (%d): %s", msgBlkNum, err) @@ -1379,6 +1384,12 @@ func (self *Server) processMsgEvent() error { // sealProposal(msg.BlockHash) // else if WaitCommitsTimer has not started: // start WaitCommitsTimer + + if err := self.verifyProposalEndorsementMessage(msgBlkNum, pMsg.BlockProposer, pMsg.CommitBlockHash, pMsg.CommitForEmpty); err != nil { + log.Errorf("failed to verify commit msg (%d): %s", msgBlkNum, err) + return nil + } + if err := self.blockPool.newBlockCommitment(pMsg); err != nil { log.Errorf("failed to add commit msg (%d): %s", msgBlkNum, err) return nil @@ -1430,6 +1441,19 @@ func (self *Server) processMsgEvent() error { return nil } +func (self *Server) verifyProposalEndorsementMessage(blockNum uint32, proposer uint32, blockHash common.Uint256, forEmpty bool) (err error) { + proposal := self.findBlockProposal(blockNum, proposer, forEmpty) + if proposal == nil { + return fmt.Errorf("missing relevant proposal, block height %v, hash %s, proposer %v", blockNum, blockHash.ToHexString(), proposal) + } + proposedBlockHash := proposal.Block.Block.Hash() + if !bytes.Equal(proposedBlockHash[:], blockHash[:]) { + return fmt.Errorf("unmatched proposal from same proposer, block height %v, hash %s, existing hash %s proposer %v", blockNum, + blockHash.ToHexString(), proposedBlockHash, proposal) + } + return +} + func (self *Server) actionLoop() { self.quitWg.Add(1) defer self.quitWg.Done()