From 690b090b56660c73b73ef251d0a4c07cddf8d929 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Thu, 7 Jan 2021 20:37:44 +0100 Subject: [PATCH] Use Get for state root instead of EthereumExt (#264) --- Cargo.lock | 4 +++- frame/ethereum/src/lib.rs | 19 ++++++++++++++----- frame/ethereum/src/mock.rs | 7 ++----- frame/evm/precompile/ed25519/src/lib.rs | 3 +-- frame/evm/precompile/modexp/src/lib.rs | 1 - primitives/rpc/src/lib.rs | 7 ------- template/runtime/src/lib.rs | 10 +++++----- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17c3963d8..a12c605e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1185,7 +1185,6 @@ name = "fc-consensus" version = "0.1.0" dependencies = [ "derive_more", - "ethereum", "fp-consensus", "futures 0.3.7", "log", @@ -1352,6 +1351,7 @@ dependencies = [ "parity-scale-codec", "sp-api", "sp-core", + "sp-io", "sp-runtime", "sp-std", ] @@ -1490,6 +1490,8 @@ version = "2.0.0-dev" dependencies = [ "fc-consensus", "fc-rpc", + "fc-rpc-core", + "fp-consensus", "fp-rpc", "frontier-template-runtime", "futures 0.3.7", diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 4f66640a9..486b25212 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -41,10 +41,10 @@ use evm::ExitReason; use fp_evm::CallOrCreateInfo; use pallet_evm::{Runner, GasWeightMapping}; use sha3::{Digest, Keccak256}; -use codec::Encode; +use codec::{Encode, Decode}; use fp_consensus::{FRONTIER_ENGINE_ID, ConsensusLog}; -pub use fp_rpc::{TransactionStatus, EthereumExt}; +pub use fp_rpc::TransactionStatus; pub use ethereum::{Transaction, Log, Block, Receipt, TransactionAction, TransactionMessage}; #[cfg(all(feature = "std", test))] @@ -62,14 +62,23 @@ pub enum ReturnValue { /// A type alias for the balance type from this pallet's point of view. pub type BalanceOf = ::Balance; +pub struct IntermediateStateRoot; + +impl Get for IntermediateStateRoot { + fn get() -> H256 { + H256::decode(&mut &sp_io::storage::root()[..]) + .expect("Node is configured to use the same hash; qed") + } +} + /// Configuration trait for Ethereum pallet. pub trait Config: frame_system::Config + pallet_balances::Config + pallet_timestamp::Config + pallet_evm::Config { /// The overarching event type. type Event: From + Into<::Event>; /// Find author for Ethereum. type FindAuthor: FindAuthor; - /// User configurable functions. - type Extension: EthereumExt; + /// How Ethereum state root is calculated. + type StateRoot: Get; } decl_storage! { @@ -310,7 +319,7 @@ impl Module { nonce: H64::default(), }; let mut block = ethereum::Block::new(partial_header, transactions.clone(), ommers); - block.header.state_root = T::Extension::eth_state_root(); + block.header.state_root = T::StateRoot::get(); let mut transaction_hashes = Vec::new(); diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 31dd9de1c..1cb16cd54 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -18,7 +18,7 @@ //! Test utilities use super::*; -use crate::{Module, Config}; +use crate::{Module, Config, IntermediateStateRoot}; use ethereum::{TransactionAction, TransactionSignature}; use frame_support::{ impl_outer_origin, parameter_types, weights::Weight, ConsensusEngineId @@ -32,7 +32,6 @@ use sp_runtime::{ ModuleId, Perbill, }; use sp_runtime::AccountId32; -use fp_rpc::EthereumExt; impl_outer_origin! { pub enum Origin for Test where system = frame_system {} @@ -150,12 +149,10 @@ impl pallet_evm::Config for Test { type ChainId = ChainId; } -impl EthereumExt for Test {} - impl Config for Test { type Event = (); type FindAuthor = EthereumFindAuthor; - type Extension = Self; + type StateRoot = IntermediateStateRoot; } pub type System = frame_system::Module; diff --git a/frame/evm/precompile/ed25519/src/lib.rs b/frame/evm/precompile/ed25519/src/lib.rs index e5aba9099..858793674 100644 --- a/frame/evm/precompile/ed25519/src/lib.rs +++ b/frame/evm/precompile/ed25519/src/lib.rs @@ -20,7 +20,7 @@ extern crate alloc; use alloc::vec::Vec; -use core::{cmp::min, convert::TryFrom}; +use core::convert::TryFrom; use fp_evm::LinearCostPrecompile; use evm::{ExitSucceed, ExitError}; use ed25519_dalek::{PublicKey, Verifier, Signature}; @@ -153,4 +153,3 @@ mod tests { } } - diff --git a/frame/evm/precompile/modexp/src/lib.rs b/frame/evm/precompile/modexp/src/lib.rs index f7b8f493d..1a70c4b44 100644 --- a/frame/evm/precompile/modexp/src/lib.rs +++ b/frame/evm/precompile/modexp/src/lib.rs @@ -20,7 +20,6 @@ extern crate alloc; use alloc::vec::Vec; -use core::cmp::max; use fp_evm::LinearCostPrecompile; use evm::{ExitSucceed, ExitError}; use num::{BigUint, Zero, One, ToPrimitive, FromPrimitive}; diff --git a/primitives/rpc/src/lib.rs b/primitives/rpc/src/lib.rs index 1e6812df5..bdc2ed7e3 100644 --- a/primitives/rpc/src/lib.rs +++ b/primitives/rpc/src/lib.rs @@ -23,13 +23,6 @@ use ethereum_types::Bloom; use codec::{Encode, Decode}; use sp_std::vec::Vec; -pub trait EthereumExt { - fn eth_state_root() -> H256 { - H256::decode(&mut &sp_io::storage::root()[..]) - .expect("Node is configured to use the same hash; qed") - } -} - #[derive(Eq, PartialEq, Clone, Encode, Decode, sp_runtime::RuntimeDebug)] pub struct TransactionStatus { pub transaction_hash: H256, diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 38976b275..fa243554c 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -46,10 +46,12 @@ use pallet_evm::{ Account as EVMAccount, FeeCalculator, HashedAddressMapping, EnsureAddressTruncated, Runner, }; -use fp_rpc::{TransactionStatus, EthereumExt}; -pub type BlockNumber = u32; +use fp_rpc::TransactionStatus; use pallet_transaction_payment::CurrencyAdapter; +/// Type of block number. +pub type BlockNumber = u32; + /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. pub type Signature = MultiSignature; @@ -314,12 +316,10 @@ impl> FindAuthor for EthereumFindAuthor } } -impl EthereumExt for Runtime {} - impl pallet_ethereum::Config for Runtime { type Event = Event; type FindAuthor = EthereumFindAuthor; - type Extension = Self; + type StateRoot = pallet_ethereum::IntermediateStateRoot; } // Create the runtime by composing the FRAME pallets that were previously configured.