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

Set the block gas limit to the value returned by a contract call #10928

Merged
merged 2 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions ethcore/block-gas-limit/Cargo.toml
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" }
16 changes: 16 additions & 0 deletions ethcore/block-gas-limit/res/block_gas_limit.json
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"
}
]
39 changes: 39 additions & 0 deletions ethcore/block-gas-limit/src/lib.rs
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()
}
}
11 changes: 11 additions & 0 deletions ethcore/engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ pub trait Engine: Sync + Send {
Ok(*header.author())
}

/// Overrides the block gas limit. Whenever this returns `Some` for a header, the next block's gas limit must be
/// exactly that value.
fn gas_limit_override(&self, _header: &Header) -> Option<U256> {
Copy link
Collaborator

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 a fn 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 remove maximum_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?

Copy link
Contributor

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 a bool as well, that tells us whether it's an override. Or it could return an enum?

enum GasLimits {
    MinMax(U256, U256),
    Override(U256),
}

Copy link
Collaborator

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?

Copy link
Contributor

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))

None
}

/// Get the general parameters of the chain.
fn params(&self) -> &CommonParams;

Expand Down Expand Up @@ -397,6 +403,11 @@ pub trait Engine: Sync + Send {
fn decode_transaction(&self, transaction: &[u8]) -> Result<UnverifiedTransaction, transaction::Error> {
self.machine().decode_transaction(transaction)
}

/// The configured minimum gas limit.
fn min_gas_limit(&self) -> U256 {
self.params().min_gas_limit
}
}

/// Verifier for all blocks within an epoch with self-contained state.
Expand Down
1 change: 1 addition & 0 deletions ethcore/engines/authority-round/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2018"
license = "GPL-3.0"

[dependencies]
block-gas-limit = { path = "../../block-gas-limit" }
block-reward = { path = "../../block-reward" }
client-traits = { path = "../../client-traits" }
common-types = { path = "../../types" }
Expand Down
55 changes: 54 additions & 1 deletion ethcore/engines/authority-round/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use std::u64;

use client_traits::{EngineClient, ForceUpdateSealing, TransactionRequest};
use engine::{Engine, ConstructedVerifier};
use block_gas_limit::block_gas_limit;
use block_reward::{self, BlockRewardContract, RewardKind};
use ethjson;
use machine::{
Expand All @@ -51,6 +52,7 @@ use machine::{
use macros::map;
use keccak_hash::keccak;
use log::{info, debug, error, trace, warn};
use lru_cache::LruCache;
use engine::signer::EngineSigner;
use parity_crypto::publickey::Signature;
use io::{IoContext, IoHandler, TimerToken, IoService};
Expand Down Expand Up @@ -78,7 +80,6 @@ use common_types::{
transaction::SignedTransaction,
};
use unexpected::{Mismatch, OutOfBounds};

use validator_set::{ValidatorSet, SimpleList, new_validator_set};

mod finality;
Expand Down Expand Up @@ -124,10 +125,16 @@ pub struct AuthorityRoundParams {
pub strict_empty_steps_transition: u64,
/// If set, enables random number contract integration. It maps the transition block to the contract address.
pub randomness_contract_address: BTreeMap<u64, Address>,
/// The addresses of contracts that determine the block gas limit with their associated block
/// numbers.
pub block_gas_limit_contract_transitions: BTreeMap<u64, Address>,
}

const U16_MAX: usize = ::std::u16::MAX as usize;

/// The number of recent block hashes for which the gas limit override is memoized.
const GAS_LIMIT_OVERRIDE_CACHE_CAPACITY: usize = 10;

impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
fn from(p: ethjson::spec::AuthorityRoundParams) -> Self {
let map_step_duration = |u: ethjson::uint::Uint| {
Expand Down Expand Up @@ -180,6 +187,12 @@ impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
(block.as_u64(), addr.into())
}).collect()
});
let block_gas_limit_contract_transitions: BTreeMap<_, _> =
p.block_gas_limit_contract_transitions
.unwrap_or_default()
.into_iter()
.map(|(block_num, address)| (block_num.into(), address.into()))
.collect();
AuthorityRoundParams {
step_durations,
validators: new_validator_set(p.validators),
Expand All @@ -196,6 +209,7 @@ impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
two_thirds_majority_transition: p.two_thirds_majority_transition.map_or_else(BlockNumber::max_value, Into::into),
strict_empty_steps_transition: p.strict_empty_steps_transition.map_or(0, Into::into),
randomness_contract_address,
block_gas_limit_contract_transitions,
}
}
}
Expand Down Expand Up @@ -565,6 +579,10 @@ pub struct AuthorityRound {
received_step_hashes: RwLock<BTreeMap<(u64, Address), H256>>,
/// If set, enables random number contract integration. It maps the transition block to the contract address.
randomness_contract_address: BTreeMap<u64, Address>,
/// The addresses of contracts that determine the block gas limit.
block_gas_limit_contract_transitions: BTreeMap<u64, Address>,
/// Memoized gas limit overrides, by block hash.
gas_limit_override_cache: Mutex<LruCache<H256, Option<U256>>>,
}

// header-chain validator.
Expand Down Expand Up @@ -867,6 +885,8 @@ impl AuthorityRound {
machine,
received_step_hashes: RwLock::new(Default::default()),
randomness_contract_address: our_params.randomness_contract_address,
block_gas_limit_contract_transitions: our_params.block_gas_limit_contract_transitions,
gas_limit_override_cache: Mutex::new(LruCache::new(GAS_LIMIT_OVERRIDE_CACHE_CAPACITY)),
});

// Do not initialize timeouts for tests.
Expand Down Expand Up @@ -1218,6 +1238,14 @@ impl Engine for AuthorityRound {

let score = calculate_score(parent_step, current_step, current_empty_steps_len);
header.set_difficulty(score);
if let Some(gas_limit) = self.gas_limit_override(header) {
trace!(target: "engine", "Setting gas limit to {} for block {}.", gas_limit, header.number());
let parent_gas_limit = *parent.gas_limit();
header.set_gas_limit(gas_limit);
if parent_gas_limit != gas_limit {
info!(target: "engine", "Block gas limit was changed from {} to {}.", parent_gas_limit, gas_limit);
}
}
}

fn sealing_state(&self) -> SealingState {
Expand Down Expand Up @@ -1834,6 +1862,30 @@ impl Engine for AuthorityRound {
fn params(&self) -> &CommonParams {
self.machine.params()
}

fn gas_limit_override(&self, header: &Header) -> Option<U256> {
let (_, &address) = self.block_gas_limit_contract_transitions.range(..=header.number()).last()?;
let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) {
Some(client) => client,
None => {
error!(target: "engine", "Unable to prepare block: missing client ref.");
return None;
}
};
let full_client = match client.as_full_client() {
Some(full_client) => full_client,
None => {
error!(target: "engine", "Failed to upgrade to BlockchainClient.");
return None;
}
};
if let Some(limit) = self.gas_limit_override_cache.lock().get_mut(&header.hash()) {
return *limit;
}
let limit = block_gas_limit(full_client, header, address);
self.gas_limit_override_cache.lock().insert(header.hash(), limit);
limit
}
}

/// A helper accumulator function mapping a step duration and a step duration transition timestamp
Expand Down Expand Up @@ -1910,6 +1962,7 @@ mod tests {
strict_empty_steps_transition: 0,
two_thirds_majority_transition: 0,
randomness_contract_address: BTreeMap::new(),
block_gas_limit_contract_transitions: BTreeMap::new(),
};

// mutate aura params
Expand Down
62 changes: 36 additions & 26 deletions ethcore/verification/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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()
})));
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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)))
Expand All @@ -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(())
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading