-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adding ConsensusHost
interface for custom self client/consensus state validation (backport #6055)
#6547
Merged
crodriguezvega
merged 5 commits into
08-wasm/release/v0.2.x+ibc-go-v8.3.x-wasmvm-v2.0.x
from
mergify/bp/08-wasm/release/v0.2.x+ibc-go-v8.3.x-wasmvm-v2.0.x/pr-6055
Jun 16, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b4af56a
feat: adding `ConsensusHost` interface for custom self client/consens…
damiannolan 069eb09
Merge branch '08-wasm/release/v0.2.x+ibc-go-v8.3.x-wasmvm-v2.0.x' int…
damiannolan ceb7b16
restore changes from release/v8.3.x
damiannolan 4a78aec
Merge branch '08-wasm/release/v0.2.x+ibc-go-v8.3.x-wasmvm-v2.0.x' int…
damiannolan 343d058
chore: update changelog
damiannolan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" | ||
"github.com/cosmos/ibc-go/v8/modules/core/exported" | ||
) | ||
|
||
// WasmConsensusHost implements the 02-client types.ConsensusHost interface. | ||
type WasmConsensusHost struct { | ||
cdc codec.BinaryCodec | ||
delegate clienttypes.ConsensusHost | ||
} | ||
|
||
var _ clienttypes.ConsensusHost = (*WasmConsensusHost)(nil) | ||
|
||
// NewWasmConsensusHost creates and returns a new ConsensusHost for wasm wrapped consensus client state and consensus state self validation. | ||
func NewWasmConsensusHost(cdc codec.BinaryCodec, delegate clienttypes.ConsensusHost) (*WasmConsensusHost, error) { | ||
if cdc == nil { | ||
return nil, fmt.Errorf("wasm consensus host codec is nil") | ||
} | ||
|
||
if delegate == nil { | ||
return nil, fmt.Errorf("wasm delegate consensus host is nil") | ||
} | ||
|
||
return &WasmConsensusHost{ | ||
cdc: cdc, | ||
delegate: delegate, | ||
}, nil | ||
} | ||
|
||
// GetSelfConsensusState implements the 02-client types.ConsensusHost interface. | ||
func (w *WasmConsensusHost) GetSelfConsensusState(ctx sdk.Context, height exported.Height) (exported.ConsensusState, error) { | ||
consensusState, err := w.delegate.GetSelfConsensusState(ctx, height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// encode consensusState to wasm.ConsensusState.Data | ||
bz, err := w.cdc.MarshalInterface(consensusState) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
wasmConsensusState := &ConsensusState{ | ||
Data: bz, | ||
} | ||
|
||
return wasmConsensusState, nil | ||
} | ||
|
||
// ValidateSelfClient implements the 02-client types.ConsensusHost interface. | ||
func (w *WasmConsensusHost) 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) | ||
} | ||
|
||
if wasmClientState.Data == nil { | ||
return errorsmod.Wrapf(clienttypes.ErrInvalidClient, "wasm client state data is nil") | ||
} | ||
|
||
// unmarshal the wasmClientState bytes into the ClientState interface and call self validation | ||
var unwrappedClientState exported.ClientState | ||
if err := w.cdc.UnmarshalInterface(wasmClientState.Data, &unwrappedClientState); err != nil { | ||
return err | ||
} | ||
|
||
return w.delegate.ValidateSelfClient(ctx, unwrappedClientState) | ||
} |
151 changes: 151 additions & 0 deletions
151
modules/light-clients/08-wasm/types/consensus_host_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package types_test | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
wasmtesting "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing" | ||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" | ||
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" | ||
"github.com/cosmos/ibc-go/v8/testing/mock" | ||
) | ||
|
||
func (suite *TypesTestSuite) TestGetSelfConsensusState() { | ||
var ( | ||
consensusHost clienttypes.ConsensusHost | ||
consensusState exported.ConsensusState | ||
height clienttypes.Height | ||
) | ||
|
||
cases := []struct { | ||
name string | ||
malleate func() | ||
expError error | ||
}{ | ||
{ | ||
name: "success", | ||
malleate: func() {}, | ||
expError: nil, | ||
}, | ||
{ | ||
name: "failure: delegate error", | ||
malleate: func() { | ||
consensusHost.(*mock.ConsensusHost).GetSelfConsensusStateFn = func(ctx sdk.Context, height exported.Height) (exported.ConsensusState, error) { | ||
return nil, mock.MockApplicationCallbackError | ||
} | ||
}, | ||
expError: mock.MockApplicationCallbackError, | ||
}, | ||
} | ||
|
||
for i, tc := range cases { | ||
tc := tc | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
height = clienttypes.ZeroHeight() | ||
|
||
wrappedClientConsensusStateBz := clienttypes.MustMarshalConsensusState(suite.chainA.App.AppCodec(), wasmtesting.MockTendermintClientConsensusState) | ||
consensusState = types.NewConsensusState(wrappedClientConsensusStateBz) | ||
|
||
consensusHost = &mock.ConsensusHost{ | ||
GetSelfConsensusStateFn: func(ctx sdk.Context, height exported.Height) (exported.ConsensusState, error) { | ||
return consensusState, nil | ||
}, | ||
} | ||
|
||
tc.malleate() | ||
|
||
var err error | ||
consensusHost, err = types.NewWasmConsensusHost(suite.chainA.Codec, consensusHost) | ||
suite.Require().NoError(err) | ||
|
||
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetConsensusHost( | ||
consensusHost, | ||
) | ||
|
||
cs, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetSelfConsensusState(suite.chainA.GetContext(), height) | ||
|
||
expPass := tc.expError == nil | ||
if expPass { | ||
suite.Require().NoError(err, "Case %d should have passed: %s", i, tc.name) | ||
suite.Require().NotNil(cs, "Case %d should have passed: %s", i, tc.name) | ||
suite.Require().NotNil(cs.(*types.ConsensusState).Data, "Case %d should have passed: %s", i, tc.name) | ||
} else { | ||
suite.Require().ErrorIs(err, tc.expError, "Case %d should have failed: %s", i, tc.name) | ||
suite.Require().Nil(cs, "Case %d should have failed: %s", i, tc.name) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (suite *TypesTestSuite) TestValidateSelfClient() { | ||
var ( | ||
clientState exported.ClientState | ||
consensusHost clienttypes.ConsensusHost | ||
) | ||
|
||
testCases := []struct { | ||
name string | ||
malleate func() | ||
expError error | ||
}{ | ||
{ | ||
name: "success", | ||
malleate: func() {}, | ||
expError: nil, | ||
}, | ||
{ | ||
name: "failure: invalid data", | ||
malleate: func() { | ||
clientState = types.NewClientState(nil, wasmtesting.Code, clienttypes.ZeroHeight()) | ||
}, | ||
expError: clienttypes.ErrInvalidClient, | ||
}, | ||
{ | ||
name: "failure: invalid clientstate type", | ||
malleate: func() { | ||
clientState = &ibctm.ClientState{} | ||
}, | ||
expError: clienttypes.ErrInvalidClient, | ||
}, | ||
{ | ||
name: "failure: delegate error propagates", | ||
malleate: func() { | ||
consensusHost.(*mock.ConsensusHost).ValidateSelfClientFn = func(ctx sdk.Context, clientState exported.ClientState) error { | ||
return mock.MockApplicationCallbackError | ||
} | ||
}, | ||
expError: mock.MockApplicationCallbackError, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
clientState = types.NewClientState(wasmtesting.CreateMockClientStateBz(suite.chainA.Codec, suite.checksum), wasmtesting.Code, clienttypes.ZeroHeight()) | ||
consensusHost = &mock.ConsensusHost{} | ||
|
||
tc.malleate() | ||
|
||
var err error | ||
consensusHost, err = types.NewWasmConsensusHost(suite.chainA.Codec, consensusHost) | ||
suite.Require().NoError(err) | ||
|
||
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetConsensusHost( | ||
consensusHost, | ||
) | ||
|
||
err = suite.chainA.App.GetIBCKeeper().ClientKeeper.ValidateSelfClient(suite.chainA.GetContext(), clientState) | ||
|
||
expPass := tc.expError == nil | ||
if expPass { | ||
suite.Require().NoError(err, "expected valid client for case: %s", tc.name) | ||
} else { | ||
suite.Require().ErrorIs(err, tc.expError, "expected %s got %s", tc.expError, err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this makes an assumption that the contract encodes consensus states with the wrapper envelope from 08-wasm. This is not currently mandated by 08-wasm, however it is mandated that the
ClientState
is wrapped invalidatePostExecutionClientState()