From 82437d39d74bd7afccc5a862a7605f8a15d04a96 Mon Sep 17 00:00:00 2001 From: JimboJ <40345116+jimjbrettj@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:35:47 -0700 Subject: [PATCH] client(consensus/grandpa): implement grandpa justification logic (#3454) Co-authored-by: Timothy Wu --- pkg/finality-grandpa/lib.go | 11 +++-------- pkg/finality-grandpa/round_test.go | 8 ++++---- pkg/finality-grandpa/voter_test.go | 6 +++--- pkg/finality-grandpa/voting_round.go | 2 +- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/pkg/finality-grandpa/lib.go b/pkg/finality-grandpa/lib.go index 39246c4985..2226182f14 100644 --- a/pkg/finality-grandpa/lib.go +++ b/pkg/finality-grandpa/lib.go @@ -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, @@ -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]) { diff --git a/pkg/finality-grandpa/round_test.go b/pkg/finality-grandpa/round_test.go index 5c60cf2cf6..19b3a39cb4 100644 --- a/pkg/finality-grandpa/round_test.go +++ b/pkg/finality-grandpa/round_test.go @@ -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, }, @@ -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, }, @@ -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, }, @@ -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, }, diff --git a/pkg/finality-grandpa/voter_test.go b/pkg/finality-grandpa/voter_test.go index fd92f9bdc9..e8acb2a6f9 100644 --- a/pkg/finality-grandpa/voter_test.go +++ b/pkg/finality-grandpa/voter_test.go @@ -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 @@ -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 @@ -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 { diff --git a/pkg/finality-grandpa/voting_round.go b/pkg/finality-grandpa/voting_round.go index 47998f3134..92d94516a3 100644 --- a/pkg/finality-grandpa/voting_round.go +++ b/pkg/finality-grandpa/voting_round.go @@ -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)