Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hasConsensusState instead of GetConsensusState #4171

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions modules/light-clients/08-wasm/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func (cs ClientState) VerifyMembership(
return errorsmod.Wrapf(ibcerrors.ErrInvalidType, "expected %T, got %T", commitmenttypes.MerklePath{}, path)
}

_, err := GetConsensusState(clientStore, cdc, height)
if err != nil {
return errorsmod.Wrap(err, "please ensure the proof was constructed against a height that exists on the client")
found := hasConsensusState(clientStore, height)
if !found {
return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "please ensure the proof was constructed against a height that exists on the client")
}

payload := verifyMembershipPayload{
Expand All @@ -191,7 +191,7 @@ func (cs ClientState) VerifyMembership(
Value: value,
},
}
_, err = call[contractResult](ctx, clientStore, &cs, payload)
_, err := call[contractResult](ctx, clientStore, &cs, payload)
return err
}

Expand Down Expand Up @@ -233,9 +233,9 @@ func (cs ClientState) VerifyNonMembership(
return errorsmod.Wrapf(ibcerrors.ErrInvalidType, "expected %T, got %T", commitmenttypes.MerklePath{}, path)
}

_, err := GetConsensusState(clientStore, cdc, height)
if err != nil {
return errorsmod.Wrap(err, "please ensure the proof was constructed against a height that exists on the client")
found := hasConsensusState(clientStore, height)
if !found {
return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "please ensure the proof was constructed against a height that exists on the client")
}

payload := verifyNonMembershipPayload{
Expand All @@ -247,7 +247,7 @@ func (cs ClientState) VerifyNonMembership(
Path: path,
},
}
_, err = call[contractResult](ctx, clientStore, &cs, payload)
_, err := call[contractResult](ctx, clientStore, &cs, payload)
return err
}

Expand Down
24 changes: 3 additions & 21 deletions modules/light-clients/08-wasm/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

wasmvmtypes "github.com/CosmWasm/wasmvm/types"

errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/listenkv"
Expand Down Expand Up @@ -110,25 +108,9 @@ func setConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensus
clientStore.Set(key, val)
}

// getConsensusState retrieves the consensus state from the client prefixed
// store. An error is returned if the consensus state does not exist or it cannot be unmarshalled.
func GetConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, height exported.Height) (*ConsensusState, error) {
bz := clientStore.Get(host.ConsensusStateKey(height))
if len(bz) == 0 {
return nil, errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "consensus state does not exist for height %s", height)
}

consensusStateI, err := clienttypes.UnmarshalConsensusState(cdc, bz)
if err != nil {
return nil, errorsmod.Wrapf(clienttypes.ErrInvalidConsensus, "unmarshal error: %v", err)
}

consensusState, ok := consensusStateI.(*ConsensusState)
if !ok {
return nil, errorsmod.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type. expected %T, got %T", &ConsensusState{}, consensusState)
}

return consensusState, nil
// hasConsensusState returns true if a consensus state at the given height exists in the store.
func hasConsensusState(clientStore sdk.KVStore, height exported.Height) bool {
return clientStore.Has(host.ConsensusStateKey(height))
}

var _ wasmvmtypes.KVStore = &storeAdapter{}
Expand Down
8 changes: 4 additions & 4 deletions modules/light-clients/08-wasm/types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func (cs ClientState) VerifyUpgradeAndUpdateState(
// Must prove against latest consensus state to ensure we are verifying against latest upgrade plan
// This verifies that upgrade is intended for the provided revision, since committed client must exist
// at this consensus state
_, err := GetConsensusState(clientStore, cdc, lastHeight)
if err != nil {
return errorsmod.Wrapf(err, "could not retrieve consensus state for height %s", lastHeight)
found := hasConsensusState(clientStore, lastHeight)
if !found {
return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "could not retrieve consensus state for height %s", lastHeight)
}

payload := verifyUpgradeAndUpdateStatePayload{
Expand All @@ -71,6 +71,6 @@ func (cs ClientState) VerifyUpgradeAndUpdateState(
},
}

_, err = call[contractResult](ctx, clientStore, &cs, payload)
_, err := call[contractResult](ctx, clientStore, &cs, payload)
return err
}