Skip to content

Commit

Permalink
chore: split key and path functions in separate files (cosmos#5670)
Browse files Browse the repository at this point in the history
  • Loading branch information
crodriguezvega authored Apr 23, 2024
1 parent 6ceba47 commit 4efbc8a
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 177 deletions.
45 changes: 0 additions & 45 deletions modules/core/24-host/channel_keys.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,15 @@
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

// ChannelPath defines the path under which channels are stored
func ChannelPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s", KeyChannelEndPrefix, channelPath(portID, channelID))
}

// ChannelKey returns the store key for a particular channel
func ChannelKey(portID, channelID string) []byte {
return []byte(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))
}

// ChannelUpgradeErrorPath defines the path under which the ErrorReceipt is stored in the case that a chain does not accept the proposed upgrade
func ChannelUpgradeErrorPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradeErrorPrefix, 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))
}

// ChannelUpgradePath defines the path which stores the information related to an upgrade attempt
func ChannelUpgradePath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradePrefix, 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))
Expand All @@ -55,12 +19,3 @@ func ChannelUpgradeKey(portID, channelID string) []byte {
func ChannelCounterpartyUpgradeKey(portID, channelID string) []byte {
return []byte(ChannelCounterpartyUpgradePath(portID, channelID))
}

// ChannelCounterpartyUpgradePath defines the path under which the upgrade used on the counterparty channel is stored.
func ChannelCounterpartyUpgradePath(portID, channelID string) string {
return 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: 46 additions & 0 deletions modules/core/24-host/channel_paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +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

// ChannelPath defines the path under which channels are stored
func ChannelPath(portID, channelID string) string {
return 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))
}

// ChannelUpgradeErrorPath defines the path under which the ErrorReceipt is stored in the case that a chain does not accept the proposed upgrade
func ChannelUpgradeErrorPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradeErrorPrefix, channelPath(portID, channelID))
}

// ChannelUpgradePath defines the path which stores the information related to an upgrade attempt
func ChannelUpgradePath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyChannelUpgradePrefix, KeyUpgradePrefix, channelPath(portID, channelID))
}

// ChannelCounterpartyUpgradePath defines the path under which the upgrade used on the counterparty channel is stored.
func ChannelCounterpartyUpgradePath(portID, channelID string) string {
return 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)
}
44 changes: 0 additions & 44 deletions modules/core/24-host/client_keys.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
package host

import (
"fmt"

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

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

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

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

// 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)))
}

// PrefixedClientStorePath returns a key path 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 PrefixedClientStorePath(prefix []byte) string {
return fmt.Sprintf("%s/%s", KeyClientStorePrefix, prefix)
}

// 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))
}

// ICS02
// The following paths are the keys to the store as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-002-client-semantics#path-space

// FullClientStatePath takes a client identifier and returns a Path under which to store a
// particular client state
func FullClientStatePath(clientID string) string {
return FullClientPath(clientID, KeyClientState)
}

// FullClientStateKey takes a client identifier and returns a Key under which to store a
// particular client state.
func FullClientStateKey(clientID string) []byte {
Expand All @@ -61,24 +29,12 @@ func ClientStateKey() []byte {
return []byte(KeyClientState)
}

// FullConsensusStatePath takes a client identifier and returns a Path under which to
// store the consensus state of a client.
func FullConsensusStatePath(clientID string, height exported.Height) string {
return FullClientPath(clientID, ConsensusStatePath(height))
}

// 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))
}

// ConsensusStatePath returns the suffix store key for the consensus state at a
// particular height stored in a client prefixed store.
func ConsensusStatePath(height exported.Height) string {
return fmt.Sprintf("%s/%s", KeyConsensusStatePrefix, 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 {
Expand Down
49 changes: 49 additions & 0 deletions modules/core/24-host/client_paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package host

import (
"fmt"

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

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

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

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

// PrefixedClientStorePath returns a key path 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 PrefixedClientStorePath(prefix []byte) string {
return fmt.Sprintf("%s/%s", KeyClientStorePrefix, prefix)
}

// ICS02
// The following paths are the keys to the store as defined in https://github.com/cosmos/ibc/tree/master/spec/core/ics-002-client-semantics#path-space

// FullClientStatePath takes a client identifier and returns a Path under which to store a
// particular client state
func FullClientStatePath(clientID string) string {
return FullClientPath(clientID, KeyClientState)
}

// FullConsensusStatePath takes a client identifier and returns a Path under which to
// store the consensus state of a client.
func FullConsensusStatePath(clientID string, height exported.Height) string {
return FullClientPath(clientID, ConsensusStatePath(height))
}

// ConsensusStatePath returns the suffix store key for the consensus state at a
// particular height stored in a client prefixed store.
func ConsensusStatePath(height exported.Height) string {
return fmt.Sprintf("%s/%s", KeyConsensusStatePrefix, height)
}
17 changes: 0 additions & 17 deletions modules/core/24-host/connection_keys.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
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

// ClientConnectionsPath defines a reverse mapping from clients to a set of connections
func ClientConnectionsPath(clientID string) string {
return FullClientPath(clientID, KeyConnectionPrefix)
}

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

// ConnectionPath defines the path under which connection paths are stored
func ConnectionPath(connectionID string) string {
return fmt.Sprintf("%s/%s", KeyConnectionPrefix, connectionID)
}

// ConnectionKey returns the store key for a particular connection
func ConnectionKey(connectionID string) []byte {
return []byte(ConnectionPath(connectionID))
Expand Down
18 changes: 18 additions & 0 deletions modules/core/24-host/connection_paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +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

// ClientConnectionsPath defines a reverse mapping from clients to a set of connections
func ClientConnectionsPath(clientID string) string {
return FullClientPath(clientID, KeyConnectionPrefix)
}

// ConnectionPath defines the path under which connection paths are stored
func ConnectionPath(connectionID string) string {
return fmt.Sprintf("%s/%s", KeyConnectionPrefix, connectionID)
}
Loading

0 comments on commit 4efbc8a

Please sign in to comment.