From 148b7fe17faca8ba3695d450d1b061b915dca95a Mon Sep 17 00:00:00 2001 From: debris Date: Sun, 21 Jul 2019 15:42:07 +0200 Subject: [PATCH 1/2] simplify BlockReward::reward implementation --- ethcore/src/engines/authority_round/mod.rs | 2 +- ethcore/src/engines/block_reward.rs | 32 +++++----------------- ethcore/src/engines/ethash.rs | 2 +- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 30df44eb41c..951e66a2d32 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -1273,7 +1273,7 @@ impl Engine for AuthorityRound { Some(ref c) if block.header.number() >= self.block_reward_contract_transition => { let mut call = super::default_system_or_code_call(&self.machine, block); - let rewards = c.reward(&beneficiaries, &mut call)?; + let rewards = c.reward(beneficiaries, &mut call)?; rewards.into_iter().map(|(author, amount)| (author, RewardKind::External, amount)).collect() }, _ => { diff --git a/ethcore/src/engines/block_reward.rs b/ethcore/src/engines/block_reward.rs index 908fc9ceb87..a1a40cb8763 100644 --- a/ethcore/src/engines/block_reward.rs +++ b/ethcore/src/engines/block_reward.rs @@ -17,9 +17,8 @@ //! A module with types for declaring block rewards and a client interface for interacting with a //! block reward contract. -use ethabi; -use ethabi::ParamType; -use ethereum_types::{H160, Address, U256}; +use ethabi::FunctionOutputDecoder; +use ethereum_types::{Address, U256}; use std::sync::Arc; use hash::keccak; @@ -112,44 +111,27 @@ impl BlockRewardContract { /// `machine.execute_as_system`). pub fn reward( &self, - beneficiaries: &[(Address, RewardKind)], + beneficiaries: Vec<(Address, RewardKind)>, caller: &mut SystemOrCodeCall, ) -> Result, Error> { - let input = block_reward_contract::functions::reward::encode_input( - beneficiaries.iter().map(|&(address, _)| H160::from(address)), - beneficiaries.iter().map(|&(_, ref reward_kind)| u16::from(*reward_kind)), - ); + let (addresses, rewards): (Vec<_>, Vec<_>) = beneficiaries.into_iter().unzip(); + let (input, decoder) = block_reward_contract::functions::reward::call(addresses, rewards.into_iter().map(u16::from)); let output = caller(self.kind.clone(), input) .map_err(Into::into) .map_err(EngineError::FailedSystemCall)?; - // since this is a non-constant call we can't use ethabi's function output - // deserialization, sadness ensues. - let types = &[ - ParamType::Array(Box::new(ParamType::Address)), - ParamType::Array(Box::new(ParamType::Uint(256))), - ]; - - let tokens = ethabi::decode(types, &output) + let (addresses, rewards) = decoder.decode(&output) .map_err(|err| err.to_string()) .map_err(EngineError::FailedSystemCall)?; - assert!(tokens.len() == 2); - - let addresses = tokens[0].clone().to_array().expect("type checked by ethabi::decode; qed"); - let rewards = tokens[1].clone().to_array().expect("type checked by ethabi::decode; qed"); - if addresses.len() != rewards.len() { return Err(EngineError::FailedSystemCall( "invalid data returned by reward contract: both arrays must have the same size".into() ).into()); } - let addresses = addresses.into_iter().map(|t| t.to_address().expect("type checked by ethabi::decode; qed")); - let rewards = rewards.into_iter().map(|t| t.to_uint().expect("type checked by ethabi::decode; qed")); - - Ok(addresses.zip(rewards).collect()) + Ok(addresses.into_iter().zip(rewards.into_iter()).collect()) } } diff --git a/ethcore/src/engines/ethash.rs b/ethcore/src/engines/ethash.rs index d25bed59b05..21aeca87c99 100644 --- a/ethcore/src/engines/ethash.rs +++ b/ethcore/src/engines/ethash.rs @@ -264,7 +264,7 @@ impl Engine for Arc { let mut call = engines::default_system_or_code_call(&self.machine, block); - let rewards = c.reward(&beneficiaries, &mut call)?; + let rewards = c.reward(beneficiaries, &mut call)?; rewards.into_iter().map(|(author, amount)| (author, RewardKind::External, amount)).collect() }, _ => { From ea9f2cf939694f3a66eb3ee8a61b197db0fd9d8b Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 29 Jul 2019 16:39:39 +0200 Subject: [PATCH 2/2] fixed failing tests --- ethcore/src/engines/block_reward.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethcore/src/engines/block_reward.rs b/ethcore/src/engines/block_reward.rs index a1a40cb8763..8df88d9f282 100644 --- a/ethcore/src/engines/block_reward.rs +++ b/ethcore/src/engines/block_reward.rs @@ -204,7 +204,7 @@ mod test { }; // if no beneficiaries are given no rewards are attributed - assert!(block_reward_contract.reward(&vec![], &mut call).unwrap().is_empty()); + assert!(block_reward_contract.reward(vec![], &mut call).unwrap().is_empty()); // the contract rewards (1000 + kind) for each benefactor let beneficiaries = vec![ @@ -213,7 +213,7 @@ mod test { (Address::from_str("0000000000000000000000000000000000000035").unwrap(), RewardKind::EmptyStep), ]; - let rewards = block_reward_contract.reward(&beneficiaries, &mut call).unwrap(); + let rewards = block_reward_contract.reward(beneficiaries, &mut call).unwrap(); let expected = vec![ (Address::from_str("0000000000000000000000000000000000000033").unwrap(), U256::from(1000)), (Address::from_str("0000000000000000000000000000000000000034").unwrap(), U256::from(1000 + 101)),