Skip to content

Commit

Permalink
fix(invariant): no longer have cons state must exist for latest heigh…
Browse files Browse the repository at this point in the history
…t invariant (#1633)
  • Loading branch information
danwt authored Dec 9, 2024
1 parent e7da850 commit 0dbcf79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
6 changes: 2 additions & 4 deletions x/lightclient/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func checkClient(ctx sdk.Context, k Keeper, client types.CanonicalClient) error
if !ok {
return gerrc.ErrNotFound.Wrapf("rollapp for rollapp ID: %s", client.RollappId)
}
_, ok = k.ibcClientKeeper.GetClientConsensusState(ctx, client.IbcClientId, cs.GetLatestHeight())
if !ok {
return gerrc.ErrNotFound.Wrapf("latest consensus state for client ID: %s", client.IbcClientId)
}
// NOTE: it is not actually an invariant that the consensus state needs to exist for the latest height
// Because they can be pruned
return nil
}

Expand Down
24 changes: 18 additions & 6 deletions x/lightclient/keeper/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func (k Keeper) RollbackCanonicalClient(ctx sdk.Context, rollappId string, lastV
}
cs := k.ibcClientKeeper.ClientStore(ctx, client)

var lastConsStateHeight exported.Height
// iterate over all consensus states and metadata in the client store
IterateConsensusStateDescending(cs, func(h exported.Height) bool {
// iterate until we pass the new revision height
if h.GetRevisionHeight() <= lastValidHeight {
lastConsStateHeight = h
return true
}

Expand All @@ -45,9 +47,10 @@ func (k Keeper) RollbackCanonicalClient(ctx sdk.Context, rollappId string, lastV
return errorsmod.Wrap(err, "prune signers above")
}

// freeze the client
// it will be released after the hardfork is resolved (on the next state update)
k.freezeClient(cs, lastValidHeight)
// will be unfrozen on next state update
if err := k.freezeClient(cs, lastConsStateHeight); err != nil {
return errorsmod.Wrap(err, "freeze client")
}

return nil
}
Expand Down Expand Up @@ -94,14 +97,23 @@ func (k Keeper) ResolveHardFork(ctx sdk.Context, rollappID string) error {
}

// freezeClient freezes the client by setting the frozen height to the current height
func (k Keeper) freezeClient(clientStore sdk.KVStore, height uint64) {
func (k Keeper) freezeClient(clientStore sdk.KVStore, heightI exported.Height) error {
tmClientState := getClientStateTM(clientStore, k.cdc)

// freeze the client
// It's not fundamentally important to have a consensus state for the latest height (since
// it can happen in normal operation due to IBC pruning) but we do best effort, because
// ibctesting doesn't like not having it.
height, ok := heightI.(clienttypes.Height)
if !ok {
return gerrc.ErrInternal.Wrap("height nil or not tm client height")
}
tmClientState.LatestHeight = height

tmClientState.FrozenHeight = ibctm.FrozenHeight
tmClientState.LatestHeight = clienttypes.NewHeight(1, height)

setClientState(clientStore, k.cdc, tmClientState)

return nil
}

// freezeClient freezes the client by setting the frozen height to the current height
Expand Down

0 comments on commit 0dbcf79

Please sign in to comment.