From 08a6ce9642eaa188072b469553e6587f0eeab44a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 11 Nov 2016 11:56:58 +0100 Subject: [PATCH] Restrict max code size to 23999 bytes for EIP-150 and after. --- ethcore/src/ethereum/ethash.rs | 12 ++++++++---- ethcore/src/evm/schedule.rs | 6 +++++- ethcore/src/externalities.rs | 2 +- json/src/spec/ethash.rs | 4 ++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index c3ced105b..580a1149e 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -62,6 +62,8 @@ pub struct EthashParams { pub eip161abc_transition: u64, /// Number of first block where EIP-161.d begins. pub eip161d_transition: u64, + /// Maximum amount of code that can be deploying into a contract. + pub max_code_size: u64, } impl From for EthashParams { @@ -74,17 +76,18 @@ impl From for EthashParams { block_reward: p.block_reward.into(), registrar: p.registrar.map_or_else(Address::new, Into::into), homestead_transition: p.homestead_transition.map_or(0, Into::into), - dao_hardfork_transition: p.dao_hardfork_transition.map_or(0x7fffffffffffffff, Into::into), + dao_hardfork_transition: p.dao_hardfork_transition.map_or(u64::max_value(), Into::into), dao_hardfork_beneficiary: p.dao_hardfork_beneficiary.map_or_else(Address::new, Into::into), dao_hardfork_accounts: p.dao_hardfork_accounts.unwrap_or_else(Vec::new).into_iter().map(Into::into).collect(), - difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(0x7fffffffffffffff, Into::into), + difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(u64::max_value(), Into::into), difficulty_hardfork_bound_divisor: p.difficulty_hardfork_bound_divisor.map_or(p.difficulty_bound_divisor.into(), Into::into), - bomb_defuse_transition: p.bomb_defuse_transition.map_or(0x7fffffffffffffff, Into::into), + bomb_defuse_transition: p.bomb_defuse_transition.map_or(u64::max_value(), Into::into), eip150_transition: p.eip150_transition.map_or(0, Into::into), eip155_transition: p.eip155_transition.map_or(0, Into::into), eip160_transition: p.eip160_transition.map_or(0, Into::into), eip161abc_transition: p.eip161abc_transition.map_or(0, Into::into), - eip161d_transition: p.eip161d_transition.map_or(0x7fffffffffffffff, Into::into), + eip161d_transition: p.eip161d_transition.map_or(u64::max_value(), Into::into), + max_code_size: p.max_code_size.map_or(u64::max_value(), Into::into), } } } @@ -137,6 +140,7 @@ impl Engine for Ethash { Schedule::new_homestead() } else { Schedule::new_post_eip150( + self.ethash_params.max_code_size as usize, env_info.number >= self.ethash_params.eip160_transition, env_info.number >= self.ethash_params.eip161abc_transition, env_info.number >= self.ethash_params.eip161d_transition diff --git a/ethcore/src/evm/schedule.rs b/ethcore/src/evm/schedule.rs index b68f6acb5..773708956 100644 --- a/ethcore/src/evm/schedule.rs +++ b/ethcore/src/evm/schedule.rs @@ -70,6 +70,8 @@ pub struct Schedule { pub quad_coeff_div: usize, /// Cost for contract length when executing `CREATE` pub create_data_gas: usize, + /// Maximum code size when creating a contract. + pub create_data_limit: usize, /// Transaction cost pub tx_gas: usize, /// `CREATE` transaction cost @@ -111,7 +113,7 @@ impl Schedule { } /// Schedule for the post-EIP-150-era of the Ethereum main net. - pub fn new_post_eip150(fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule { + pub fn new_post_eip150(max_code_size: usize, fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule { Schedule { exceptional_failed_code_deposit: true, have_delegate_call: true, @@ -139,6 +141,7 @@ impl Schedule { memory_gas: 3, quad_coeff_div: 512, create_data_gas: 200, + create_data_limit: max_code_size, tx_gas: 21000, tx_create_gas: 53000, tx_data_zero_gas: 4, @@ -183,6 +186,7 @@ impl Schedule { memory_gas: 3, quad_coeff_div: 512, create_data_gas: 200, + create_data_limit: usize::max_value(), tx_gas: 21000, tx_create_gas: tcg, tx_data_zero_gas: 4, diff --git a/ethcore/src/externalities.rs b/ethcore/src/externalities.rs index 1e9f82fcb..918b35d4b 100644 --- a/ethcore/src/externalities.rs +++ b/ethcore/src/externalities.rs @@ -241,7 +241,7 @@ impl<'a, T, V> Ext for Externalities<'a, T, V> where T: 'a + Tracer, V: 'a + VMT }, OutputPolicy::InitContract(ref mut copy) => { let return_cost = U256::from(data.len()) * U256::from(self.schedule.create_data_gas); - if return_cost > *gas { + if return_cost > *gas || data.len() > self.schedule.create_data_limit { return match self.schedule.exceptional_failed_code_deposit { true => Err(evm::Error::OutOfGas), false => Ok(*gas) diff --git a/json/src/spec/ethash.rs b/json/src/spec/ethash.rs index 841debcec..998656380 100644 --- a/json/src/spec/ethash.rs +++ b/json/src/spec/ethash.rs @@ -78,6 +78,10 @@ pub struct EthashParams { /// See main EthashParams docs. #[serde(rename="eip161dTransition")] pub eip161d_transition: Option, + + /// See main EthashParams docs. + #[serde(rename="maxCodeSize")] + pub max_code_size: Option, } /// Ethash engine deserialization.