|
| 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 | +} |
0 commit comments