diff --git a/x/valset/keeper/keeper.go b/x/valset/keeper/keeper.go index 767a8886..5a1f854d 100644 --- a/x/valset/keeper/keeper.go +++ b/x/valset/keeper/keeper.go @@ -256,6 +256,28 @@ func (k Keeper) isNewSnapshotWorthy(currentSnapshot, newSnapshot *types.Snapshot } } + // we also need to see if validators added or removed any external chain info. + // If they did, then this change is also considered to be worthy. + for i := 0; i < len(sortedCurrent); i++ { + currentVal, newVal := sortedCurrent[i], sortedNew[i] + if len(currentVal.ExternalChainInfos) != len(newVal.ExternalChainInfos) { + return true + } + + keyFnc := func(acc *types.ExternalChainInfo) string { + return fmt.Sprintf("%s-%s-%s", acc.GetChainReferenceID(), acc.GetChainType(), acc.GetAddress()) + } + + currentMap := slice.MakeMapKeys(currentVal.ExternalChainInfos, keyFnc) + newMap := slice.MakeMapKeys(newVal.ExternalChainInfos, keyFnc) + + for _, acc := range currentMap { + if _, ok := newMap[keyFnc(acc)]; !ok { + return true + } + } + } + return false } diff --git a/x/valset/keeper/keeper_test.go b/x/valset/keeper/keeper_test.go index 8927ced9..6fe72a06 100644 --- a/x/valset/keeper/keeper_test.go +++ b/x/valset/keeper/keeper_test.go @@ -321,20 +321,92 @@ func TestIsNewSnapshotWorthy(t *testing.T) { }, }, }, + { + expRes: true, + name: "two snapshots are same, but they have removed one of their accounts", + curr: &types.Snapshot{ + TotalShares: sdk.NewInt(100), + Validators: []types.Validator{ + { + Address: sdk.ValAddress("123"), + ShareCount: sdk.NewInt(20), + ExternalChainInfos: []*types.ExternalChainInfo{ + {}, {}, + }, + }, + {Address: sdk.ValAddress("456"), ShareCount: sdk.NewInt(80)}, + }, + }, + neww: &types.Snapshot{ + TotalShares: sdk.NewInt(100), + Validators: []types.Validator{ + { + Address: sdk.ValAddress("123"), + ShareCount: sdk.NewInt(20), + ExternalChainInfos: []*types.ExternalChainInfo{ + {}, + }, + }, + {Address: sdk.ValAddress("456"), ShareCount: sdk.NewInt(80)}, + }, + }, + }, + { + expRes: true, + name: "two snapshots are same, but they have different external chain infos", + curr: &types.Snapshot{ + TotalShares: sdk.NewInt(100), + Validators: []types.Validator{ + { + Address: sdk.ValAddress("123"), + ShareCount: sdk.NewInt(20), + ExternalChainInfos: []*types.ExternalChainInfo{ + {Address: "abc"}, {Address: "def"}, + }, + }, + {Address: sdk.ValAddress("456"), ShareCount: sdk.NewInt(80)}, + }, + }, + neww: &types.Snapshot{ + TotalShares: sdk.NewInt(100), + Validators: []types.Validator{ + { + Address: sdk.ValAddress("123"), + ShareCount: sdk.NewInt(20), + ExternalChainInfos: []*types.ExternalChainInfo{ + {Address: "abc"}, {Address: "123"}, + }, + }, + {Address: sdk.ValAddress("456"), ShareCount: sdk.NewInt(80)}, + }, + }, + }, { expRes: false, name: "two snapshots have same validators and same relative power orders", curr: &types.Snapshot{ TotalShares: sdk.NewInt(100), Validators: []types.Validator{ - {Address: sdk.ValAddress("123"), ShareCount: sdk.NewInt(20)}, + { + Address: sdk.ValAddress("123"), + ShareCount: sdk.NewInt(20), + ExternalChainInfos: []*types.ExternalChainInfo{ + {Address: "abc"}, {Address: "def"}, + }, + }, {Address: sdk.ValAddress("456"), ShareCount: sdk.NewInt(80)}, }, }, neww: &types.Snapshot{ TotalShares: sdk.NewInt(1000), Validators: []types.Validator{ - {Address: sdk.ValAddress("123"), ShareCount: sdk.NewInt(200)}, + { + Address: sdk.ValAddress("123"), + ShareCount: sdk.NewInt(200), + ExternalChainInfos: []*types.ExternalChainInfo{ + {Address: "abc"}, {Address: "def"}, + }, + }, {Address: sdk.ValAddress("456"), ShareCount: sdk.NewInt(800)}, }, },