From 046d5b030ae3ce304b601c0252919cbd7e34c7c3 Mon Sep 17 00:00:00 2001 From: zhi Date: Thu, 1 Dec 2022 13:25:57 -0800 Subject: [PATCH 1/2] full comparison of candidates --- action/protocol/staking/candidate.go | 32 ++++- action/protocol/staking/candidate_test.go | 149 ++++++++++++++++++++++ 2 files changed, 180 insertions(+), 1 deletion(-) diff --git a/action/protocol/staking/candidate.go b/action/protocol/staking/candidate.go index 8dc9c231bc..38b71206e7 100644 --- a/action/protocol/staking/candidate.go +++ b/action/protocol/staking/candidate.go @@ -253,7 +253,37 @@ 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 + switch strings.Compare(l[i].Owner.String(), l[j].Owner.String()) { + case 1: + return true + case -1: + return false + } + switch strings.Compare(l[i].Reward.String(), l[j].Reward.String()) { + case 1: + return true + case -1: + return false + } + switch strings.Compare(l[i].Operator.String(), l[j].Operator.String()) { + case 1: + return true + case -1: + return false + } + switch { + case l[i].SelfStakeBucketIdx > l[j].SelfStakeBucketIdx: + return true + case l[i].SelfStakeBucketIdx < l[j].SelfStakeBucketIdx: + return false + } + switch l[i].SelfStake.Cmp(l[j].SelfStake) { + case 1: + return true + case -1: + return false + } + return strings.Compare(l[i].Name, l[j].Name) == 1 } // Serialize serializes candidate to bytes diff --git a/action/protocol/staking/candidate_test.go b/action/protocol/staking/candidate_test.go index 9ba9e7c2fd..49d9edcb31 100644 --- a/action/protocol/staking/candidate_test.go +++ b/action/protocol/staking/candidate_test.go @@ -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)) + } +} From aada84735cc02cbe7d4cdc4948e70315c8fcb15f Mon Sep 17 00:00:00 2001 From: zhi Date: Mon, 5 Dec 2022 11:05:30 -0800 Subject: [PATCH 2/2] address comment --- action/protocol/staking/candidate.go | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/action/protocol/staking/candidate.go b/action/protocol/staking/candidate.go index 38b71206e7..65802f631d 100644 --- a/action/protocol/staking/candidate.go +++ b/action/protocol/staking/candidate.go @@ -253,23 +253,14 @@ func (l CandidateList) Less(i, j int) bool { if res := l[i].Votes.Cmp(l[j].Votes); res != 0 { return res == 1 } - switch strings.Compare(l[i].Owner.String(), l[j].Owner.String()) { - case 1: - return true - case -1: - return false + if res := strings.Compare(l[i].Owner.String(), l[j].Owner.String()); res != 0 { + return res == 1 } - switch strings.Compare(l[i].Reward.String(), l[j].Reward.String()) { - case 1: - return true - case -1: - return false + if res := strings.Compare(l[i].Reward.String(), l[j].Reward.String()); res != 0 { + return res == 1 } - switch strings.Compare(l[i].Operator.String(), l[j].Operator.String()) { - case 1: - return true - case -1: - return false + 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: @@ -277,11 +268,8 @@ func (l CandidateList) Less(i, j int) bool { case l[i].SelfStakeBucketIdx < l[j].SelfStakeBucketIdx: return false } - switch l[i].SelfStake.Cmp(l[j].SelfStake) { - case 1: - return true - case -1: - return false + if res := l[i].SelfStake.Cmp(l[j].SelfStake); res != 0 { + return res == 1 } return strings.Compare(l[i].Name, l[j].Name) == 1 }