Skip to content

Commit

Permalink
make New() return non pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj committed Sep 11, 2023
1 parent f8b3ee2 commit 09bb401
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
24 changes: 12 additions & 12 deletions client/consensus/grandpa/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (tve *VoterSetState[H, N]) Value() (val scale.VaryingDataTypeValue, err err
}

// New ...
func (tve VoterSetState[H, N]) New() *VoterSetState[H, N] {
func (tve VoterSetState[H, N]) New() VoterSetState[H, N] {
vdt, err := scale.NewVaryingDataType(Live[H, N]{
CompletedRounds: CompletedRounds[H, N]{
Rounds: make([]CompletedRound[H, N], 0, NumLastCompletedRounds),
Expand All @@ -146,7 +146,7 @@ func (tve VoterSetState[H, N]) New() *VoterSetState[H, N] {
panic(err)
}
vs := VoterSetState[H, N](vdt)
return &vs
return vs
}

// NewVoterSetState is constructor for VoterSetState
Expand Down Expand Up @@ -180,13 +180,13 @@ func (tve *VoterSetState[H, N]) Live(setId uint64, authSet AuthoritySet[H, N], g
currentRounds := CurrentRounds[H, N](
make(map[uint64]HasVoted[H, N]),
)
hasVoted := &HasVoted[H, N]{}
hasVoted := HasVoted[H, N]{}
hasVoted = hasVoted.New()
err := hasVoted.Set(No{})
if err != nil {
return VoterSetState[H, N]{}, err
}
currentRounds[1] = *hasVoted
currentRounds[1] = hasVoted

voterSetState := *NewVoterSetState[H, N]()
err = voterSetState.Set(Live[H, N]{
Expand Down Expand Up @@ -294,11 +294,11 @@ func (hasVoted *HasVoted[H, N]) Value() (val scale.VaryingDataTypeValue, err err
}

// New is constructor for HasVoted
func (hasVoted HasVoted[H, N]) New() *HasVoted[H, N] {
func (hasVoted HasVoted[H, N]) New() HasVoted[H, N] {
vdt, _ := scale.NewVaryingDataType(No{}, Yes[H, N]{})

hv := HasVoted[H, N](vdt)
return &hv
return hv
}

// No Has not voted already in this round
Expand All @@ -316,11 +316,11 @@ type Yes[H comparable, N constraints.Unsigned] struct {
// Index returns VDT index
func (Yes[H, N]) Index() uint { return 1 }

func (Yes[H, N]) New() *Yes[H, N] {
vote := &Vote[H, N]{}
func (Yes[H, N]) New() Yes[H, N] {
vote := Vote[H, N]{}
vote = vote.New()
return &Yes[H, N]{
Vote: *vote,
return Yes[H, N]{
Vote: vote,
}
}

Expand Down Expand Up @@ -429,13 +429,13 @@ func (vote *Vote[H, N]) Value() (val scale.VaryingDataTypeValue, err error) { //
}

// New is constructor for Vote
func (vote Vote[H, N]) New() *Vote[H, N] {
func (vote Vote[H, N]) New() Vote[H, N] {
vdt, err := scale.NewVaryingDataType(Propose[H, N]{}, Prevote[H, N]{}, Precommit[H, N]{})
if err != nil {
panic(err)
}
hv := Vote[H, N](vdt)
return &hv
return hv
}

// Propose Has cast a proposal
Expand Down
68 changes: 34 additions & 34 deletions client/consensus/grandpa/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,19 @@ func TestCurrentRoundsEncoding(t *testing.T) {
make(map[uint64]HasVoted[string, uint]),
)

hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err := hasVoted.Set(No{})
require.NoError(t, err)
currentRounds[1] = *hasVoted
currentRounds[1] = hasVoted

enc, err := scale.Marshal(currentRounds)
require.NoError(t, err)

hasVotedNew := &HasVoted[string, uint]{}
hasVotedNew := HasVoted[string, uint]{}
hasVotedNew = hasVoted.New()
example := make(map[uint64]HasVoted[string, uint])
example[1] = *hasVotedNew
example[1] = hasVotedNew
newCurrentRounds := CurrentRounds[string, uint](
example,
)
Expand Down Expand Up @@ -394,11 +394,11 @@ func TestVoterSetState_WithCurrentRound(t *testing.T) {
make(map[uint64]HasVoted[string, uint]),
)

hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(No{})
require.NoError(t, err)
currentRounds[1] = *hasVoted
currentRounds[1] = hasVoted
err = voterSetState.Set(Live[string, uint]{
CompletedRounds: completedRounds,
CurrentRounds: currentRounds,
Expand All @@ -414,24 +414,24 @@ 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[string, uint]{}
vote = vote.New()
err = vote.Set(Propose[string, uint]{})
require.NoError(t, err)

yes := Yes[string, uint]{
AuthId: *pub,
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)

res, err := scale.Marshal(hasVoted)
require.NoError(t, err)

newHasVoted := &HasVoted[string, uint]{}
newHasVoted := HasVoted[string, uint]{}
newHasVoted = hasVoted.New()
err = scale.Unmarshal(res, &newHasVoted)
require.NoError(t, err)
Expand All @@ -443,15 +443,15 @@ func TestHasVoted_Propose(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Propose[string, uint]{*primaryPropose})
require.NoError(t, err)

yes := Yes[string, uint]{
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)
Expand All @@ -465,15 +465,15 @@ func TestHasVoted_Prevote(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Prevote[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, *prevote})
require.NoError(t, err)

yes := Yes[string, uint]{
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)
Expand All @@ -485,13 +485,13 @@ func TestHasVoted_Prevote(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
proposeVote := &Vote[string, uint]{}
proposeVote := Vote[string, uint]{}
proposeVote = vote.New()
err = proposeVote.Set(Propose[string, uint]{PrimaryPropose: *primaryPropose})
require.NoError(t, err)

yes = Yes[string, uint]{
Vote: *proposeVote,
Vote: proposeVote,
}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
Expand All @@ -506,15 +506,15 @@ func TestHasVoted_Precommit(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Precommit[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, grandpa.Prevote[string, uint]{}, *precommit})
require.NoError(t, err)

yes := Yes[string, uint]{
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)
Expand All @@ -526,13 +526,13 @@ func TestHasVoted_Precommit(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
proposeVote := &Vote[string, uint]{}
proposeVote := Vote[string, uint]{}
proposeVote = proposeVote.New()
err = proposeVote.Set(Propose[string, uint]{PrimaryPropose: *primaryPropose})
require.NoError(t, err)

yes = Yes[string, uint]{
Vote: *proposeVote,
Vote: proposeVote,
}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
Expand All @@ -547,15 +547,15 @@ func TestHasVoted_CanPropose(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Propose[string, uint]{*primaryPropose})
require.NoError(t, err)

yes := Yes[string, uint]{
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)
Expand All @@ -572,15 +572,15 @@ func TestHasVoted_CanPrevote(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Prevote[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, *prevote})
require.NoError(t, err)

yes := Yes[string, uint]{
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)
Expand All @@ -597,15 +597,15 @@ func TestHasVoted_CanPrecommit(t *testing.T) {
TargetHash: "a",
TargetNumber: 2,
}
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Precommit[string, uint]{&grandpa.PrimaryPropose[string, uint]{}, grandpa.Prevote[string, uint]{}, *precommit})
require.NoError(t, err)

yes := Yes[string, uint]{
Vote: *vote,
Vote: vote,
}
hasVoted := &HasVoted[string, uint]{}
hasVoted := HasVoted[string, uint]{}
hasVoted = hasVoted.New()
err = hasVoted.Set(yes)
require.NoError(t, err)
Expand All @@ -618,7 +618,7 @@ func TestHasVoted_CanPrecommit(t *testing.T) {
}

func TestVoteEncoding(t *testing.T) {
vote := &Vote[string, uint]{}
vote := Vote[string, uint]{}
vote = vote.New()
err := vote.Set(Propose[string, uint]{
PrimaryPropose: grandpa.PrimaryPropose[string, uint]{
Expand All @@ -631,7 +631,7 @@ func TestVoteEncoding(t *testing.T) {
enc, err := scale.Marshal(vote)
require.NoError(t, err)

newVote := &Vote[string, uint]{}
newVote := Vote[string, uint]{}
newVote = newVote.New()
err = scale.Unmarshal(enc, &newVote)
require.NoError(t, err)
Expand Down

0 comments on commit 09bb401

Please sign in to comment.