-
Notifications
You must be signed in to change notification settings - Fork 110
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
Gas consumed #29
Comments
The contract should definitely fail in that case. You are right that there is a bit of a space for abuse, that if gas limit was 100, I could spend 99 gas running wasm, and 99 gas accessing storage and thus spend 198 gas before triggering an out-of-gas panic. So, failure is right, but it should fail earlier. There is an open pull request in wasmer that will make this much easier. The use of gas in the sdk storage can reduce the limit left on the contract, while the contract keeps track of gas_used. Unless there is a strong reason otherwise, I would leave the current behavior (with mild DoS escalation potential) and fix it properly when the "dynamic limit" PR lands. There is also another open PR to make gas limits stricter in wasmer, so it would be nice to tackle them both together to really tighten up any leaks before putting this in production. (Not that either can do anything horrible or halt the chain, but they can be used as a multiplier to slow down the system beyond the gas paid). |
To fully solve this issue several components are required:
From what i can tell, today the code does not correctly represent contract-out-of-gas scenarios at all. It also invokes UB (because of cross-language stack unwinding) on sdk-out-of-gas events. PR #67 currently fixes the UB, but makes the out-of-gas event more opaque than it was, until the above changes are implemented. |
That does make the panics more secure, but to solve this info we must somehow update (reduce) the remaining gas allowed for the wasmer instance after each call out to the go storage code. Currently, the go panic unwrapping over rust then back to go does seem to work. In fact, we do test this case: https://github.com/CosmWasm/wasmd/blob/master/x/wasm/internal/keeper/keeper_test.go#L420-L436 Currently this just "magically" works, and yes, it is relying on UB. The above suggestion would correctly implement this in a more stable way, but not change the functionality. In fact, I am happy for the above PR, but really it is a different issue. This issue requires updating |
This isn't finished yet, we just decided to merge the progress in the PR mentioned here. |
Actually, I think it would be interesting to change the API interfaces to return something like:
We no longer need to worry about OutOfGas error as long as we return proper gas prices, the wasm contract should hit the out of gas state before (and simultaneous to) the runtime. And we never get double charges, cuz if we used the gas in the sdk storage, we will never spend the same gas in the contract (and this also tightens up abuse of the api calls). |
The limit vs used gas measurement should be in the API as it is correct, but it not yet supported in wasmer. Here is a 6 month old PR that seems to have stagnated, but I will try to revive it: wasmerio/wasmer#996 |
Three notes:
|
Yes, we can handle out of gas as you plan to, this adds finer-grain measurements
I am not sure for the storage usage. I think it would require more arguments in the Simply deducting gas (incrementing I agree not everything is in place, but adding a gas price to each api call and then deducting it should allow us to block one abuse potential and test out this gas metering in the easiest location (no deep integration with cosmos sdk gas metering) |
I don't know the Cosmos APIs that well, but if there's a way to query the gas-used-so-far in the SDK, then we can measure it before and after the storage access to deduce the amount of gas it consumed. |
If we have access to We pass in a Thus, trying with api first (which doesn't require such hoops, just a return value), would be a nice step to see how to do it, then we can do it fully. |
Those PRs were merged and this is now done! Thanks @reuvenpo |
Modified arguments in txs
Suppose
gas limit
passed in wasm is 100, then amount of gas used for instructions is 90 when call function in contract, and reading or writing storage consumed 30 gas which was deducted from the context gas limit in the call for cotract. The call can be excuted finally because thegas limit
(100) is greater than which needed(90). But now the context gas limit is 70. So it will failed when callconsumeGas(ctx, res.GasUsed)
. I think it maybe not be a bug, but it will increase the failure rate of calling contract.The text was updated successfully, but these errors were encountered: