Skip to content

Commit a7ce91d

Browse files
authored
Custom self client validation for 08 wasm tendermint clients (#5858)
Co-Authored-By: @charleenfei
1 parent 7200c7f commit a7ce91d

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ $(BUILD_TARGETS): go.sum $(BUILDDIR)/
121121
$(BUILDDIR)/:
122122
mkdir -p $(BUILDDIR)/
123123

124+
#? build-docker-wasm: Build wasm simapp with specified tag.
125+
build-docker-wasm:
126+
./scripts/build-wasm-simapp-docker.sh $(tag)
127+
124128
.PHONY: build build-linux
125129

126130
#? distclean: Run `make clean`

modules/light-clients/08-wasm/testing/simapp/app.go

+5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ import (
128128
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
129129
ibc "github.com/cosmos/ibc-go/v8/modules/core"
130130
ibcclient "github.com/cosmos/ibc-go/v8/modules/core/02-client"
131+
clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
131132
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
132133
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
133134
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
@@ -416,6 +417,10 @@ func NewSimApp(
416417
app.IBCKeeper = ibckeeper.NewKeeper(
417418
appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
418419
)
420+
421+
tmClientValidator := clientkeeper.NewTendermintClientValidator(app.StakingKeeper)
422+
app.IBCKeeper.ClientKeeper.SetSelfClientValidator(wasmtypes.NewWasmTMClientValidator(appCodec, tmClientValidator))
423+
419424
// Register the proposal types
420425
// Deprecated: Avoid adding new handlers, instead use the new proposal flow
421426
// by granting the governance module the right to execute the message.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package types
2+
3+
import (
4+
errorsmod "cosmossdk.io/errors"
5+
6+
"github.com/cosmos/cosmos-sdk/codec"
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
9+
clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
10+
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
11+
"github.com/cosmos/ibc-go/v8/modules/core/exported"
12+
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
13+
)
14+
15+
type WasmTMClientValidator struct {
16+
cdc codec.BinaryCodec
17+
tm *clientkeeper.TendermintClientValidator
18+
}
19+
20+
var _ clienttypes.SelfClientValidator = (*WasmTMClientValidator)(nil)
21+
22+
// NewWasmTMClientValidator creates and returns a new SelfClientValidator for wasm tendermint consensus.
23+
func NewWasmTMClientValidator(cdc codec.BinaryCodec, tm *clientkeeper.TendermintClientValidator) *WasmTMClientValidator {
24+
return &WasmTMClientValidator{
25+
cdc: cdc,
26+
tm: tm,
27+
}
28+
}
29+
30+
func (w WasmTMClientValidator) GetSelfConsensusState(ctx sdk.Context, height exported.Height) (exported.ConsensusState, error) {
31+
consensusState, err := w.tm.GetSelfConsensusState(ctx, height)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
// encode consensusState to wasm.ConsensusState.Data
37+
bz, err := w.cdc.Marshal(consensusState)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
wasmConsensusState := &ConsensusState{
43+
Data: bz,
44+
}
45+
46+
return wasmConsensusState, nil
47+
}
48+
49+
func (w WasmTMClientValidator) ValidateSelfClient(ctx sdk.Context, clientState exported.ClientState) error {
50+
wasmClientState, ok := clientState.(*ClientState)
51+
if !ok {
52+
return errorsmod.Wrapf(clienttypes.ErrInvalidClient, "client must be a wasm client, expected: %T, got: %T", ClientState{}, wasmClientState)
53+
}
54+
55+
// unmarshal the wasmClientState bytes into tendermint client and call self validation
56+
var tmClientState *ibctm.ClientState
57+
if err := w.cdc.Unmarshal(wasmClientState.Data, tmClientState); err != nil {
58+
return err
59+
}
60+
61+
return w.tm.ValidateSelfClient(ctx, tmClientState)
62+
}

scripts/build-wasm-simapp-docker.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -eou pipefail
4+
5+
# build_wasm_image extracts the correct libwasm version and checksum
6+
# based on the go.mod and builds a docker image with the provided tag.
7+
function build_wasm_image(){
8+
local version="$(scripts/get-libwasm-version.py --get-version)"
9+
local checksum="$(scripts/get-libwasm-version.py --get-checksum)"
10+
docker build . -t "${1}" -f modules/light-clients/08-wasm/Dockerfile --build-arg LIBWASM_VERSION=${version} --build-arg LIBWASM_CHECKSUM=${checksum}
11+
}
12+
13+
# default to latest if no tag is specified.
14+
TAG="${1:-ibc-go-wasm-simd:latest}"
15+
16+
build_wasm_image "${TAG}"

0 commit comments

Comments
 (0)