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

full comparison of candidates #3702

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 19 additions & 1 deletion action/protocol/staking/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,25 @@ func (l CandidateList) Less(i, j int) bool {
if res := l[i].Votes.Cmp(l[j].Votes); res != 0 {
return res == 1
}
return strings.Compare(l[i].Owner.String(), l[j].Owner.String()) == 1
if res := strings.Compare(l[i].Owner.String(), l[j].Owner.String()); res != 0 {
return res == 1
}
if res := strings.Compare(l[i].Reward.String(), l[j].Reward.String()); res != 0 {
return res == 1
}
if res := strings.Compare(l[i].Operator.String(), l[j].Operator.String()); res != 0 {
return res == 1
}
switch {
case l[i].SelfStakeBucketIdx > l[j].SelfStakeBucketIdx:
return true
case l[i].SelfStakeBucketIdx < l[j].SelfStakeBucketIdx:
return false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if l[i].SelfStakeBucketIdx != l[j].SelfStakeBucketIdx {
return l[i].SelfStakeBucketIdx > l[j].SelfStakeBucketIdx
}

if res := l[i].SelfStake.Cmp(l[j].SelfStake); res != 0 {
return res == 1
}
return strings.Compare(l[i].Name, l[j].Name) == 1
}

// Serialize serializes candidate to bytes
Expand Down
149 changes: 149 additions & 0 deletions action/protocol/staking/candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,152 @@ func TestGetPutCandidate(t *testing.T) {
require.Equal(state.ErrStateNotExist, errors.Cause(err))
}
}

func TestLess(t *testing.T) {
r := require.New(t)
pairs := []CandidateList{
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(2),
Name: "test6",
Votes: big.NewInt(2),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(2),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(2),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
&Candidate{
Owner: identityset.Address(5),
Operator: identityset.Address(12),
Reward: identityset.Address(2),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(6),
Reward: identityset.Address(2),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(5),
Reward: identityset.Address(2),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(5),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test5",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 1,
SelfStake: unit.ConvertIotxToRau(1100000),
},
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
{
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100001),
},
&Candidate{
Owner: identityset.Address(6),
Operator: identityset.Address(12),
Reward: identityset.Address(6),
Name: "test6",
Votes: big.NewInt(1),
SelfStakeBucketIdx: 0,
SelfStake: unit.ConvertIotxToRau(1100000),
},
},
}
for _, pair := range pairs {
r.True(pair.Less(0, 1))
}
}