Skip to content

Commit

Permalink
fix: ballot sorting (backport #1851) (#1857)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Feb 20, 2023
1 parent 844532c commit 6874111
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef
google.golang.org/grpc v1.52.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.2.0
mvdan.cc/gofumpt v0.4.0
)

Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1899,10 +1899,10 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
gotest.tools/v3 v3.2.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 2 additions & 0 deletions price-feeder/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,9 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
8 changes: 7 additions & 1 deletion x/oracle/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ func (pb ExchangeRateBallot) Len() int {
// Less reports whether the element with
// index i should sort before the element with index j.
func (pb ExchangeRateBallot) Less(i, j int) bool {
return pb[i].ExchangeRate.LT(pb[j].ExchangeRate)
if pb[i].ExchangeRate.LT(pb[j].ExchangeRate) {
return true
}
if pb[i].ExchangeRate.Equal(pb[j].ExchangeRate) {
return bytes.Compare(pb[i].Voter, pb[j].Voter) < 0
}
return false
}

// Swap implements sort.Interface.
Expand Down
28 changes: 28 additions & 0 deletions x/oracle/types/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package types
import (
"fmt"
"math"
"sort"
"strconv"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/secp256k1"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"gotest.tools/v3/assert"
)

func TestToMap(t *testing.T) {
Expand Down Expand Up @@ -376,3 +378,29 @@ func TestClaimMapToSlice(t *testing.T) {
})
require.Equal(t, []Claim{claim, claim}, claimSlice)
}

func TestExchangeRateBallotSort(t *testing.T) {
v1 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.2"), Voter: sdk.ValAddress{0, 1}}
v1Cpy := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.2"), Voter: sdk.ValAddress{0, 1}}
v2 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.1"), Voter: sdk.ValAddress{0, 1, 1}}
v3 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.1"), Voter: sdk.ValAddress{0, 1}}
v4 := VoteForTally{ExchangeRate: sdk.MustNewDecFromStr("0.5"), Voter: sdk.ValAddress{1}}

tcs := []struct {
got ExchangeRateBallot
expected ExchangeRateBallot
}{
{got: ExchangeRateBallot{v1, v2, v3, v4},
expected: ExchangeRateBallot{v3, v2, v1, v4}},
{got: ExchangeRateBallot{v1},
expected: ExchangeRateBallot{v1}},
{got: ExchangeRateBallot{v1, v1Cpy},
expected: ExchangeRateBallot{v1, v1Cpy}},
}
for i, tc := range tcs {
t.Run(fmt.Sprint(i), func(t *testing.T) {
sort.Sort(tc.got)
assert.DeepEqual(t, tc.expected, tc.got)
})
}
}

0 comments on commit 6874111

Please # to comment.