Skip to content

Commit

Permalink
Charge gas on reply
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Mar 9, 2021
1 parent 976c134 commit 00e99b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 5 additions & 3 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,16 @@ func (k Keeper) Sudo(ctx sdk.Context, contractAddress sdk.AccAddress, msg []byte
// reply is only called from keeper internal functions (dispatchSubmessages) after processing the submessage
// it
func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply wasmvmtypes.Reply) (*sdk.Result, error) {
// we can assume this is still in cache and not charge again for returning the reply. correct?
//ctx.GasMeter().ConsumeGas(InstanceCost, "Loading CosmWasm module: reply")

contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddress)
if err != nil {
return nil, err
}

// current thought is to charge gas like a fresh run, we can revisit whether to give it a discount later
if !k.IsPinnedCode(ctx, contractInfo.CodeID) {
ctx.GasMeter().ConsumeGas(InstanceCost, "Loading CosmWasm module: reply")
}

env := types.NewEnv(ctx, contractAddress)

// prepare querier
Expand Down
17 changes: 9 additions & 8 deletions x/wasm/internal/keeper/submsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,17 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) {
resultAssertions []assertion
}{
"send tokens": {
submsgID: 5,
msg: validBankSend,
resultAssertions: []assertion{assertReturnedEvents(3), assertGasUsed(81000, 83000)},
submsgID: 5,
msg: validBankSend,
// note we charge another 40k for the reply call
resultAssertions: []assertion{assertReturnedEvents(3), assertGasUsed(123000, 125000)},
},
"not enough tokens": {
submsgID: 6,
msg: invalidBankSend,
subMsgError: true,
// uses less gas than the send tokens (cost of bank transfer)
resultAssertions: []assertion{assertGasUsed(55000, 57000), assertErrorString("insufficient funds")},
resultAssertions: []assertion{assertGasUsed(97000, 99000), assertErrorString("insufficient funds")},
},
"out of gas panic with no gas limit": {
submsgID: 7,
Expand All @@ -280,23 +281,23 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) {
msg: validBankSend,
gasLimit: &subGasLimit,
// uses same gas as call without limit
resultAssertions: []assertion{assertReturnedEvents(3), assertGasUsed(81000, 83000)},
resultAssertions: []assertion{assertReturnedEvents(3), assertGasUsed(123000, 125000)},
},
"not enough tokens with limit": {
submsgID: 16,
msg: invalidBankSend,
subMsgError: true,
gasLimit: &subGasLimit,
// uses same gas as call without limit
resultAssertions: []assertion{assertGasUsed(55000, 57000), assertErrorString("insufficient funds")},
resultAssertions: []assertion{assertGasUsed(97000, 99000), assertErrorString("insufficient funds")},
},
"out of gas caught with gas limit": {
submsgID: 17,
msg: infiniteLoop,
subMsgError: true,
gasLimit: &subGasLimit,
// uses all the subGasLimit, plus the 50k or so for the main contract
resultAssertions: []assertion{assertGasUsed(subGasLimit+51000, subGasLimit+53000), assertErrorString("out of gas")},
// uses all the subGasLimit, plus the 92k or so for the main contract
resultAssertions: []assertion{assertGasUsed(subGasLimit+92000, subGasLimit+94000), assertErrorString("out of gas")},
},

"instantiate contract gets address in data and events": {
Expand Down

0 comments on commit 00e99b8

Please sign in to comment.