Skip to content
New issue

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

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

Already on GitHub? # to your account

imp: custom self client validation for 08 wasm tendermint clients #5863

Closed
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ $(BUILD_TARGETS): go.sum $(BUILDDIR)/
$(BUILDDIR)/:
mkdir -p $(BUILDDIR)/

#? build-docker-wasm: Build wasm simapp with specified tag.
build-docker-wasm:
./scripts/build-wasm-simapp-docker.sh $(tag)

.PHONY: build build-linux

#? distclean: Run `make clean`
Expand Down
5 changes: 5 additions & 0 deletions modules/light-clients/08-wasm/testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import (
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcclient "github.com/cosmos/ibc-go/v8/modules/core/02-client"
clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
Expand Down Expand Up @@ -416,6 +417,10 @@ func NewSimApp(
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

tmClientValidator := clientkeeper.NewTendermintClientValidator(app.StakingKeeper)
app.IBCKeeper.ClientKeeper.SetSelfClientValidator(wasmtypes.NewWasmTMClientValidator(appCodec, tmClientValidator))

// Register the proposal types
// Deprecated: Avoid adding new handlers, instead use the new proposal flow
// by granting the governance module the right to execute the message.
Expand Down
62 changes: 62 additions & 0 deletions modules/light-clients/08-wasm/types/client_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package types

import (
errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
)

type WasmTMClientValidator struct {
cdc codec.BinaryCodec
tm *clientkeeper.TendermintClientValidator
}

var _ clienttypes.SelfClientValidator = (*WasmTMClientValidator)(nil)

// NewWasmTMClientValidator creates and returns a new SelfClientValidator for wasm tendermint consensus.
func NewWasmTMClientValidator(cdc codec.BinaryCodec, tm *clientkeeper.TendermintClientValidator) *WasmTMClientValidator {
return &WasmTMClientValidator{
cdc: cdc,
tm: tm,
}
}

func (w WasmTMClientValidator) GetSelfConsensusState(ctx sdk.Context, height exported.Height) (exported.ConsensusState, error) {
consensusState, err := w.tm.GetSelfConsensusState(ctx, height)
if err != nil {
return nil, err
}

// encode consensusState to wasm.ConsensusState.Data
bz, err := w.cdc.Marshal(consensusState)
if err != nil {
return nil, err
}

wasmConsensusState := &ConsensusState{
Data: bz,
}

return wasmConsensusState, nil
}

func (w WasmTMClientValidator) ValidateSelfClient(ctx sdk.Context, clientState exported.ClientState) error {
wasmClientState, ok := clientState.(*ClientState)
if !ok {
return errorsmod.Wrapf(clienttypes.ErrInvalidClient, "client must be a wasm client, expected: %T, got: %T", ClientState{}, wasmClientState)
}

// unmarshal the wasmClientState bytes into tendermint client and call self validation
var tmClientState *ibctm.ClientState
if err := w.cdc.Unmarshal(wasmClientState.Data, tmClientState); err != nil {
return err
}

return w.tm.ValidateSelfClient(ctx, tmClientState)
}
16 changes: 16 additions & 0 deletions scripts/build-wasm-simapp-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -eou pipefail

# build_wasm_image extracts the correct libwasm version and checksum
# based on the go.mod and builds a docker image with the provided tag.
function build_wasm_image(){
local version="$(scripts/get-libwasm-version.py --get-version)"
local checksum="$(scripts/get-libwasm-version.py --get-checksum)"
docker build . -t "${1}" -f modules/light-clients/08-wasm/Dockerfile --build-arg LIBWASM_VERSION=${version} --build-arg LIBWASM_CHECKSUM=${checksum}
}

# default to latest if no tag is specified.
TAG="${1:-ibc-go-wasm-simd:latest}"

build_wasm_image "${TAG}"
Loading