Skip to content

Commit

Permalink
add sorting into AccumulateChanges and add a unit test for it (#584)
Browse files Browse the repository at this point in the history
* add sorting into AccumulateChanges and add a unit test for it

* Fix sorting to make it more understandable
  • Loading branch information
jtremback committed Dec 13, 2022
1 parent 4e60bbf commit 3e339ea
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions x/ccv/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"sort"
"time"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -30,6 +31,16 @@ func AccumulateChanges(currentChanges, newChanges []abci.ValidatorUpdate) []abci
for _, update := range m {
out = append(out, update)
}

// The list of tendermint updates should hash the same across all consensus nodes
// that means it is necessary to sort for determinism.
sort.Slice(out, func(i, j int) bool {
if out[i].Power != out[j].Power {
return out[i].Power > out[j].Power
}
return out[i].PubKey.String() > out[j].PubKey.String()
})

return out
}

Expand Down
87 changes: 87 additions & 0 deletions x/ccv/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package utils_test

import (
"testing"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
ibcsimapp "github.com/cosmos/ibc-go/v3/testing/simapp"
"github.com/cosmos/interchain-security/x/ccv/utils"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestAccumulateChanges(t *testing.T) {
testKeys := ibcsimapp.CreateTestPubKeys(2)

tmPubKey, _ := cryptocodec.ToTmProtoPublicKey(testKeys[0])
tmPubKey2, _ := cryptocodec.ToTmProtoPublicKey(testKeys[1])

testCases := []struct {
name string
changes1 []abci.ValidatorUpdate
changes2 []abci.ValidatorUpdate
expected []abci.ValidatorUpdate
}{
{
name: "no changes",
changes1: []abci.ValidatorUpdate{},
changes2: []abci.ValidatorUpdate{},
expected: []abci.ValidatorUpdate(nil),
},
{
name: "one change",
changes1: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 1},
},
changes2: []abci.ValidatorUpdate{},
expected: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 1},
},
},
{
name: "two changes",
changes1: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 1},
},
changes2: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 2},
},
expected: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 2},
},
},
{
name: "two changes with different pubkeys",
changes1: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 1},
},
changes2: []abci.ValidatorUpdate{
{PubKey: tmPubKey2, Power: 2},
},
expected: []abci.ValidatorUpdate{
{PubKey: tmPubKey2, Power: 2},
{PubKey: tmPubKey, Power: 1},
},
},
{
name: "two changes with different pubkeys and same power",
changes1: []abci.ValidatorUpdate{
{PubKey: tmPubKey, Power: 1},
},
changes2: []abci.ValidatorUpdate{
{PubKey: tmPubKey2, Power: 1},
},
expected: []abci.ValidatorUpdate{
{PubKey: tmPubKey2, Power: 1},
{PubKey: tmPubKey, Power: 1},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
changes := utils.AccumulateChanges(tc.changes1, tc.changes2)
require.Equal(t, tc.expected, changes)
})
}
}

0 comments on commit 3e339ea

Please sign in to comment.