Skip to content

Commit

Permalink
Update to wasmvm v0.14.0-beta5 (cosmos#504)
Browse files Browse the repository at this point in the history
* Bump deps and contracts to v0.14.0-beta5

* Add admin field/variants to WasmMsg handling

* Compiles

* Update usage of IBC Timeout in tests

* Minor doc/test/Dockerfile updates

Co-authored-by: Alex Peters <alpe@users.noreply.github.com>
  • Loading branch information
ethanfrey and alpe authored Apr 28, 2021
1 parent 305f13c commit 846611b
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ WORKDIR /code
COPY . /code/

# See https://github.com/CosmWasm/wasmvm/releases
ADD https://github.com/CosmWasm/wasmvm/releases/download/v0.14.0-beta3/libwasmvm_muslc.a /lib/libwasmvm_muslc.a
ADD https://github.com/CosmWasm/wasmvm/releases/download/v0.14.0-beta5/libwasmvm_muslc.a /lib/libwasmvm_muslc.a
RUN sha256sum /lib/libwasmvm_muslc.a | grep adea8f977601daa8daa9885e02b31ca6dd0ab6d4dbbd8ba2ccfa447ffebda37c

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd
go 1.15

require (
github.com/CosmWasm/wasmvm v0.14.0-beta4
github.com/CosmWasm/wasmvm v0.14.0-beta5
github.com/cosmos/cosmos-sdk v0.42.4
github.com/cosmos/iavl v0.15.3
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4=
github.com/CosmWasm/wasmvm v0.14.0-beta4 h1:H8zVI/zRc+GAO+dk9jgGlo+5w0AENDJi6KN8Phvd2O0=
github.com/CosmWasm/wasmvm v0.14.0-beta4/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A=
github.com/CosmWasm/wasmvm v0.14.0-beta5 h1:lYe8tQr/m/utoAPyfp4vbka59CT7GH25QDUA0sZ4xN8=
github.com/CosmWasm/wasmvm v0.14.0-beta5/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
Expand Down
27 changes: 22 additions & 5 deletions x/wasm/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,33 @@ func (i IBCHandler) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet)
}

func newIBCPacket(packet channeltypes.Packet) wasmvmtypes.IBCPacket {
var timeout wasmvmtypes.IBCTimeout
// detect set/unset
if packet.TimeoutHeight.RevisionHeight != 0 {
if packet.TimeoutTimestamp != 0 {
timeout.Both = &wasmvmtypes.IBCTimeoutBoth{
Block: wasmvmtypes.IBCTimeoutBlock{
Height: packet.TimeoutHeight.RevisionHeight,
Revision: packet.TimeoutHeight.RevisionNumber,
},
Timestamp: packet.TimeoutTimestamp,
}
} else {
timeout.Block = &wasmvmtypes.IBCTimeoutBlock{
Height: packet.TimeoutHeight.RevisionHeight,
Revision: packet.TimeoutHeight.RevisionNumber,
}
}
} else {
timeout.Timestamp = &packet.TimeoutTimestamp
}

return wasmvmtypes.IBCPacket{
Data: packet.Data,
Src: wasmvmtypes.IBCEndpoint{ChannelID: packet.SourceChannel, PortID: packet.SourcePort},
Dest: wasmvmtypes.IBCEndpoint{ChannelID: packet.DestinationChannel, PortID: packet.DestinationPort},
Sequence: packet.Sequence,
TimeoutBlock: &wasmvmtypes.IBCTimeoutBlock{
Height: packet.TimeoutHeight.RevisionHeight,
Revision: packet.TimeoutHeight.RevisionNumber,
},
TimeoutTimestamp: &packet.TimeoutTimestamp,
Timeout: timeout,
}
}

Expand Down
81 changes: 81 additions & 0 deletions x/wasm/ibc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package wasm

import (
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMapToWasmVMIBCPacket(t *testing.T) {
var myTimestamp uint64 = 1
specs := map[string]struct {
src channeltypes.Packet
exp wasmvmtypes.IBCPacket
}{
"with hight timeout": {
src: IBCPacketFixture(),
exp: wasmvmtypes.IBCPacket{
Data: []byte("myData"),
Src: wasmvmtypes.IBCEndpoint{PortID: "srcPort", ChannelID: "channel-1"},
Dest: wasmvmtypes.IBCEndpoint{PortID: "destPort", ChannelID: "channel-2"},
Sequence: 1,
Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{Height: 1, Revision: 2}},
},
},
"with time timeout": {
src: IBCPacketFixture(func(p *channeltypes.Packet) {
p.TimeoutTimestamp = myTimestamp
p.TimeoutHeight = clienttypes.Height{}
}),
exp: wasmvmtypes.IBCPacket{
Data: []byte("myData"),
Src: wasmvmtypes.IBCEndpoint{PortID: "srcPort", ChannelID: "channel-1"},
Dest: wasmvmtypes.IBCEndpoint{PortID: "destPort", ChannelID: "channel-2"},
Sequence: 1,
Timeout: wasmvmtypes.IBCTimeout{Timestamp: &myTimestamp},
},
}, "with time and height timeout": {
src: IBCPacketFixture(func(p *channeltypes.Packet) {
p.TimeoutTimestamp = myTimestamp
}),
exp: wasmvmtypes.IBCPacket{
Data: []byte("myData"),
Src: wasmvmtypes.IBCEndpoint{PortID: "srcPort", ChannelID: "channel-1"},
Dest: wasmvmtypes.IBCEndpoint{PortID: "destPort", ChannelID: "channel-2"},
Sequence: 1,
Timeout: wasmvmtypes.IBCTimeout{Both: &wasmvmtypes.IBCTimeoutBoth{
Block: wasmvmtypes.IBCTimeoutBlock{Height: 1, Revision: 2},
Timestamp: myTimestamp,
}},
},
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
got := newIBCPacket(spec.src)
assert.Equal(t, spec.exp, got)
})
}
}

func IBCPacketFixture(mutators ...func(p *channeltypes.Packet)) channeltypes.Packet {
r := channeltypes.Packet{
Sequence: 1,
SourcePort: "srcPort",
SourceChannel: "channel-1",
DestinationPort: "destPort",
DestinationChannel: "channel-2",
Data: []byte("myData"),
TimeoutHeight: clienttypes.Height{
RevisionHeight: 1,
RevisionNumber: 2,
},
TimeoutTimestamp: 0,
}
for _, m := range mutators {
m(&r)
}
return r
}
5 changes: 3 additions & 2 deletions x/wasm/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,16 @@ func (h IBCRawPacketHandler) DispatchMsg(ctx sdk.Context, _ sdk.AccAddress, cont
if !ok {
return nil, nil, sdkerrors.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
}
timestamp, height := ConvertWasmIBCTimeout(msg.IBC.SendPacket.Timeout)
packet := channeltypes.NewPacket(
msg.IBC.SendPacket.Data,
sequence,
contractIBCPortID,
contractIBCChannelID,
channelInfo.Counterparty.PortId,
channelInfo.Counterparty.ChannelId,
convertWasmIBCTimeoutHeightToCosmosHeight(msg.IBC.SendPacket.TimeoutBlock),
convertWasmIBCTimeoutTimestampToCosmosTimestamp(msg.IBC.SendPacket.TimeoutTimestamp),
height,
timestamp,
)
return nil, nil, h.channelKeeper.SendPacket(ctx, channelCap, packet)
}
Expand Down
43 changes: 31 additions & 12 deletions x/wasm/keeper/handler_plugin_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func EncodeWasmMsg(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg,
CodeID: msg.Instantiate.CodeID,
Label: msg.Instantiate.Label,
InitMsg: msg.Instantiate.Msg,
Admin: msg.Instantiate.Admin,
Funds: coins,
}
return []sdk.Msg{&sdkMsg}, nil
Expand All @@ -232,6 +233,19 @@ func EncodeWasmMsg(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg,
MigrateMsg: msg.Migrate.Msg,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.ClearAdmin != nil:
sdkMsg := types.MsgClearAdmin{
Sender: sender.String(),
Contract: msg.ClearAdmin.ContractAddr,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.UpdateAdmin != nil:
sdkMsg := types.MsgUpdateAdmin{
Sender: sender.String(),
Contract: msg.UpdateAdmin.ContractAddr,
NewAdmin: msg.UpdateAdmin.Admin,
}
return []sdk.Msg{&sdkMsg}, nil
default:
return nil, sdkerrors.Wrap(types.ErrUnknownMsg, "unknown variant of Wasm")
}
Expand All @@ -251,14 +265,15 @@ func EncodeIBCMsg(portSource types.ICS20TransferPortSource) func(ctx sdk.Context
if err != nil {
return nil, sdkerrors.Wrap(err, "amount")
}
timestamp, height := ConvertWasmIBCTimeout(msg.Transfer.Timeout)
msg := &ibctransfertypes.MsgTransfer{
SourcePort: portSource.GetPort(ctx),
SourceChannel: msg.Transfer.ChannelID,
Token: amount,
Sender: sender.String(),
Receiver: msg.Transfer.ToAddress,
TimeoutHeight: convertWasmIBCTimeoutHeightToCosmosHeight(msg.Transfer.TimeoutBlock),
TimeoutTimestamp: convertWasmIBCTimeoutTimestampToCosmosTimestamp(msg.Transfer.TimeoutTimestamp),
TimeoutHeight: height,
TimeoutTimestamp: timestamp,
}
return []sdk.Msg{msg}, nil
default:
Expand All @@ -267,18 +282,22 @@ func EncodeIBCMsg(portSource types.ICS20TransferPortSource) func(ctx sdk.Context
}
}

func convertWasmIBCTimeoutHeightToCosmosHeight(ibcTimeoutBlock *wasmvmtypes.IBCTimeoutBlock) ibcclienttypes.Height {
if ibcTimeoutBlock == nil {
return ibcclienttypes.NewHeight(0, 0)
}
return ibcclienttypes.NewHeight(ibcTimeoutBlock.Revision, ibcTimeoutBlock.Height)
}
// ConvertWasmIBCTimeout converts the wasmvm ibc timeout type to cosmos-sdk height and relative block timeout
func ConvertWasmIBCTimeout(ibcTimeout wasmvmtypes.IBCTimeout) (uint64, ibcclienttypes.Height) {
var timestamp uint64
var height ibcclienttypes.Height

func convertWasmIBCTimeoutTimestampToCosmosTimestamp(timestamp *uint64) uint64 {
if timestamp == nil {
return 0
switch {
case ibcTimeout.Timestamp != nil:
timestamp = *ibcTimeout.Timestamp
case ibcTimeout.Block != nil:
height = ibcclienttypes.NewHeight(ibcTimeout.Block.Revision, ibcTimeout.Block.Height)
case ibcTimeout.Both != nil:
timestamp = ibcTimeout.Both.Timestamp
height = ibcclienttypes.NewHeight(ibcTimeout.Both.Block.Revision, ibcTimeout.Both.Block.Height)
}
return *timestamp

return timestamp, height
}

func convertWasmCoinsToSdkCoins(coins []wasmvmtypes.Coin) (sdk.Coins, error) {
Expand Down
90 changes: 85 additions & 5 deletions x/wasm/keeper/handler_plugin_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import (
)

func TestEncoding(t *testing.T) {
addr1 := RandomAccountAddress(t)
addr2 := RandomAccountAddress(t)
invalidAddr := "xrnd1d02kd90n38qvr3qb9qof83fn2d2"
var (
addr1 = RandomAccountAddress(t)
addr2 = RandomAccountAddress(t)
addr3 = RandomAccountAddress(t)
invalidAddr = "xrnd1d02kd90n38qvr3qb9qof83fn2d2"
)
valAddr := make(sdk.ValAddress, sdk.AddrLen)
valAddr[0] = 12
valAddr2 := make(sdk.ValAddress, sdk.AddrLen)
Expand Down Expand Up @@ -171,6 +174,7 @@ func TestEncoding(t *testing.T) {
wasmvmtypes.NewCoin(123, "eth"),
},
Label: "myLabel",
Admin: addr2.String(),
},
},
},
Expand All @@ -181,6 +185,7 @@ func TestEncoding(t *testing.T) {
Label: "myLabel",
InitMsg: jsonMsg,
Funds: sdk.NewCoins(sdk.NewInt64Coin("eth", 123)),
Admin: addr2.String(),
},
},
},
Expand All @@ -204,6 +209,40 @@ func TestEncoding(t *testing.T) {
},
},
},
"wasm update admin": {
sender: addr2,
srcMsg: wasmvmtypes.CosmosMsg{
Wasm: &wasmvmtypes.WasmMsg{
UpdateAdmin: &wasmvmtypes.UpdateAdminMsg{
ContractAddr: addr1.String(),
Admin: addr3.String(),
},
},
},
output: []sdk.Msg{
&types.MsgUpdateAdmin{
Sender: addr2.String(),
Contract: addr1.String(),
NewAdmin: addr3.String(),
},
},
},
"wasm clear admin": {
sender: addr2,
srcMsg: wasmvmtypes.CosmosMsg{
Wasm: &wasmvmtypes.WasmMsg{
ClearAdmin: &wasmvmtypes.ClearAdminMsg{
ContractAddr: addr1.String(),
},
},
},
output: []sdk.Msg{
&types.MsgClearAdmin{
Sender: addr2.String(),
Contract: addr1.String(),
},
},
},
"staking delegate": {
sender: addr1,
srcMsg: wasmvmtypes.CosmosMsg{
Expand Down Expand Up @@ -353,7 +392,9 @@ func TestEncoding(t *testing.T) {
Denom: "ALX",
Amount: "1",
},
TimeoutBlock: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2},
Timeout: wasmvmtypes.IBCTimeout{
Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2},
},
},
},
},
Expand Down Expand Up @@ -386,7 +427,45 @@ func TestEncoding(t *testing.T) {
Denom: "ALX",
Amount: "1",
},
TimeoutTimestamp: &timeoutVal,
Timeout: wasmvmtypes.IBCTimeout{Timestamp: &timeoutVal},
},
},
},
transferPortSource: wasmtesting.MockIBCTransferKeeper{GetPortFn: func(ctx sdk.Context) string {
return "transfer"
}},
output: []sdk.Msg{
&ibctransfertypes.MsgTransfer{
SourcePort: "transfer",
SourceChannel: "myChanID",
Token: sdk.Coin{
Denom: "ALX",
Amount: sdk.NewInt(1),
},
Sender: addr1.String(),
Receiver: addr2.String(),
TimeoutTimestamp: 100,
},
},
}, "IBC transfer with time and height timeout": {
sender: addr1,
srcContractIBCPort: "myIBCPort",
srcMsg: wasmvmtypes.CosmosMsg{
IBC: &wasmvmtypes.IBCMsg{
Transfer: &wasmvmtypes.TransferMsg{
ChannelID: "myChanID",
ToAddress: addr2.String(),
Amount: wasmvmtypes.Coin{
Denom: "ALX",
Amount: "1",
},
Timeout: wasmvmtypes.IBCTimeout{Both: &wasmvmtypes.IBCTimeoutBoth{
Block: wasmvmtypes.IBCTimeoutBlock{
Height: 1,
Revision: 2,
},
Timestamp: timeoutVal,
}},
},
},
},
Expand All @@ -404,6 +483,7 @@ func TestEncoding(t *testing.T) {
Sender: addr1.String(),
Receiver: addr2.String(),
TimeoutTimestamp: 100,
TimeoutHeight: clienttypes.Height{RevisionNumber: 2, RevisionHeight: 1},
},
},
},
Expand Down
Loading

0 comments on commit 846611b

Please sign in to comment.