-
Notifications
You must be signed in to change notification settings - Fork 440
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
Add extension point for ibc app version decoding #1100
Closed
+211
−24
Closed
Changes from all commits
Commits
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
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,68 @@ | ||
package wasm | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ibcfees "github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types" | ||
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types" | ||
|
||
"github.com/CosmWasm/wasmd/x/wasm/types" | ||
) | ||
|
||
var _ IBCAppVersionDecoder = AppVersionDecoderFn(nil) | ||
|
||
// AppVersionDecoderFn custom type that implements IBCAppVersionDecoder | ||
type AppVersionDecoderFn func(ctx sdk.Context, rawVersion, portID, channelID string) (string, error) | ||
|
||
// Decode implements IBCAppVersionDecoder.Decode | ||
func (a AppVersionDecoderFn) Decode(ctx sdk.Context, rawVersion, portID, channelID string) (string, error) { | ||
return a(ctx, rawVersion, portID, channelID) | ||
} | ||
|
||
// AppVersionDecoderChain set up a chain of decoders where output of decoder n is feeded into n+1 as raw version | ||
// until the last one is reached | ||
func AppVersionDecoderChain(decoders ...IBCAppVersionDecoder) IBCAppVersionDecoder { | ||
if len(decoders) == 0 { | ||
panic("decoders must not be empty") | ||
} | ||
return AppVersionDecoderFn(func(ctx sdk.Context, rawVersion, portID, channelID string) (string, error) { | ||
version := rawVersion | ||
var err error | ||
for _, d := range decoders { | ||
version, err = d.Decode(ctx, version, portID, channelID) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return version, nil | ||
}) | ||
} | ||
|
||
// ICS29AppVersionDecoder decodes the ibc app version from an ics-29 fee middleware version when supported | ||
// fallback is raw version when it can not be determined | ||
func ICS29AppVersionDecoder(channelSource interface { | ||
IsFeeEnabled(ctx sdk.Context, portID, channelID string) bool | ||
}, | ||
) IBCAppVersionDecoder { | ||
return AppVersionDecoderFn(func(ctx sdk.Context, rawVersion, portID, channelID string) (string, error) { | ||
if channelSource.IsFeeEnabled(ctx, portID, channelID) { | ||
var meta ibcfees.Metadata | ||
if err := types.ModuleCdc.UnmarshalJSON([]byte(rawVersion), &meta); err != nil { | ||
return "", channeltypes.ErrInvalidChannelVersion | ||
} | ||
return meta.AppVersion, nil | ||
} | ||
if strings.TrimSpace(rawVersion) != "" && json.Valid([]byte(rawVersion)) { | ||
// check for ics-29 middleware versions | ||
var versionMetadata ibcfees.Metadata | ||
if err := types.ModuleCdc.UnmarshalJSON([]byte(rawVersion), &versionMetadata); err == nil { | ||
if strings.HasPrefix(versionMetadata.FeeVersion, "ics29-") { // sanity check | ||
return versionMetadata.AppVersion, nil | ||
} | ||
} | ||
} | ||
return rawVersion, nil | ||
}) | ||
} |
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,103 @@ | ||
package wasm | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAppVersionDecoderChain(t *testing.T) { | ||
dropLastCharDec := AppVersionDecoderFn(func(_ sdk.Context, rawVersion, _, _ string) (string, error) { | ||
return rawVersion[0 : len(rawVersion)-1], nil | ||
}) | ||
alwaysErrDec := AppVersionDecoderFn(func(_ sdk.Context, rawVersion, _, _ string) (string, error) { | ||
return "", errors.New("testing") | ||
}) | ||
specs := map[string]struct { | ||
dec IBCAppVersionDecoder | ||
rawVersion string | ||
expVersion string | ||
expErr bool | ||
}{ | ||
"single decoder": { | ||
dec: AppVersionDecoderChain(ICS29AppVersionDecoder(IsFeeEnabledMock{true})), | ||
rawVersion: `{"fee_version":"ics29-1", "app_version":"my version"}`, | ||
expVersion: "my version", | ||
}, | ||
"multiple decoders": { | ||
dec: AppVersionDecoderChain(ICS29AppVersionDecoder(IsFeeEnabledMock{true}), dropLastCharDec, dropLastCharDec), | ||
rawVersion: `{"fee_version":"ics29-1", "app_version":"123"}`, | ||
expVersion: "1", | ||
}, | ||
"multiple decoders with err": { | ||
dec: AppVersionDecoderChain(ICS29AppVersionDecoder(IsFeeEnabledMock{true}), alwaysErrDec, dropLastCharDec), | ||
expErr: true, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
gotVersion, gotErr := spec.dec.Decode(sdk.Context{}, spec.rawVersion, "foo", "bar") | ||
if spec.expErr { | ||
require.Error(t, gotErr) | ||
return | ||
} | ||
require.NoError(t, gotErr) | ||
assert.Equal(t, spec.expVersion, gotVersion) | ||
}) | ||
} | ||
} | ||
|
||
func TestICS29AppVersionDecoder(t *testing.T) { | ||
specs := map[string]struct { | ||
rawVersion string | ||
isFeeChannel bool | ||
expVersion string | ||
expErr bool | ||
}{ | ||
"raw version": { | ||
rawVersion: "my version", | ||
expVersion: "my version", | ||
}, | ||
"ics29 version on fee channel": { | ||
rawVersion: `{"fee_version":"ics29-1", "app_version":"my version"}`, | ||
isFeeChannel: true, | ||
expVersion: "my version", | ||
}, | ||
"invalid ics29 version on fee channel": { | ||
rawVersion: `not-a-json-string`, | ||
isFeeChannel: true, | ||
expErr: true, | ||
}, | ||
"ics29 version on non fee channel": { | ||
rawVersion: `{"fee_version":"ics29-1", "app_version":"my version"}`, | ||
expVersion: "my version", | ||
}, | ||
"non ics29 version on non fee channel": { | ||
rawVersion: `{"fee_version":"alx29-1", "app_version":"my version"}`, | ||
expVersion: `{"fee_version":"alx29-1", "app_version":"my version"}`, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
gotVersion, gotErr := ICS29AppVersionDecoder(IsFeeEnabledMock{spec.isFeeChannel}). | ||
Decode(sdk.Context{}, spec.rawVersion, "foo", "bar") | ||
if spec.expErr { | ||
require.Error(t, gotErr) | ||
return | ||
} | ||
require.NoError(t, gotErr) | ||
assert.Equal(t, spec.expVersion, gotVersion) | ||
}) | ||
} | ||
} | ||
|
||
type IsFeeEnabledMock struct { | ||
result bool | ||
} | ||
|
||
func (f IsFeeEnabledMock) IsFeeEnabled(_ sdk.Context, _, _ string) bool { | ||
return f.result | ||
} |
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.
If needed to get it to work, it is a hack and shows middleware stack is not complete.
There may be many new middleware and the wrapped applications shouldn't need to care, so no need to add eg ICS49 middleware that comes later