From 202d7223d91dcd095d55648658455bdbfceb6c23 Mon Sep 17 00:00:00 2001 From: Qinxuan Chen Date: Sat, 6 May 2023 23:13:19 +0800 Subject: [PATCH] chore: decouple pallet-timestamp from pallet-evm and pallet-ethereum (#1050) --- frame/ethereum/Cargo.toml | 7 +------ frame/ethereum/src/lib.rs | 8 +++----- frame/ethereum/src/mock.rs | 1 + frame/evm/Cargo.toml | 5 +---- frame/evm/precompile/dispatch/src/mock.rs | 1 + frame/evm/src/lib.rs | 7 +++++-- frame/evm/src/mock.rs | 3 ++- frame/evm/src/runner/stack.rs | 4 ++-- template/runtime/src/lib.rs | 1 + 9 files changed, 17 insertions(+), 20 deletions(-) diff --git a/frame/ethereum/Cargo.toml b/frame/ethereum/Cargo.toml index e40f4b1e7d..1320e27dd8 100644 --- a/frame/ethereum/Cargo.toml +++ b/frame/ethereum/Cargo.toml @@ -17,15 +17,12 @@ ethereum-types = { workspace = true } evm = { workspace = true, features = ["with-codec"] } scale-codec = { package = "parity-scale-codec", workspace = true } scale-info = { workspace = true } - # Substrate frame-support = { workspace = true } frame-system = { workspace = true } -pallet-timestamp = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } sp-std = { workspace = true } - # Frontier fp-consensus = { workspace = true } fp-ethereum = { workspace = true } @@ -40,6 +37,7 @@ libsecp256k1 = { workspace = true, features = ["static-context", "hmac"] } rlp = { workspace = true } # Substrate pallet-balances = { workspace = true, features = ["default"] } +pallet-timestamp = { workspace = true, features = ["default"] } sp-core = { workspace = true, features = ["default"] } # Frontier fp-self-contained = { workspace = true, features = ["default"] } @@ -57,7 +55,6 @@ std = [ # Substrate "frame-support/std", "frame-system/std", - "pallet-timestamp/std", "sp-io/std", "sp-runtime/std", "sp-std/std", @@ -73,13 +70,11 @@ std = [ runtime-benchmarks = [ "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", - "pallet-timestamp/runtime-benchmarks", "pallet-evm/runtime-benchmarks", ] try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", - "pallet-timestamp/try-runtime", "pallet-evm/try-runtime", ] forbid-evm-reentrancy = ["pallet-evm/forbid-evm-reentrancy"] diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 8f192d0cac..0ae5c566e9 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -47,7 +47,7 @@ use frame_support::{ codec::{Decode, Encode, MaxEncodedLen}, dispatch::{DispatchInfo, DispatchResultWithPostInfo, Pays, PostDispatchInfo}, scale_info::TypeInfo, - traits::{EnsureOrigin, Get, PalletInfoAccess}, + traits::{EnsureOrigin, Get, PalletInfoAccess, Time}, weights::Weight, }; use frame_system::{pallet_prelude::OriginFor, CheckWeight, WeightInfo}; @@ -191,7 +191,7 @@ pub mod pallet { pub type Origin = RawOrigin; #[pallet::config] - pub trait Config: frame_system::Config + pallet_timestamp::Config + pallet_evm::Config { + pub trait Config: frame_system::Config + pallet_evm::Config { /// The overarching event type. type RuntimeEvent: From + IsType<::RuntimeEvent>; /// How Ethereum state root is calculated. @@ -426,9 +426,7 @@ impl Pallet { number: block_number, gas_limit: T::BlockGasLimit::get(), gas_used: cumulative_gas_used, - timestamp: UniqueSaturatedInto::::unique_saturated_into( - pallet_timestamp::Pallet::::get(), - ), + timestamp: T::Timestamp::now().unique_saturated_into(), extra_data: Vec::new(), mix_hash: H256::default(), nonce: H64::default(), diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 1692edf96e..5eceb3d0ae 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -169,6 +169,7 @@ impl pallet_evm::Config for Test { type OnChargeTransaction = (); type OnCreate = (); type FindAuthor = FindAuthorTruncated; + type Timestamp = Timestamp; type WeightInfo = (); } diff --git a/frame/evm/Cargo.toml b/frame/evm/Cargo.toml index 8a82b5a60e..03b429fedc 100644 --- a/frame/evm/Cargo.toml +++ b/frame/evm/Cargo.toml @@ -24,7 +24,6 @@ scale-info = { workspace = true } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } frame-system = { workspace = true } -pallet-timestamp = { workspace = true } sp-core = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } @@ -37,6 +36,7 @@ fp-evm = { workspace = true } # Substrate pallet-balances = { workspace = true, features = ["default"] } pallet-evm-precompile-simple = { workspace = true, features = ["default"] } +pallet-timestamp = { workspace = true, features = ["default"] } [features] default = ["std"] @@ -53,7 +53,6 @@ std = [ "frame-benchmarking/std", "frame-support/std", "frame-system/std", - "pallet-timestamp/std", "sp-core/std", "sp-io/std", "sp-runtime/std", @@ -66,11 +65,9 @@ runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", - "pallet-timestamp/runtime-benchmarks", ] try-runtime = [ "frame-support/try-runtime", "frame-system/try-runtime", - "pallet-timestamp/try-runtime", ] forbid-evm-reentrancy = ["dep:environmental"] diff --git a/frame/evm/precompile/dispatch/src/mock.rs b/frame/evm/precompile/dispatch/src/mock.rs index 3cb2f802d5..d55d73d2cf 100644 --- a/frame/evm/precompile/dispatch/src/mock.rs +++ b/frame/evm/precompile/dispatch/src/mock.rs @@ -159,6 +159,7 @@ impl pallet_evm::Config for Test { type OnChargeTransaction = (); type OnCreate = (); type FindAuthor = FindAuthorTruncated; + type Timestamp = Timestamp; type WeightInfo = (); } diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 9fd80aef01..a7b0a65680 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -70,7 +70,7 @@ use frame_support::{ dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo}, traits::{ tokens::fungible::Inspect, Currency, ExistenceRequirement, FindAuthor, Get, Imbalance, - OnUnbalanced, SignedImbalance, WithdrawReasons, + OnUnbalanced, SignedImbalance, Time, WithdrawReasons, }, weights::Weight, }; @@ -112,7 +112,7 @@ pub mod pallet { pub struct Pallet(PhantomData); #[pallet::config] - pub trait Config: frame_system::Config + pallet_timestamp::Config { + pub trait Config: frame_system::Config { /// Calculator for current gas price. type FeeCalculator: FeeCalculator; @@ -158,6 +158,9 @@ pub mod pallet { /// Find author for the current block. type FindAuthor: FindAuthor; + /// Get the timestamp for the current block. + type Timestamp: Time; + /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; diff --git a/frame/evm/src/mock.rs b/frame/evm/src/mock.rs index c3c6c867bf..948810b72a 100644 --- a/frame/evm/src/mock.rs +++ b/frame/evm/src/mock.rs @@ -152,10 +152,11 @@ impl crate::Config for Test { type OnChargeTransaction = (); type OnCreate = (); type FindAuthor = FindAuthorTruncated; + type Timestamp = Timestamp; type WeightInfo = (); } -/// Exemple PrecompileSet with only Identity precompile. +/// Example PrecompileSet with only Identity precompile. pub struct MockPrecompileSet; impl PrecompileSet for MockPrecompileSet { diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 3193b1c049..68d5c0d8c3 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -28,7 +28,7 @@ use evm::{ ExitError, ExitReason, Transfer, }; use fp_evm::{CallInfo, CreateInfo, ExecutionInfo, Log, PrecompileSet, Vicinity}; -use frame_support::traits::{Currency, ExistenceRequirement, Get}; +use frame_support::traits::{Currency, ExistenceRequirement, Get, Time}; use sp_core::{H160, H256, U256}; use sp_runtime::traits::UniqueSaturatedInto; use sp_std::{ @@ -634,7 +634,7 @@ impl<'vicinity, 'config, T: Config> BackendT for SubstrateStackState<'vicinity, } fn block_timestamp(&self) -> U256 { - let now: u128 = pallet_timestamp::Pallet::::get().unique_saturated_into(); + let now: u128 = T::Timestamp::now().unique_saturated_into(); U256::from(now / 1000) } diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 2edaf8f0d2..4fd19970c7 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -345,6 +345,7 @@ impl pallet_evm::Config for Runtime { type OnChargeTransaction = (); type OnCreate = (); type FindAuthor = FindAuthorTruncated; + type Timestamp = Timestamp; type WeightInfo = pallet_evm::weights::SubstrateWeight; }