This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Set the block gas limit to the value returned by a contract call #10928
Merged
ordian
merged 2 commits into
openethereum:master
from
poanetwork:poanetwork-block-gas-limit
Jan 13, 2020
Merged
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
description = "A crate to interact with the block gas limit contract" | ||
name = "block-gas-limit" | ||
version = "0.1.0" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
edition = "2018" | ||
license = "GPL-3.0" | ||
|
||
[dependencies] | ||
client-traits = { path = "../client-traits" } | ||
common-types = { path = "../types" } | ||
ethabi = "9.0.1" | ||
ethabi-derive = "9.0.1" | ||
ethabi-contract = "9.0.0" | ||
ethereum-types = "0.8.0" | ||
log = "0.4" | ||
|
||
[dev-dependencies] | ||
ethcore = { path = "..", features = ["test-helpers"] } | ||
spec = { path = "../spec" } |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[ | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "blockGasLimit", | ||
"outputs": [ | ||
{ | ||
"name": "", | ||
"type": "uint256" | ||
} | ||
], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2015-2019 Parity Technologies (UK) Ltd. | ||
// This file is part of Parity Ethereum. | ||
|
||
// Parity Ethereum is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity Ethereum is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! A client interface for interacting with the block gas limit contract. | ||
|
||
use client_traits::BlockChainClient; | ||
use common_types::{header::Header, ids::BlockId}; | ||
use ethabi::FunctionOutputDecoder; | ||
use ethabi_contract::use_contract; | ||
use ethereum_types::{Address, U256}; | ||
use log::{debug, error}; | ||
|
||
use_contract!(contract, "res/block_gas_limit.json"); | ||
|
||
pub fn block_gas_limit(full_client: &dyn BlockChainClient, header: &Header, address: Address) -> Option<U256> { | ||
let (data, decoder) = contract::functions::block_gas_limit::call(); | ||
let value = full_client.call_contract(BlockId::Hash(*header.parent_hash()), address, data).map_err(|err| { | ||
error!(target: "block_gas_limit", "Contract call failed. Not changing the block gas limit. {:?}", err); | ||
}).ok()?; | ||
if value.is_empty() { | ||
debug!(target: "block_gas_limit", "Contract call returned nothing. Not changing the block gas limit."); | ||
None | ||
} else { | ||
decoder.decode(&value).ok() | ||
} | ||
} |
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 |
---|---|---|
|
@@ -61,6 +61,14 @@ pub fn verify_block_basic(block: &Unverified, engine: &dyn Engine, check_seal: b | |
} | ||
} | ||
|
||
if let Some(gas_limit) = engine.gas_limit_override(&block.header) { | ||
if *block.header.gas_limit() != gas_limit { | ||
return Err(From::from(BlockError::InvalidGasLimit( | ||
OutOfBounds { min: Some(gas_limit), max: Some(gas_limit), found: *block.header.gas_limit() } | ||
))); | ||
} | ||
} | ||
|
||
for t in &block.transactions { | ||
engine.verify_transaction_basic(t, &block.header)?; | ||
} | ||
|
@@ -284,22 +292,24 @@ pub(crate) fn verify_header_params(header: &Header, engine: &dyn Engine, check_s | |
found: *header.gas_used() | ||
}))); | ||
} | ||
let min_gas_limit = engine.params().min_gas_limit; | ||
if header.gas_limit() < &min_gas_limit { | ||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { | ||
min: Some(min_gas_limit), | ||
max: None, | ||
found: *header.gas_limit() | ||
}))); | ||
} | ||
if let Some(limit) = engine.maximum_gas_limit() { | ||
if header.gas_limit() > &limit { | ||
if engine.gas_limit_override(header).is_none() { | ||
let min_gas_limit = engine.min_gas_limit(); | ||
if header.gas_limit() < &min_gas_limit { | ||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { | ||
min: None, | ||
max: Some(limit), | ||
min: Some(min_gas_limit), | ||
max: None, | ||
found: *header.gas_limit() | ||
}))); | ||
} | ||
if let Some(limit) = engine.maximum_gas_limit() { | ||
if header.gas_limit() > &limit { | ||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { | ||
min: None, | ||
max: Some(limit), | ||
found: *header.gas_limit() | ||
}))); | ||
} | ||
} | ||
} | ||
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. Would it be better to keep these checks, whenever the gas limit contract returns 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. They've moved to line 331 below. |
||
let maximum_extra_data_size = engine.maximum_extra_data_size(); | ||
if header.number() != 0 && header.extra_data().len() > maximum_extra_data_size { | ||
|
@@ -358,8 +368,6 @@ fn verify_parent(header: &Header, parent: &Header, engine: &dyn Engine) -> Resul | |
assert!(header.parent_hash().is_zero() || &parent.hash() == header.parent_hash(), | ||
"Parent hash should already have been verified; qed"); | ||
|
||
let gas_limit_divisor = engine.params().gas_limit_bound_divisor; | ||
|
||
if !engine.is_timestamp_valid(header.timestamp(), parent.timestamp()) { | ||
let now = SystemTime::now(); | ||
let min = CheckedSystemTime::checked_add(now, Duration::from_secs(parent.timestamp().saturating_add(1))) | ||
|
@@ -382,16 +390,18 @@ fn verify_parent(header: &Header, parent: &Header, engine: &dyn Engine) -> Resul | |
found: header.number() | ||
}).into()); | ||
} | ||
|
||
let parent_gas_limit = *parent.gas_limit(); | ||
let min_gas = parent_gas_limit - parent_gas_limit / gas_limit_divisor; | ||
let max_gas = parent_gas_limit + parent_gas_limit / gas_limit_divisor; | ||
if header.gas_limit() <= &min_gas || header.gas_limit() >= &max_gas { | ||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { | ||
min: Some(min_gas), | ||
max: Some(max_gas), | ||
found: *header.gas_limit() | ||
}))); | ||
if engine.gas_limit_override(header).is_none() { | ||
let gas_limit_divisor = engine.params().gas_limit_bound_divisor; | ||
let parent_gas_limit = *parent.gas_limit(); | ||
let min_gas = parent_gas_limit - parent_gas_limit / gas_limit_divisor; | ||
let max_gas = parent_gas_limit + parent_gas_limit / gas_limit_divisor; | ||
if header.gas_limit() <= &min_gas || header.gas_limit() >= &max_gas { | ||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { | ||
min: Some(min_gas), | ||
max: Some(max_gas), | ||
found: *header.gas_limit() | ||
}))); | ||
} | ||
} | ||
|
||
Ok(()) | ||
|
@@ -522,7 +532,7 @@ mod tests { | |
// that's an invalid transaction list rlp | ||
let invalid_transactions = vec![vec![0u8]]; | ||
header.set_transactions_root(ordered_trie_root(&invalid_transactions)); | ||
header.set_gas_limit(engine.params().min_gas_limit); | ||
header.set_gas_limit(engine.min_gas_limit()); | ||
rlp.append(&header); | ||
rlp.append_list::<Vec<u8>, _>(&invalid_transactions); | ||
rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); | ||
|
@@ -541,7 +551,7 @@ mod tests { | |
let spec = spec::new_test(); | ||
let engine = &*spec.engine; | ||
|
||
let min_gas_limit = engine.params().min_gas_limit; | ||
let min_gas_limit = engine.min_gas_limit(); | ||
good.set_gas_limit(min_gas_limit); | ||
good.set_timestamp(40); | ||
good.set_number(10); | ||
|
Oops, something went wrong.
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.
So I think I made this suggestion before but perhaps it got lost.
In order to keep the
Engine
trait from growing ever fatter, how about we have afn gas_limits(&self) -> Option<(U256, U256)>
that return the min/max limit. The default impl could return(self.params().min_gas_limit, self.params().max_gas_limit)
and then Aura can provide its own impl with the limit override logic tucked away nicely. This would require refactoring other code around the project but lets us removemaximum_gas_limit(&self)
and saves us from adding two new methods.Not sure if you tried this and it doesn't work or if you feel it's a bad idea?
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.
Sorry, I missed that!
I'm not sure about this idea: When the engine changes the gas limit, we don't want to do these checks:
At the moment, it seems to be impossible to produce a valid block if the interval based on the gas limit divisor doesn't overlap with the interval based on the engine's min and max gas limits.
Maybe engines that use a gas limit contract should just use a gas limit divisor of 1, but that would still prevent them from more than doubling the gas limit from one block to the next.
But we could make
gas_limits
return abool
as well, that tells us whether it's an override. Or it could return an enum?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.
Hmm, so why do you need to know that there was an override? Why isn't it enough to know what the bounds must be at that block? In this PR there are 3 error cases: gas too low, too high, overridden but mismatching – why is it important to know that the error came from an override?
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.
There seem to be two different intervals for the block gas limit:
The second limitation can conflict with the first, if the engine makes sudden changes to the gas limit. So I think it should be disabled if there is an override.
I'm not really sure at all what's the best way to approach this. To give you some context, the gist is:
In POSDAO, there will be rather expensive implicit calls to the governance contracts once per week, to distribute rewards and select new validators. While these are ongoing, we want to limit the gas available for transactions, so that the total CPU usage per block remains roughly constant. (For details see: poanetwork#119 (comment))