Skip to content

Commit

Permalink
Merge pull request #247 from CosmWasm/cosmwasm-0.16
Browse files Browse the repository at this point in the history
Cosmwasm 0.16
  • Loading branch information
webmaster128 authored Jul 20, 2021
2 parents 0c245cf + 2631cc0 commit d767103
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 141 deletions.
24 changes: 12 additions & 12 deletions api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func IBCChannelOpen(
cache Cache,
checksum []byte,
env []byte,
channel []byte,
msg []byte,
gasMeter *GasMeter,
store KVStore,
api *GoAPI,
Expand All @@ -381,8 +381,8 @@ func IBCChannelOpen(
defer runtime.KeepAlive(checksum)
e := makeView(env)
defer runtime.KeepAlive(env)
ch := makeView(channel)
defer runtime.KeepAlive(channel)
m := makeView(msg)
defer runtime.KeepAlive(msg)

// set up a new stack frame to handle iterators
counter := startContract()
Expand All @@ -395,7 +395,7 @@ func IBCChannelOpen(
var gasUsed cu64
errmsg := newUnmanagedVector(nil)

res, err := C.ibc_channel_open(cache.ptr, cs, e, ch, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg)
res, err := C.ibc_channel_open(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg)
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
// Depending on the nature of the error, `gasUsed` will either have a meaningful value, or just 0.
return nil, uint64(gasUsed), errorWithMessage(err, errmsg)
Expand All @@ -407,7 +407,7 @@ func IBCChannelConnect(
cache Cache,
checksum []byte,
env []byte,
channel []byte,
msg []byte,
gasMeter *GasMeter,
store KVStore,
api *GoAPI,
Expand All @@ -419,8 +419,8 @@ func IBCChannelConnect(
defer runtime.KeepAlive(checksum)
e := makeView(env)
defer runtime.KeepAlive(env)
ch := makeView(channel)
defer runtime.KeepAlive(channel)
m := makeView(msg)
defer runtime.KeepAlive(msg)

// set up a new stack frame to handle iterators
counter := startContract()
Expand All @@ -433,7 +433,7 @@ func IBCChannelConnect(
var gasUsed cu64
errmsg := newUnmanagedVector(nil)

res, err := C.ibc_channel_connect(cache.ptr, cs, e, ch, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg)
res, err := C.ibc_channel_connect(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg)
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
// Depending on the nature of the error, `gasUsed` will either have a meaningful value, or just 0.
return nil, uint64(gasUsed), errorWithMessage(err, errmsg)
Expand All @@ -445,7 +445,7 @@ func IBCChannelClose(
cache Cache,
checksum []byte,
env []byte,
channel []byte,
msg []byte,
gasMeter *GasMeter,
store KVStore,
api *GoAPI,
Expand All @@ -457,8 +457,8 @@ func IBCChannelClose(
defer runtime.KeepAlive(checksum)
e := makeView(env)
defer runtime.KeepAlive(env)
ch := makeView(channel)
defer runtime.KeepAlive(channel)
m := makeView(msg)
defer runtime.KeepAlive(msg)

// set up a new stack frame to handle iterators
counter := startContract()
Expand All @@ -471,7 +471,7 @@ func IBCChannelClose(
var gasUsed cu64
errmsg := newUnmanagedVector(nil)

res, err := C.ibc_channel_close(cache.ptr, cs, e, ch, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg)
res, err := C.ibc_channel_close(cache.ptr, cs, e, m, db, a, q, cu64(gasLimit), cbool(printDebug), &gasUsed, &errmsg)
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
// Depending on the nature of the error, `gasUsed` will either have a meaningful value, or just 0.
return nil, uint64(gasUsed), errorWithMessage(err, errmsg)
Expand Down
16 changes: 8 additions & 8 deletions api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestGetMetrics(t *testing.T) {
assert.Equal(t, &types.Metrics{
HitsFsCache: 1,
ElementsMemoryCache: 1,
SizeMemoryCache: 2953616,
SizeMemoryCache: 2953668,
}, metrics)

// Instantiate 2
Expand All @@ -223,7 +223,7 @@ func TestGetMetrics(t *testing.T) {
HitsMemoryCache: 1,
HitsFsCache: 1,
ElementsMemoryCache: 1,
SizeMemoryCache: 2953616,
SizeMemoryCache: 2953668,
}, metrics)

// Pin
Expand All @@ -238,8 +238,8 @@ func TestGetMetrics(t *testing.T) {
HitsFsCache: 1,
ElementsPinnedMemoryCache: 1,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 2953616,
SizeMemoryCache: 2953616,
SizePinnedMemoryCache: 2953668,
SizeMemoryCache: 2953668,
}, metrics)

// Instantiate 3
Expand All @@ -256,8 +256,8 @@ func TestGetMetrics(t *testing.T) {
HitsFsCache: 1,
ElementsPinnedMemoryCache: 1,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 2953616,
SizeMemoryCache: 2953616,
SizePinnedMemoryCache: 2953668,
SizeMemoryCache: 2953668,
}, metrics)

// Unpin
Expand All @@ -274,7 +274,7 @@ func TestGetMetrics(t *testing.T) {
ElementsPinnedMemoryCache: 0,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 0,
SizeMemoryCache: 2953616,
SizeMemoryCache: 2953668,
}, metrics)

// Instantiate 4
Expand All @@ -292,7 +292,7 @@ func TestGetMetrics(t *testing.T) {
ElementsPinnedMemoryCache: 0,
ElementsMemoryCache: 1,
SizePinnedMemoryCache: 0,
SizeMemoryCache: 2953616,
SizeMemoryCache: 2953668,
}, metrics)
}

Expand Down
88 changes: 84 additions & 4 deletions api/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,65 @@ func MockIBCChannel(channelID string, ordering types.IBCOrder, ibcVersion string
PortID: "their_port",
ChannelID: "channel-7",
},
Order: ordering,
Version: ibcVersion,
CounterpartyVersion: ibcVersion,
ConnectionID: "connection-3",
Order: ordering,
Version: ibcVersion,
ConnectionID: "connection-3",
}
}

func MockIBCChannelOpenInit(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelOpenMsg {
return types.IBCChannelOpenMsg{
OpenInit: &types.IBCOpenInit{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
OpenTry: nil,
}
}

func MockIBCChannelOpenTry(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelOpenMsg {
return types.IBCChannelOpenMsg{
OpenInit: nil,
OpenTry: &types.IBCOpenTry{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
CounterpartyVersion: ibcVersion,
},
}
}

func MockIBCChannelConnectAck(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelConnectMsg {
return types.IBCChannelConnectMsg{
OpenAck: &types.IBCOpenAck{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
CounterpartyVersion: ibcVersion,
},
OpenConfirm: nil,
}
}

func MockIBCChannelConnectConfirm(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelConnectMsg {
return types.IBCChannelConnectMsg{
OpenAck: nil,
OpenConfirm: &types.IBCOpenConfirm{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
}
}

func MockIBCChannelCloseInit(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelCloseMsg {
return types.IBCChannelCloseMsg{
CloseInit: &types.IBCCloseInit{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
CloseConfirm: nil,
}
}

func MockIBCChannelCloseConfirm(channelID string, ordering types.IBCOrder, ibcVersion string) types.IBCChannelCloseMsg {
return types.IBCChannelCloseMsg{
CloseInit: nil,
CloseConfirm: &types.IBCCloseConfirm{
Channel: MockIBCChannel(channelID, ordering, ibcVersion),
},
}
}

Expand All @@ -96,6 +151,31 @@ func MockIBCPacket(myChannel string, data []byte) types.IBCPacket {
}
}

func MockIBCPacketReceive(myChannel string, data []byte) types.IBCPacketReceiveMsg {
return types.IBCPacketReceiveMsg{
Packet: MockIBCPacket(myChannel, data),
}
}

func MockIBCPacketAck(myChannel string, data []byte, ack types.IBCAcknowledgement) types.IBCPacketAckMsg {
packet := MockIBCPacket(myChannel, data)

return types.IBCPacketAckMsg{
Ack: types.IBCAcknowledgementWithPacket{
Acknowledgement: ack,
OriginalPacket: packet,
},
}
}

func MockIBCPacketTimeout(myChannel string, data []byte) types.IBCPacketTimeoutMsg {
packet := MockIBCPacket(myChannel, data)

return types.IBCPacketTimeoutMsg{
Packet: packet,
}
}

/*** Mock GasMeter ****/
// This code is borrowed from Cosmos-SDK store/types/gas.go

Expand Down
Binary file modified api/testdata/hackatom.wasm
Binary file not shown.
Binary file modified api/testdata/ibc_reflect.wasm
Binary file not shown.
Binary file modified api/testdata/queue.wasm
Binary file not shown.
Binary file modified api/testdata/reflect.wasm
Binary file not shown.
54 changes: 36 additions & 18 deletions ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func TestIBCHandshake(t *testing.T) {
// instantiate
env := api.MockEnv()
info := api.MockInfo("creator", nil)
msg := IBCInstantiateMsg{
init_msg := IBCInstantiateMsg{
ReflectCodeID: REFLECT_ID,
}
ires, _, err := vm.Instantiate(checksum, env, info, toBytes(t, msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
ires, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))

Expand All @@ -110,25 +110,34 @@ func TestIBCHandshake(t *testing.T) {
store.SetGasMeter(gasMeter2)
env = api.MockEnv()
// fails on bad version
channel := api.MockIBCChannel(CHANNEL_ID, types.Ordered, "random-garbage")
_, err = vm.IBCChannelOpen(checksum, env, channel, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
openMsg := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, "random-garbage")
_, err = vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.Error(t, err)
// passes on good version
channel = api.MockIBCChannel(CHANNEL_ID, types.Ordered, IBC_VERSION)
channel.CounterpartyVersion = ""
_, err = vm.IBCChannelOpen(checksum, env, channel, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
openMsg = api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, IBC_VERSION)
_, err = vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)

// channel connect
gasMeter3 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter3)
env = api.MockEnv()
// completes and dispatches message to create reflect contract
channel = api.MockIBCChannel(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, channel, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
connectMsg := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 1, len(res.Messages))

// check for the expected custom event
expected_events := []types.Event{types.Event{
Type: "ibc",
Attributes: []types.EventAttribute{types.EventAttribute{
Key: "channel",
Value: "connect",
}},
}}
require.Equal(t, expected_events, res.Events)

// make sure it read the balance properly and we got 250 atoms
dispatch := res.Messages[0].Msg
require.NotNil(t, dispatch.Wasm, "%#v", dispatch)
Expand Down Expand Up @@ -169,17 +178,16 @@ func TestIBCPacketDispatch(t *testing.T) {
// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
channel := api.MockIBCChannel(CHANNEL_ID, types.Ordered, IBC_VERSION)
channel.CounterpartyVersion = ""
_, err = vm.IBCChannelOpen(checksum, env, channel, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
openMsg := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, IBC_VERSION)
_, err = vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)

// channel connect
gasMeter3 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter3)
// completes and dispatches message to create reflect contract
channel = api.MockIBCChannel(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, channel, store, *goapi, querier, gasMeter3, TESTING_GAS_LIMIT, deserCost)
connectMsg := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter3, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 1, len(res.Messages))
id := res.Messages[0].ID
Expand Down Expand Up @@ -236,8 +244,8 @@ func TestIBCPacketDispatch(t *testing.T) {
}},
},
}
packet := api.MockIBCPacket(CHANNEL_ID, toBytes(t, ibcMsg))
pres, _, err := vm.IBCPacketReceive(checksum, env, packet, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
msg := api.MockIBCPacketReceive(CHANNEL_ID, toBytes(t, ibcMsg))
pres, _, err := vm.IBCPacketReceive(checksum, env, msg, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)

// assert app-level success
Expand All @@ -246,13 +254,23 @@ func TestIBCPacketDispatch(t *testing.T) {
require.Empty(t, ack.Err)

// error on message from another channel
packet2 := api.MockIBCPacket("no-such-channel", toBytes(t, ibcMsg))
pres2, _, err := vm.IBCPacketReceive(checksum, env, packet2, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
msg2 := api.MockIBCPacketReceive("no-such-channel", toBytes(t, ibcMsg))
pres2, _, err := vm.IBCPacketReceive(checksum, env, msg2, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
// assert app-level failure
var ack2 AcknowledgeDispatch
err = json.Unmarshal(pres2.Acknowledgement, &ack2)
require.Equal(t, "invalid packet: cosmwasm_std::addresses::Addr not found", ack2.Err)

// check for the expected custom event
expected_events := []types.Event{types.Event{
Type: "ibc",
Attributes: []types.EventAttribute{types.EventAttribute{
Key: "packet",
Value: "receive",
}},
}}
require.Equal(t, expected_events, pres2.Events)
}

func TestAnalyzeCode(t *testing.T) {
Expand Down
Loading

0 comments on commit d767103

Please sign in to comment.