Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizualni committed Jul 29, 2022
1 parent 5e893a8 commit 5a63e32
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
22 changes: 22 additions & 0 deletions x/valset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
76 changes: 74 additions & 2 deletions x/valset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
},
},
Expand Down

0 comments on commit 5a63e32

Please sign in to comment.