diff --git a/client/consensus/grandpa/aux_schema_test.go b/client/consensus/grandpa/aux_schema_test.go index 7ecafc5722..87e3bd66c5 100644 --- a/client/consensus/grandpa/aux_schema_test.go +++ b/client/consensus/grandpa/aux_schema_test.go @@ -275,7 +275,7 @@ func TestWriteVoterSetState(t *testing.T) { } completedRounds := NewCompletedRounds[string, uint](completedRound, 1, authorities) - currentRounds := make(map[uint64]hasVoted[string, uint]) + currentRounds := make(map[uint64]hasVoted[string, uint, uint]) liveState := voterSetStateLive[string, uint, uint, uint]{ CompletedRounds: completedRounds, @@ -326,7 +326,7 @@ func TestWriteConcludedRound(t *testing.T) { } completedRounds := NewCompletedRounds[string, uint](completedRound, 1, authorities) - currentRounds := make(map[uint64]hasVoted[string, uint]) + currentRounds := make(map[uint64]hasVoted[string, uint, uint]) liveState := voterSetStateLive[string, uint, uint, uint]{ CompletedRounds: completedRounds, diff --git a/client/consensus/grandpa/environment.go b/client/consensus/grandpa/environment.go index b412e3d110..f6a97863c6 100644 --- a/client/consensus/grandpa/environment.go +++ b/client/consensus/grandpa/environment.go @@ -5,7 +5,7 @@ package grandpa import ( "fmt" - "github.com/ChainSafe/gossamer/lib/crypto/ed25519" + grandpa "github.com/ChainSafe/gossamer/pkg/finality-grandpa" "github.com/ChainSafe/gossamer/pkg/scale" "golang.org/x/exp/constraints" @@ -107,7 +107,7 @@ func (cr *completedRounds[H, N, ID, Sig]) push(compRound completedRound[H, N, ID // CurrentRounds A map with voter status information for currently live rounds, // which votes have we cast and what are they. // TODO convert to btree after #3480 is implemented -type CurrentRounds[H comparable, N constraints.Unsigned] map[uint64]hasVoted[H, N] +type CurrentRounds[H comparable, N constraints.Unsigned, ID AuthorityID] map[uint64]hasVoted[H, N, ID] // A tracker for the rounds that we are actively participating on (i.e. voting) // and the authority id under which we are doing it. @@ -171,11 +171,11 @@ func (svss *SharedVoterSetState[H, N, ID, Sig]) finishedVotingOn(round uint64) { } // Return vote status information for the current round -func (svss *SharedVoterSetState[H, N, ID, Sig]) hasVoted(round uint64) (hasVoted[H, N], error) { +func (svss *SharedVoterSetState[H, N, ID, Sig]) hasVoted(round uint64) (hasVoted[H, N, ID], error) { svss.Inner.Lock() defer svss.Inner.Unlock() - hasNotVotedFunc := func(newHasVoted hasVoted[H, N]) (hasVoted[H, N], error) { + hasNotVotedFunc := func(newHasVoted hasVoted[H, N, ID]) (hasVoted[H, N, ID], error) { err := newHasVoted.Set(no{}) if err != nil { return newHasVoted, err @@ -184,7 +184,7 @@ func (svss *SharedVoterSetState[H, N, ID, Sig]) hasVoted(round uint64) (hasVoted return newHasVoted, nil } - newHasVoted := hasVoted[H, N]{} + newHasVoted := hasVoted[H, N, ID]{} newHasVoted = newHasVoted.New() vss, err := svss.Inner.Inner.Value() @@ -204,7 +204,7 @@ func (svss *SharedVoterSetState[H, N, ID, Sig]) hasVoted(round uint64) (hasVoted return newHasVoted, err } switch hasVotedValue.(type) { - case yes[H, N]: + case yes[H, N, ID]: return hasVoted, nil } } @@ -242,7 +242,7 @@ func (tve voterSetState[H, N, ID, Sig]) New() voterSetState[H, N, ID, Sig] { CompletedRounds: completedRounds[H, N, ID, Sig]{ Rounds: make([]completedRound[H, N, ID, Sig], 0, numLastCompletedRounds), }, - CurrentRounds: make(map[uint64]hasVoted[H, N]), // init the map + CurrentRounds: make(map[uint64]hasVoted[H, N, ID]), // init the map }, voterSetStatePaused[H, N, ID, Sig]{}) if err != nil { panic(err) @@ -257,7 +257,7 @@ func NewVoterSetState[H comparable, N constraints.Unsigned, ID AuthorityID, Sig CompletedRounds: completedRounds[H, N, ID, Sig]{ Rounds: make([]completedRound[H, N, ID, Sig], 0, numLastCompletedRounds), }, - CurrentRounds: make(map[uint64]hasVoted[H, N]), // init the map + CurrentRounds: make(map[uint64]hasVoted[H, N, ID]), // init the map }, voterSetStatePaused[H, N, ID, Sig]{}) if err != nil { panic(err) @@ -280,10 +280,10 @@ func NewLiveVoterSetState[H comparable, N constraints.Unsigned, ID AuthorityID, authSet, ) //currentRounds := make(map[uint64]hasVoted[string, uint]) - currentRounds := CurrentRounds[H, N]( - make(map[uint64]hasVoted[H, N]), + currentRounds := CurrentRounds[H, N, ID]( + make(map[uint64]hasVoted[H, N, ID]), ) - hasVoted := hasVoted[H, N]{} + hasVoted := hasVoted[H, N, ID]{} hasVoted = hasVoted.New() err := hasVoted.Set(no{}) if err != nil { @@ -339,10 +339,10 @@ func (tve *voterSetState[H, N, ID, Sig]) lastCompletedRound() (completedRound[H, // withCurrentRound Returns the voter set state validating that it includes the given round // in current rounds and that the voter isn't paused -func (tve *voterSetState[H, N, ID, Sig]) withCurrentRound(round uint64) (completedRounds[H, N, ID, Sig], CurrentRounds[H, N], error) { +func (tve *voterSetState[H, N, ID, Sig]) withCurrentRound(round uint64) (completedRounds[H, N, ID, Sig], CurrentRounds[H, N, ID], error) { value, err := tve.Value() if err != nil { - return completedRounds[H, N, ID, Sig]{}, CurrentRounds[H, N]{}, err + return completedRounds[H, N, ID, Sig]{}, CurrentRounds[H, N, ID]{}, err } switch v := value.(type) { case voterSetStateLive[H, N, ID, Sig]: @@ -350,9 +350,9 @@ func (tve *voterSetState[H, N, ID, Sig]) withCurrentRound(round uint64) (complet if contains { return v.CompletedRounds, v.CurrentRounds, nil } - return completedRounds[H, N, ID, Sig]{}, CurrentRounds[H, N]{}, fmt.Errorf("voter acting on a live round we are not tracking") + return completedRounds[H, N, ID, Sig]{}, CurrentRounds[H, N, ID]{}, fmt.Errorf("voter acting on a live round we are not tracking") case voterSetStatePaused[H, N, ID, Sig]: - return completedRounds[H, N, ID, Sig]{}, CurrentRounds[H, N]{}, fmt.Errorf("voter acting while in paused state") + return completedRounds[H, N, ID, Sig]{}, CurrentRounds[H, N, ID]{}, fmt.Errorf("voter acting while in paused state") default: panic("completedRounds: invalid voter set state") } @@ -363,7 +363,7 @@ type voterSetStateLive[H comparable, N constraints.Unsigned, ID AuthorityID, Sig // The previously completed rounds CompletedRounds completedRounds[H, N, ID, Sig] // Voter status for the currently live rounds. - CurrentRounds CurrentRounds[H, N] + CurrentRounds CurrentRounds[H, N, ID] } // Index returns VDT index @@ -379,30 +379,30 @@ type voterSetStatePaused[H comparable, N constraints.Unsigned, ID AuthorityID, S func (voterSetStatePaused[H, N, ID, Sig]) Index() uint { return 1 } // hasVoted Whether we've voted already during a prior run of the program -type hasVoted[H comparable, N constraints.Unsigned] scale.VaryingDataType +type hasVoted[H comparable, N constraints.Unsigned, ID AuthorityID] scale.VaryingDataType // Set will set a VaryingDataTypeValue using the underlying VaryingDataType -func (hv *hasVoted[H, N]) Set(val scale.VaryingDataTypeValue) (err error) { //skipcq: GO-W1029 +func (hv *hasVoted[H, N, ID]) Set(val scale.VaryingDataTypeValue) (err error) { //skipcq: GO-W1029 vdt := scale.VaryingDataType(*hv) err = vdt.Set(val) if err != nil { return err } - *hv = hasVoted[H, N](vdt) + *hv = hasVoted[H, N, ID](vdt) return nil } // Value will return the value from the underlying VaryingDataType -func (hv *hasVoted[H, N]) Value() (val scale.VaryingDataTypeValue, err error) { //skipcq: GO-W1029 +func (hv *hasVoted[H, N, ID]) Value() (val scale.VaryingDataTypeValue, err error) { //skipcq: GO-W1029 vdt := scale.VaryingDataType(*hv) return vdt.Value() } // New is constructor for hasVoted -func (hv hasVoted[H, N]) New() hasVoted[H, N] { - vdt, _ := scale.NewVaryingDataType(no{}, yes[H, N]{}) +func (hv hasVoted[H, N, ID]) New() hasVoted[H, N, ID] { + vdt, _ := scale.NewVaryingDataType(no{}, yes[H, N, ID]{}) - newHv := hasVoted[H, N](vdt) + newHv := hasVoted[H, N, ID](vdt) return newHv } @@ -413,30 +413,30 @@ type no struct{} func (no) Index() uint { return 0 } // yes Has voted in this round -type yes[H comparable, N constraints.Unsigned] struct { - AuthId ed25519.PublicKey +type yes[H comparable, N constraints.Unsigned, ID AuthorityID] struct { + AuthId ID Vote vote[H, N] } // Index returns VDT index -func (yes[H, N]) Index() uint { return 1 } +func (yes[H, N, ID]) Index() uint { return 1 } -func (yes[H, N]) New() yes[H, N] { +func (yes[H, N, ID]) New() yes[H, N, ID] { vote := vote[H, N]{} vote = vote.New() - return yes[H, N]{ + return yes[H, N, ID]{ Vote: vote, } } // propose Returns the proposal we should vote with (if any.) -func (hv *hasVoted[H, N]) Propose() *grandpa.PrimaryPropose[H, N] { +func (hv *hasVoted[H, N, ID]) Propose() *grandpa.PrimaryPropose[H, N] { value, err := hv.Value() if err != nil { return nil } switch v := value.(type) { - case yes[H, N]: + case yes[H, N, ID]: value, err = v.Vote.Value() if err != nil { return nil @@ -455,13 +455,13 @@ func (hv *hasVoted[H, N]) Propose() *grandpa.PrimaryPropose[H, N] { } // prevote Returns the prevote we should vote with (if any.) -func (hv *hasVoted[H, N]) Prevote() *grandpa.Prevote[H, N] { +func (hv *hasVoted[H, N, ID]) Prevote() *grandpa.Prevote[H, N] { value, err := hv.Value() if err != nil { return nil } switch v := value.(type) { - case yes[H, N]: + case yes[H, N, ID]: value, err = v.Vote.Value() if err != nil { return nil @@ -478,13 +478,13 @@ func (hv *hasVoted[H, N]) Prevote() *grandpa.Prevote[H, N] { } // precommit Returns the precommit we should vote with (if any.) -func (hv *hasVoted[H, N]) Precommit() *grandpa.Precommit[H, N] { +func (hv *hasVoted[H, N, ID]) Precommit() *grandpa.Precommit[H, N] { value, err := hv.Value() if err != nil { return nil } switch v := value.(type) { - case yes[H, N]: + case yes[H, N, ID]: value, err = v.Vote.Value() if err != nil { return nil @@ -499,17 +499,17 @@ func (hv *hasVoted[H, N]) Precommit() *grandpa.Precommit[H, N] { } // CanPropose Returns true if the voter can still propose, false otherwise -func (hv *hasVoted[H, N]) CanPropose() bool { +func (hv *hasVoted[H, N, ID]) CanPropose() bool { return hv.Propose() == nil } // CanPrevote Returns true if the voter can still prevote, false otherwise -func (hv *hasVoted[H, N]) CanPrevote() bool { +func (hv *hasVoted[H, N, ID]) CanPrevote() bool { return hv.Prevote() == nil } // CanPrecommit Returns true if the voter can still precommit, false otherwise -func (hv *hasVoted[H, N]) CanPrecommit() bool { +func (hv *hasVoted[H, N, ID]) CanPrecommit() bool { return hv.Precommit() == nil } diff --git a/client/consensus/grandpa/environment_test.go b/client/consensus/grandpa/environment_test.go index 5c59b2b44a..eba90d2690 100644 --- a/client/consensus/grandpa/environment_test.go +++ b/client/consensus/grandpa/environment_test.go @@ -1,7 +1,6 @@ package grandpa import ( - "github.com/ChainSafe/gossamer/lib/crypto/ed25519" grandpa "github.com/ChainSafe/gossamer/pkg/finality-grandpa" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" @@ -10,7 +9,7 @@ import ( func TestSharedVoterSetState_hasVoted(t *testing.T) { // Has Not Voted - hasNotVoted := hasVoted[string, uint]{} + hasNotVoted := hasVoted[string, uint, uint]{} hasNotVoted = hasNotVoted.New() err := hasNotVoted.Set(no{}) require.NoError(t, err) @@ -21,27 +20,24 @@ func TestSharedVoterSetState_hasVoted(t *testing.T) { require.Equal(t, hasNotVoted, voted) // Has Voted - pub, err := ed25519.NewPublicKey([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) - require.NoError(t, err) - vote := vote[string, uint]{} vote = vote.New() err = vote.Set(propose[string, uint]{}) require.NoError(t, err) - yes := yes[string, uint]{ - AuthId: *pub, + yes := yes[string, uint, uint]{ + AuthId: key, Vote: vote, } - hasIndeedVoted := hasVoted[string, uint]{} + hasIndeedVoted := hasVoted[string, uint, uint]{} hasIndeedVoted = hasIndeedVoted.New() err = hasIndeedVoted.Set(yes) require.NoError(t, err) - example := make(map[uint64]hasVoted[string, uint]) + example := make(map[uint64]hasVoted[string, uint, uint]) example[1] = hasIndeedVoted - newCurrentRounds := CurrentRounds[string, uint]( + newCurrentRounds := CurrentRounds[string, uint, uint]( example, ) liveState := voterSetStateLive[string, uint, uint, uint]{ @@ -210,11 +206,11 @@ func TestCompletedRounds_Push(t *testing.T) { func TestCurrentRoundsEncoding(t *testing.T) { // TODO this fails - currentRounds := CurrentRounds[string, uint]( - make(map[uint64]hasVoted[string, uint]), + currentRounds := CurrentRounds[string, uint, uint]( + make(map[uint64]hasVoted[string, uint, uint]), ) - hv := hasVoted[string, uint]{} + hv := hasVoted[string, uint, uint]{} hv = hv.New() err := hv.Set(no{}) require.NoError(t, err) @@ -223,11 +219,11 @@ func TestCurrentRoundsEncoding(t *testing.T) { enc, err := scale.Marshal(currentRounds) require.NoError(t, err) - hasVotedNew := hasVoted[string, uint]{} + hasVotedNew := hasVoted[string, uint, uint]{} hasVotedNew = hv.New() - example := make(map[uint64]hasVoted[string, uint]) + example := make(map[uint64]hasVoted[string, uint, uint]) example[1] = hasVotedNew - newCurrentRounds := CurrentRounds[string, uint]( + newCurrentRounds := CurrentRounds[string, uint, uint]( example, ) err = scale.Unmarshal(enc, &newCurrentRounds) @@ -255,8 +251,8 @@ func TestVoterSetStateEncoding(t *testing.T) { } completedRounds := NewCompletedRounds[string, uint, uint, uint](compRound, 1, authorities) - currentRounds := CurrentRounds[string, uint]( - make(map[uint64]hasVoted[string, uint]), + currentRounds := CurrentRounds[string, uint, uint]( + make(map[uint64]hasVoted[string, uint, uint]), ) liveState := voterSetStateLive[string, uint, uint, uint]{ @@ -434,11 +430,11 @@ func TestVoterSetState_WithCurrentRound(t *testing.T) { require.Equal(t, "voter acting on a live round we are not tracking", err.Error()) // Valid - currentRounds := CurrentRounds[string, uint]( - make(map[uint64]hasVoted[string, uint]), + currentRounds := CurrentRounds[string, uint, uint]( + make(map[uint64]hasVoted[string, uint, uint]), ) - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(no{}) require.NoError(t, err) @@ -455,19 +451,16 @@ func TestVoterSetState_WithCurrentRound(t *testing.T) { } func TestHasVotedEncoding(t *testing.T) { - pub, err := ed25519.NewPublicKey([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) - require.NoError(t, err) - vote := vote[string, uint]{} vote = vote.New() - err = vote.Set(propose[string, uint]{}) + err := vote.Set(propose[string, uint]{}) require.NoError(t, err) - yes := yes[string, uint]{ - AuthId: *pub, + yes := yes[string, uint, uint]{ + AuthId: key, Vote: vote, } - hv := hasVoted[string, uint]{} + hv := hasVoted[string, uint, uint]{} hv = hv.New() err = hv.Set(yes) require.NoError(t, err) @@ -475,7 +468,7 @@ func TestHasVotedEncoding(t *testing.T) { res, err := scale.Marshal(hv) require.NoError(t, err) - newHasVoted := hasVoted[string, uint]{} + newHasVoted := hasVoted[string, uint, uint]{} newHasVoted = hv.New() err = scale.Unmarshal(res, &newHasVoted) require.NoError(t, err) @@ -492,10 +485,10 @@ func TestHasVoted_Propose(t *testing.T) { err := vote.Set(propose[string, uint]{*primaryPropose}) require.NoError(t, err) - yes := yes[string, uint]{ + yes := yes[string, uint, uint]{ Vote: vote, } - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(yes) require.NoError(t, err) @@ -514,10 +507,10 @@ func TestHasVoted_Prevote(t *testing.T) { err := voteVal.Set(prevote[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, *prevoteVal}) require.NoError(t, err) - y := yes[string, uint]{ + y := yes[string, uint, uint]{ Vote: voteVal, } - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(y) require.NoError(t, err) @@ -534,7 +527,7 @@ func TestHasVoted_Prevote(t *testing.T) { err = proposeVote.Set(propose[string, uint]{PrimaryPropose: *primaryPropose}) require.NoError(t, err) - y = yes[string, uint]{ + y = yes[string, uint, uint]{ Vote: proposeVote, } hasVoted = hasVoted.New() @@ -555,10 +548,10 @@ func TestHasVoted_Precommit(t *testing.T) { err := voteVal.Set(precommit[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, grandpa.Prevote[string, uint]{}, *precommitVal}) require.NoError(t, err) - y := yes[string, uint]{ + y := yes[string, uint, uint]{ Vote: voteVal, } - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(y) require.NoError(t, err) @@ -575,7 +568,7 @@ func TestHasVoted_Precommit(t *testing.T) { err = proposeVote.Set(propose[string, uint]{PrimaryPropose: *primaryPropose}) require.NoError(t, err) - y = yes[string, uint]{ + y = yes[string, uint, uint]{ Vote: proposeVote, } hasVoted = hasVoted.New() @@ -596,10 +589,10 @@ func TestHasVoted_CanPropose(t *testing.T) { err := voteVal.Set(propose[string, uint]{*primaryPropose}) require.NoError(t, err) - yes := yes[string, uint]{ + yes := yes[string, uint, uint]{ Vote: voteVal, } - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(yes) require.NoError(t, err) @@ -621,10 +614,10 @@ func TestHasVoted_CanPrevote(t *testing.T) { err := voteVal.Set(prevote[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, *prevoteVal}) require.NoError(t, err) - yes := yes[string, uint]{ + yes := yes[string, uint, uint]{ Vote: voteVal, } - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(yes) require.NoError(t, err) @@ -646,10 +639,10 @@ func TestHasVoted_CanPrecommit(t *testing.T) { err := vote.Set(precommit[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, grandpa.Prevote[string, uint]{}, *precommitVal}) require.NoError(t, err) - yes := yes[string, uint]{ + yes := yes[string, uint, uint]{ Vote: vote, } - hasVoted := hasVoted[string, uint]{} + hasVoted := hasVoted[string, uint, uint]{} hasVoted = hasVoted.New() err = hasVoted.Set(yes) require.NoError(t, err)