From fb219ba224300db08253e67d9cd62d32093cc0da Mon Sep 17 00:00:00 2001 From: kourin Date: Thu, 11 Aug 2022 17:12:05 +0900 Subject: [PATCH] Update interface of IsValidCommittedSeal and InsertBlock by go-ibft's update (#677) * Update interface of IsValidCommittedSeal and InsertBlock based on go-ibft's update * Update go-ibft * Additional check of signer in IsValidCommittedSeal --- consensus/ibft/consensus_backend.go | 13 +++++++++++-- consensus/ibft/verifier.go | 18 ++++++++++++++---- go.mod | 2 +- go.sum | 2 ++ .../0xPolygon/go-ibft/core/backend.go | 5 +++-- .../github.com/0xPolygon/go-ibft/core/ibft.go | 7 ++++--- .../github.com/0xPolygon/go-ibft/core/state.go | 9 +++++---- .../0xPolygon/go-ibft/messages/helpers.go | 17 +++++++++++++---- vendor/modules.txt | 2 +- 9 files changed, 54 insertions(+), 21 deletions(-) diff --git a/consensus/ibft/consensus_backend.go b/consensus/ibft/consensus_backend.go index e2f4dd00b3..e25ed19137 100644 --- a/consensus/ibft/consensus_backend.go +++ b/consensus/ibft/consensus_backend.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "github.com/0xPolygon/go-ibft/messages" "github.com/0xPolygon/polygon-edge/consensus" "github.com/0xPolygon/polygon-edge/state" "github.com/0xPolygon/polygon-edge/types" @@ -42,7 +43,10 @@ func (i *backendIBFT) BuildProposal(blockNumber uint64) []byte { return block.MarshalRLP() } -func (i *backendIBFT) InsertBlock(proposal []byte, committedSeals [][]byte) { +func (i *backendIBFT) InsertBlock( + proposal []byte, + committedSeals []*messages.CommittedSeal, +) { newBlock := &types.Block{} if err := newBlock.UnmarshalRLP(proposal); err != nil { i.logger.Error("cannot unmarshal proposal", "err", err) @@ -50,8 +54,13 @@ func (i *backendIBFT) InsertBlock(proposal []byte, committedSeals [][]byte) { return } + seals := make([][]byte, len(committedSeals)) + for idx := range committedSeals { + seals[idx] = committedSeals[idx].Signature + } + // Push the committed seals to the header - header, err := writeCommittedSeals(newBlock.Header, committedSeals) + header, err := writeCommittedSeals(newBlock.Header, seals) if err != nil { i.logger.Error("cannot write committed seals", "err", err) diff --git a/consensus/ibft/verifier.go b/consensus/ibft/verifier.go index 20b50cd388..c00e69a7be 100644 --- a/consensus/ibft/verifier.go +++ b/consensus/ibft/verifier.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/0xPolygon/go-ibft/messages" protoIBFT "github.com/0xPolygon/go-ibft/messages/proto" "github.com/0xPolygon/polygon-edge/types" ) @@ -117,17 +118,26 @@ func (i *backendIBFT) IsValidProposalHash(proposal, hash []byte) bool { return bytes.Equal(blockHash, hash) } -func (i *backendIBFT) IsValidCommittedSeal(proposalHash, seal []byte) bool { +func (i *backendIBFT) IsValidCommittedSeal( + proposalHash []byte, + committedSeal *messages.CommittedSeal, +) bool { // seal was generated based on the proposal hash - validator, err := ecrecoverImpl(seal, wrapCommitHash(proposalHash)) + signer, err := ecrecoverImpl(committedSeal.Signature, wrapCommitHash(proposalHash)) if err != nil { i.logger.Error("unable to recover seal", "err", err) return false } - if !i.activeValidatorSet.Includes(validator) { - i.logger.Error("seal generated by validator not in the active set", "validator", validator.String()) + if signer != types.BytesToAddress(committedSeal.Signer) { + i.logger.Error("mismatch between signer and message sender", "err", err) + + return false + } + + if !i.activeValidatorSet.Includes(signer) { + i.logger.Error("seal generated by validator not in the active set", "validator", signer.String()) return false } diff --git a/go.mod b/go.mod index 503065f996..b59421051a 100644 --- a/go.mod +++ b/go.mod @@ -60,7 +60,7 @@ require ( lukechampine.com/blake3 v1.1.7 // indirect ) -require github.com/0xPolygon/go-ibft v0.0.0-20220729102524-0f95a9c3a08c +require github.com/0xPolygon/go-ibft v0.0.0-20220810095021-e43142f8d267 require ( cloud.google.com/go v0.102.0 // indirect diff --git a/go.sum b/go.sum index 6df35482b4..3ea431f88f 100644 --- a/go.sum +++ b/go.sum @@ -69,6 +69,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= github.com/0xPolygon/go-ibft v0.0.0-20220729102524-0f95a9c3a08c h1:y9Fw+byHhcrV8378z20d2WlyIoHOZiZbPIVAQ9VDuyw= github.com/0xPolygon/go-ibft v0.0.0-20220729102524-0f95a9c3a08c/go.mod h1:pXhBaK+CcHr3eUAWynKyHGTxgDwpJ3O1tvbGthN+pN4= +github.com/0xPolygon/go-ibft v0.0.0-20220810095021-e43142f8d267 h1:+mFLx9IKW16fOcTKjZjkom3TGnihOuPwYAz2c6+UUWQ= +github.com/0xPolygon/go-ibft v0.0.0-20220810095021-e43142f8d267/go.mod h1:QPrugDXgsCFy2FeCJ0YokPrnyi1GoLhDj/PLO1dSoNY= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= diff --git a/vendor/github.com/0xPolygon/go-ibft/core/backend.go b/vendor/github.com/0xPolygon/go-ibft/core/backend.go index 75bd355574..258b12084c 100644 --- a/vendor/github.com/0xPolygon/go-ibft/core/backend.go +++ b/vendor/github.com/0xPolygon/go-ibft/core/backend.go @@ -1,6 +1,7 @@ package core import ( + "github.com/0xPolygon/go-ibft/messages" "github.com/0xPolygon/go-ibft/messages/proto" ) @@ -42,7 +43,7 @@ type Verifier interface { IsValidProposalHash(proposal, hash []byte) bool // IsValidCommittedSeal checks if the seal for the proposal is valid - IsValidCommittedSeal(proposal, seal []byte) bool + IsValidCommittedSeal(proposal []byte, committedSeal *messages.CommittedSeal) bool } // Backend defines an interface all backend implementations @@ -55,7 +56,7 @@ type Backend interface { BuildProposal(blockNumber uint64) []byte // InsertBlock inserts a proposal with the specified committed seals - InsertBlock(proposal []byte, committedSeals [][]byte) + InsertBlock(proposal []byte, committedSeals []*messages.CommittedSeal) // ID returns the validator's ID ID() []byte diff --git a/vendor/github.com/0xPolygon/go-ibft/core/ibft.go b/vendor/github.com/0xPolygon/go-ibft/core/ibft.go index 958fd3312a..69778601cb 100644 --- a/vendor/github.com/0xPolygon/go-ibft/core/ibft.go +++ b/vendor/github.com/0xPolygon/go-ibft/core/ibft.go @@ -4,11 +4,12 @@ import ( "bytes" "context" "errors" - "github.com/0xPolygon/go-ibft/messages" - "github.com/0xPolygon/go-ibft/messages/proto" "math" "sync" "time" + + "github.com/0xPolygon/go-ibft/messages" + "github.com/0xPolygon/go-ibft/messages/proto" ) type Logger interface { @@ -109,7 +110,7 @@ func NewIBFT( Height: 0, Round: 0, }, - seals: make([][]byte, 0), + seals: make([]*messages.CommittedSeal, 0), roundStarted: false, name: newRound, }, diff --git a/vendor/github.com/0xPolygon/go-ibft/core/state.go b/vendor/github.com/0xPolygon/go-ibft/core/state.go index 70544e1993..2c773dd8ee 100644 --- a/vendor/github.com/0xPolygon/go-ibft/core/state.go +++ b/vendor/github.com/0xPolygon/go-ibft/core/state.go @@ -1,9 +1,10 @@ package core import ( + "sync" + "github.com/0xPolygon/go-ibft/messages" "github.com/0xPolygon/go-ibft/messages/proto" - "sync" ) type stateType uint8 @@ -47,7 +48,7 @@ type state struct { proposalMessage *proto.Message // validated commit seals - seals [][]byte + seals []*messages.CommittedSeal // flags for different states roundStarted bool @@ -142,7 +143,7 @@ func (s *state) getProposal() []byte { return nil } -func (s *state) getCommittedSeals() [][]byte { +func (s *state) getCommittedSeals() []*messages.CommittedSeal { s.RLock() defer s.RUnlock() @@ -177,7 +178,7 @@ func (s *state) setView(view *proto.View) { s.view = view } -func (s *state) setCommittedSeals(seals [][]byte) { +func (s *state) setCommittedSeals(seals []*messages.CommittedSeal) { s.Lock() defer s.Unlock() diff --git a/vendor/github.com/0xPolygon/go-ibft/messages/helpers.go b/vendor/github.com/0xPolygon/go-ibft/messages/helpers.go index 51485949b1..cc11693f4b 100644 --- a/vendor/github.com/0xPolygon/go-ibft/messages/helpers.go +++ b/vendor/github.com/0xPolygon/go-ibft/messages/helpers.go @@ -2,12 +2,18 @@ package messages import ( "bytes" + "github.com/0xPolygon/go-ibft/messages/proto" ) +type CommittedSeal struct { + Signer []byte + Signature []byte +} + // ExtractCommittedSeals extracts the committed seals from the passed in messages -func ExtractCommittedSeals(commitMessages []*proto.Message) [][]byte { - committedSeals := make([][]byte, 0) +func ExtractCommittedSeals(commitMessages []*proto.Message) []*CommittedSeal { + committedSeals := make([]*CommittedSeal, 0) for _, commitMessage := range commitMessages { if commitMessage.Type != proto.MessageType_COMMIT { @@ -21,10 +27,13 @@ func ExtractCommittedSeals(commitMessages []*proto.Message) [][]byte { } // ExtractCommittedSeal extracts the committed seal from the passed in message -func ExtractCommittedSeal(commitMessage *proto.Message) []byte { +func ExtractCommittedSeal(commitMessage *proto.Message) *CommittedSeal { commitData, _ := commitMessage.Payload.(*proto.Message_CommitData) - return commitData.CommitData.CommittedSeal + return &CommittedSeal{ + Signer: commitMessage.From, + Signature: commitData.CommitData.CommittedSeal, + } } // ExtractCommitHash extracts the commit proposal hash from the passed in message diff --git a/vendor/modules.txt b/vendor/modules.txt index c1c8a7ddc8..9b14585fd7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -11,7 +11,7 @@ cloud.google.com/go/iam ## explicit; go 1.16 cloud.google.com/go/secretmanager/apiv1 cloud.google.com/go/secretmanager/internal -# github.com/0xPolygon/go-ibft v0.0.0-20220729102524-0f95a9c3a08c +# github.com/0xPolygon/go-ibft v0.0.0-20220810095021-e43142f8d267 ## explicit; go 1.17 github.com/0xPolygon/go-ibft/core github.com/0xPolygon/go-ibft/messages