From d6ac7fc8f4d2bb9613d7f0b05be55b9431598c0a Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Tue, 25 Jul 2023 13:32:25 +0300 Subject: [PATCH 1/2] Use hasConsensusState instead of GetConsensusState --- .../08-wasm/types/client_state.go | 16 ++++++------- modules/light-clients/08-wasm/types/store.go | 24 +++---------------- .../light-clients/08-wasm/types/upgrade.go | 8 +++---- 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/modules/light-clients/08-wasm/types/client_state.go b/modules/light-clients/08-wasm/types/client_state.go index ced9598d5b6..ece6be68fb0 100644 --- a/modules/light-clients/08-wasm/types/client_state.go +++ b/modules/light-clients/08-wasm/types/client_state.go @@ -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, cdc, height) + if !found { + return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "please ensure the proof was constructed against a height that exists on the client") } payload := verifyMembershipPayload{ @@ -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 } @@ -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, cdc, height) + if !found { + return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "please ensure the proof was constructed against a height that exists on the client") } payload := verifyNonMembershipPayload{ @@ -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 } diff --git a/modules/light-clients/08-wasm/types/store.go b/modules/light-clients/08-wasm/types/store.go index 7ebbf3fc320..ea3e2330a9b 100644 --- a/modules/light-clients/08-wasm/types/store.go +++ b/modules/light-clients/08-wasm/types/store.go @@ -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" @@ -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, cdc codec.BinaryCodec, height exported.Height) bool { + return clientStore.Has(host.ConsensusStateKey(height)) } var _ wasmvmtypes.KVStore = &storeAdapter{} diff --git a/modules/light-clients/08-wasm/types/upgrade.go b/modules/light-clients/08-wasm/types/upgrade.go index 8e80aff7de7..5e2922c65ab 100644 --- a/modules/light-clients/08-wasm/types/upgrade.go +++ b/modules/light-clients/08-wasm/types/upgrade.go @@ -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, cdc, lastHeight) + if !found { + return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "could not retrieve consensus state for height %s", lastHeight) } payload := verifyUpgradeAndUpdateStatePayload{ @@ -71,6 +71,6 @@ func (cs ClientState) VerifyUpgradeAndUpdateState( }, } - _, err = call[contractResult](ctx, clientStore, &cs, payload) + _, err := call[contractResult](ctx, clientStore, &cs, payload) return err } From db3eefbe86fa469097fb86581b820a37193c7a77 Mon Sep 17 00:00:00 2001 From: DimitrisJim Date: Tue, 25 Jul 2023 19:56:36 +0300 Subject: [PATCH 2/2] Remove unused argument. --- modules/light-clients/08-wasm/types/client_state.go | 4 ++-- modules/light-clients/08-wasm/types/store.go | 2 +- modules/light-clients/08-wasm/types/upgrade.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/light-clients/08-wasm/types/client_state.go b/modules/light-clients/08-wasm/types/client_state.go index ece6be68fb0..ade27c30e14 100644 --- a/modules/light-clients/08-wasm/types/client_state.go +++ b/modules/light-clients/08-wasm/types/client_state.go @@ -176,7 +176,7 @@ func (cs ClientState) VerifyMembership( return errorsmod.Wrapf(ibcerrors.ErrInvalidType, "expected %T, got %T", commitmenttypes.MerklePath{}, path) } - found := hasConsensusState(clientStore, cdc, height) + 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") } @@ -233,7 +233,7 @@ func (cs ClientState) VerifyNonMembership( return errorsmod.Wrapf(ibcerrors.ErrInvalidType, "expected %T, got %T", commitmenttypes.MerklePath{}, path) } - found := hasConsensusState(clientStore, cdc, height) + 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") } diff --git a/modules/light-clients/08-wasm/types/store.go b/modules/light-clients/08-wasm/types/store.go index ea3e2330a9b..f8013df6ebf 100644 --- a/modules/light-clients/08-wasm/types/store.go +++ b/modules/light-clients/08-wasm/types/store.go @@ -109,7 +109,7 @@ func setConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensus } // hasConsensusState returns true if a consensus state at the given height exists in the store. -func hasConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, height exported.Height) bool { +func hasConsensusState(clientStore sdk.KVStore, height exported.Height) bool { return clientStore.Has(host.ConsensusStateKey(height)) } diff --git a/modules/light-clients/08-wasm/types/upgrade.go b/modules/light-clients/08-wasm/types/upgrade.go index 5e2922c65ab..5ef13d84d5f 100644 --- a/modules/light-clients/08-wasm/types/upgrade.go +++ b/modules/light-clients/08-wasm/types/upgrade.go @@ -57,7 +57,7 @@ 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 - found := hasConsensusState(clientStore, cdc, lastHeight) + found := hasConsensusState(clientStore, lastHeight) if !found { return errorsmod.Wrapf(clienttypes.ErrConsensusStateNotFound, "could not retrieve consensus state for height %s", lastHeight) }