Skip to content

Commit

Permalink
add ClientMessage from #4049
Browse files Browse the repository at this point in the history
  • Loading branch information
crodriguezvega committed Jul 21, 2023
1 parent 741989d commit a0febb6
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 41 deletions.
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
cosmossdk.io/errors v1.0.0-beta.7
github.com/CosmWasm/wasmvm v1.2.4
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.2.0
Expand Down Expand Up @@ -49,7 +50,6 @@ require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/coinbase/rosetta-sdk-go/types v1.0.0 // indirect
github.com/cometbft/cometbft-db v0.8.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
Expand Down
23 changes: 23 additions & 0 deletions modules/light-clients/08-wasm/types/client_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package types

import (
errorsmod "cosmossdk.io/errors"

"github.com/cosmos/ibc-go/v7/modules/core/exported"
)

var _ exported.ClientMessage = &ClientMessage{}

// ClientType defines that the client message is a Wasm client consensus algorithm
func (h ClientMessage) ClientType() string {
return exported.Wasm
}

// ValidateBasic defines a basic validation for the wasm client message.
func (h ClientMessage) ValidateBasic() error {
if len(h.Data) == 0 {
return errorsmod.Wrap(ErrInvalidData, "data cannot be empty")
}

return nil
}
15 changes: 8 additions & 7 deletions modules/light-clients/08-wasm/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package types
import errorsmod "cosmossdk.io/errors"

var (
ErrInvalid = errorsmod.Register(ModuleName, 1, "invalid")
ErrInvalidData = errorsmod.Register(ModuleName, 2, "invalid data")
ErrInvalidCodeID = errorsmod.Register(ModuleName, 3, "invalid code ID")
ErrInvalid = errorsmod.Register(ModuleName, 1, "invalid")
ErrInvalidData = errorsmod.Register(ModuleName, 2, "invalid data")
ErrInvalidCodeID = errorsmod.Register(ModuleName, 3, "invalid code ID")
ErrInvalidClientMessage = errorsmod.Register(ModuleName, 4, "invalid client message")
// Wasm specific
ErrWasmEmptyCode = errorsmod.Register(ModuleName, 4, "empty wasm code")
ErrWasmCodeTooLarge = errorsmod.Register(ModuleName, 5, "wasm code too large")
ErrWasmCodeExists = errorsmod.Register(ModuleName, 6, "wasm code already exists")
ErrWasmCodeIDNotFound = errorsmod.Register(ModuleName, 7, "wasm code id not found")
ErrWasmEmptyCode = errorsmod.Register(ModuleName, 5, "empty wasm code")
ErrWasmCodeTooLarge = errorsmod.Register(ModuleName, 6, "wasm code too large")
ErrWasmCodeExists = errorsmod.Register(ModuleName, 7, "wasm code already exists")
ErrWasmCodeIDNotFound = errorsmod.Register(ModuleName, 8, "wasm code id not found")
)
7 changes: 6 additions & 1 deletion modules/light-clients/08-wasm/types/misbehaviour_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (
// CheckForMisbehaviour detects misbehaviour in a submitted Header message and verifies
// the correctness of a submitted Misbehaviour ClientMessage
func (cs ClientState) CheckForMisbehaviour(ctx sdk.Context, _ codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) bool {
clientMessage, ok := clientMsg.(*ClientMessage)
if !ok {
return false
}

payload := QueryMsg{
CheckForMisbehaviour: &checkForMisbehaviourMsg{ClientMessage: clientMsg},
CheckForMisbehaviour: &checkForMisbehaviourMsg{ClientMessage: clientMessage},
}

result, err := call[contractResult](ctx, clientStore, &cs, payload)
Expand Down
8 changes: 4 additions & 4 deletions modules/light-clients/08-wasm/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type (
Height exported.Height `json:"height"`
}
verifyClientMessageMsg struct {
ClientMessage exported.ClientMessage `json:"client_message"`
ClientMessage *ClientMessage `json:"client_message"`
}
verifyMembershipMsg struct {
Height exported.Height `json:"height"`
Expand All @@ -71,7 +71,7 @@ type (
Path exported.Path `json:"path"`
}
checkForMisbehaviourMsg struct {
ClientMessage exported.ClientMessage `json:"client_message"`
ClientMessage *ClientMessage `json:"client_message"`
}
)

Expand All @@ -89,10 +89,10 @@ type (
ConsensusState *ConsensusState `json:"consensus_state"`
}
updateStateMsg struct {
ClientMessage exported.ClientMessage `json:"client_message"`
ClientMessage *ClientMessage `json:"client_message"`
}
updateStateOnMisbehaviourMsg struct {
ClientMessage exported.ClientMessage `json:"client_message"`
ClientMessage *ClientMessage `json:"client_message"`
}
verifyUpgradeAndUpdateStateMsg struct {
UpgradeClientState exported.ClientState `json:"upgrade_client_state"`
Expand Down
22 changes: 17 additions & 5 deletions modules/light-clients/08-wasm/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"fmt"

errorsmod "cosmossdk.io/errors"

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

Expand All @@ -16,22 +18,27 @@ var _ exported.ClientState = (*ClientState)(nil)
// will assume that the content of the ClientMessage has been verified and can be trusted. An error should be returned
// if the ClientMessage fails to verify.
func (cs ClientState) VerifyClientMessage(ctx sdk.Context, _ codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) error {
clientMessage, ok := clientMsg.(*ClientMessage)
if !ok {
return errorsmod.Wrapf(ErrInvalidClientMessage, "expected type %T, got %T", &ClientMessage{}, clientMsg)
}

payload := QueryMsg{
VerifyClientMessage: &verifyClientMessageMsg{ClientMessage: clientMsg},
VerifyClientMessage: &verifyClientMessageMsg{ClientMessage: clientMessage},
}
_, err := call[contractResult](ctx, clientStore, &cs, payload)
return err
}

// Client state and new consensus states are updated in the store by the contract
func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) []exported.Height {
_, ok := clientMsg.(*Header)
clientMessage, ok := clientMsg.(*ClientMessage)
if !ok {
panic(fmt.Errorf("expected type %T, got %T", &Header{}, clientMsg))
panic(fmt.Errorf("expected type %T, got %T", &ClientMessage{}, clientMsg))
}

payload := SudoMsg{
UpdateState: &updateStateMsg{ClientMessage: clientMsg},
UpdateState: &updateStateMsg{ClientMessage: clientMessage},
}

_, err := call[contractResult](ctx, clientStore, &cs, payload)
Expand All @@ -45,8 +52,13 @@ func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, client
// UpdateStateOnMisbehaviour should perform appropriate state changes on a client state given that misbehaviour has been detected and verified
// Client state is updated in the store by contract.
func (cs ClientState) UpdateStateOnMisbehaviour(ctx sdk.Context, _ codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) {
clientMessage, ok := clientMsg.(*ClientMessage)
if !ok {
panic(fmt.Errorf("expected type %T, got %T", &ClientMessage{}, clientMsg))
}

payload := SudoMsg{
UpdateStateOnMisbehaviour: &updateStateOnMisbehaviourMsg{ClientMessage: clientMsg},
UpdateStateOnMisbehaviour: &updateStateOnMisbehaviourMsg{ClientMessage: clientMessage},
}

_, err := call[contractResult](ctx, clientStore, &cs, payload)
Expand Down
Loading

0 comments on commit a0febb6

Please sign in to comment.