Skip to content
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!: add filtering function in StargateMsg of wasm #352

Merged
merged 9 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

### Breaking Changes
* (app) [\#352](https://github.com/Finschia/finschia/pull/352) Add filtering function in StargateMsg of wasm

### Build, CI
* (build) [\#340](https://github.com/Finschia/finschia/pull/340) Set Finschia/ostracon version
Expand Down
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ func NewLinkApp(
panic("error while reading wasm config: " + err.Error())
}

// change wasm's StargateMsgEncoder to filtered encoder
wasmOpts = append(wasmOpts, wasmkeeper.WithMessageEncoders(filteredStargateMsgEncoders(appCodec)))

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
availableCapabilities := "iterator,staking,stargate,cosmwasm_1_1"
Expand Down
34 changes: 34 additions & 0 deletions app/wasm_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package app

import (
"strings"

"github.com/Finschia/finschia-sdk/codec"
codectypes "github.com/Finschia/finschia-sdk/codec/types"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
wasmkeeper "github.com/Finschia/wasmd/x/wasm/keeper"
wasmtypes "github.com/Finschia/wasmd/x/wasm/types"
wasmvmtypes "github.com/Finschia/wasmvm/types"
)

func filteredStargateMsgEncoders(cdc codec.Codec) *wasmkeeper.MessageEncoders {
return &wasmkeeper.MessageEncoders{
Stargate: wasmFilteredEncodeStargateMsg(cdc),
}
}

func wasmFilteredEncodeStargateMsg(unpakcer codectypes.AnyUnpacker) wasmkeeper.StargateEncoder {
// deny list in StargateMsg of wasm
deniedModulesInStargateMsg := []string{"/lbm.fswap.v1", "/lbm.fbridge.v1"}
zemyblue marked this conversation as resolved.
Show resolved Hide resolved
stargateMsgEncoder := wasmkeeper.EncodeStargateMsg(unpakcer)
return func(sender sdk.AccAddress, msg *wasmvmtypes.StargateMsg) ([]sdk.Msg, error) {
for _, moduleName := range deniedModulesInStargateMsg {
if strings.HasPrefix(msg.TypeURL, moduleName) {
return nil, sdkerrors.Wrap(wasmtypes.ErrUnsupportedForContract, moduleName+" not supported by Stargate")
loloicci marked this conversation as resolved.
Show resolved Hide resolved
}
}

return stargateMsgEncoder(sender, msg)
}
}
128 changes: 128 additions & 0 deletions app/wasm_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package app

import (
"testing"

"github.com/golang/protobuf/proto" // nolint: staticcheck
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

codectypes "github.com/Finschia/finschia-sdk/codec/types"
sdk "github.com/Finschia/finschia-sdk/types"
banktypes "github.com/Finschia/finschia-sdk/x/bank/types"
govtypes "github.com/Finschia/finschia-sdk/x/gov/types"
wasmkeeper "github.com/Finschia/wasmd/x/wasm/keeper"
wasmtypes "github.com/Finschia/wasmd/x/wasm/types"
wasmvmtypes "github.com/Finschia/wasmvm/types"
)

func TestFilteredStargateMsgEncoders(t *testing.T) {
tkxkd0159 marked this conversation as resolved.
Show resolved Hide resolved
var (
addr1 = wasmkeeper.RandomAccountAddress(t)
addr2 = wasmkeeper.RandomAccountAddress(t)
)
valAddr := make(sdk.ValAddress, wasmtypes.SDKAddrLen)
valAddr[0] = 12
valAddr2 := make(sdk.ValAddress, wasmtypes.SDKAddrLen)
valAddr2[1] = 123

bankMsg := &banktypes.MsgSend{
FromAddress: addr2.String(),
ToAddress: addr1.String(),
Amount: sdk.Coins{
sdk.NewInt64Coin("uatom", 12345),
sdk.NewInt64Coin("utgd", 54321),
},
}
bankMsgBin, err := proto.Marshal(bankMsg)
require.NoError(t, err)

content, err := codectypes.NewAnyWithValue(wasmtypes.StoreCodeProposalFixture())
require.NoError(t, err)

proposalMsg := &govtypes.MsgSubmitProposal{
Proposer: addr1.String(),
InitialDeposit: sdk.NewCoins(sdk.NewInt64Coin("uatom", 12345)),
Content: content,
}
proposalMsgBin, err := proto.Marshal(proposalMsg)
require.NoError(t, err)

cases := map[string]struct {
sender sdk.AccAddress
srcMsg wasmvmtypes.CosmosMsg
srcContractIBCPort string
transferPortSource wasmtypes.ICS20TransferPortSource
// set if valid
output []sdk.Msg
// set if invalid
isError bool
}{
"stargate encoded bank msg": {
sender: addr2,
srcMsg: wasmvmtypes.CosmosMsg{
Stargate: &wasmvmtypes.StargateMsg{
TypeURL: "/cosmos.bank.v1beta1.MsgSend",
Value: bankMsgBin,
},
},
output: []sdk.Msg{bankMsg},
},
"stargate encoded msg with any type": {
sender: addr2,
srcMsg: wasmvmtypes.CosmosMsg{
Stargate: &wasmvmtypes.StargateMsg{
TypeURL: "/cosmos.gov.v1beta1.MsgSubmitProposal",
Value: proposalMsgBin,
},
},
output: []sdk.Msg{proposalMsg},
},
"stargate encoded invalid typeUrl": {
sender: addr2,
srcMsg: wasmvmtypes.CosmosMsg{
Stargate: &wasmvmtypes.StargateMsg{
TypeURL: "/cosmos.bank.v2.MsgSend",
Value: bankMsgBin,
},
},
isError: true,
},
"stargate encoded filtered (fswap)": {
sender: addr1,
srcMsg: wasmvmtypes.CosmosMsg{
Stargate: &wasmvmtypes.StargateMsg{
TypeURL: "/lbm.fswap.v1.MsgSwap",
Value: nil,
},
},
isError: true,
},
"stargate encoded filtered (fbridge)": {
sender: addr1,
srcMsg: wasmvmtypes.CosmosMsg{
Stargate: &wasmvmtypes.StargateMsg{
TypeURL: "/lbm.fbridge.v1.MsgTransfer",
Value: nil,
},
},
isError: true,
},
}
encodingConfig := MakeEncodingConfig()
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
var ctx sdk.Context
encoder := wasmkeeper.DefaultEncoders(encodingConfig.Marshaler, tc.transferPortSource)
encoder.Merge(filteredStargateMsgEncoders(encodingConfig.Marshaler))
res, err := encoder.Encode(ctx, tc.sender, tc.srcContractIBCPort, tc.srcMsg)
if tc.isError {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.output, res)
}
})
}
}
Loading