Skip to content

Commit

Permalink
Rebuild proto start manual renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Oct 26, 2022
1 parent a96bfbc commit d6dd825
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 147 deletions.
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
}
if cs.KeyMap != nil {
for _, pkToCk := range cs.KeyMap.PkToCk {
k.KeyMap(ctx, cs.ChainId).Store.SetPkToCk(*pkToCk.From, *pkToCk.To)
k.KeyMap(ctx, cs.ChainId).Store.SetPcaToCk(*pkToCk.From, *pkToCk.To)
}
for _, ckToPk := range cs.KeyMap.CkToPk {
k.KeyMap(ctx, cs.ChainId).Store.SetCkToPk(*ckToPk.From, *ckToPk.To)
Expand Down Expand Up @@ -134,7 +134,7 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
km.PkToCk = []ccv.KeyToKey{}
km.CkToPk = []ccv.KeyToKey{}
km.CcaToLastUpdateMemo = []ccv.ConsAddrToLastUpdateMemo{}
k.KeyMap(ctx, chainID).Store.IteratePkToCk(func(pk ProviderPubKey, ck ConsumerPubKey) bool {
k.KeyMap(ctx, chainID).Store.IteratePcaToCk(func(pk ProviderPubKey, ck ConsumerPubKey) bool {
km.PkToCk = append(km.PkToCk, ccv.KeyToKey{From: &pk, To: &ck})
return false
})
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func TestIniAndExportGenesis(t *testing.T) {
require.True(t, pk.GetPendingConsumerRemovalProp(ctx, cChainIDs[0], oneHourFromNow))
require.Equal(t, pGenesis.Params, pk.GetParams(ctx))

_, found = pk.KeyMap(ctx, cChainIDs[0]).Store.GetPkToCk(crypto.PublicKey{})
_, found = pk.KeyMap(ctx, cChainIDs[0]).Store.GetPcaToCk(crypto.PublicKey{})
require.True(t, found)
_, found = pk.KeyMap(ctx, cChainIDs[1]).Store.GetPkToCk(crypto.PublicKey{})
_, found = pk.KeyMap(ctx, cChainIDs[1]).Store.GetPcaToCk(crypto.PublicKey{})
require.False(t, found)

// check provider chain's consumer chain states
Expand Down
70 changes: 35 additions & 35 deletions x/ccv/provider/keeper/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@ func DeterministicStringify(k crypto.PublicKey) string {
return string(bz)
}

func ConsumerPubKeyToConsumerConsAddr(ck ConsumerPubKey) ConsumerConsAddr {
sdkCk, err := cryptocodec.FromTmProtoPublicKey(ck)
func PubKeyToConsAddr(k crypto.PublicKey) sdk.ConsAddress {
sdkK, err := cryptocodec.FromTmProtoPublicKey(k)
if err != nil {
panic("could not get public key from tm proto public key")
}
return sdk.GetConsAddress(sdkCk)
return sdk.GetConsAddress(sdkK)
}

type Store interface {
SetPkToCk(ProviderPubKey, ConsumerPubKey)
SetPcaToCk(ProviderConsAddr, ConsumerPubKey)
SetCkToPk(ConsumerPubKey, ProviderPubKey)
SetCcaToLastUpdateMemo(ConsumerConsAddr, ccvtypes.LastUpdateMemo)
GetPkToCk(ProviderPubKey) (ConsumerPubKey, bool)
GetPcaToCk(ProviderConsAddr) (ConsumerPubKey, bool)
GetCkToPk(ConsumerPubKey) (ProviderPubKey, bool)
GetCcaToLastUpdateMemo(ConsumerConsAddr) (ccvtypes.LastUpdateMemo, bool)
DelPkToCk(ProviderPubKey)
DelPcaToCk(ProviderConsAddr)
DelCkToPk(ConsumerPubKey)
DelCcaToLastUpdateMemo(ConsumerConsAddr)
IteratePkToCk(func(ProviderPubKey, ConsumerPubKey) bool)
IteratePcaToCk(func(ProviderConsAddr, ConsumerPubKey) bool)
IterateCkToPk(func(ConsumerPubKey, ProviderPubKey) bool)
IterateCcaToLastUpdateMemo(func(ConsumerConsAddr, ccvtypes.LastUpdateMemo) bool)
}
Expand All @@ -131,19 +131,19 @@ 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.GetCcaToLastUpdateMemo(ConsumerPubKeyToConsumerConsAddr(ck)); ok {
if _, ok := e.Store.GetCcaToLastUpdateMemo(PubKeyToConsAddr(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 {
if oldCk, ok := e.Store.GetPcaToCk(pk); ok {
e.Store.DelCkToPk(oldCk)
}
e.Store.SetPkToCk(pk, ck)
e.Store.SetPcaToCk(pk, ck)
e.Store.SetCkToPk(ck, pk)
return nil
}

func (e *KeyMap) GetCurrentConsumerPubKeyFromProviderPubKey(pk ProviderPubKey) (ck ConsumerPubKey, found bool) {
return e.Store.GetPkToCk(pk)
return e.Store.GetPcaToCk(pk)
}

func (e *KeyMap) GetProviderPubKeyFromConsumerPubKey(ck ConsumerPubKey) (pk ProviderPubKey, found bool) {
Expand Down Expand Up @@ -180,7 +180,7 @@ func (e *KeyMap) getProviderKeysForUpdate(stakingUpdates map[ProviderPubKey]int6
// last update sent to the consumer was a positive power update
// and the assigned key has changed since that update.
e.Store.IterateCcaToLastUpdateMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
if newCk, ok := e.Store.GetPkToCk(*m.Pk); ok { // TODO: do away with ok, should always be ok
if newCk, ok := e.Store.GetPcaToCk(*m.Pk); ok { // TODO: do away with ok, should always be ok
oldCk := m.Ck
if !oldCk.Equal(newCk) && 0 < m.Power {
keys = append(keys, *m.Pk)
Expand Down Expand Up @@ -239,7 +239,7 @@ func (e *KeyMap) getConsumerUpdates(vscid VSCID, stakingUpdates map[ProviderPubK
s := DeterministicStringify(*u.Ck)
canonicalConsumerKey[s] = *u.Ck
consumerUpdates[*u.Ck] = 0
cca := ConsumerPubKeyToConsumerConsAddr(*u.Ck)
cca := PubKeyToConsAddr(*u.Ck)
e.Store.SetCcaToLastUpdateMemo(cca, ccvtypes.LastUpdateMemo{Ck: u.Ck, Pk: &pk, Vscid: vscid, Power: 0})
}
}
Expand Down Expand Up @@ -268,11 +268,11 @@ func (e *KeyMap) getConsumerUpdates(vscid VSCID, stakingUpdates map[ProviderPubK

// Only ship update with positive powers.
if 0 < power {
ck, found := e.Store.GetPkToCk(pk)
ck, found := e.Store.GetPcaToCk(pk)
if !found {
panic("must find ck for pk")
}
cca := ConsumerPubKeyToConsumerConsAddr(ck)
cca := PubKeyToConsAddr(ck)
e.Store.SetCcaToLastUpdateMemo(cca, ccvtypes.LastUpdateMemo{Ck: &ck, Pk: &pk, Vscid: vscid, Power: power})
if k, found := canonicalConsumerKey[DeterministicStringify(ck)]; found {
consumerUpdates[k] = power
Expand Down Expand Up @@ -314,7 +314,7 @@ func (e *KeyMap) InternalInvariants() bool {
// No two provider keys can map to the same consumer key
// (pkToCk is sane)
seen := map[string]bool{}
e.Store.IteratePkToCk(func(_ ProviderPubKey, ck ConsumerPubKey) bool {
e.Store.IteratePcaToCk(func(_ ProviderPubKey, ck ConsumerPubKey) bool {
if seen[DeterministicStringify(ck)] {
good = false
}
Expand All @@ -326,7 +326,7 @@ func (e *KeyMap) InternalInvariants() bool {
{
// All values of pkToCk is a key of ckToPk
// (reverse lookup is always possible)
e.Store.IteratePkToCk(func(pk ProviderPubKey, ck ConsumerPubKey) bool {
e.Store.IteratePcaToCk(func(pk ProviderPubKey, ck ConsumerPubKey) bool {
if pkQueried, ok := e.Store.GetCkToPk(ck); ok {
good = good && pkQueried.Equal(pk)
} else {
Expand All @@ -342,7 +342,7 @@ func (e *KeyMap) InternalInvariants() bool {
// (ckToPk is sane)
e.Store.IterateCkToPk(func(ck ConsumerPubKey, _ ProviderPubKey) bool {
found := false
e.Store.IteratePkToCk(func(_, candidateCk ConsumerPubKey) bool {
e.Store.IteratePcaToCk(func(_, candidateCk ConsumerPubKey) bool {
if candidateCk.Equal(ck) {
found = true
return true
Expand All @@ -360,7 +360,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.GetCcaToLastUpdateMemo(ConsumerPubKeyToConsumerConsAddr(ck)); ok {
if m, ok := e.Store.GetCcaToLastUpdateMemo(PubKeyToConsAddr(ck)); ok {
if !pk.Equal(m.Pk) {
good = false
}
Expand All @@ -373,7 +373,7 @@ func (e *KeyMap) InternalInvariants() bool {
// All entries in ckToMemo have a consumer consensus
// address which is the address held inside
e.Store.IterateCcaToLastUpdateMemo(func(cca ConsumerConsAddr, m ccvtypes.LastUpdateMemo) bool {
cons := ConsumerPubKeyToConsumerConsAddr(*m.Ck)
cons := PubKeyToConsAddr(*m.Ck)
if len(cca) != len(cons) {
good = false
}
Expand Down Expand Up @@ -412,7 +412,7 @@ type KeyMapStore struct {
ChainID ChainID
}

func (s *KeyMapStore) SetPkToCk(k ProviderPubKey, v ConsumerPubKey) {
func (s *KeyMapStore) SetPcaToCk(k ProviderConsAddr, v ConsumerPubKey) {
kbz, err := k.Marshal()
if err != nil {
panic(err)
Expand All @@ -421,7 +421,7 @@ func (s *KeyMapStore) SetPkToCk(k ProviderPubKey, v ConsumerPubKey) {
if err != nil {
panic(err)
}
s.Store.Set(types.KeyMapPkToCkKey(s.ChainID, kbz), vbz)
s.Store.Set(types.KeyMapPcaToCkKey(s.ChainID, kbz), vbz)
}
func (s *KeyMapStore) SetCkToPk(k ConsumerPubKey, v ProviderPubKey) {
kbz, err := k.Marshal()
Expand All @@ -443,14 +443,14 @@ func (s *KeyMapStore) SetCcaToLastUpdateMemo(k ConsumerConsAddr, v ccvtypes.Last
if err != nil {
panic(err)
}
s.Store.Set(types.KeyMapCkToMemoKey(s.ChainID, kbz), vbz)
s.Store.Set(types.KeyMapCcaToLastUpdateMemoKey(s.ChainID, kbz), vbz)
}
func (s *KeyMapStore) GetPkToCk(k ProviderPubKey) (v ConsumerPubKey, found bool) {
func (s *KeyMapStore) GetPcaToCk(k ProviderConsAddr) (v ConsumerPubKey, found bool) {
kbz, err := k.Marshal()
if err != nil {
panic(err)
}
if vbz := s.Store.Get(types.KeyMapPkToCkKey(s.ChainID, kbz)); vbz != nil {
if vbz := s.Store.Get(types.KeyMapPcaToCkKey(s.ChainID, kbz)); vbz != nil {
err := v.Unmarshal(vbz)
if err != nil {
panic(err)
Expand Down Expand Up @@ -478,7 +478,7 @@ func (s *KeyMapStore) GetCcaToLastUpdateMemo(k ConsumerConsAddr) (v ccvtypes.Las
if err != nil {
panic(err)
}
if vbz := s.Store.Get(types.KeyMapCkToMemoKey(s.ChainID, kbz)); vbz != nil {
if vbz := s.Store.Get(types.KeyMapCcaToLastUpdateMemoKey(s.ChainID, kbz)); vbz != nil {
v := ccvtypes.LastUpdateMemo{}
err := v.Unmarshal(vbz)
if err != nil {
Expand All @@ -488,12 +488,12 @@ func (s *KeyMapStore) GetCcaToLastUpdateMemo(k ConsumerConsAddr) (v ccvtypes.Las
}
return v, false
}
func (s *KeyMapStore) DelPkToCk(k ProviderPubKey) {
func (s *KeyMapStore) DelPcaToCk(k ProviderConsAddr) {
kbz, err := k.Marshal()
if err != nil {
panic(err)
}
s.Store.Delete(types.KeyMapPkToCkKey(s.ChainID, kbz))
s.Store.Delete(types.KeyMapPcaToCkKey(s.ChainID, kbz))
}
func (s *KeyMapStore) DelCkToPk(k ConsumerPubKey) {
kbz, err := k.Marshal()
Expand All @@ -507,14 +507,14 @@ func (s *KeyMapStore) DelCcaToLastUpdateMemo(k ConsumerConsAddr) {
if err != nil {
panic(err)
}
s.Store.Delete(types.KeyMapCkToMemoKey(s.ChainID, kbz))
s.Store.Delete(types.KeyMapCcaToLastUpdateMemoKey(s.ChainID, kbz))
}
func (s *KeyMapStore) IteratePkToCk(cb func(ProviderPubKey, ConsumerPubKey) bool) {
prefix := types.KeyMapPkToCkChainPrefix(s.ChainID)
func (s *KeyMapStore) IteratePcaToCk(cb func(ProviderConsAddr, ConsumerPubKey) bool) {
prefix := types.KeyMapPcaToCkChainPrefix(s.ChainID)
iterator := sdk.KVStorePrefixIterator(s.Store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
k := ProviderPubKey{}
k := ProviderConsAddr{}
err := k.Unmarshal(iterator.Key()[len(prefix):])
if err != nil {
panic(err)
Expand Down Expand Up @@ -550,7 +550,7 @@ func (s *KeyMapStore) IterateCkToPk(cb func(ConsumerPubKey, ProviderPubKey) bool
}
}
func (s *KeyMapStore) IterateCcaToLastUpdateMemo(cb func(ConsumerConsAddr, ccvtypes.LastUpdateMemo) bool) {
prefix := types.KeyMapCkToMemoChainPrefix(s.ChainID)
prefix := types.KeyMapCcaToLastUpdateMemoChainPrefix(s.ChainID)
iterator := sdk.KVStorePrefixIterator(s.Store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -573,9 +573,9 @@ func (s *KeyMapStore) IterateCcaToLastUpdateMemo(cb func(ConsumerConsAddr, ccvty
func (k Keeper) DeleteKeyMap(ctx sdk.Context, chainID string) {
store := ctx.KVStore(k.storeKey)
for _, pref := range [][]byte{
types.KeyMapPkToCkChainPrefix(chainID),
types.KeyMapPcaToCkChainPrefix(chainID),
types.KeyMapCkToPkChainPrefix(chainID),
types.KeyMapCkToMemoChainPrefix(chainID),
types.KeyMapCcaToLastUpdateMemoChainPrefix(chainID),
} {
iter := sdk.KVStorePrefixIterator(store, pref)
defer iter.Close()
Expand Down
24 changes: 12 additions & 12 deletions x/ccv/provider/keeper/keymap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (d *driver) applyKeyMapEntries(entries []keyMapEntry) {
}
// Duplicate the mapping for referencing later in tests.
copy := map[keeper.ProviderPubKey]keeper.ConsumerPubKey{}
d.km.Store.IteratePkToCk(func(pk, ck keeper.ConsumerPubKey) bool {
d.km.Store.IteratePcaToCk(func(pk, ck keeper.ConsumerPubKey) bool {
copy[pk] = ck
return false
})
Expand Down Expand Up @@ -299,7 +299,7 @@ func (d *driver) externalInvariants() {
// The query must return a result
pkQueried, found := d.km.GetProviderPubKeyFromConsumerPubKey(ckOnConsumer)
require.True(d.t, found)
pkQueriedByConsAddr, found := d.km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(ckOnConsumer))
pkQueriedByConsAddr, found := d.km.GetProviderPubKeyFromConsumerConsAddress(keeper.PubKeyToConsAddr(ckOnConsumer))
require.True(d.t, found)
require.Equal(d.t, pkQueried, pkQueriedByConsAddr)

Expand Down Expand Up @@ -375,7 +375,7 @@ func (d *driver) externalInvariants() {
// Simply check every consumer key for the correct queryable-ness.
for ck := uint64(0); ck < NUM_CKS; ck++ {
ck += 100 //TODO: fix with others
cca := keeper.ConsumerPubKeyToConsumerConsAddr(key(ck))
cca := keeper.PubKeyToConsAddr(key(ck))
_, actualQueryable := d.km.GetProviderPubKeyFromConsumerConsAddress(cca)
if expect, found := expectQueryable[keeper.DeterministicStringify(key(ck))]; found && expect {
require.True(d.t, actualQueryable)
Expand Down Expand Up @@ -661,13 +661,13 @@ func TestKeyMapSetUseReplaceAndReverse(t *testing.T) {
km.ComputeUpdates(100, updates)
km.SetProviderPubKeyToConsumerPubKey(key(42), key(44)) // New consumer key
// actual, _ := km.GetProviderPubKeyFromConsumerPubKey(key(43)) // Old is queryable
actual, _ := km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
actual, _ := km.GetProviderPubKeyFromConsumerConsAddress(keeper.PubKeyToConsAddr(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 no longer known to consumer
km.PruneUnusedKeys(102) // Old is garbage collected on provider
_, found := km.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
_, found := km.GetProviderPubKeyFromConsumerConsAddress(keeper.PubKeyToConsAddr(key(43)))
require.False(t, found)
actual, _ = km.GetProviderPubKeyFromConsumerPubKey(key(44)) // New key is still queryable
require.Equal(t, key(42), actual)
Expand All @@ -679,12 +679,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.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
actual, _ := km.GetProviderPubKeyFromConsumerConsAddress(keeper.PubKeyToConsAddr(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.GetProviderPubKeyFromConsumerConsAddress(keeper.ConsumerPubKeyToConsumerConsAddr(key(43)))
_, found := km.GetProviderPubKeyFromConsumerConsAddress(keeper.PubKeyToConsAddr(key(43)))
require.True(t, found)
actual, _ = km.GetProviderPubKeyFromConsumerPubKey(key(44)) // New key is still queryable
require.Equal(t, key(42), actual)
Expand Down Expand Up @@ -723,7 +723,7 @@ func compareForEquality(t *testing.T,
ckToMemo map[string]ccvtypes.LastUpdateMemo) {

cnt := 0
km.Store.IteratePkToCk(func(_, _ keeper.ConsumerPubKey) bool {
km.Store.IteratePcaToCk(func(_, _ keeper.ConsumerPubKey) bool {
cnt += 1
return false
})
Expand All @@ -744,7 +744,7 @@ func compareForEquality(t *testing.T,
require.Equal(t, len(ckToMemo), cnt)

for k, vExpect := range pkToCk {
vActual, found := km.Store.GetPkToCk(k)
vActual, found := km.Store.GetPcaToCk(k)
require.True(t, found)
require.Equal(t, vExpect, vActual)
}
Expand Down Expand Up @@ -786,13 +786,13 @@ func checkCorrectSerializationAndDeserialization(t *testing.T,
pkToCk[keys[2]] = keys[3]
ckToPk[keys[4]] = keys[5]
ckToPk[keys[6]] = keys[7]
ckToMemo[string(keeper.ConsumerPubKeyToConsumerConsAddr(keys[8]))] = ccvtypes.LastUpdateMemo{
ckToMemo[string(keeper.PubKeyToConsAddr(keys[8]))] = ccvtypes.LastUpdateMemo{
Ck: &keys[9],
Pk: &keys[10],
Vscid: uint64_0,
Power: int64_0,
}
ckToMemo[string(keeper.ConsumerPubKeyToConsumerConsAddr(keys[11]))] = ccvtypes.LastUpdateMemo{
ckToMemo[string(keeper.PubKeyToConsAddr(keys[11]))] = ccvtypes.LastUpdateMemo{
Ck: &keys[12],
Pk: &keys[13],
Vscid: uint64_1,
Expand All @@ -804,7 +804,7 @@ func checkCorrectSerializationAndDeserialization(t *testing.T,
store := keeper.KeyMapStore{keeperParams.Ctx.KVStore(keeperParams.StoreKey), chainID}
km := keeper.MakeKeyMap(&store)
for k, v := range pkToCk {
km.Store.SetPkToCk(k, v)
km.Store.SetPcaToCk(k, v)
}
for k, v := range ckToPk {
km.Store.SetCkToPk(k, v)
Expand Down
Loading

0 comments on commit d6dd825

Please sign in to comment.