Skip to content

Commit

Permalink
Merge pull request #630 from CosmWasm/450-reply-gas-costs
Browse files Browse the repository at this point in the history
Treat all contracts as pinned for gas costs in reply
  • Loading branch information
ethanfrey authored Oct 7, 2021
2 parents 50458e0 + 61bf235 commit 2b7d28a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
3 changes: 2 additions & 1 deletion x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions x/wasm/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ 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"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math"
"testing"
)

func TestOnOpenChannel(t *testing.T) {
Expand Down Expand Up @@ -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 + 2707,
contractResp: &wasmvmtypes.IBCReceiveResponse{
Acknowledgement: []byte("myAck"),
Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyAlways, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}},
Expand Down
24 changes: 12 additions & 12 deletions x/wasm/keeper/submsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 2b7d28a

Please sign in to comment.