Skip to content

Commit

Permalink
imp: delegate store operation of clientState and consensusState i…
Browse files Browse the repository at this point in the history
…n `initialize` to contract (#4033)

* remove calls to set client state and consensus state from initialize

* linting

* Update contracts

* use errorsmod

---------

Co-authored-by: Vladislav Markushin <negigic@gmail.com>
  • Loading branch information
crodriguezvega and vmarkushin authored Aug 1, 2023
1 parent ed41863 commit de89920
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
Binary file modified modules/light-clients/08-wasm/test_data/ics07_tendermint_cw.wasm.gz
100644 → 100755
Binary file not shown.
Binary file modified modules/light-clients/08-wasm/test_data/ics10_grandpa_cw.wasm.gz
100644 → 100755
Binary file not shown.
21 changes: 17 additions & 4 deletions modules/light-clients/08-wasm/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,35 @@ func (cs ClientState) GetTimestampAtHeight(
return consState.GetTimestamp(), nil
}

type instantiateMessage struct {
ClientState *ClientState `json:"client_state"`
ConsensusState *ConsensusState `json:"consensus_state"`
}

// Initialize checks that the initial consensus state is an 08-wasm consensus state and
// sets the client state, consensus state in the provided client store.
// It also initializes the wasm contract for the client.
func (cs ClientState) Initialize(ctx sdk.Context, marshaler codec.BinaryCodec, clientStore sdk.KVStore, state exported.ConsensusState) error {
func (cs ClientState) Initialize(ctx sdk.Context, _ codec.BinaryCodec, clientStore sdk.KVStore, state exported.ConsensusState) error {
consensusState, ok := state.(*ConsensusState)
if !ok {
return errorsmod.Wrapf(clienttypes.ErrInvalidConsensus, "invalid initial consensus state. expected type: %T, got: %T",
&ConsensusState{}, state)
}
setClientState(clientStore, marshaler, &cs)
setConsensusState(clientStore, marshaler, consensusState, cs.GetLatestHeight())

payload := instantiateMessage{
ClientState: &cs,
ConsensusState: consensusState,
}

encodedData, err := json.Marshal(payload)
if err != nil {
return errorsmod.Wrapf(err, "failed to marshal payload for wasm contract instantiation")
}

// The global store key can be used here to implement #4085
// wasmStore := ctx.KVStore(WasmStoreKey)

_, err := initContract(ctx, clientStore, cs.CodeHash)
_, err := initContract(ctx, clientStore, cs.CodeHash, encodedData)
if err != nil {
return errorsmod.Wrapf(err, "failed to initialize contract")
}
Expand Down
14 changes: 0 additions & 14 deletions modules/light-clients/08-wasm/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,6 @@ func (ws updateProposalWrappedStore) getStore(key []byte) sdk.KVStore {
return ws.substituteStore
}

// setClientState stores the client state.
func setClientState(clientStore sdk.KVStore, cdc codec.BinaryCodec, clientState *ClientState) {
key := host.ClientStateKey()
val := clienttypes.MustMarshalClientState(cdc, clientState)
clientStore.Set(key, val)
}

// setConsensusState stores the consensus state at the given height.
func setConsensusState(clientStore sdk.KVStore, cdc codec.BinaryCodec, consensusState *ConsensusState, height exported.Height) {
key := host.ConsensusStateKey(height)
val := clienttypes.MustMarshalConsensusState(cdc, consensusState)
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) {
Expand Down
7 changes: 3 additions & 4 deletions modules/light-clients/08-wasm/types/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r contractResult) Error() string {
}

// initContract calls vm.Init with appropriate arguments.
func initContract(ctx sdk.Context, clientStore sdk.KVStore, codeHash []byte) (*wasmvmtypes.Response, error) {
func initContract(ctx sdk.Context, clientStore sdk.KVStore, codeHash []byte, msg []byte) (*wasmvmtypes.Response, error) {
sdkGasMeter := ctx.GasMeter()
multipliedGasMeter := NewMultipliedGasMeter(sdkGasMeter, VMGasRegister)
gasLimit := VMGasRegister.runtimeGasForContract(ctx)
Expand All @@ -59,9 +59,8 @@ func initContract(ctx sdk.Context, clientStore sdk.KVStore, codeHash []byte) (*w
Funds: nil,
}

initMsg := []byte("{}")
ctx.GasMeter().ConsumeGas(VMGasRegister.NewContractInstanceCosts(len(initMsg)), "Loading CosmWasm module: instantiate")
response, gasUsed, err := WasmVM.Instantiate(codeHash, env, msgInfo, initMsg, newStoreAdapter(clientStore), cosmwasm.GoAPI{}, nil, multipliedGasMeter, gasLimit, costJSONDeserialization)
ctx.GasMeter().ConsumeGas(VMGasRegister.NewContractInstanceCosts(len(msg)), "Loading CosmWasm module: instantiate")
response, gasUsed, err := WasmVM.Instantiate(codeHash, env, msgInfo, msg, newStoreAdapter(clientStore), cosmwasm.GoAPI{}, nil, multipliedGasMeter, gasLimit, costJSONDeserialization)
VMGasRegister.consumeRuntimeGas(ctx, gasUsed)
return response, err
}
Expand Down

0 comments on commit de89920

Please sign in to comment.