diff --git a/Dockerfile b/Dockerfile index 70c29d96..157ddcf4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,8 +15,8 @@ WORKDIR /code COPY . /code/ # See https://github.com/CosmWasm/wasmvm/releases -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 0a90493799f19f95d39910d4ac5b6d9fc70d482dabec1f3ffb7c81428c125d4c +ADD https://github.com/CosmWasm/wasmvm/releases/download/v0.14.0-rc1/libwasmvm_muslc.a /lib/libwasmvm_muslc.a +RUN sha256sum /lib/libwasmvm_muslc.a | grep 46b77410f55b3d70ce77088aa70f8c54c574c19471437d28409cb584c0062c4e # force it to use static lib (from above) not standard libgo_cosmwasm.so file RUN LEDGER_ENABLED=false BUILD_TAGS=muslc make build diff --git a/go.mod b/go.mod index d3d221cd..7714ad95 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd go 1.15 require ( - github.com/CosmWasm/wasmvm v0.14.0-beta5 + github.com/CosmWasm/wasmvm v0.14.0-rc1 github.com/cosmos/cosmos-sdk v0.42.4 github.com/cosmos/iavl v0.15.3 github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b diff --git a/go.sum b/go.sum index cc213ff3..794637cf 100644 --- a/go.sum +++ b/go.sum @@ -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-beta5 h1:lYe8tQr/m/utoAPyfp4vbka59CT7GH25QDUA0sZ4xN8= -github.com/CosmWasm/wasmvm v0.14.0-beta5/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A= +github.com/CosmWasm/wasmvm v0.14.0-rc1 h1:otpTbV93S2J5Fk67LB8On8MV/nFS1m2H4ULn9tGihCg= +github.com/CosmWasm/wasmvm v0.14.0-rc1/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= @@ -573,7 +573,6 @@ github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoM github.com/tendermint/tendermint v0.34.0-rc4/go.mod h1:yotsojf2C1QBOw4dZrTcxbyxmPUrT4hNuOQWX9XUwB4= github.com/tendermint/tendermint v0.34.0-rc6/go.mod h1:ugzyZO5foutZImv0Iyx/gOFCX6mjJTgbLHTwi17VDVg= github.com/tendermint/tendermint v0.34.0/go.mod h1:Aj3PIipBFSNO21r+Lq3TtzQ+uKESxkbA3yo/INM4QwQ= -github.com/tendermint/tendermint v0.34.9 h1:9P2MXDEPOcPW0NBcHQ/HDSfvczZm+q5nUUw7AZ6f1Vc= github.com/tendermint/tendermint v0.34.9/go.mod h1:kl4Z1JwGx1I+u1SXIzMDy7Z3T8LiMeCAOnzNn6AIMT4= github.com/tendermint/tendermint v0.34.10 h1:wBOc/It8sh/pVH9np2V5fBvRmIyFN/bUrGPx+eAHexs= github.com/tendermint/tendermint v0.34.10/go.mod h1:aeHL7alPh4uTBIJQ8mgFEE8VwJLXI1VD3rVOmH2Mcy0= diff --git a/x/wasm/ibc.go b/x/wasm/ibc.go index f7d162a6..e9355987 100644 --- a/x/wasm/ibc.go +++ b/x/wasm/ibc.go @@ -244,25 +244,14 @@ 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, - } + timeout := wasmvmtypes.IBCTimeout{ + Timestamp: packet.TimeoutTimestamp, + } + if !packet.TimeoutHeight.IsZero() { + timeout.Block = &wasmvmtypes.IBCTimeoutBlock{ + Height: packet.TimeoutHeight.RevisionHeight, + Revision: packet.TimeoutHeight.RevisionNumber, } - } else { - timeout.Timestamp = &packet.TimeoutTimestamp } return wasmvmtypes.IBCPacket{ diff --git a/x/wasm/ibc_reflect_test.go b/x/wasm/ibc_reflect_test.go index 8abe168c..fb5e04ec 100644 --- a/x/wasm/ibc_reflect_test.go +++ b/x/wasm/ibc_reflect_test.go @@ -12,6 +12,13 @@ import ( ) func TestIBCReflectContract(t *testing.T) { + // scenario: + // chain A: ibc_reflect_send.wasm + // chain B: reflect.wasm + ibc_reflect.wasm + // + // Chain A "ibc_reflect_send" sends a IBC packet "on channel connect" event to chain B "ibc_reflect" + // "ibc_reflect" sends a submessage to "reflect" which is returned as submessage. + var ( coordinator = ibctesting.NewCoordinator(t, 2, nil, nil) chainA = coordinator.GetChain(ibctesting.GetChainID(0)) @@ -96,7 +103,7 @@ type AccountQuery struct { } type AccountResponse struct { - LastUpdateTime uint64 `json:"last_update_time"` + LastUpdateTime uint64 `json:"last_update_time,string"` RemoteAddr string `json:"remote_addr"` RemoteBalance wasmvmtypes.Coins `json:"remote_balance"` } diff --git a/x/wasm/ibc_test.go b/x/wasm/ibc_test.go index 1510f740..25106d42 100644 --- a/x/wasm/ibc_test.go +++ b/x/wasm/ibc_test.go @@ -14,7 +14,7 @@ func TestMapToWasmVMIBCPacket(t *testing.T) { src channeltypes.Packet exp wasmvmtypes.IBCPacket }{ - "with hight timeout": { + "with height timeout": { src: IBCPacketFixture(), exp: wasmvmtypes.IBCPacket{ Data: []byte("myData"), @@ -34,7 +34,7 @@ func TestMapToWasmVMIBCPacket(t *testing.T) { Src: wasmvmtypes.IBCEndpoint{PortID: "srcPort", ChannelID: "channel-1"}, Dest: wasmvmtypes.IBCEndpoint{PortID: "destPort", ChannelID: "channel-2"}, Sequence: 1, - Timeout: wasmvmtypes.IBCTimeout{Timestamp: &myTimestamp}, + Timeout: wasmvmtypes.IBCTimeout{Timestamp: myTimestamp}, }, }, "with time and height timeout": { src: IBCPacketFixture(func(p *channeltypes.Packet) { @@ -45,10 +45,10 @@ func TestMapToWasmVMIBCPacket(t *testing.T) { 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}, + Timeout: wasmvmtypes.IBCTimeout{ + Block: &wasmvmtypes.IBCTimeoutBlock{Height: 1, Revision: 2}, Timestamp: myTimestamp, - }}, + }, }, }, } diff --git a/x/wasm/keeper/handler_plugin.go b/x/wasm/keeper/handler_plugin.go index d39b51a4..5de2256e 100644 --- a/x/wasm/keeper/handler_plugin.go +++ b/x/wasm/keeper/handler_plugin.go @@ -165,7 +165,6 @@ 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, @@ -173,8 +172,8 @@ func (h IBCRawPacketHandler) DispatchMsg(ctx sdk.Context, _ sdk.AccAddress, cont contractIBCChannelID, channelInfo.Counterparty.PortId, channelInfo.Counterparty.ChannelId, - height, - timestamp, + convertWasmIBCTimeoutHeightToCosmosHeight(msg.IBC.SendPacket.Timeout.Block), + msg.IBC.SendPacket.Timeout.Timestamp, ) return nil, nil, h.channelKeeper.SendPacket(ctx, channelCap, packet) } diff --git a/x/wasm/keeper/handler_plugin_encoders.go b/x/wasm/keeper/handler_plugin_encoders.go index 9e6bb28e..5cba58eb 100644 --- a/x/wasm/keeper/handler_plugin_encoders.go +++ b/x/wasm/keeper/handler_plugin_encoders.go @@ -265,15 +265,14 @@ 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: height, - TimeoutTimestamp: timestamp, + TimeoutHeight: convertWasmIBCTimeoutHeightToCosmosHeight(msg.Transfer.Timeout.Block), + TimeoutTimestamp: msg.Transfer.Timeout.Timestamp, } return []sdk.Msg{msg}, nil default: @@ -281,23 +280,11 @@ func EncodeIBCMsg(portSource types.ICS20TransferPortSource) func(ctx sdk.Context } } } - -// 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 - - 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) +func convertWasmIBCTimeoutHeightToCosmosHeight(ibcTimeoutBlock *wasmvmtypes.IBCTimeoutBlock) ibcclienttypes.Height { + if ibcTimeoutBlock == nil { + return ibcclienttypes.NewHeight(0, 0) } - - return timestamp, height + return ibcclienttypes.NewHeight(ibcTimeoutBlock.Revision, ibcTimeoutBlock.Height) } func convertWasmCoinsToSdkCoins(coins []wasmvmtypes.Coin) (sdk.Coins, error) { diff --git a/x/wasm/keeper/handler_plugin_encoders_test.go b/x/wasm/keeper/handler_plugin_encoders_test.go index 5088141e..28af7f6b 100644 --- a/x/wasm/keeper/handler_plugin_encoders_test.go +++ b/x/wasm/keeper/handler_plugin_encoders_test.go @@ -9,6 +9,7 @@ import ( 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/golang/protobuf/proto" + "github.com/stretchr/testify/assert" "testing" "github.com/CosmWasm/wasmd/x/wasm/types" @@ -17,7 +18,6 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -32,7 +32,6 @@ func TestEncoding(t *testing.T) { valAddr[0] = 12 valAddr2 := make(sdk.ValAddress, sdk.AddrLen) valAddr2[1] = 123 - var timeoutVal uint64 = 100 jsonMsg := json.RawMessage(`{"foo": 123}`) @@ -427,7 +426,7 @@ func TestEncoding(t *testing.T) { Denom: "ALX", Amount: "1", }, - Timeout: wasmvmtypes.IBCTimeout{Timestamp: &timeoutVal}, + Timeout: wasmvmtypes.IBCTimeout{Timestamp: 100}, }, }, }, @@ -447,7 +446,8 @@ func TestEncoding(t *testing.T) { TimeoutTimestamp: 100, }, }, - }, "IBC transfer with time and height timeout": { + }, + "IBC transfer with time and height timeout": { sender: addr1, srcContractIBCPort: "myIBCPort", srcMsg: wasmvmtypes.CosmosMsg{ @@ -459,13 +459,7 @@ func TestEncoding(t *testing.T) { Denom: "ALX", Amount: "1", }, - Timeout: wasmvmtypes.IBCTimeout{Both: &wasmvmtypes.IBCTimeoutBoth{ - Block: wasmvmtypes.IBCTimeoutBlock{ - Height: 1, - Revision: 2, - }, - Timestamp: timeoutVal, - }}, + Timeout: wasmvmtypes.IBCTimeout{Timestamp: 100, Block: &wasmvmtypes.IBCTimeoutBlock{Height: 1, Revision: 2}}, }, }, }, @@ -483,7 +477,7 @@ func TestEncoding(t *testing.T) { Sender: addr1.String(), Receiver: addr2.String(), TimeoutTimestamp: 100, - TimeoutHeight: clienttypes.Height{RevisionNumber: 2, RevisionHeight: 1}, + TimeoutHeight: clienttypes.NewHeight(2, 1), }, }, }, diff --git a/x/wasm/keeper/handler_plugin_test.go b/x/wasm/keeper/handler_plugin_test.go index 6d0dcd04..7bc6f48d 100644 --- a/x/wasm/keeper/handler_plugin_test.go +++ b/x/wasm/keeper/handler_plugin_test.go @@ -251,9 +251,7 @@ func TestIBCRawPacketHandler(t *testing.T) { srcMsg: wasmvmtypes.SendPacketMsg{ ChannelID: "channel-1", Data: []byte("myData"), - Timeout: wasmvmtypes.IBCTimeout{ - Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2}, - }, + Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2}}, }, chanKeeper: chanKeeper, capKeeper: capKeeper, @@ -271,9 +269,7 @@ func TestIBCRawPacketHandler(t *testing.T) { srcMsg: wasmvmtypes.SendPacketMsg{ ChannelID: "channel-1", Data: []byte("myData"), - Timeout: wasmvmtypes.IBCTimeout{ - Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2}, - }, + Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2}}, }, chanKeeper: &wasmtesting.MockChannelKeeper{ GetNextSequenceSendFn: func(ctx sdk.Context, portID, channelID string) (uint64, bool) { @@ -285,9 +281,7 @@ func TestIBCRawPacketHandler(t *testing.T) { srcMsg: wasmvmtypes.SendPacketMsg{ ChannelID: "channel-1", Data: []byte("myData"), - Timeout: wasmvmtypes.IBCTimeout{ - Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2}, - }, + Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{Revision: 1, Height: 2}}, }, chanKeeper: chanKeeper, capKeeper: wasmtesting.MockCapabilityKeeper{ diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index 616b412d..be8c61da 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -283,7 +283,7 @@ func TestInstantiate(t *testing.T) { gasAfter := ctx.GasMeter().GasConsumed() if types.EnableGasVerification { - require.Equal(t, uint64(0x1229f), gasAfter-gasBefore) + require.Equal(t, uint64(0x122a0), gasAfter-gasBefore) } // ensure it is stored properly @@ -516,7 +516,7 @@ func TestExecute(t *testing.T) { // make sure gas is properly deducted from ctx gasAfter := ctx.GasMeter().GasConsumed() if types.EnableGasVerification { - require.Equal(t, uint64(0x12916), gasAfter-gasBefore) + require.Equal(t, uint64(0x12917), gasAfter-gasBefore) } // ensure bob now exists and got both payments released bobAcct = accKeeper.GetAccount(ctx, bob) diff --git a/x/wasm/keeper/recurse_test.go b/x/wasm/keeper/recurse_test.go index 2b65d3c2..58434407 100644 --- a/x/wasm/keeper/recurse_test.go +++ b/x/wasm/keeper/recurse_test.go @@ -59,10 +59,10 @@ func TestGasCostOnQuery(t *testing.T) { const ( GasNoWork uint64 = 44_072 // Note: about 100 SDK gas (10k wasmer gas) for each round of sha256 - GasWork50 uint64 = 49_763 // this is a little shy of 50k gas - to keep an eye on the limit + GasWork50 uint64 = 49_764 // this is a little shy of 50k gas - to keep an eye on the limit GasReturnUnhashed uint64 = 283 - GasReturnHashed uint64 = 258 + GasReturnHashed uint64 = 257 ) cases := map[string]struct { @@ -80,14 +80,14 @@ func TestGasCostOnQuery(t *testing.T) { msg: Recurse{ Work: 50, // 50 rounds of sha256 inside the contract }, - expectedGas: GasWork50 + 1, + expectedGas: GasWork50, }, "recursion 1, no work": { gasLimit: 400_000, msg: Recurse{ Depth: 1, }, - expectedGas: 2*GasNoWork + GasReturnUnhashed - 2, + expectedGas: 2*GasNoWork + GasReturnUnhashed, }, "recursion 1, some work": { gasLimit: 400_000, @@ -221,7 +221,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) { const ( // Note: about 100 SDK gas (10k wasmer gas) for each round of sha256 - GasWork2k uint64 = 273_566 // = InstanceCost + x // we have 6x gas used in cpu than in the instance + GasWork2k uint64 = 273_567 // = InstanceCost + x // we have 6x gas used in cpu than in the instance // This is overhead for calling into a sub-contract GasReturnHashed uint64 = 262 ) diff --git a/x/wasm/keeper/testdata/burner.wasm b/x/wasm/keeper/testdata/burner.wasm index 6413e515..16ed88b7 100644 Binary files a/x/wasm/keeper/testdata/burner.wasm and b/x/wasm/keeper/testdata/burner.wasm differ diff --git a/x/wasm/keeper/testdata/hackatom.wasm b/x/wasm/keeper/testdata/hackatom.wasm index d2a4b250..eb53ae26 100644 Binary files a/x/wasm/keeper/testdata/hackatom.wasm and b/x/wasm/keeper/testdata/hackatom.wasm differ diff --git a/x/wasm/keeper/testdata/hackatom.wasm.gzip b/x/wasm/keeper/testdata/hackatom.wasm.gzip index 5d93ff3b..6327dedc 100644 Binary files a/x/wasm/keeper/testdata/hackatom.wasm.gzip and b/x/wasm/keeper/testdata/hackatom.wasm.gzip differ diff --git a/x/wasm/keeper/testdata/ibc_reflect.wasm b/x/wasm/keeper/testdata/ibc_reflect.wasm index 82a11df2..481783a9 100644 Binary files a/x/wasm/keeper/testdata/ibc_reflect.wasm and b/x/wasm/keeper/testdata/ibc_reflect.wasm differ diff --git a/x/wasm/keeper/testdata/ibc_reflect_send.wasm b/x/wasm/keeper/testdata/ibc_reflect_send.wasm index 2ce39a1c..4441f94d 100644 Binary files a/x/wasm/keeper/testdata/ibc_reflect_send.wasm and b/x/wasm/keeper/testdata/ibc_reflect_send.wasm differ diff --git a/x/wasm/keeper/testdata/reflect.wasm b/x/wasm/keeper/testdata/reflect.wasm index 8fa02741..691d0a1d 100644 Binary files a/x/wasm/keeper/testdata/reflect.wasm and b/x/wasm/keeper/testdata/reflect.wasm differ diff --git a/x/wasm/keeper/testdata/staking.wasm b/x/wasm/keeper/testdata/staking.wasm index 4fd5cea0..968bb946 100644 Binary files a/x/wasm/keeper/testdata/staking.wasm and b/x/wasm/keeper/testdata/staking.wasm differ diff --git a/x/wasm/relay_pingpong_test.go b/x/wasm/relay_pingpong_test.go index 29412d12..7b986f0f 100644 --- a/x/wasm/relay_pingpong_test.go +++ b/x/wasm/relay_pingpong_test.go @@ -171,12 +171,10 @@ func (p *player) Execute(code wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmt SendPacket: &wasmvmtypes.SendPacketMsg{ ChannelID: start.ChannelID, Data: service.GetBytes(), - Timeout: wasmvmtypes.IBCTimeout{ - Block: &wasmvmtypes.IBCTimeoutBlock{ - Revision: doNotTimeout.RevisionNumber, - Height: doNotTimeout.RevisionHeight, - }, - }, + Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{ + Revision: doNotTimeout.RevisionNumber, + Height: doNotTimeout.RevisionHeight, + }}, }, }, }, @@ -273,12 +271,10 @@ func (p player) IBCPacketReceive(codeID wasmvm.Checksum, env wasmvmtypes.Env, pa respHit := &wasmvmtypes.IBCMsg{SendPacket: &wasmvmtypes.SendPacketMsg{ ChannelID: packet.Src.ChannelID, Data: newHit.GetBytes(), - Timeout: wasmvmtypes.IBCTimeout{ - Block: &wasmvmtypes.IBCTimeoutBlock{ - Revision: doNotTimeout.RevisionNumber, - Height: doNotTimeout.RevisionHeight, - }, - }, + Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{ + Revision: doNotTimeout.RevisionNumber, + Height: doNotTimeout.RevisionHeight, + }}, }} p.incrementCounter(sentBallsCountKey, store) p.t.Logf("[%s] received %d, returning %d: %v\n", p.actor, otherCount, nextValue, newHit) diff --git a/x/wasm/relay_test.go b/x/wasm/relay_test.go index 1aed06a7..163551d6 100644 --- a/x/wasm/relay_test.go +++ b/x/wasm/relay_test.go @@ -325,12 +325,10 @@ func (s *sendViaIBCTransferContract) Execute(code wasmvm.Checksum, env wasmvmtyp ToAddress: in.ReceiverAddr, Amount: wasmvmtypes.NewCoin(in.CoinsToSend.Amount.Uint64(), in.CoinsToSend.Denom), ChannelID: in.ChannelID, - Timeout: wasmvmtypes.IBCTimeout{ - Block: &wasmvmtypes.IBCTimeoutBlock{ - Revision: 0, - Height: 110, - }, - }, + Timeout: wasmvmtypes.IBCTimeout{Block: &wasmvmtypes.IBCTimeoutBlock{ + Revision: 0, + Height: 110, + }}, }, } @@ -364,9 +362,7 @@ func (s *sendEmulatedIBCTransferContract) Execute(code wasmvm.Checksum, env wasm SendPacket: &wasmvmtypes.SendPacketMsg{ ChannelID: in.ChannelID, Data: dataPacket.GetBytes(), - Timeout: wasmvmtypes.IBCTimeout{ - Timestamp: &in.Timeout, - }, + Timeout: wasmvmtypes.IBCTimeout{Timestamp: in.Timeout}, }, } return &wasmvmtypes.Response{Messages: []wasmvmtypes.CosmosMsg{{IBC: ibcMsg}}}, 0, nil @@ -483,7 +479,10 @@ func (s *contractStub) IBCPacketTimeout(codeID wasmvm.Checksum, env wasmvmtypes. } func toIBCPacket(p wasmvmtypes.IBCPacket) channeltypes.Packet { - timeout, height := wasmkeeper.ConvertWasmIBCTimeout(p.Timeout) + var height clienttypes.Height + if p.Timeout.Block != nil { + height = clienttypes.NewHeight(p.Timeout.Block.Revision, p.Timeout.Block.Height) + } return channeltypes.Packet{ Sequence: p.Sequence, SourcePort: p.Src.PortID, @@ -492,6 +491,6 @@ func toIBCPacket(p wasmvmtypes.IBCPacket) channeltypes.Packet { DestinationChannel: p.Dest.ChannelID, Data: p.Data, TimeoutHeight: height, - TimeoutTimestamp: timeout, + TimeoutTimestamp: p.Timeout.Timestamp, } } diff --git a/x/wasm/types/types.go b/x/wasm/types/types.go index edd0112d..106465fe 100644 --- a/x/wasm/types/types.go +++ b/x/wasm/types/types.go @@ -260,17 +260,15 @@ func NewEnv(ctx sdk.Context, contractAddr sdk.AccAddress) wasmvmtypes.Env { if ctx.BlockHeight() < 0 { panic("Block height must never be negative") } - sec := ctx.BlockTime().Unix() - if sec < 0 { - panic("Block (unix) time must never be negative ") + nano := ctx.BlockTime().UnixNano() + if nano < 1 { + panic("Block (unix) time must never be empty or negative ") } - nano := ctx.BlockTime().Nanosecond() env := wasmvmtypes.Env{ Block: wasmvmtypes.BlockInfo{ - Height: uint64(ctx.BlockHeight()), - Time: uint64(sec), - TimeNanos: uint64(nano), - ChainID: ctx.ChainID(), + Height: uint64(ctx.BlockHeight()), + Time: uint64(nano), + ChainID: ctx.ChainID(), }, Contract: wasmvmtypes.ContractInfo{ Address: contractAddr.String(), diff --git a/x/wasm/types/types_test.go b/x/wasm/types/types_test.go index 32083414..6e916e3e 100644 --- a/x/wasm/types/types_test.go +++ b/x/wasm/types/types_test.go @@ -1,6 +1,7 @@ package types import ( + wasmvmtypes "github.com/CosmWasm/wasmvm/types" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -308,3 +309,33 @@ func TestContractInfoReadExtension(t *testing.T) { }) } } + +func TestNewEnv(t *testing.T) { + myTime := time.Unix(0, 1619700924259075000) + t.Logf("++ unix: %d", myTime.UnixNano()) + var myContractAddr sdk.AccAddress = randBytes(sdk.AddrLen) + specs := map[string]struct { + srcCtx sdk.Context + exp wasmvmtypes.Env + }{ + "all good": { + srcCtx: sdk.Context{}.WithBlockHeight(1).WithBlockTime(myTime).WithChainID("testing"), + exp: wasmvmtypes.Env{ + Block: wasmvmtypes.BlockInfo{ + Height: 1, + Time: 1619700924259075000, + ChainID: "testing", + }, + Contract: wasmvmtypes.ContractInfo{ + Address: myContractAddr.String(), + }, + }, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + assert.Equal(t, spec.exp, NewEnv(spec.srcCtx, myContractAddr)) + }) + } + +}