Skip to content

Commit 6874111

Browse files
mergify[bot]robert-zarembatoteki
authored
fix: ballot sorting (backport #1851) (#1857)
* fix: ballot sorting (#1851) * fix: ballot sorting * test sorting (cherry picked from commit 9ba9310) * make go-mod-tidy --------- Co-authored-by: Robert Zaremba <robert@zaremba.ch> Co-authored-by: toteki <63419657+toteki@users.noreply.github.com>
1 parent 844532c commit 6874111

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef
3333
google.golang.org/grpc v1.52.0
3434
gopkg.in/yaml.v3 v3.0.1
35+
gotest.tools/v3 v3.2.0
3536
mvdan.cc/gofumpt v0.4.0
3637
)
3738

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1899,10 +1899,10 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
18991899
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19001900
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19011901
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1902-
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
19031902
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
19041903
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
19051904
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
1905+
gotest.tools/v3 v3.2.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
19061906
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
19071907
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
19081908
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

price-feeder/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,9 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
18131813
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18141814
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
18151815
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1816+
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
18161817
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
1818+
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
18171819
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
18181820
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
18191821
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

x/oracle/types/ballot.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ func (pb ExchangeRateBallot) Len() int {
114114
// Less reports whether the element with
115115
// index i should sort before the element with index j.
116116
func (pb ExchangeRateBallot) Less(i, j int) bool {
117-
return pb[i].ExchangeRate.LT(pb[j].ExchangeRate)
117+
if pb[i].ExchangeRate.LT(pb[j].ExchangeRate) {
118+
return true
119+
}
120+
if pb[i].ExchangeRate.Equal(pb[j].ExchangeRate) {
121+
return bytes.Compare(pb[i].Voter, pb[j].Voter) < 0
122+
}
123+
return false
118124
}
119125

120126
// Swap implements sort.Interface.

x/oracle/types/ballot_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package types
33
import (
44
"fmt"
55
"math"
6+
"sort"
67
"strconv"
78
"testing"
89

910
sdk "github.com/cosmos/cosmos-sdk/types"
1011
"github.com/stretchr/testify/require"
1112
"github.com/tendermint/tendermint/crypto/secp256k1"
1213
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
14+
"gotest.tools/v3/assert"
1315
)
1416

1517
func TestToMap(t *testing.T) {
@@ -376,3 +378,29 @@ func TestClaimMapToSlice(t *testing.T) {
376378
})
377379
require.Equal(t, []Claim{claim, claim}, claimSlice)
378380
}
381+
382+
func TestExchangeRateBallotSort(t *testing.T) {
383+
v1 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.2"), Voter: sdk.ValAddress{0, 1}}
384+
v1Cpy := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.2"), Voter: sdk.ValAddress{0, 1}}
385+
v2 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.1"), Voter: sdk.ValAddress{0, 1, 1}}
386+
v3 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.1"), Voter: sdk.ValAddress{0, 1}}
387+
v4 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.5"), Voter: sdk.ValAddress{1}}
388+
389+
tcs := []struct {
390+
got ExchangeRateBallot
391+
expected ExchangeRateBallot
392+
}{
393+
{got: ExchangeRateBallot{v1, v2, v3, v4},
394+
expected: ExchangeRateBallot{v3, v2, v1, v4}},
395+
{got: ExchangeRateBallot{v1},
396+
expected: ExchangeRateBallot{v1}},
397+
{got: ExchangeRateBallot{v1, v1Cpy},
398+
expected: ExchangeRateBallot{v1, v1Cpy}},
399+
}
400+
for i, tc := range tcs {
401+
t.Run(fmt.Sprint(i), func(t *testing.T) {
402+
sort.Sort(tc.got)
403+
assert.DeepEqual(t, tc.expected, tc.got)
404+
})
405+
}
406+
}

0 commit comments

Comments
 (0)