Skip to content

Commit

Permalink
Simplify internals internal tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Oct 25, 2022
1 parent 0755b73 commit 4ab1956
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
7 changes: 3 additions & 4 deletions x/ccv/provider/keeper/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,11 @@ func (e *KeyMap) inner(vscid VSCID, providerUpdates map[ProviderPubKey]int64) ma
// and where the assigned consumer key has changed
e.Store.IterateCkToMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
oldCk := m.Ck
if newCk, ok := e.Store.GetPkToCk(*m.Pk); ok {
str := DeterministicStringify(*m.Pk)
if newCk, ok := e.Store.GetPkToCk(*m.Pk); ok { // TODO: do away with ok, should always be ok
// TODO: is !seen[str] needed?
if !oldCk.Equal(newCk) && 0 < m.Power { // TODO: comment why 0 < needed
if !oldCk.Equal(newCk) && 0 < m.Power {
providerKeysToSendUpdateFor = append(providerKeysToSendUpdateFor, *m.Pk)
keyInProviderKeysToSendUpdateFor[str] = true
keyInProviderKeysToSendUpdateFor[DeterministicStringify(*m.Pk)] = true
}
}
return false
Expand Down
28 changes: 10 additions & 18 deletions x/ccv/provider/keeper/keymap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,12 @@ func (d *driver) externalInvariants() {
expectQueryable[keeper.DeterministicStringify(u.PubKey)] = true
}
}
// If a consumer key is CURRENTLY mapped to by a provider key, it
// must be queryable.
d.km.Store.IteratePkToCk(func(_, ck keeper.ConsumerPubKey) bool {
expectQueryable[keeper.DeterministicStringify(ck)] = true
return false
})

// Simply check every consumer key for the correct queryable-ness.
for ck := uint64(0); ck < NUM_CKS; ck++ {
ck += 100 //TODO: fix with others
pk, actualQueryable := d.km.GetProviderPubKeyFromConsumerPubKey(key(ck))
cca := keeper.ConsumerPubKeyToConsumerConsAddr(key(ck))
pkByConsAddr, actualQueryableByConsAddr := d.km.GetProviderPubKeyFromConsumerConsAddress(cca)
require.Equal(d.t, pk, pkByConsAddr)
require.Equal(d.t, actualQueryable, actualQueryableByConsAddr)
_, actualQueryable := d.km.GetProviderPubKeyFromConsumerConsAddress(cca)
if expect, found := expectQueryable[keeper.DeterministicStringify(key(ck))]; found && expect {
require.True(d.t, actualQueryable)
} else {
Expand Down Expand Up @@ -657,14 +648,15 @@ func TestKeyMapSetUseReplaceAndReverse(t *testing.T) {
km.SetProviderPubKeyToConsumerPubKey(key(42), key(43))
updates := []abci.ValidatorUpdate{{PubKey: key(42), Power: 999}}
km.ComputeUpdates(100, updates)
km.SetProviderPubKeyToConsumerPubKey(key(42), key(44)) // New consumer key
actual, _ := km.GetProviderPubKeyFromConsumerPubKey(key(43)) // Old is queryable
km.SetProviderPubKeyToConsumerPubKey(key(42), key(44)) // New consumer key
// actual, _ := km.GetProviderPubKeyFromConsumerPubKey(key(43)) // Old is queryable
actual, _ := km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
require.Equal(t, key(42), actual)
actual, _ = km.GetProviderPubKeyFromConsumerPubKey(key(44)) // New is queryable
require.Equal(t, key(42), actual)
km.ComputeUpdates(101, updates) // Old is garbage collected on consumer
km.PruneUnusedKeys(102) // Old is garbage collected on provider
_, found := km.GetProviderPubKeyFromConsumerPubKey(key(43)) // Not queryable
km.ComputeUpdates(101, updates) // Old is no longer known to consumer
km.PruneUnusedKeys(102) // Old is garbage collected on provider
_, found := km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
require.False(t, found)
actual, _ = km.GetProviderPubKeyFromConsumerPubKey(key(44)) // New key is still queryable
require.Equal(t, key(42), actual)
Expand All @@ -676,12 +668,12 @@ func TestKeyMapSetUseReplaceAndPrune(t *testing.T) {
updates := []abci.ValidatorUpdate{{PubKey: key(42), Power: 999}}
km.ComputeUpdates(100, updates)
km.SetProviderPubKeyToConsumerPubKey(key(42), key(44))
actual, _ := km.GetProviderPubKeyFromConsumerPubKey(key(43)) // Queryable
actual, _ := km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
require.Equal(t, key(42), actual)
actual, _ = km.GetProviderPubKeyFromConsumerPubKey(key(44)) // Queryable
require.Equal(t, key(42), actual)
km.PruneUnusedKeys(101) // Should not be pruned
_, found := km.GetProviderPubKeyFromConsumerPubKey(key(43)) // Still queryable
km.PruneUnusedKeys(101) // Should not be pruned
_, found := km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
require.True(t, found)
actual, _ = km.GetProviderPubKeyFromConsumerPubKey(key(44)) // New key is still queryable
require.Equal(t, key(42), actual)
Expand Down

0 comments on commit 4ab1956

Please sign in to comment.