Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow to distinguish out of gas from other traps #4883

Merged
merged 3 commits into from
Feb 14, 2020
Merged

Conversation

athei
Copy link
Member

@athei athei commented Feb 10, 2020

Description
When a contract encounters a runtime error a wasm trap is
triggered and the execution is halted. Currently, no matter
what was the cause for the trap it is always reported as:
DispatchError::Other("contract trapped during execution").

However, the trap that is triggered if a contract exhausts
its gas budget is particulary interesting. Therefore we add
a seperate error message for this cause:
DispatchError::Other("ran out of gas during contract execution").

A test is added hat executes a contract that never terminates.
Therefore it always exhausts is gas budget.

fixes #2584

Open Issues

  • I am not sure whether to bump impl_versionor spec_version.

spec_version should be bumped

  • The field of the Other error variant (the error string) seems not to be passed through to the PolkadotAppsUI which renders this improvement rather unsatisfying:
    extrinsic_failed

This is OK for now

@athei athei added A0-please_review Pull request needs code review. M8-contracts labels Feb 10, 2020
@athei athei requested a review from pepyakin February 10, 2020 22:38
@parity-cla-bot
Copy link

It looks like @athei hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement at the following URL: https://cla.parity.io

Once you've signed, please reply to this thread with [clabot:check] to prove it.

Many thanks,

Parity Technologies CLA Bot

@athei
Copy link
Member Author

athei commented Feb 10, 2020

[clabot:check]

@parity-cla-bot
Copy link

It looks like @athei signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

Copy link
Contributor

@pepyakin pepyakin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start! I have only a few nits.

frame/contracts/src/tests.rs Outdated Show resolved Hide resolved
@@ -300,7 +318,7 @@ define_env!(Env, <E: Ext>,
//
// - amount: How much gas is used.
gas(ctx, amount: u32) => {
charge_gas(&mut ctx.gas_meter, ctx.schedule, RuntimeToken::Explicit(amount))?;
charge_gas(&mut ctx.gas_meter, ctx.schedule, &mut ctx.special_trap, RuntimeToken::Explicit(amount))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (an a few other) line exceeds the soft limit of 100. It would be great if we wrapped this line, like

charge_gas(
    &mut ctx.gas_meter,
    ctx.schedule,
    &mut ctx.special_trap,
    RuntimeToken::Explicit(amount)
)?;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into the overlong lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the long lines I added / mofified.

@@ -39,6 +39,7 @@ const TRAP_RETURN_CODE: u32 = 0x0100;
enum SpecialTrap {
/// Signals that trap was generated in response to call `ext_return` host function.
Return(Vec<u8>),
OutOfGas,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be useful to leave some words about this variant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment

@athei
Copy link
Member Author

athei commented Feb 12, 2020

Any idea about the Open Issues part in my PR description?

@pepyakin
Copy link
Contributor

Ah yeah sorry. Yes, since you change the runtime behavior you need to bump the spec version as well. Otherwise, clients with older native runtime compiled in will execute that stale code rather than the wasm version under some circumstances.

The second concern is valid, but I believe we cannot do anything with it atm.

@athei
Copy link
Member Author

athei commented Feb 12, 2020

The second concern is valid, but I believe we cannot do anything with it atm.

I guess the change would be to the javascript? I do not even see the extrinsic error on the node console. Is this intended or do I change the loglevel? The only way that I see that the change is working is that the test succeeds.

@pepyakin
Copy link
Contributor

I think yeah you need to change the log level, try RUST_LOG=runtime=trace.

@xlc
Copy link
Contributor

xlc commented Feb 13, 2020

This fixes #2584

When a contract encounters a runtime error a wasm trap is
triggered and the execution is halted. Currently, no matter
what was the cause for the trap it is always reported as:
DispatchError::Other("contract trapped during execution").

However, the trap that is triggered if a contract exhausts
its gas budget is particulary interesting. Therefore we add
a seperate error message for this cause:
DispatchError::Other("ran out of gas during contract execution").

A test is added hat executes a contract that never terminates.
Therefore it always exhausts is gas budget.
@pepyakin pepyakin merged commit a6e7c05 into master Feb 14, 2020
@pepyakin pepyakin deleted the athei-out-of-gas branch February 14, 2020 10:46
General-Beck pushed a commit to General-Beck/substrate that referenced this pull request Feb 18, 2020
* contracts: Allow to distinguish out of gas from other traps

When a contract encounters a runtime error a wasm trap is
triggered and the execution is halted. Currently, no matter
what was the cause for the trap it is always reported as:
DispatchError::Other("contract trapped during execution").

However, the trap that is triggered if a contract exhausts
its gas budget is particulary interesting. Therefore we add
a seperate error message for this cause:
DispatchError::Other("ran out of gas during contract execution").

A test is added hat executes a contract that never terminates.
Therefore it always exhausts is gas budget.

* fixup! contracts: Allow to distinguish out of gas from other traps

Remove overlong lines.

* fixup! contracts: Allow to distinguish out of gas from other traps

Rename Contract to Contracts
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is it possible to tell if a contract execution error is caused by out of gas?
6 participants