Skip to content

Commit

Permalink
Get,Del,Iter method renames
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Oct 25, 2022
1 parent 231d934 commit a4f9765
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
km.CkToPk = append(km.CkToPk, ccv.KeyToKey{From: &ck, To: &pk})
return false
})
k.KeyMap(ctx, chainID).Store.IterateCkToMemo(func(ck ConsumerConsAddr, m ccv.LastUpdateMemo) bool {
k.KeyMap(ctx, chainID).Store.IterateCcaToLastUpdateMemo(func(ck ConsumerConsAddr, m ccv.LastUpdateMemo) bool {
km.CcaToLastUpdateMemo = append(km.CcaToLastUpdateMemo, ccv.ConsAddrToLastUpdateMemo{ConsAddr: ck, LastUpdateMemo: &m})
return false
})
Expand Down
32 changes: 14 additions & 18 deletions x/ccv/provider/keeper/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ type Store interface {
SetCcaToLastUpdateMemo(ConsumerConsAddr, ccvtypes.LastUpdateMemo)
GetPkToCk(ProviderPubKey) (ConsumerPubKey, bool)
GetCkToPk(ConsumerPubKey) (ProviderPubKey, bool)
GetCkToMemo(ConsumerConsAddr) (ccvtypes.LastUpdateMemo, bool)
GetCcaToLastUpdateMemo(ConsumerConsAddr) (ccvtypes.LastUpdateMemo, bool)
DelPkToCk(ProviderPubKey)
DelCkToPk(ConsumerPubKey)
DelCkToMemo(ConsumerConsAddr)
DelCcaToLastUpdateMemo(ConsumerConsAddr)
IteratePkToCk(func(ProviderPubKey, ConsumerPubKey) bool)
IterateCkToPk(func(ConsumerPubKey, ProviderPubKey) bool)
IterateCkToMemo(func(ConsumerConsAddr, ccvtypes.LastUpdateMemo) bool)
IterateCcaToLastUpdateMemo(func(ConsumerConsAddr, ccvtypes.LastUpdateMemo) bool)
}

type KeyMap struct {
Expand All @@ -123,7 +123,7 @@ func (e *KeyMap) SetProviderPubKeyToConsumerPubKey(pk ProviderPubKey, ck Consume
if _, ok := e.Store.GetCkToPk(ck); ok {
return errors.New(`cannot reuse key which is in use or was recently in use`)
}
if _, ok := e.Store.GetCkToMemo(ConsumerPubKeyToConsumerConsAddr(ck)); ok {
if _, ok := e.Store.GetCcaToLastUpdateMemo(ConsumerPubKeyToConsumerConsAddr(ck)); ok {
return errors.New(`cannot reuse key which is in use or was recently in use`)
}
if oldCk, ok := e.Store.GetPkToCk(pk); ok {
Expand All @@ -143,22 +143,22 @@ func (e *KeyMap) GetProviderPubKeyFromConsumerPubKey(ck ConsumerPubKey) (pk Prov
}

func (e *KeyMap) GetProviderPubKeyFromConsumerConsAddress(cca sdk.ConsAddress) (pk ProviderPubKey, found bool) {
if memo, found := e.Store.GetCkToMemo(cca); found {
if memo, found := e.Store.GetCcaToLastUpdateMemo(cca); found {
return *memo.Pk, true
}
return pk, false
}

func (e *KeyMap) PruneUnusedKeys(latestVscid VSCID) {
toDel := []ConsumerConsAddr{}
e.Store.IterateCkToMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
e.Store.IterateCcaToLastUpdateMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
if m.Power == 0 && m.Vscid <= latestVscid {
toDel = append(toDel, cca)
}
return false
})
for _, cca := range toDel {
e.Store.DelCkToMemo(cca)
e.Store.DelCcaToLastUpdateMemo(cca)
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ func (e *KeyMap) inner(vscid VSCID, providerUpdates map[ProviderPubKey]int64) ma

// Grab provider keys where the last update had positive power
// and where the assigned consumer key has changed
e.Store.IterateCkToMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
e.Store.IterateCcaToLastUpdateMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
oldCk := m.Ck
if newCk, ok := e.Store.GetPkToCk(*m.Pk); ok { // TODO: do away with ok, should always be ok
// TODO: is !seen[str] needed?
Expand All @@ -214,7 +214,7 @@ func (e *KeyMap) inner(vscid VSCID, providerUpdates map[ProviderPubKey]int64) ma
canonicalKey := map[string]ConsumerPubKey{}
ret := map[ConsumerPubKey]int64{}

e.Store.IterateCkToMemo(func(_ ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
e.Store.IterateCcaToLastUpdateMemo(func(_ ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
str := DeterministicStringify(*m.Pk)
if 0 < m.Power {
if _, found := keyInProviderKeysToSendUpdateFor[str]; found {
Expand Down Expand Up @@ -327,7 +327,7 @@ func (e *KeyMap) InternalInvariants() bool {
// mapping.
// (Ensures lookups are correct)
e.Store.IterateCkToPk(func(ck ConsumerPubKey, pk ProviderPubKey) bool {
if m, ok := e.Store.GetCkToMemo(ConsumerPubKeyToConsumerConsAddr(ck)); ok {
if m, ok := e.Store.GetCcaToLastUpdateMemo(ConsumerPubKeyToConsumerConsAddr(ck)); ok {
if !pk.Equal(m.Pk) {
good = false
}
Expand All @@ -339,7 +339,7 @@ func (e *KeyMap) InternalInvariants() bool {
{
// All entries in ckToMemo have a consumer consensus
// address which is the address held inside
e.Store.IterateCkToMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
e.Store.IterateCcaToLastUpdateMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
cons := ConsumerPubKeyToConsumerConsAddr(*m.Ck)
if len(cca) != len(cons) {
good = false
Expand Down Expand Up @@ -395,7 +395,6 @@ func (s *KeyMapStore) SetCcaToLastUpdateMemo(k ConsumerConsAddr, v ccvtypes.Last
}
s.Store.Set(types.KeyMapCkToMemoKey(s.ChainID, kbz), vbz)
}

func (s *KeyMapStore) GetPkToCk(k ProviderPubKey) (v ConsumerPubKey, found bool) {
kbz, err := k.Marshal()
if err != nil {
Expand Down Expand Up @@ -424,7 +423,7 @@ func (s *KeyMapStore) GetCkToPk(k ConsumerPubKey) (v ProviderPubKey, found bool)
}
return v, false
}
func (s *KeyMapStore) GetCkToMemo(k ConsumerConsAddr) (v ccvtypes.LastUpdateMemo, found bool) {
func (s *KeyMapStore) GetCcaToLastUpdateMemo(k ConsumerConsAddr) (v ccvtypes.LastUpdateMemo, found bool) {
kbz, err := k.Marshal()
if err != nil {
panic(err)
Expand All @@ -439,7 +438,6 @@ func (s *KeyMapStore) GetCkToMemo(k ConsumerConsAddr) (v ccvtypes.LastUpdateMemo
}
return v, false
}

func (s *KeyMapStore) DelPkToCk(k ProviderPubKey) {
kbz, err := k.Marshal()
if err != nil {
Expand All @@ -454,14 +452,13 @@ func (s *KeyMapStore) DelCkToPk(k ConsumerPubKey) {
}
s.Store.Delete(types.KeyMapCkToPkKey(s.ChainID, kbz))
}
func (s *KeyMapStore) DelCkToMemo(k ConsumerConsAddr) {
func (s *KeyMapStore) DelCcaToLastUpdateMemo(k ConsumerConsAddr) {
kbz, err := k.Marshal()
if err != nil {
panic(err)
}
s.Store.Delete(types.KeyMapCkToMemoKey(s.ChainID, kbz))
}

func (s *KeyMapStore) IteratePkToCk(cb func(ProviderPubKey, ConsumerPubKey) bool) {
prefix := types.KeyMapPkToCkChainPrefix(s.ChainID)
iterator := sdk.KVStorePrefixIterator(s.Store, prefix)
Expand Down Expand Up @@ -502,12 +499,11 @@ func (s *KeyMapStore) IterateCkToPk(cb func(ConsumerPubKey, ProviderPubKey) bool
}
}
}
func (s *KeyMapStore) IterateCkToMemo(cb func(ConsumerConsAddr, ccvtypes.LastUpdateMemo) bool) {
func (s *KeyMapStore) IterateCcaToLastUpdateMemo(cb func(ConsumerConsAddr, ccvtypes.LastUpdateMemo) bool) {
prefix := types.KeyMapCkToMemoChainPrefix(s.ChainID)
iterator := sdk.KVStorePrefixIterator(s.Store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// k := ConsumerPubKey{}
k := ConsumerConsAddr{}
err := k.Unmarshal(iterator.Key()[len(prefix):])
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/keymap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func compareForEquality(t *testing.T,
require.Equal(t, len(ckToPk), cnt)

cnt = 0
km.Store.IterateCkToMemo(func(_ keeper.ConsumerConsAddr, _ ccvtypes.LastUpdateMemo) bool {
km.Store.IterateCcaToLastUpdateMemo(func(_ keeper.ConsumerConsAddr, _ ccvtypes.LastUpdateMemo) bool {
cnt += 1
return false
})
Expand All @@ -744,7 +744,7 @@ func compareForEquality(t *testing.T,
}
for k, vExpect := range ckToMemo {
k := sdk.ConsAddress(k)
m, found := km.Store.GetCkToMemo(k)
m, found := km.Store.GetCcaToLastUpdateMemo(k)
require.True(t, found)
require.Equal(t, vExpect.Pk, m.Pk)
require.Equal(t, vExpect.Ck, m.Ck)
Expand Down

0 comments on commit a4f9765

Please sign in to comment.