Skip to content
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

feat(lib/grandpa): improve determinePrevote method #2745

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,44 @@ func (s *Service) deleteVote(key ed25519.PublicKeyBytes, stage Subround) {
}
}

// implements `BeforeBestBlockBy` a custom voting rule that guarantees that our vote is always
// behind the best block by at least N blocks, unless the base number is < N blocks behind the
// best, in which case it votes for the base.
// (https://github.com/paritytech/substrate/blob/1fd71c7845d6c28c532795ec79106d959dd1fe30/client/finality-grandpa/src/voting_rule.rs#L92)
func (s *Service) determineBestHeaderToPrevote(finalizedHeader, bestBlockHeader *types.Header) (
headerToPrevote *types.Header, err error) {
gensisHash := s.blockState.GenesisHash()
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
isGenesisHash := gensisHash.Equal(bestBlockHeader.Hash())
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
if isGenesisHash || finalizedHeader.Hash().Equal(bestBlockHeader.Hash()) {
return bestBlockHeader, nil
}

isDescendant, err := s.blockState.IsDescendantOf(finalizedHeader.Hash(), bestBlockHeader.Hash())
if err != nil {
return headerToPrevote, fmt.Errorf("determine ancestry: %w", err)
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
}

if !isDescendant {
return headerToPrevote, fmt.Errorf("%w: %s is not ancestor of %s",
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
blocktree.ErrNoCommonAncestor, bestBlockHeader.Hash().Short(), bestBlockHeader.Hash().Short())
}

headerToPrevote = bestBlockHeader
for i := 0; i < 2; i++ {
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
headerToPrevote, err := s.blockState.GetHeader(headerToPrevote.ParentHash)
if err != nil {
return headerToPrevote, fmt.Errorf("get parent header: %w", err)
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
}

isGenesisHash := gensisHash.Equal(headerToPrevote.Hash())
if finalizedHeader.Hash().Equal(headerToPrevote.Hash()) || isGenesisHash {
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
break
}
}

return headerToPrevote, nil
}

// determinePreVote determines what block is our pre-voted block for the current round
func (s *Service) determinePreVote() (*Vote, error) {
var vote *Vote
Expand All @@ -688,19 +726,15 @@ func (s *Service) determinePreVote() (*Vote, error) {
return nil, fmt.Errorf("cannot get best block header: %w", err)
}

// if we receive a vote message from the primary with a
// block that's greater than or equal to the current pre-voted block
// and greater than the best final candidate from the last round, we choose that.
// otherwise, we simply choose the head of our chain.
primary := s.derivePrimary()
prm, has := s.loadVote(primary.PublicKeyBytes(), prevote)
if has && prm.Vote.Number >= uint32(s.head.Number) {
vote = &prm.Vote
} else {
vote = NewVoteFromHeader(bestBlockHeader)
headerToPrevote, err := s.determineBestHeaderToPrevote(s.head, bestBlockHeader)
if err != nil {
return nil, fmt.Errorf("determine best hash to prevote: %w", err)
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
}

nextChange, err := s.grandpaState.NextGrandpaAuthorityChange(bestBlockHeader.Hash(), bestBlockHeader.Number)
vote = NewVoteFromHeader(headerToPrevote)
nextChange, err := s.grandpaState.NextGrandpaAuthorityChange(
headerToPrevote.Hash(), headerToPrevote.Number)

if errors.Is(err, state.ErrNoNextAuthorityChange) {
return vote, nil
} else if err != nil {
Expand Down Expand Up @@ -962,7 +996,8 @@ func (s *Service) getPreVotedBlock() (Vote, error) {

// if there are multiple, find the one with the highest number and return it
highest := Vote{
Number: uint32(0),
Hash: s.head.Hash(),
Number: uint32(s.head.Number),
}

for h, n := range blocks {
Expand Down Expand Up @@ -1006,7 +1041,8 @@ func (s *Service) getGrandpaGHOST() (Vote, error) {

// if there are multiple, find the one with the highest number and return it
highest := Vote{
Number: uint32(0),
Hash: s.head.Hash(),
Number: uint32(s.head.Number),
}

for h, n := range blocks {
Expand Down
4 changes: 4 additions & 0 deletions lib/grandpa/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type VoteMessage struct {
Message SignedMessage
}

func (v VoteMessage) String() string {
return fmt.Sprintf("round=%d, setID=%d, message=%s", v.Round, v.SetID, v.Message)
}

// Index Returns VDT index
func (VoteMessage) Index() uint { return 0 }

Expand Down
1 change: 0 additions & 1 deletion lib/grandpa/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package grandpa

import (
//"fmt"
"math/rand"
"sync"
"testing"
Expand Down