Skip to content

Commit

Permalink
refactor!: remove unnecessary getters on connection end (cosmos#5777)
Browse files Browse the repository at this point in the history
* refactor: rm GetClientID() from connection interface

* refactor: remove GetCounterparty() from connection type

* refactor: remove GetVersions() from connection type

* refactor: remove GetDelayPeriod() on connection + test fixes/docs
  • Loading branch information
colin-axner committed Jan 31, 2024
1 parent bc4ef0c commit 1c0c756
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 72 deletions.
1 change: 1 addition & 0 deletions docs/docs/05-migrations/13-v8-to-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The `exported.ConnectionI` and `exported.CounterpartyConnectionI` interfaces hav

The functions `GetState()`, `GetOrdering()`, `GetCounterparty()`, `GetConnectionHops()`, `GetVersion()` of the `Channel` type have been removed.
The functions `GetPortID()`, `GetChannelID()` of the `CounterpartyChannel` type have been removed.
The function `GetClientID()`, `GetState()`, `GetCounterparty()`, `GetVersions()`, and `GetDelayPeriod` of the `Connection` type have been removed.

### API deprecation notice

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (k Keeper) OnChanOpenInit(
return "", err
}

metadata = icatypes.NewDefaultMetadata(connectionHops[0], connection.GetCounterparty().GetConnectionID())
metadata = icatypes.NewDefaultMetadata(connectionHops[0], connection.Counterparty.GetConnectionID())
} else {
metadata, err = icatypes.MetadataFromVersion(version)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/types/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func ValidateControllerMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, co
return err
}

if err := validateConnectionParams(metadata, connectionHops[0], connection.GetCounterparty().GetConnectionID()); err != nil {
if err := validateConnectionParams(metadata, connectionHops[0], connection.Counterparty.GetConnectionID()); err != nil {
return err
}

Expand Down Expand Up @@ -126,7 +126,7 @@ func ValidateHostMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connecti
return err
}

if err := validateConnectionParams(metadata, connection.GetCounterparty().GetConnectionID(), connectionHops[0]); err != nil {
if err := validateConnectionParams(metadata, connection.Counterparty.GetConnectionID(), connectionHops[0]); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions modules/core/03-connection/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ func (k Keeper) SetConnection(ctx sdk.Context, connectionID string, connection t
// GetTimestampAtHeight returns the timestamp in nanoseconds of the consensus state at the
// given height.
func (k Keeper) GetTimestampAtHeight(ctx sdk.Context, connection types.ConnectionEnd, height exported.Height) (uint64, error) {
clientState, found := k.clientKeeper.GetClientState(ctx, connection.GetClientID())
clientState, found := k.clientKeeper.GetClientState(ctx, connection.ClientId)
if !found {
return 0, errorsmod.Wrapf(
clienttypes.ErrClientNotFound, "clientID (%s)", connection.GetClientID(),
clienttypes.ErrClientNotFound, "clientID (%s)", connection.ClientId,
)
}

timestamp, err := clientState.GetTimestampAtHeight(ctx, k.clientKeeper.ClientStore(ctx, connection.GetClientID()), k.cdc, height)
timestamp, err := clientState.GetTimestampAtHeight(ctx, k.clientKeeper.ClientStore(ctx, connection.ClientId), k.cdc, height)
if err != nil {
return 0, err
}
Expand Down
54 changes: 27 additions & 27 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (k Keeper) VerifyClientState(
proof []byte,
clientState exported.ClientState,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
targetClient, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -35,8 +35,8 @@ func (k Keeper) VerifyClientState(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.FullClientStatePath(connection.GetCounterparty().GetClientID()))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath := commitmenttypes.NewMerklePath(host.FullClientStatePath(connection.Counterparty.ClientId))
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (k Keeper) VerifyClientConsensusState(
proof []byte,
consensusState exported.ConsensusState,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -77,8 +77,8 @@ func (k Keeper) VerifyClientConsensusState(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStatePath(connection.GetCounterparty().GetClientID(), consensusHeight))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath := commitmenttypes.NewMerklePath(host.FullConsensusStatePath(connection.Counterparty.ClientId, consensusHeight))
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (k Keeper) VerifyConnectionState(
connectionID string,
counterpartyConnection types.ConnectionEnd, // opposite connection
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -120,7 +120,7 @@ func (k Keeper) VerifyConnectionState(
}

merklePath := commitmenttypes.NewMerklePath(host.ConnectionPath(connectionID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func (k Keeper) VerifyChannelState(
channelID string,
channel channeltypes.Channel,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -163,7 +163,7 @@ func (k Keeper) VerifyChannelState(
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelPath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (k Keeper) VerifyPacketCommitment(
sequence uint64,
commitmentBytes []byte,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -207,11 +207,11 @@ func (k Keeper) VerifyPacketCommitment(
}

// get time and block delays
timeDelay := connection.GetDelayPeriod()
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, sequence))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func (k Keeper) VerifyPacketAcknowledgement(
sequence uint64,
acknowledgement []byte,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -250,11 +250,11 @@ func (k Keeper) VerifyPacketAcknowledgement(
}

// get time and block delays
timeDelay := connection.GetDelayPeriod()
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, sequence))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (k Keeper) VerifyPacketReceiptAbsence(
channelID string,
sequence uint64,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -293,11 +293,11 @@ func (k Keeper) VerifyPacketReceiptAbsence(
}

// get time and block delays
timeDelay := connection.GetDelayPeriod()
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, sequence))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand All @@ -324,7 +324,7 @@ func (k Keeper) VerifyNextSequenceRecv(
channelID string,
nextSequenceRecv uint64,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -335,11 +335,11 @@ func (k Keeper) VerifyNextSequenceRecv(
}

// get time and block delays
timeDelay := connection.GetDelayPeriod()
timeDelay := connection.DelayPeriod
blockDelay := k.getBlockDelay(ctx, connection)

merklePath := commitmenttypes.NewMerklePath(host.NextSequenceRecvPath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand All @@ -365,7 +365,7 @@ func (k Keeper) VerifyChannelUpgradeError(
channelID string,
errorReceipt channeltypes.ErrorReceipt,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -376,7 +376,7 @@ func (k Keeper) VerifyChannelUpgradeError(
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorPath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func (k Keeper) VerifyChannelUpgrade(
channelID string,
upgrade channeltypes.Upgrade,
) error {
clientID := connection.GetClientID()
clientID := connection.ClientId
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
Expand All @@ -418,7 +418,7 @@ func (k Keeper) VerifyChannelUpgrade(
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradePath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
merklePath, err = commitmenttypes.ApplyPrefix(connection.Counterparty.GetPrefix(), merklePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func (k Keeper) getBlockDelay(ctx sdk.Context, connection types.ConnectionEnd) u
}
// calculate minimum block delay by dividing time delay period
// by the expected time per block. Round up the block delay.
timeDelay := connection.GetDelayPeriod()
timeDelay := connection.DelayPeriod
return uint64(math.Ceil(float64(timeDelay) / float64(expectedTimePerBlock)))
}

Expand Down
20 changes: 0 additions & 20 deletions modules/core/03-connection/types/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ func NewConnectionEnd(state State, clientID string, counterparty Counterparty, v
}
}

// GetClientID implements the Connection interface
func (c ConnectionEnd) GetClientID() string {
return c.ClientId
}

// GetCounterparty implements the Connection interface
func (c ConnectionEnd) GetCounterparty() Counterparty {
return c.Counterparty
}

// GetVersions implements the Connection interface
func (c ConnectionEnd) GetVersions() []*Version {
return c.Versions
}

// GetDelayPeriod implements the Connection interface
func (c ConnectionEnd) GetDelayPeriod() uint64 {
return c.DelayPeriod
}

// ValidateBasic implements the Connection interface.
// NOTE: the protocol supports that the connection and client IDs match the
// counterparty's.
Expand Down
12 changes: 6 additions & 6 deletions modules/core/04-channel/keeper/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (k Keeper) ChanOpenInit(
return "", nil, errorsmod.Wrap(connectiontypes.ErrConnectionNotFound, connectionHops[0])
}

getVersions := connectionEnd.GetVersions()
getVersions := connectionEnd.Versions
if len(getVersions) != 1 {
return "", nil, errorsmod.Wrapf(
connectiontypes.ErrInvalidVersion,
Expand Down Expand Up @@ -135,7 +135,7 @@ func (k Keeper) ChanOpenTry(
return "", nil, errorsmod.Wrapf(connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectionEnd.State)
}

getVersions := connectionEnd.GetVersions()
getVersions := connectionEnd.Versions
if len(getVersions) != 1 {
return "", nil, errorsmod.Wrapf(
connectiontypes.ErrInvalidVersion,
Expand All @@ -152,7 +152,7 @@ func (k Keeper) ChanOpenTry(
)
}

counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()}
counterpartyHops := []string{connectionEnd.Counterparty.GetConnectionID()}

// expectedCounterpaty is the counterparty of the counterparty's channel end
// (i.e self)
Expand Down Expand Up @@ -243,7 +243,7 @@ func (k Keeper) ChanOpenAck(
return errorsmod.Wrapf(connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectionEnd.State)
}

counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()}
counterpartyHops := []string{connectionEnd.Counterparty.GetConnectionID()}

// counterparty of the counterparty channel end (i.e self)
expectedCounterparty := types.NewCounterparty(portID, channelID)
Expand Down Expand Up @@ -319,7 +319,7 @@ func (k Keeper) ChanOpenConfirm(
return errorsmod.Wrapf(connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectionEnd.State)
}

counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()}
counterpartyHops := []string{connectionEnd.Counterparty.GetConnectionID()}

counterparty := types.NewCounterparty(portID, channelID)
expectedChannel := types.NewChannel(
Expand Down Expand Up @@ -445,7 +445,7 @@ func (k Keeper) ChanCloseConfirm(
return errorsmod.Wrapf(connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectionEnd.State)
}

counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()}
counterpartyHops := []string{connectionEnd.Counterparty.GetConnectionID()}

counterparty := types.NewCounterparty(portID, channelID)
expectedChannel := types.Channel{
Expand Down
6 changes: 3 additions & 3 deletions modules/core/04-channel/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func (k Keeper) SendPacket(
return 0, errorsmod.Wrap(connectiontypes.ErrConnectionNotFound, channel.ConnectionHops[0])
}

clientState, found := k.clientKeeper.GetClientState(ctx, connectionEnd.GetClientID())
clientState, found := k.clientKeeper.GetClientState(ctx, connectionEnd.ClientId)
if !found {
return 0, clienttypes.ErrClientNotFound
}

// prevent accidental sends with clients that cannot be updated
if status := k.clientKeeper.GetClientStatus(ctx, clientState, connectionEnd.GetClientID()); status != exported.Active {
return 0, errorsmod.Wrapf(clienttypes.ErrClientNotActive, "cannot send packet using client (%s) with status %s", connectionEnd.GetClientID(), status)
if status := k.clientKeeper.GetClientStatus(ctx, clientState, connectionEnd.ClientId); status != exported.Active {
return 0, errorsmod.Wrapf(clienttypes.ErrClientNotActive, "cannot send packet using client (%s) with status %s", connectionEnd.ClientId, status)
}

latestHeight := clientState.GetLatestHeight()
Expand Down
2 changes: 1 addition & 1 deletion modules/core/04-channel/keeper/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (k Keeper) TimeoutOnClose(
return errorsmod.Wrapf(types.ErrInvalidPacket, "packet commitment bytes are not equal: got (%v), expected (%v)", commitment, packetCommitment)
}

counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()}
counterpartyHops := []string{connectionEnd.Counterparty.GetConnectionID()}

counterparty := types.NewCounterparty(packet.GetSourcePort(), packet.GetSourceChannel())
expectedChannel := types.Channel{
Expand Down
Loading

0 comments on commit 1c0c756

Please sign in to comment.