-
Notifications
You must be signed in to change notification settings - Fork 140
fix(lib/grandpa): verify equivocatory votes in grandpa justifications #2486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kishansagathiya
merged 11 commits into
development
from
kishan/fix/verify-equivocatory-votes
May 4, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8daf9fc
fix(lib/grandpa): verify equivocatory votes in grandpa justifications
kishansagathiya 9a3a846
more changes
kishansagathiya aec59c3
Merge branch 'development' into kishan/fix/verify-equivocatory-votes
kishansagathiya decf6df
adjusting with latest changes
kishansagathiya c4980d4
Update lib/grandpa/message_handler.go
kishansagathiya d3ebcc7
addressed reviews
kishansagathiya 4e21f1c
Merge branch 'kishan/fix/verify-equivocatory-votes' of github.com:Cha…
kishansagathiya 47ed623
addressed reviews
kishansagathiya 286fb93
remove comment
kishansagathiya 9141e31
Merge branch 'development' into kishan/fix/verify-equivocatory-votes
kishansagathiya 8cb7846
meh
kishansagathiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,23 +302,40 @@ func getEquivocatoryVoters(votes []AuthData) (map[ed25519.PublicKeyBytes]struct{ | |
return eqvVoters, nil | ||
} | ||
|
||
func isDescendantOfHighestFinalisedBlock(blockState BlockState, hash common.Hash) (bool, error) { | ||
highestHeader, err := blockState.GetHighestFinalisedHeader() | ||
if err != nil { | ||
return false, fmt.Errorf("could not get highest finalised header: %w", err) | ||
} | ||
|
||
return blockState.IsDescendantOf(highestHeader.Hash(), hash) | ||
} | ||
|
||
func (h *MessageHandler) verifyCommitMessageJustification(fm *CommitMessage) error { | ||
if len(fm.Precommits) != len(fm.AuthData) { | ||
return ErrPrecommitSignatureMismatch | ||
} | ||
|
||
if fm.SetID != h.grandpa.state.setID { | ||
return fmt.Errorf("%w: grandpa state set id %d, set id in the commit message %d", | ||
ErrSetIDMismatch, h.grandpa.state.setID, fm.SetID) | ||
} | ||
|
||
isDescendant, err := isDescendantOfHighestFinalisedBlock(h.blockState, fm.Vote.Hash) | ||
if err != nil { | ||
return fmt.Errorf("cannot verify ancestry of highest finalised block: %w", err) | ||
} | ||
if !isDescendant { | ||
return errVoteBlockMismatch | ||
} | ||
|
||
eqvVoters, err := getEquivocatoryVoters(fm.AuthData) | ||
if err != nil { | ||
return fmt.Errorf("could not get valid equivocatory voters: %w", err) | ||
} | ||
|
||
var count int | ||
for i, pc := range fm.Precommits { | ||
_, ok := eqvVoters[fm.AuthData[i].AuthorityID] | ||
if ok { | ||
continue | ||
} | ||
|
||
just := &SignedVote{ | ||
Vote: pc, | ||
Signature: fm.AuthData[i].Signature, | ||
|
@@ -327,7 +344,8 @@ func (h *MessageHandler) verifyCommitMessageJustification(fm *CommitMessage) err | |
|
||
err := h.verifyJustification(just, fm.Round, h.grandpa.state.setID, precommit) | ||
if err != nil { | ||
logger.Errorf("could not verify justification: %s", err) | ||
logger.Errorf("failed to verify justification for vote from authority id %s, for block hash %s: %s", | ||
just.AuthorityID.String(), just.Vote.Hash, err) | ||
continue | ||
} | ||
|
||
|
@@ -337,6 +355,15 @@ func (h *MessageHandler) verifyCommitMessageJustification(fm *CommitMessage) err | |
continue | ||
} | ||
|
||
err = verifyBlockHashAgainstBlockNumber(h.blockState, pc.Hash, uint(pc.Number)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, ok := eqvVoters[fm.AuthData[i].AuthorityID]; ok { | ||
continue | ||
} | ||
|
||
if isDescendant { | ||
count++ | ||
} | ||
|
@@ -425,23 +452,19 @@ func (h *MessageHandler) verifyPreVoteJustification(msg *CatchUpResponse) (commo | |
} | ||
|
||
func (h *MessageHandler) verifyPreCommitJustification(msg *CatchUpResponse) error { | ||
for _, pcj := range msg.PreCommitJustification { | ||
err := verifyBlockHashAgainstBlockNumber(h.blockState, pcj.Vote.Hash, uint(pcj.Vote.Number)) | ||
if err != nil { | ||
if errors.Is(err, chaindb.ErrKeyNotFound) { | ||
h.grandpa.tracker.addCatchUpResponse(msg) | ||
logger.Infof("we might not have synced to the given block %s yet: %s", pcj.Vote.Hash, err) | ||
continue | ||
} | ||
return err | ||
} | ||
} | ||
|
||
auths := make([]AuthData, len(msg.PreCommitJustification)) | ||
for i, pcj := range msg.PreCommitJustification { | ||
auths[i] = AuthData{AuthorityID: pcj.AuthorityID} | ||
} | ||
|
||
isDescendant, err := isDescendantOfHighestFinalisedBlock(h.blockState, msg.Hash) | ||
if err != nil { | ||
return err | ||
} | ||
if !isDescendant { | ||
return errVoteBlockMismatch | ||
} | ||
|
||
eqvVoters, err := getEquivocatoryVoters(auths) | ||
if err != nil { | ||
return fmt.Errorf("could not get valid equivocatory voters: %w", err) | ||
|
@@ -452,12 +475,24 @@ func (h *MessageHandler) verifyPreCommitJustification(msg *CatchUpResponse) erro | |
for idx := range msg.PreCommitJustification { | ||
just := &msg.PreCommitJustification[idx] | ||
|
||
if _, ok := eqvVoters[just.AuthorityID]; ok { | ||
continue | ||
err = verifyBlockHashAgainstBlockNumber(h.blockState, just.Vote.Hash, uint(just.Vote.Number)) | ||
if err != nil { | ||
if errors.Is(err, chaindb.ErrKeyNotFound) { | ||
h.grandpa.tracker.addCatchUpResponse(msg) | ||
logger.Infof("we might not have synced to the given block %s yet: %s", just.Vote.Hash, err) | ||
kishansagathiya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
return err | ||
} | ||
|
||
err := h.verifyJustification(just, msg.Round, msg.SetID, precommit) | ||
if err != nil { | ||
logger.Errorf("could not verify precommit justification for block %s from authority %s: %s", | ||
just.Vote.Hash.String(), just.AuthorityID.String(), err) | ||
continue | ||
} | ||
|
||
if _, ok := eqvVoters[just.AuthorityID]; ok { | ||
continue | ||
} | ||
|
||
|
@@ -540,6 +575,15 @@ func (s *Service) VerifyBlockJustification(hash common.Hash, justification []byt | |
return fmt.Errorf("already have finalised block with setID=%d and round=%d", setID, fj.Round) | ||
} | ||
|
||
isDescendant, err := isDescendantOfHighestFinalisedBlock(s.blockState, fj.Commit.Hash) | ||
if err != nil { | ||
return err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit maybe wrap the error |
||
} | ||
|
||
if !isDescendant { | ||
return errVoteBlockMismatch | ||
} | ||
|
||
auths, err := s.grandpaState.GetAuthorities(setID) | ||
if err != nil { | ||
return fmt.Errorf("cannot get authorities for set ID: %w", err) | ||
|
@@ -570,10 +614,6 @@ func (s *Service) VerifyBlockJustification(hash common.Hash, justification []byt | |
setID, fj.Round, fj.Commit.Hash, fj.Commit.Number, len(fj.Commit.Precommits)) | ||
|
||
for _, just := range fj.Commit.Precommits { | ||
if _, ok := equivocatoryVoters[just.AuthorityID]; ok { | ||
continue | ||
} | ||
|
||
// check if vote was for descendant of committed block | ||
isDescendant, err := s.blockState.IsDescendantOf(hash, just.Vote.Hash) | ||
if err != nil { | ||
|
@@ -613,6 +653,10 @@ func (s *Service) VerifyBlockJustification(hash common.Hash, justification []byt | |
return ErrInvalidSignature | ||
} | ||
|
||
if _, ok := equivocatoryVoters[just.AuthorityID]; ok { | ||
continue | ||
} | ||
|
||
count++ | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.