Skip to content

Commit

Permalink
Add upgrade type verification function (#3453)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Apr 18, 2023
1 parent cd7afe2 commit e62634d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 211 deletions.
88 changes: 25 additions & 63 deletions modules/core/03-connection/keeper/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,15 @@ func (k Keeper) VerifyNextSequenceRecv(
return nil
}

// VerifyChannelUpgradeSequence verifies a proof of the upgrade sequence number to be
// used during channel upgrades.
func (k Keeper) VerifyChannelUpgradeSequence(
// VerifyChannelUpgradeError verifies a proof of the provided upgrade error receipt.
func (k Keeper) VerifyChannelUpgradeError(
ctx sdk.Context,
connection exported.ConnectionI,
height exported.Height,
proof []byte,
portID,
channelID string,
upgradeSequence uint64,
errorReceipt channeltypes.ErrorReceipt,
) error {
clientID := connection.GetClientID()
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
Expand All @@ -385,32 +384,37 @@ func (k Keeper) VerifyChannelUpgradeSequence(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeSequencePath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorPath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

bz, err := k.cdc.Marshal(&errorReceipt)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
ctx, clientStore, k.cdc, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, sdk.Uint64ToBigEndian(upgradeSequence),
proof, merklePath, bz,
); err != nil {
return errorsmod.Wrapf(err, "failed upgrade sequence verification for client (%s)", clientID)
return errorsmod.Wrapf(err, "failed upgrade error receipt verification for client (%s)", clientID)
}

return nil
}

// VerifyChannelUpgradeTimeout verifies the proof that a particular timeout has been stored in the upgrade timeout path.
func (k Keeper) VerifyChannelUpgradeTimeout(
// VerifyChannelUpgradeErrorAbsence verifies a proof of the absence of a
// channel upgrade error.
func (k Keeper) VerifyChannelUpgradeErrorAbsence(
ctx sdk.Context,
connection exported.ConnectionI,
height exported.Height,
proof []byte,
portID,
channelID string,
upgradeTimeout channeltypes.UpgradeTimeout,
) error {
clientID := connection.GetClientID()
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
Expand All @@ -422,37 +426,32 @@ func (k Keeper) VerifyChannelUpgradeTimeout(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeTimeoutPath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorPath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

bz, err := k.cdc.Marshal(&upgradeTimeout)
if err != nil {
return err
}

if err := clientState.VerifyMembership(
if err := clientState.VerifyNonMembership(
ctx, clientStore, k.cdc, height,
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
0, 0,
proof, merklePath,
); err != nil {
return errorsmod.Wrapf(err, "failed upgrade timeout verification for client (%s) on channel (%s)", clientID, channelID)
return errorsmod.Wrapf(err, "failed upgrade error receipt absence verification for client (%s)", clientID)
}

return nil
}

// VerifyChannelUpgradeError verifies a proof of the provided upgrade error receipt.
func (k Keeper) VerifyChannelUpgradeError(
// VerifyChannelUpgrade verifies the proof that a particular proposed upgrade has been stored in the upgrade path.
func (k Keeper) VerifyChannelUpgrade(
ctx sdk.Context,
connection exported.ConnectionI,
height exported.Height,
proof []byte,
portID,
channelID string,
errorReceipt channeltypes.ErrorReceipt,
upgrade channeltypes.Upgrade,
) error {
clientID := connection.GetClientID()
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
Expand All @@ -464,13 +463,13 @@ func (k Keeper) VerifyChannelUpgradeError(
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorPath(portID, channelID))
merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradePath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

bz, err := k.cdc.Marshal(&errorReceipt)
bz, err := k.cdc.Marshal(&upgrade)
if err != nil {
return err
}
Expand All @@ -480,44 +479,7 @@ func (k Keeper) VerifyChannelUpgradeError(
0, 0, // skip delay period checks for non-packet processing verification
proof, merklePath, bz,
); err != nil {
return errorsmod.Wrapf(err, "failed upgrade error receipt verification for client (%s)", clientID)
}

return nil
}

// VerifyChannelUpgradeErrorAbsence verifies a proof of the absence of a
// channel upgrade error.
func (k Keeper) VerifyChannelUpgradeErrorAbsence(
ctx sdk.Context,
connection exported.ConnectionI,
height exported.Height,
proof []byte,
portID,
channelID string,
) error {
clientID := connection.GetClientID()
clientState, clientStore, err := k.getClientStateAndVerificationStore(ctx, clientID)
if err != nil {
return err
}

if status := k.clientKeeper.GetClientStatus(ctx, clientState, clientID); status != exported.Active {
return errorsmod.Wrapf(clienttypes.ErrClientNotActive, "client (%s) status is %s", clientID, status)
}

merklePath := commitmenttypes.NewMerklePath(host.ChannelUpgradeErrorPath(portID, channelID))
merklePath, err = commitmenttypes.ApplyPrefix(connection.GetCounterparty().GetPrefix(), merklePath)
if err != nil {
return err
}

if err := clientState.VerifyNonMembership(
ctx, clientStore, k.cdc, height,
0, 0,
proof, merklePath,
); err != nil {
return errorsmod.Wrapf(err, "failed upgrade error receipt absence verification for client (%s)", clientID)
return errorsmod.Wrapf(err, "failed upgrade verification for client (%s) on channel (%s)", clientID, channelID)
}

return nil
Expand Down
Loading

0 comments on commit e62634d

Please sign in to comment.