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

Use signed 256-bit integer for sstore gas refund substate #9746

Merged
merged 6 commits into from
Oct 15, 2018

Conversation

sorpaas
Copy link
Collaborator

@sorpaas sorpaas commented Oct 14, 2018

While the overall transaction sstore gas refund cannot go below zero, on an individual execution frame it can. This replaces saturating_sub by a signed integer version to fix the issue.

@sorpaas sorpaas added A0-pleasereview 🤓 Pull request needs code review. P0-dropeverything 🌋 Everyone should address the issue now. B1-patch-beta 🕷🕷 M4-core ⛓ Core client code / Rust. B0-patch-stable 🕷 Pull request should also be back-ported to the stable branch. labels Oct 14, 2018
@sorpaas sorpaas added this to the 2.2 milestone Oct 14, 2018
fn add_assign(&mut self, other: U256) {
match self.0 {
Sign::Positive => {
self.1 += other;
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we care about overflows/panics here (and in other cases/SubAssign)?

@5chdn 5chdn added the B9-blocker 🚧 This pull request blocks the next release from happening. Use only in extreme cases. label Oct 15, 2018
@@ -1091,7 +1091,8 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
let schedule = self.schedule;

// refunds from SSTORE nonzero -> zero
let sstore_refunds = substate.sstore_clears_refund;
assert!(substate.sstore_clears_refund.is_nonnegative(), "On transaction level, sstore clears refund cannot go below zero.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Setting to 0 in case of negative plus a trace could be another way to do it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would rather panic here -- our recent experience shows that it's easier to find consensus bug this way, and if negative ever happens (which we do have some informal proof that it shouldn't), then the network is broken anyway!


/// Representation of a signed 256-bit integer.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct I256(Sign, U256);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not call it I256 but U256_And_Signed or anything that shows that we are not on 256bit only.

ethcore/src/externalities.rs Show resolved Hide resolved
@@ -1091,7 +1091,8 @@ impl<'a, B: 'a + StateBackend> Executive<'a, B> {
let schedule = self.schedule;

// refunds from SSTORE nonzero -> zero
let sstore_refunds = substate.sstore_clears_refund;
assert!(substate.sstore_clears_refund >= 0, "On transaction level, sstore clears refund cannot go below zero.");
let sstore_refunds = U256::from(substate.sstore_clears_refund as u64);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i128::max_value() as u64 == u64::max_value(), and we checked above that it's always non-negative.

@cheme
Copy link
Contributor

cheme commented Oct 15, 2018

Seems good using i128, but I would also switch sstore_refund and others usize parameters to explicit u64.

@sorpaas
Copy link
Collaborator Author

sorpaas commented Oct 15, 2018

@cheme The issue is that all our gas definitions use usize. So if we do that it means we do another casting from usize to u64.

@cheme
Copy link
Contributor

cheme commented Oct 15, 2018

@sorpaas yes it may be to much a change for this pr and is not strictly required (but it is something I am considering for instance if we want at sometime to compile the interpreter to wasm this usize definition is problematic).

@sorpaas sorpaas merged commit 5319d33 into master Oct 15, 2018
@sorpaas sorpaas deleted the sp-signed-refund branch October 15, 2018 09:10
sorpaas added a commit that referenced this pull request Oct 15, 2018
* Add signed refund

* Use signed 256-bit integer for sstore gas refund substate

* Fix tests

* Remove signed mod and use i128 directly

* Fix evm test case casting

* Fix jsontests ext signature
sorpaas added a commit that referenced this pull request Oct 15, 2018
* Add signed refund

* Use signed 256-bit integer for sstore gas refund substate

* Fix tests

* Remove signed mod and use i128 directly

* Fix evm test case casting

* Fix jsontests ext signature
5chdn added a commit that referenced this pull request Oct 15, 2018
* parity-version: mark 2.0.8 stable as critical

* Use signed 256-bit integer for sstore gas refund substate  (#9746)

* Add signed refund

* Use signed 256-bit integer for sstore gas refund substate

* Fix tests

* Remove signed mod and use i128 directly

* Fix evm test case casting

* Fix jsontests ext signature

* Add --force to cargo audit install script (#9735)

* heads ref not present for branches beta and stable (#9741)

* aura: fix panic on extra_info with unsealed block (#9755)

* aura: fix panic when unsealed block passed to extra_info

* aura: use hex formatting for EmptyStep hashes

* aura: add test for extra_info
5chdn added a commit that referenced this pull request Oct 15, 2018
* parity-version: mark 2.1.3 beta as critical

* Use signed 256-bit integer for sstore gas refund substate  (#9746)

* Add signed refund

* Use signed 256-bit integer for sstore gas refund substate

* Fix tests

* Remove signed mod and use i128 directly

* Fix evm test case casting

* Fix jsontests ext signature

* Add --force to cargo audit install script (#9735)

* heads ref not present for branches beta and stable (#9741)

* aura: fix panic on extra_info with unsealed block (#9755)

* aura: fix panic when unsealed block passed to extra_info

* aura: use hex formatting for EmptyStep hashes

* aura: add test for extra_info
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-pleasereview 🤓 Pull request needs code review. B0-patch-stable 🕷 Pull request should also be back-ported to the stable branch. B9-blocker 🚧 This pull request blocks the next release from happening. Use only in extreme cases. M4-core ⛓ Core client code / Rust. P0-dropeverything 🌋 Everyone should address the issue now.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants