Skip to content

Commit

Permalink
adding initial localhost client testing (#3085)
Browse files Browse the repository at this point in the history
* adding initial localhost client testing scaffolding

* add testing placeholders, adapt initialise code, wire AppModuleBasic

* adding happy path test cases, fixing store prefixing in verify membership/non-membership

* formatting

* adding additional test cases

* adding remaining test cases

* adding inline comments
  • Loading branch information
damiannolan authored Feb 2, 2023
1 parent d1b0825 commit 72fcd65
Show file tree
Hide file tree
Showing 3 changed files with 491 additions and 9 deletions.
36 changes: 27 additions & 9 deletions modules/light-clients/09-localhost/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)
Expand All @@ -23,11 +24,6 @@ func NewClientState(chainID string, height clienttypes.Height) exported.ClientSt
}
}

// GetChainID returns the client state chain ID.
func (cs ClientState) GetChainID() string {
return cs.ChainId
}

// ClientType returns the 09-localhost client type.
func (cs ClientState) ClientType() string {
return exported.Localhost
Expand Down Expand Up @@ -95,7 +91,17 @@ func (cs ClientState) VerifyMembership(
path exported.Path,
value []byte,
) error {
bz := store.Get([]byte(path.String()))
merklePath, ok := path.(commitmenttypes.MerklePath)
if !ok {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "todo: update error -- not found for path %s", path)
}

if len(merklePath.GetKeyPath()) != 2 {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "todo: update error -- not found for path %s", path)
}

// The commitment prefix (eg: "ibc") is omitted when operating on the core IBC store
bz := store.Get([]byte(merklePath.KeyPath[1]))
if bz == nil {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "todo: update error -- not found for path %s", path)
}
Expand All @@ -122,7 +128,17 @@ func (cs ClientState) VerifyNonMembership(
proof []byte,
path exported.Path,
) error {
bz := store.Get([]byte(path.String()))
merklePath, ok := path.(commitmenttypes.MerklePath)
if !ok {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "todo: update error -- not found for path %s", path)
}

if len(merklePath.GetKeyPath()) != 2 {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "todo: update error -- not found for path %s", path)
}

// The commitment prefix (eg: "ibc") is omitted when operating on the core IBC store
bz := store.Get([]byte(merklePath.KeyPath[1]))
if bz != nil {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "todo: update error -- found for path %s", path)
}
Expand Down Expand Up @@ -153,8 +169,10 @@ func (cs ClientState) UpdateStateOnMisbehaviour(_ sdk.Context, _ codec.BinaryCod
func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) []exported.Height {
height := clienttypes.GetSelfHeight(ctx)

clientState := NewClientState(ctx.ChainID(), height)
clientStore.Set([]byte(host.KeyClientState), clienttypes.MustMarshalClientState(cdc, clientState))
cs.ChainId = ctx.ChainID()
cs.LatestHeight = height

clientStore.Set([]byte(host.KeyClientState), clienttypes.MustMarshalClientState(cdc, &cs))

return []exported.Height{height}
}
Expand Down
Loading

0 comments on commit 72fcd65

Please sign in to comment.