From e5dc6fe583e9de078f8c06a873771057f8641696 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 7 Oct 2021 11:23:58 +0200 Subject: [PATCH 1/3] Treat all contracts as pinned for gas costs in reply --- x/wasm/keeper/keeper.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index f57af29017..bd7bfdf03e 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -481,7 +481,8 @@ func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply was return nil, err } - replyCosts := k.gasRegister.ReplyCosts(k.IsPinnedCode(ctx, contractInfo.CodeID), reply) + // always consider this pinned + replyCosts := k.gasRegister.ReplyCosts(true, reply) ctx.GasMeter().ConsumeGas(replyCosts, "Loading CosmWasm module: reply") env := types.NewEnv(ctx, contractAddress) From e87884213651d407d58880631a64d16653de7f56 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 7 Oct 2021 12:26:25 +0200 Subject: [PATCH 2/3] Adjust gas prices in tests --- x/wasm/keeper/relay_test.go | 7 ++++--- x/wasm/keeper/submsg_test.go | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/x/wasm/keeper/relay_test.go b/x/wasm/keeper/relay_test.go index 50381d5f6e..b91a107df0 100644 --- a/x/wasm/keeper/relay_test.go +++ b/x/wasm/keeper/relay_test.go @@ -3,6 +3,9 @@ package keeper import ( "encoding/json" "errors" + "math" + "testing" + "github.com/CosmWasm/wasmd/x/wasm/keeper/wasmtesting" "github.com/CosmWasm/wasmd/x/wasm/types" wasmvm "github.com/CosmWasm/wasmvm" @@ -10,8 +13,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "math" - "testing" ) func TestOnOpenChannel(t *testing.T) { @@ -398,7 +399,7 @@ func TestOnRecvPacket(t *testing.T) { }, "submessage reply can overwrite ack data": { contractAddr: example.Contract, - expContractGas: myContractGas + 10 + DefaultInstanceCost + 3707, + expContractGas: myContractGas + 10 + 3707, contractResp: &wasmvmtypes.IBCReceiveResponse{ Acknowledgement: []byte("myAck"), Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyAlways, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, diff --git a/x/wasm/keeper/submsg_test.go b/x/wasm/keeper/submsg_test.go index 11c1647ced..7489b072ae 100644 --- a/x/wasm/keeper/submsg_test.go +++ b/x/wasm/keeper/submsg_test.go @@ -3,11 +3,12 @@ package keeper import ( "encoding/json" "fmt" - "github.com/stretchr/testify/assert" "io/ioutil" "strconv" "testing" + "github.com/stretchr/testify/assert" + wasmvmtypes "github.com/CosmWasm/wasmvm/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" @@ -243,17 +244,16 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) { resultAssertions []assertion }{ "send tokens": { - submsgID: 5, - msg: validBankSend, - // note we charge another 40k for the reply call - resultAssertions: []assertion{assertReturnedEvents(1), assertGasUsed(116000, 121000)}, + submsgID: 5, + msg: validBankSend, + resultAssertions: []assertion{assertReturnedEvents(1), assertGasUsed(76000, 81000)}, }, "not enough tokens": { submsgID: 6, msg: invalidBankSend, subMsgError: true, // uses less gas than the send tokens (cost of bank transfer) - resultAssertions: []assertion{assertGasUsed(97000, 99000), assertErrorString("insufficient funds")}, + resultAssertions: []assertion{assertGasUsed(57000, 59000), assertErrorString("insufficient funds")}, }, "out of gas panic with no gas limit": { submsgID: 7, @@ -265,24 +265,24 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) { submsgID: 15, msg: validBankSend, gasLimit: &subGasLimit, - // uses same gas as call without limit - resultAssertions: []assertion{assertReturnedEvents(1), assertGasUsed(116000, 121000)}, + // uses same gas as call without limit (note we do not charge the 40k on reply) + resultAssertions: []assertion{assertReturnedEvents(1), assertGasUsed(76000, 81000)}, }, "not enough tokens with limit": { submsgID: 16, msg: invalidBankSend, subMsgError: true, gasLimit: &subGasLimit, - // uses same gas as call without limit - resultAssertions: []assertion{assertGasUsed(97000, 99000), assertErrorString("insufficient funds")}, + // uses same gas as call without limit (note we do not charge the 40k on reply) + resultAssertions: []assertion{assertGasUsed(57000, 59000), assertErrorString("insufficient funds")}, }, "out of gas caught with gas limit": { submsgID: 17, msg: infiniteLoop, subMsgError: true, gasLimit: &subGasLimit, - // uses all the subGasLimit, plus the 92k or so for the main contract - resultAssertions: []assertion{assertGasUsed(subGasLimit+92000, subGasLimit+94000), assertErrorString("out of gas")}, + // uses all the subGasLimit, plus the 52k or so for the main contract + resultAssertions: []assertion{assertGasUsed(subGasLimit+52000, subGasLimit+54000), assertErrorString("out of gas")}, }, "instantiate contract gets address in data and events": { submsgID: 21, From 61bf2353cbc89af354347ea4f0c91bd0a7ac5cf7 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 7 Oct 2021 12:34:12 +0200 Subject: [PATCH 3/3] Last gas fix --- x/wasm/keeper/relay_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/wasm/keeper/relay_test.go b/x/wasm/keeper/relay_test.go index b91a107df0..c5570553be 100644 --- a/x/wasm/keeper/relay_test.go +++ b/x/wasm/keeper/relay_test.go @@ -399,7 +399,7 @@ func TestOnRecvPacket(t *testing.T) { }, "submessage reply can overwrite ack data": { contractAddr: example.Contract, - expContractGas: myContractGas + 10 + 3707, + expContractGas: myContractGas + 10 + 2707, contractResp: &wasmvmtypes.IBCReceiveResponse{ Acknowledgement: []byte("myAck"), Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyAlways, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}},