Skip to content

Commit

Permalink
Update IBC host keys + cleanup (#7873)
Browse files Browse the repository at this point in the history
* init changes

* cleanup keys and remove redunancdy

* revert unintentional changes

* KeyChannel -> ChannelKey
  • Loading branch information
colin-axner authored Nov 10, 2020
1 parent fdcb028 commit 300b739
Show file tree
Hide file tree
Showing 38 changed files with 272 additions and 250 deletions.
2 changes: 1 addition & 1 deletion x/ibc/applications/transfer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (suite *KeeperTestSuite) TestSendTransfer() {
packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, channelB.PortID, channelB.ID, channelA.PortID, channelA.ID, clienttypes.NewHeight(0, 110), 0)

// get proof of packet commitment from chainB
packetKey := host.KeyPacketCommitment(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := suite.chainB.QueryProof(packetKey)

recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, suite.chainA.SenderAccount.GetAddress())
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/core/02-client/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func QueryClientState(
func QueryClientStateABCI(
clientCtx client.Context, clientID string,
) (*types.QueryClientStateResponse, error) {
key := host.FullKeyClientPath(clientID, host.KeyClientState())
key := host.FullClientStateKey(clientID)

value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
Expand Down Expand Up @@ -91,7 +91,7 @@ func QueryConsensusState(
func QueryConsensusStateABCI(
clientCtx client.Context, clientID string, height exported.Height,
) (*types.QueryConsensusStateResponse, error) {
key := host.FullKeyClientPath(clientID, host.KeyConsensusState(height))
key := host.FullConsensusStateKey(clientID, height)

value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/core/02-client/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (q Keeper) ConsensusStates(c context.Context, req *types.QueryConsensusStat
ctx := sdk.UnwrapSDKContext(c)

consensusStates := []types.ConsensusStateWithHeight{}
store := prefix.NewStore(ctx.KVStore(q.storeKey), host.FullKeyClientPath(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatesPrefix))))
store := prefix.NewStore(ctx.KVStore(q.storeKey), host.FullClientKey(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatePrefix))))

pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
height, err := types.ParseHeight(string(key))
Expand Down
18 changes: 8 additions & 10 deletions x/ibc/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
// GetClientState gets a particular client from the store
func (k Keeper) GetClientState(ctx sdk.Context, clientID string) (exported.ClientState, bool) {
store := k.ClientStore(ctx, clientID)
bz := store.Get(host.KeyClientState())
bz := store.Get(host.ClientStateKey())
if bz == nil {
return nil, false
}
Expand All @@ -58,13 +58,13 @@ func (k Keeper) GetClientState(ctx sdk.Context, clientID string) (exported.Clien
// SetClientState sets a particular Client to the store
func (k Keeper) SetClientState(ctx sdk.Context, clientID string, clientState exported.ClientState) {
store := k.ClientStore(ctx, clientID)
store.Set(host.KeyClientState(), k.MustMarshalClientState(clientState))
store.Set(host.ClientStateKey(), k.MustMarshalClientState(clientState))
}

// GetClientConsensusState gets the stored consensus state from a client at a given height.
func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height exported.Height) (exported.ConsensusState, bool) {
store := k.ClientStore(ctx, clientID)
bz := store.Get(host.KeyConsensusState(height))
bz := store.Get(host.ConsensusStateKey(height))
if bz == nil {
return nil, false
}
Expand All @@ -77,7 +77,7 @@ func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height
// height
func (k Keeper) SetClientConsensusState(ctx sdk.Context, clientID string, height exported.Height, consensusState exported.ConsensusState) {
store := k.ClientStore(ctx, clientID)
store.Set(host.KeyConsensusState(height), k.MustMarshalConsensusState(consensusState))
store.Set(host.ConsensusStateKey(height), k.MustMarshalConsensusState(consensusState))
}

// IterateConsensusStates provides an iterator over all stored consensus states.
Expand All @@ -91,7 +91,7 @@ func (k Keeper) IterateConsensusStates(ctx sdk.Context, cb func(clientID string,
for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")
// consensus key is in the format "clients/<clientID>/consensusStates/<height>"
if len(keySplit) != 4 || keySplit[2] != string(host.KeyConsensusStatesPrefix) {
if len(keySplit) != 4 || keySplit[2] != string(host.KeyConsensusStatePrefix) {
continue
}
clientID := keySplit[1]
Expand Down Expand Up @@ -144,7 +144,7 @@ func (k Keeper) GetAllConsensusStates(ctx sdk.Context) types.ClientsConsensusSta
// client at the given height
func (k Keeper) HasClientConsensusState(ctx sdk.Context, clientID string, height exported.Height) bool {
store := k.ClientStore(ctx, clientID)
return store.Has(host.KeyConsensusState(height))
return store.Has(host.ConsensusStateKey(height))
}

// GetLatestClientConsensusState gets the latest ConsensusState stored for a given client
Expand Down Expand Up @@ -266,7 +266,7 @@ func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs expo
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")
if keySplit[len(keySplit)-1] != "clientState" {
if keySplit[len(keySplit)-1] != host.KeyClientState {
continue
}
clientState := k.MustUnmarshalClientState(iterator.Value())
Expand All @@ -291,8 +291,6 @@ func (k Keeper) GetAllClients(ctx sdk.Context) (states []exported.ClientState) {
// ClientStore returns isolated prefix store for each client so they can read/write in separate
// namespace without being able to read/write other client's data
func (k Keeper) ClientStore(ctx sdk.Context, clientID string) sdk.KVStore {
// append here is safe, appends within a function won't cause
// weird side effects when its singlethreaded
clientPrefix := append([]byte("clients/"+clientID), '/')
clientPrefix := []byte(fmt.Sprintf("%s/%s/", host.KeyClientStorePrefix, clientID))
return prefix.NewStore(ctx.KVStore(k.storeKey), clientPrefix)
}
4 changes: 2 additions & 2 deletions x/ibc/core/02-client/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type ClientUnmarshaler interface {
// Value to the corresponding client type.
func NewDecodeStore(cdc ClientUnmarshaler, kvA, kvB kv.Pair) (string, bool) {
switch {
case bytes.HasPrefix(kvA.Key, host.KeyClientStorePrefix) && bytes.HasSuffix(kvA.Key, host.KeyClientState()):
case bytes.HasPrefix(kvA.Key, host.KeyClientStorePrefix) && bytes.HasSuffix(kvA.Key, []byte(host.KeyClientState)):
clientStateA := cdc.MustUnmarshalClientState(kvA.Value)
clientStateB := cdc.MustUnmarshalClientState(kvB.Value)
return fmt.Sprintf("ClientState A: %v\nClientState B: %v", clientStateA, clientStateB), true

case bytes.HasPrefix(kvA.Key, host.KeyClientStorePrefix) && bytes.Contains(kvA.Key, []byte("consensusState")):
case bytes.HasPrefix(kvA.Key, host.KeyClientStorePrefix) && bytes.Contains(kvA.Key, []byte(host.KeyConsensusStatePrefix)):
consensusStateA := cdc.MustUnmarshalConsensusState(kvA.Value)
consensusStateB := cdc.MustUnmarshalConsensusState(kvB.Value)
return fmt.Sprintf("ConsensusState A: %v\nConsensusState B: %v", consensusStateA, consensusStateB), true
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/core/02-client/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func TestDecodeStore(t *testing.T) {
kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{
Key: host.FullKeyClientPath(clientID, host.KeyClientState()),
Key: host.FullClientStateKey(clientID),
Value: app.IBCKeeper.ClientKeeper.MustMarshalClientState(clientState),
},
{
Key: host.FullKeyClientPath(clientID, host.KeyConsensusState(height)),
Key: host.FullConsensusStateKey(clientID, height),
Value: app.IBCKeeper.ClientKeeper.MustMarshalConsensusState(consState),
},
{
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/core/03-connection/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func QueryConnection(
}

func queryConnectionABCI(clientCtx client.Context, connectionID string) (*types.QueryConnectionResponse, error) {
key := host.KeyConnection(connectionID)
key := host.ConnectionKey(connectionID)

value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
Expand Down Expand Up @@ -79,7 +79,7 @@ func QueryClientConnections(
}

func queryClientConnectionsABCI(clientCtx client.Context, clientID string) (*types.QueryClientConnectionsResponse, error) {
key := host.KeyClientConnections(clientID)
key := host.ClientConnectionsKey(clientID)

value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/core/03-connection/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (q Keeper) Connections(c context.Context, req *types.QueryConnectionsReques
ctx := sdk.UnwrapSDKContext(c)

connections := []*types.IdentifiedConnection{}
store := prefix.NewStore(ctx.KVStore(q.storeKey), host.KeyConnectionPrefix)
store := prefix.NewStore(ctx.KVStore(q.storeKey), []byte(host.KeyConnectionPrefix))

pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
var result types.ConnectionEnd
Expand Down
20 changes: 10 additions & 10 deletions x/ibc/core/03-connection/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,18 @@ func (suite *KeeperTestSuite) TestConnOpenTry() {
counterpartyChosenConnectionID = connection.Counterparty.ConnectionId
}

connectionKey := host.KeyConnection(connA.ID)
connectionKey := host.ConnectionKey(connA.ID)
proofInit, proofHeight := suite.chainA.QueryProof(connectionKey)

if consensusHeight.IsZero() {
// retrieve consensus state height to provide proof for
consensusHeight = counterpartyClient.GetLatestHeight()
}
consensusKey := host.FullKeyClientPath(clientA, host.KeyConsensusState(consensusHeight))
consensusKey := host.FullConsensusStateKey(clientA, consensusHeight)
proofConsensus, _ := suite.chainA.QueryProof(consensusKey)

// retrieve proof of counterparty clientstate on chainA
clientKey := host.FullKeyClientPath(clientA, host.KeyClientState())
clientKey := host.FullClientStateKey(clientA)
proofClient, _ := suite.chainA.QueryProof(clientKey)

err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenTry(
Expand Down Expand Up @@ -661,10 +661,10 @@ func (suite *KeeperTestSuite) TestConnOpenAck() {
for _, tc := range testCases {
tc := tc
suite.Run(tc.msg, func() {
suite.SetupTest() // reset
suite.SetupTest() // reset
version = types.ExportedVersionsToProto(types.GetCompatibleVersions())[0] // must be explicitly changed in malleate
consensusHeight = clienttypes.ZeroHeight() // must be explicitly changed in malleate
counterpartyConnectionID = "" // must be explicitly changed in malleate
consensusHeight = clienttypes.ZeroHeight() // must be explicitly changed in malleate
counterpartyConnectionID = "" // must be explicitly changed in malleate

tc.malleate()

Expand All @@ -675,19 +675,19 @@ func (suite *KeeperTestSuite) TestConnOpenAck() {
counterpartyConnectionID = connB.ID
}

connectionKey := host.KeyConnection(connB.ID)
connectionKey := host.ConnectionKey(connB.ID)
proofTry, proofHeight := suite.chainB.QueryProof(connectionKey)

if consensusHeight.IsZero() {
// retrieve consensus state height to provide proof for
clientState := suite.chainB.GetClientState(clientB)
consensusHeight = clientState.GetLatestHeight()
}
consensusKey := host.FullKeyClientPath(clientB, host.KeyConsensusState(consensusHeight))
consensusKey := host.FullConsensusStateKey(clientB, consensusHeight)
proofConsensus, _ := suite.chainB.QueryProof(consensusKey)

// retrieve proof of counterparty clientstate on chainA
clientKey := host.FullKeyClientPath(clientB, host.KeyClientState())
clientKey := host.FullClientStateKey(clientB)
proofClient, _ := suite.chainB.QueryProof(clientKey)

err := suite.chainA.App.IBCKeeper.ConnectionKeeper.ConnOpenAck(
Expand Down Expand Up @@ -757,7 +757,7 @@ func (suite *KeeperTestSuite) TestConnOpenConfirm() {
connA := suite.chainA.GetFirstTestConnection(clientA, clientB)
connB := suite.chainB.GetFirstTestConnection(clientB, clientA)

connectionKey := host.KeyConnection(connA.ID)
connectionKey := host.ConnectionKey(connA.ID)
proofAck, proofHeight := suite.chainA.QueryProof(connectionKey)

err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenConfirm(
Expand Down
10 changes: 5 additions & 5 deletions x/ibc/core/03-connection/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (k Keeper) GetCommitmentPrefix() exported.Prefix {
// GetConnection returns a connection with a particular identifier
func (k Keeper) GetConnection(ctx sdk.Context, connectionID string) (types.ConnectionEnd, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.KeyConnection(connectionID))
bz := store.Get(host.ConnectionKey(connectionID))
if bz == nil {
return types.ConnectionEnd{}, false
}
Expand All @@ -63,7 +63,7 @@ func (k Keeper) GetConnection(ctx sdk.Context, connectionID string) (types.Conne
func (k Keeper) SetConnection(ctx sdk.Context, connectionID string, connection types.ConnectionEnd) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&connection)
store.Set(host.KeyConnection(connectionID), bz)
store.Set(host.ConnectionKey(connectionID), bz)
}

// GetTimestampAtHeight returns the timestamp in nanoseconds of the consensus state at the
Expand All @@ -87,7 +87,7 @@ func (k Keeper) GetTimestampAtHeight(ctx sdk.Context, connection types.Connectio
// particular client
func (k Keeper) GetClientConnectionPaths(ctx sdk.Context, clientID string) ([]string, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.KeyClientConnections(clientID))
bz := store.Get(host.ClientConnectionsKey(clientID))
if bz == nil {
return nil, false
}
Expand All @@ -102,7 +102,7 @@ func (k Keeper) SetClientConnectionPaths(ctx sdk.Context, clientID string, paths
store := ctx.KVStore(k.storeKey)
clientPaths := types.ClientPaths{Paths: paths}
bz := k.cdc.MustMarshalBinaryBare(&clientPaths)
store.Set(host.KeyClientConnections(clientID), bz)
store.Set(host.ClientConnectionsKey(clientID), bz)
}

// GetAllClientConnectionPaths returns all stored clients connection id paths. It
Expand All @@ -129,7 +129,7 @@ func (k Keeper) GetAllClientConnectionPaths(ctx sdk.Context) []types.ConnectionP
// iterator will close and stop.
func (k Keeper) IterateConnections(ctx sdk.Context, cb func(types.IdentifiedConnection) bool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, host.KeyConnectionPrefix)
iterator := sdk.KVStorePrefixIterator(store, []byte(host.KeyConnectionPrefix))

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down
12 changes: 6 additions & 6 deletions x/ibc/core/03-connection/keeper/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (suite *KeeperTestSuite) TestVerifyConnectionState() {
}
expectedConnection := suite.chainB.GetConnection(connB)

connectionKey := host.KeyConnection(connB.ID)
connectionKey := host.ConnectionKey(connB.ID)
proof, proofHeight := suite.chainB.QueryProof(connectionKey)

if tc.changeConnectionState {
Expand Down Expand Up @@ -227,7 +227,7 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() {
connection.ClientId = ibctesting.InvalidID
}

channelKey := host.KeyChannel(channelB.PortID, channelB.ID)
channelKey := host.ChannelKey(channelB.PortID, channelB.ID)
proof, proofHeight := suite.chainB.QueryProof(channelKey)

channel := suite.chainB.GetChannel(channelB)
Expand Down Expand Up @@ -282,7 +282,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketCommitment() {
err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB)
suite.Require().NoError(err)

commitmentKey := host.KeyPacketCommitment(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
commitmentKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := suite.chainA.QueryProof(commitmentKey)

if tc.changePacketCommitmentState {
Expand Down Expand Up @@ -340,7 +340,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgement() {
err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet)
suite.Require().NoError(err)

packetAckKey := host.KeyPacketAcknowledgement(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
packetAckKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
proof, proofHeight := suite.chainB.QueryProof(packetAckKey)

ack := ibcmock.MockAcknowledgement
Expand Down Expand Up @@ -405,7 +405,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketReceiptAbsence() {
suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, ibctesting.Tendermint)
}

packetReceiptKey := host.KeyPacketReceipt(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
packetReceiptKey := host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
proof, proofHeight := suite.chainB.QueryProof(packetReceiptKey)

err = suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyPacketReceiptAbsence(
Expand Down Expand Up @@ -459,7 +459,7 @@ func (suite *KeeperTestSuite) TestVerifyNextSequenceRecv() {
err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet)
suite.Require().NoError(err)

nextSeqRecvKey := host.KeyNextSequenceRecv(packet.GetDestPort(), packet.GetDestChannel())
nextSeqRecvKey := host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel())
proof, proofHeight := suite.chainB.QueryProof(nextSeqRecvKey)

err = suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyNextSequenceRecv(
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/core/03-connection/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
// Value to the corresponding connection type.
func NewDecodeStore(cdc codec.BinaryMarshaler, kvA, kvB kv.Pair) (string, bool) {
switch {
case bytes.HasPrefix(kvA.Key, host.KeyClientStorePrefix) && bytes.HasSuffix(kvA.Key, host.KeyConnectionPrefix):
case bytes.HasPrefix(kvA.Key, host.KeyClientStorePrefix) && bytes.HasSuffix(kvA.Key, []byte(host.KeyConnectionPrefix)):
var clientConnectionsA, clientConnectionsB types.ClientPaths
cdc.MustUnmarshalBinaryBare(kvA.Value, &clientConnectionsA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &clientConnectionsB)
return fmt.Sprintf("ClientPaths A: %v\nClientPaths B: %v", clientConnectionsA, clientConnectionsB), true

case bytes.HasPrefix(kvA.Key, host.KeyConnectionPrefix):
case bytes.HasPrefix(kvA.Key, []byte(host.KeyConnectionPrefix)):
var connectionA, connectionB types.ConnectionEnd
cdc.MustUnmarshalBinaryBare(kvA.Value, &connectionA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &connectionB)
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/core/03-connection/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func TestDecodeStore(t *testing.T) {
kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{
Key: host.KeyClientConnections(connection.ClientId),
Key: host.ClientConnectionsKey(connection.ClientId),
Value: cdc.MustMarshalBinaryBare(&paths),
},
{
Key: host.KeyConnection(connectionID),
Key: host.ConnectionKey(connectionID),
Value: cdc.MustMarshalBinaryBare(&connection),
},
{
Expand Down
Loading

0 comments on commit 300b739

Please sign in to comment.