Skip to content

Commit

Permalink
client(consensus/grandpa): implement grandpa justification logic (#3454)
Browse files Browse the repository at this point in the history
Co-authored-by: Timothy Wu <[email protected]>
  • Loading branch information
2 people authored and EclesioMeloJunior committed Jul 9, 2024
1 parent 5bf7fcf commit 82437d3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
11 changes: 3 additions & 8 deletions pkg/finality-grandpa/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ type Equivocation[ID constraints.Ordered, Vote, Signature comparable] struct {

// Message is a protocol message or vote.
type Message[Hash, Number any] struct {
value any
Value any
}

// Target returns the target block of the vote.
func (m Message[H, N]) Target() HashNumber[H, N] {
switch message := m.value.(type) {
switch message := m.Value.(type) {
case Prevote[H, N]:
return HashNumber[H, N]{
message.TargetHash,
Expand All @@ -80,18 +80,13 @@ func (m Message[H, N]) Target() HashNumber[H, N] {
}
}

// Value returns the message constrained by `Messages`
func (m Message[H, N]) Value() any {
return m.value
}

// Messages is the interface constraint for `Message`
type Messages[Hash, Number any] interface {
Prevote[Hash, Number] | Precommit[Hash, Number] | PrimaryPropose[Hash, Number]
}

func setMessage[Hash, Number any, T Messages[Hash, Number]](m *Message[Hash, Number], val T) {
m.value = val
m.Value = val
}

func newMessage[Hash, Number any, T Messages[Hash, Number]](val T) (m Message[Hash, Number]) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/finality-grandpa/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestRound_HistoricalVotesWorks(t *testing.T) {
seen: []SignedMessage[string, uint32, string, string]{
{
Message: Message[string, uint32]{
value: Prevote[string, uint32]{
Value: Prevote[string, uint32]{
TargetHash: "FC",
TargetNumber: 10,
},
Expand All @@ -288,7 +288,7 @@ func TestRound_HistoricalVotesWorks(t *testing.T) {
},
{
Message: Message[string, uint32]{
value: Prevote[string, uint32]{
Value: Prevote[string, uint32]{
TargetHash: "EA",
TargetNumber: 7,
},
Expand All @@ -298,7 +298,7 @@ func TestRound_HistoricalVotesWorks(t *testing.T) {
},
{
Message: Message[string, uint32]{
value: Precommit[string, uint32]{
Value: Precommit[string, uint32]{
TargetHash: "EA",
TargetNumber: 7,
},
Expand All @@ -308,7 +308,7 @@ func TestRound_HistoricalVotesWorks(t *testing.T) {
},
{
Message: Message[string, uint32]{
value: Prevote[string, uint32]{
Value: Prevote[string, uint32]{
TargetHash: "EC",
TargetNumber: 10,
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/finality-grandpa/voter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func TestVoter_BroadcastCommitOnlyIfNewer(t *testing.T) {
item := <-roundIn
// wait for a prevote
assert.NoError(t, item.Error)
assert.IsType(t, Prevote[string, uint32]{}, item.SignedMessage.Message.value)
assert.IsType(t, Prevote[string, uint32]{}, item.SignedMessage.Message.Value)
assert.Equal(t, localID, item.SignedMessage.ID)

// send our prevote and precommit
Expand All @@ -310,7 +310,7 @@ waitForPrecommit:
item = <-roundIn
// wait for a precommit
assert.NoError(t, item.Error)
switch item.SignedMessage.Message.value.(type) {
switch item.SignedMessage.Message.Value.(type) {
case Precommit[string, uint32]:
if item.SignedMessage.ID == localID {
break waitForPrecommit
Expand Down Expand Up @@ -649,7 +649,7 @@ waitForPrevote:
t.Errorf("wtf?")
}

msg := sme.SignedMessage.Message.Value()
msg := sme.SignedMessage.Message.Value
switch msg.(type) {
case Prevote[string, uint32]:
if sme.SignedMessage.ID == localID {
Expand Down
2 changes: 1 addition & 1 deletion pkg/finality-grandpa/voting_round.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (vr *votingRound[Hash, Number, Signature, ID, E]) handleVote(vote SignedMes
return nil
}

switch message := message.Value().(type) {
switch message := message.Value.(type) {
case Prevote[Hash, Number]:
prevote := message
importResult, err := vr.votes.importPrevote(vr.env, prevote, vote.ID, vote.Signature)
Expand Down

0 comments on commit 82437d3

Please sign in to comment.