-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Limit simulation gas #674
Merged
Merged
Limit simulation gas #674
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package keeper | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
|
||
"github.com/cosmos/cosmos-sdk/store" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
|
@@ -97,7 +101,7 @@ func TestCountTxDecorator(t *testing.T) { | |
var anyTx sdk.Tx | ||
|
||
// when | ||
ante := NewCountTXDecorator(keyWasm) | ||
ante := keeper.NewCountTXDecorator(keyWasm) | ||
_, gotErr := ante.AnteHandle(ctx, anyTx, spec.simulate, spec.nextAssertAnte) | ||
if spec.expErr { | ||
require.Error(t, gotErr) | ||
|
@@ -107,3 +111,77 @@ func TestCountTxDecorator(t *testing.T) { | |
}) | ||
} | ||
} | ||
func TestLimitSimulationGasDecorator(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice tests |
||
var ( | ||
hundred sdk.Gas = 100 | ||
zero sdk.Gas = 0 | ||
) | ||
specs := map[string]struct { | ||
customLimit *sdk.Gas | ||
consumeGas sdk.Gas | ||
maxBlockGas int64 | ||
simulation bool | ||
expErr interface{} | ||
}{ | ||
"custom limit set": { | ||
customLimit: &hundred, | ||
consumeGas: hundred + 1, | ||
maxBlockGas: -1, | ||
simulation: true, | ||
expErr: sdk.ErrorOutOfGas{Descriptor: "testing"}, | ||
}, | ||
"block limit set": { | ||
maxBlockGas: 100, | ||
consumeGas: hundred + 1, | ||
simulation: true, | ||
expErr: sdk.ErrorOutOfGas{Descriptor: "testing"}, | ||
}, | ||
"no limits set": { | ||
maxBlockGas: -1, | ||
consumeGas: hundred + 1, | ||
simulation: true, | ||
}, | ||
"both limits set, custom applies": { | ||
customLimit: &hundred, | ||
consumeGas: hundred - 1, | ||
maxBlockGas: 10, | ||
simulation: true, | ||
}, | ||
"not a simulation": { | ||
customLimit: &hundred, | ||
consumeGas: hundred + 1, | ||
simulation: false, | ||
}, | ||
"zero custom limit": { | ||
customLimit: &zero, | ||
simulation: true, | ||
expErr: "gas limit must not be zero", | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
nextAnte := consumeGasAnteHandler(spec.consumeGas) | ||
ctx := sdk.Context{}. | ||
WithGasMeter(sdk.NewInfiniteGasMeter()). | ||
WithConsensusParams(&abci.ConsensusParams{ | ||
Block: &abci.BlockParams{MaxGas: spec.maxBlockGas}}) | ||
// when | ||
if spec.expErr != nil { | ||
require.PanicsWithValue(t, spec.expErr, func() { | ||
ante := keeper.NewLimitSimulationGasDecorator(spec.customLimit) | ||
ante.AnteHandle(ctx, nil, spec.simulation, nextAnte) | ||
}) | ||
return | ||
} | ||
ante := keeper.NewLimitSimulationGasDecorator(spec.customLimit) | ||
ante.AnteHandle(ctx, nil, spec.simulation, nextAnte) | ||
}) | ||
} | ||
} | ||
|
||
func consumeGasAnteHandler(gasToConsume sdk.Gas) sdk.AnteHandler { | ||
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { | ||
ctx.GasMeter().ConsumeGas(gasToConsume, "testing") | ||
return ctx, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,9 @@ import ( | |
) | ||
|
||
const ( | ||
defaultMemoryCacheSize uint32 = 100 // in MiB | ||
defaultQueryGasLimit uint64 = 3000000 | ||
defaultContractDebugMode = false | ||
defaultMemoryCacheSize uint32 = 100 // in MiB | ||
defaultSmartQueryGasLimit uint64 = 3_000_000 | ||
defaultContractDebugMode = false | ||
) | ||
|
||
func (m Model) ValidateBasic() error { | ||
|
@@ -296,6 +296,10 @@ func NewWasmCoins(cosmosCoins sdk.Coins) (wasmCoins []wasmvmtypes.Coin) { | |
|
||
// WasmConfig is the extra config required for wasm | ||
type WasmConfig struct { | ||
// SimulationGasLimit is the max gas to be used in a tx simulation call. | ||
// When not set the consensus max block gas is used instead | ||
SimulationGasLimit *uint64 | ||
// SimulationGasLimit is the max gas to be used in a smart query contract call | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just taking a look at this. You mean, |
||
SmartQueryGasLimit uint64 | ||
// MemoryCacheSize in MiB not bytes | ||
MemoryCacheSize uint32 | ||
|
@@ -306,7 +310,7 @@ type WasmConfig struct { | |
// DefaultWasmConfig returns the default settings for WasmConfig | ||
func DefaultWasmConfig() WasmConfig { | ||
return WasmConfig{ | ||
SmartQueryGasLimit: defaultQueryGasLimit, | ||
SmartQueryGasLimit: defaultSmartQueryGasLimit, | ||
MemoryCacheSize: defaultMemoryCacheSize, | ||
ContractDebugMode: defaultContractDebugMode, | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Maybe this could be "simplified"? But fine as is.