Skip to content

Commit

Permalink
refactor!: remove 24-host path function in favour of sybling key func…
Browse files Browse the repository at this point in the history
…tions (#7016)

* refactor: remove all Path functions from 24-host in favour of Key functions

* docs: migration + changelog

* Update docs/docs/05-migrations/13-v8-to-v9.md

* lint
  • Loading branch information
colin-axner authored Aug 1, 2024
1 parent b8f944e commit f4eba71
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 210 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (light-clients/06-solomachine) [\#6888](https://github.com/cosmos/ibc-go/pull/6888) Remove `TypeClientMisbehaviour` constant and the `Type` method on `Misbehaviour`.
* (light-clients/06-solomachine, light-clients/07-tendermint) [\#6891](https://github.com/cosmos/ibc-go/pull/6891) The `VerifyMembership` and `VerifyNonMembership` functions of solomachine's `ClientState` have been made private. The `VerifyMembership`, `VerifyNonMembership`, `GetTimestampAtHeight`, `Status` and `Initialize` functions of tendermint's `ClientState` have been made private.
* (core/04-channel) [\#6902](https://github.com/cosmos/ibc-go/pull/6902) Add channel version to core application callbacks.
* (core/24-host) [\#6882](https://github.com/cosmos/ibc-go/issues/6882) All functions ending in `Path` have been removed from 24-host in favour of their sybling functions ending in `Key`.

### State Machine Breaking

Expand Down
5 changes: 5 additions & 0 deletions docs/docs/05-migrations/13-v8-to-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ There are four sections based on the four potential user groups of this document
- [04-channel](#04-channel)
- [05-port](#05-port)
- [23-commitment](#23-commitment)
- [24-host](#24-host)
- [IBC Apps](#ibc-apps)
- [ICS20 - Transfer](#ics20---transfer)
- [ICS20 v2](#ics20-v2)
Expand Down Expand Up @@ -195,6 +196,10 @@ type PacketDataUnmarshaler interface {
- The [`MerklePath` type](https://github.com/cosmos/ibc-go/blob/release/v9.0.x/modules/core/23-commitment/types/commitment.pb.go#L113-L119) has been deprecated and a new [`commitment.v2.MerklePath` type](https://github.com/cosmos/ibc-go/blob/release/v9.0.x/modules/core/23-commitment/types/v2/commitment.pb.go#L25-L30) has been introduced in [#6644](https://github.com/cosmos/ibc-go/pull/6644). The new `commitment.v2.MerklePath` contains `repeated bytes` in favour of `repeated string`. This allows users to prove values stored under keys which contain non-utf8 encoded symbols. As a result, changes have been made to the 02-client `Query` service and 08-wasm contract API messages for JSON blobs. See [02-client](#02-client) and [08-wasm](#08-wasm), respectively.
- The `commitment.v1.MerklePath` type has been removed and a new `commitment.v2.MerklePath` type has been introduced in [#6644](https://github.com/cosmos/ibc-go/pull/6644). The new `commitment.v2.MerklePath` contains `repeated bytes` in favour of `repeated string`. This allows users to prove values stored under keys which contain non-utf8 encoded symbols. As a result, changes have been made to the 02-client `Query` service and 08-wasm contract API messages for JSON blobs. See [02-client](#02-client) and [08-wasm](#08-wasm), respectively.

### 24-host

All functions ending with `Path` naming have been removed in favour of their sybling function which ends in `Key`.

## IBC Apps

### ICS20 - Transfer
Expand Down
4 changes: 2 additions & 2 deletions modules/core/04-channel/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (q *queryServer) PacketCommitments(c context.Context, req *types.QueryPacke
)
}
var commitments []*types.PacketState
store := prefix.NewStore(ctx.KVStore(q.storeKey), []byte(host.PacketCommitmentPrefixPath(req.PortId, req.ChannelId)))
store := prefix.NewStore(ctx.KVStore(q.storeKey), host.PacketCommitmentPrefixKey(req.PortId, req.ChannelId))

pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error {
keySplit := strings.Split(string(key), "/")
Expand Down Expand Up @@ -370,7 +370,7 @@ func (q *queryServer) PacketAcknowledgements(c context.Context, req *types.Query
)
}
var acks []*types.PacketState
store := prefix.NewStore(ctx.KVStore(q.storeKey), []byte(host.PacketAcknowledgementPrefixPath(req.PortId, req.ChannelId)))
store := prefix.NewStore(ctx.KVStore(q.storeKey), host.PacketAcknowledgementPrefixKey(req.PortId, req.ChannelId))

// if a list of packet sequences is provided then query for each specific ack and return a list <= len(req.PacketCommitmentSequences)
// otherwise, maintain previous behaviour and perform paginated query
Expand Down
4 changes: 2 additions & 2 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (k *Keeper) GetAllPacketCommitments(ctx sdk.Context) (commitments []types.P
// true, the iterator will close and stop.
func (k *Keeper) IteratePacketCommitmentAtChannel(ctx sdk.Context, portID, channelID string, cb func(_, _ string, sequence uint64, hash []byte) bool) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte(host.PacketCommitmentPrefixPath(portID, channelID)))
iterator := storetypes.KVStorePrefixIterator(store, host.PacketCommitmentPrefixKey(portID, channelID))
k.iterateHashes(ctx, iterator, cb)
}

Expand Down Expand Up @@ -629,7 +629,7 @@ func (Keeper) iterateHashes(ctx sdk.Context, iterator db.Iterator, cb func(portI
// HasInflightPackets returns true if there are packet commitments stored at the specified
// port and channel, and false otherwise.
func (k *Keeper) HasInflightPackets(ctx sdk.Context, portID, channelID string) bool {
iterator := storetypes.KVStorePrefixIterator(ctx.KVStore(k.storeKey), []byte(host.PacketCommitmentPrefixPath(portID, channelID)))
iterator := storetypes.KVStorePrefixIterator(ctx.KVStore(k.storeKey), host.PacketCommitmentPrefixKey(portID, channelID))
defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() })

return iterator.Valid()
Expand Down
33 changes: 29 additions & 4 deletions modules/core/24-host/channel_keys.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
package host

import "fmt"

const (
KeyChannelEndPrefix = "channelEnds"
KeyChannelPrefix = "channels"
KeyChannelUpgradePrefix = "channelUpgrades"
KeyUpgradePrefix = "upgrades"
KeyUpgradeErrorPrefix = "upgradeError"
KeyCounterpartyUpgrade = "counterpartyUpgrade"
KeyChannelCapabilityPrefix = "capabilities"
)

// ICS04
// The following paths are the keys to the store as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-004-channel-and-packet-semantics#store-paths

// ChannelKey returns the store key for a particular channel
func ChannelKey(portID, channelID string) []byte {
return []byte(ChannelPath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s", KeyChannelEndPrefix, channelPath(portID, channelID)))
}

// ChannelCapabilityPath defines the path under which capability keys associated
// with a channel are stored
func ChannelCapabilityPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s", KeyChannelCapabilityPrefix, channelPath(portID, channelID))
}

// ChannelUpgradeErrorKey returns the store key for a particular channelEnd used to stor the ErrorReceipt in the case that a chain does not accept the proposed upgrade
func ChannelUpgradeErrorKey(portID, channelID string) []byte {
return []byte(ChannelUpgradeErrorPath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradeErrorPrefix, channelPath(portID, channelID)))
}

// ChannelUpgradeKey returns the store key for a particular channel upgrade attempt
func ChannelUpgradeKey(portID, channelID string) []byte {
return []byte(ChannelUpgradePath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradePrefix, channelPath(portID, channelID)))
}

// ChannelCounterpartyUpgradeKey returns the store key for the upgrade used on the counterparty channel.
func ChannelCounterpartyUpgradeKey(portID, channelID string) []byte {
return []byte(ChannelCounterpartyUpgradePath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyCounterpartyUpgrade, channelPath(portID, channelID)))
}

func channelPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s/%s", KeyPortPrefix, portID, KeyChannelPrefix, channelID)
}
46 changes: 0 additions & 46 deletions modules/core/24-host/channel_paths.go

This file was deleted.

18 changes: 14 additions & 4 deletions modules/core/24-host/client_keys.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package host

import (
"fmt"

"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

// KeyClientStorePrefix defines the KVStore key prefix for IBC clients
var KeyClientStorePrefix = []byte("clients")

const (
KeyClientState = "clientState"
KeyConsensusStatePrefix = "consensusStates"
)

// FullClientKey returns the full path of specific client path in the format:
// "clients/{clientID}/{path}" as a byte array.
func FullClientKey(clientID string, path []byte) []byte {
return []byte(FullClientPath(clientID, string(path)))
return []byte(fmt.Sprintf("%s/%s/%s", KeyClientStorePrefix, clientID, path))
}

// PrefixedClientStoreKey returns a key which can be used for prefixed
// key store iteration. The prefix may be a clientType, clientID, or any
// valid key prefix which may be concatenated with the client store constant.
func PrefixedClientStoreKey(prefix []byte) []byte {
return []byte(PrefixedClientStorePath(prefix))
return []byte(fmt.Sprintf("%s/%s", KeyClientStorePrefix, prefix))
}

// FullClientStateKey takes a client identifier and returns a Key under which to store a
Expand All @@ -32,11 +42,11 @@ func ClientStateKey() []byte {
// FullConsensusStateKey returns the store key for the consensus state of a particular
// client.
func FullConsensusStateKey(clientID string, height exported.Height) []byte {
return []byte(FullConsensusStatePath(clientID, height))
return FullClientKey(clientID, ConsensusStateKey(height))
}

// ConsensusStateKey returns the store key for a the consensus state of a particular
// client stored in a client prefixed store.
func ConsensusStateKey(height exported.Height) []byte {
return []byte(ConsensusStatePath(height))
return []byte(fmt.Sprintf("%s/%s", KeyConsensusStatePrefix, height))
}
49 changes: 0 additions & 49 deletions modules/core/24-host/client_paths.go

This file was deleted.

11 changes: 9 additions & 2 deletions modules/core/24-host/connection_keys.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package host

import "fmt"

const KeyConnectionPrefix = "connections"

// ICS03
// The following paths are the keys to the store as defined in https://github.com/cosmos/ibc/blob/master/spec/core/ics-003-connection-semantics#store-paths

// ClientConnectionsKey returns the store key for the connections of a given client
func ClientConnectionsKey(clientID string) []byte {
return []byte(ClientConnectionsPath(clientID))
return FullClientKey(clientID, []byte(KeyConnectionPrefix))
}

// ConnectionKey returns the store key for a particular connection
func ConnectionKey(connectionID string) []byte {
return []byte(ConnectionPath(connectionID))
return []byte(fmt.Sprintf("%s/%s", KeyConnectionPrefix, connectionID))
}
18 changes: 0 additions & 18 deletions modules/core/24-host/connection_paths.go

This file was deleted.

47 changes: 39 additions & 8 deletions modules/core/24-host/packet_keys.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
package host

import "fmt"

const (
KeySequencePrefix = "sequences"
KeyNextSeqSendPrefix = "nextSequenceSend"
KeyNextSeqRecvPrefix = "nextSequenceRecv"
KeyNextSeqAckPrefix = "nextSequenceAck"
KeyPacketCommitmentPrefix = "commitments"
KeyPacketAckPrefix = "acks"
KeyPacketReceiptPrefix = "receipts"
KeyPruningSequenceStart = "pruningSequenceStart"
KeyRecvStartSequence = "recvStartSequence"
)

// ICS04
// The following paths are the keys to the store as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-004-channel-and-packet-semantics#store-paths

// NextSequenceSendKey returns the store key for the send sequence of a particular
// channel binded to a specific port.
func NextSequenceSendKey(portID, channelID string) []byte {
return []byte(NextSequenceSendPath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s", KeyNextSeqSendPrefix, channelPath(portID, channelID)))
}

// NextSequenceRecvKey returns the store key for the receive sequence of a particular
// channel binded to a specific port
func NextSequenceRecvKey(portID, channelID string) []byte {
return []byte(NextSequenceRecvPath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s", KeyNextSeqRecvPrefix, channelPath(portID, channelID)))
}

// NextSequenceAckKey returns the store key for the acknowledgement sequence of
// a particular channel binded to a specific port.
func NextSequenceAckKey(portID, channelID string) []byte {
return []byte(NextSequenceAckPath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s", KeyNextSeqAckPrefix, channelPath(portID, channelID)))
}

// PacketCommitmentKey returns the store key of under which a packet commitment
// is stored
func PacketCommitmentKey(portID, channelID string, sequence uint64) []byte {
return []byte(PacketCommitmentPath(portID, channelID, sequence))
return []byte(fmt.Sprintf("%s/%d", PacketCommitmentPrefixKey(portID, channelID), sequence))
}

// PacketCommitmentPrefixKey defines the prefix for commitments to packet data fields store path.
func PacketCommitmentPrefixKey(portID, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", KeyPacketCommitmentPrefix, channelPath(portID, channelID), KeySequencePrefix))
}

// PacketAcknowledgementKey returns the store key of under which a packet
// acknowledgement is stored
func PacketAcknowledgementKey(portID, channelID string, sequence uint64) []byte {
return []byte(PacketAcknowledgementPath(portID, channelID, sequence))
return []byte(fmt.Sprintf("%s/%d", PacketAcknowledgementPrefixKey(portID, channelID), sequence))
}

// PacketAcknowledgementPrefixKey defines the prefix for commitments to packet data fields store path.
func PacketAcknowledgementPrefixKey(portID, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", KeyPacketAckPrefix, channelPath(portID, channelID), KeySequencePrefix))
}

// PacketReceiptKey returns the store key of under which a packet
// receipt is stored
func PacketReceiptKey(portID, channelID string, sequence uint64) []byte {
return []byte(PacketReceiptPath(portID, channelID, sequence))
return []byte(fmt.Sprintf("%s/%s/%s", KeyPacketReceiptPrefix, channelPath(portID, channelID), sequencePath(sequence)))
}

// PruningSequenceStartKey returns the store key for the pruning sequence start of a particular channel
func PruningSequenceStartKey(portID, channelID string) []byte {
return []byte(PruningSequenceStartPath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s", KeyPruningSequenceStart, channelPath(portID, channelID)))
}

// RecvStartSequenceKey returns the store key for the recv start sequence of a particular channel
func RecvStartSequenceKey(portID, channelID string) []byte {
return []byte(RecvStartSequencePath(portID, channelID))
return []byte(fmt.Sprintf("%s/%s", KeyRecvStartSequence, channelPath(portID, channelID)))
}

func sequencePath(sequence uint64) string {
return fmt.Sprintf("%s/%d", KeySequencePrefix, sequence)
}
Loading

0 comments on commit f4eba71

Please sign in to comment.