Skip to main content

revive_dev_runtime/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#![cfg_attr(not(feature = "std"), no_std)]
19
20// Make the WASM binary available.
21#[cfg(feature = "std")]
22include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
23
24extern crate alloc;
25
26use alloc::{vec, vec::Vec};
27use currency::*;
28use frame_support::weights::{
29	constants::{BlockExecutionWeight, ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND},
30	Weight,
31};
32use frame_system::limits::BlockWeights;
33use pallet_revive::{
34	evm::{
35		fees::{BlockRatioFee, Info as FeeInfo},
36		runtime::EthExtra,
37	},
38	AccountId32Mapper,
39};
40use pallet_transaction_payment::{ConstFeeMultiplier, FeeDetails, Multiplier, RuntimeDispatchInfo};
41use polkadot_sdk::{
42	polkadot_sdk_frame::{
43		deps::sp_genesis_builder,
44		runtime::{apis, prelude::*},
45		traits::Block as BlockT,
46	},
47	*,
48};
49use sp_weights::ConstantMultiplier;
50
51pub use polkadot_sdk::{
52	parachains_common::{AccountId, Balance, BlockNumber, Hash, Header, Nonce, Signature},
53	polkadot_sdk_frame::runtime::types_common::OpaqueBlock,
54};
55
56pub mod currency {
57	use super::Balance;
58	pub const DOLLARS: Balance = 1_000_000_000_000;
59	pub const CENTS: Balance = DOLLARS / 100;
60	pub const MILLICENTS: Balance = CENTS / 1_000;
61}
62
63/// Provides getters for genesis configuration presets.
64pub mod genesis_config_presets {
65	use super::*;
66	use crate::{
67		currency::DOLLARS, sp_keyring::Sr25519Keyring, Balance, BalancesConfig,
68		RuntimeGenesisConfig, SudoConfig,
69	};
70
71	use alloc::{vec, vec::Vec};
72	use serde_json::Value;
73
74	pub const ENDOWMENT: Balance = 10_000_000_000_001 * DOLLARS;
75
76	fn well_known_accounts() -> Vec<AccountId> {
77		Sr25519Keyring::well_known()
78			.map(|k| k.to_account_id())
79			.chain([
80				// subxt_signer::eth::dev::alith()
81				array_bytes::hex_n_into_unchecked(
82					"f24ff3a9cf04c71dbc94d0b566f7a27b94566caceeeeeeeeeeeeeeeeeeeeeeee",
83				),
84				// subxt_signer::eth::dev::baltathar()
85				array_bytes::hex_n_into_unchecked(
86					"3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0eeeeeeeeeeeeeeeeeeeeeeee",
87				),
88				// subxt_signer::eth::dev::charleth()
89				array_bytes::hex_n_into_unchecked(
90					"798d4ba9baf0064ec19eb4f0a1a45785ae9d6dfceeeeeeeeeeeeeeeeeeeeeeee",
91				),
92				// subxt_signer::eth::dev::dorothy()
93				array_bytes::hex_n_into_unchecked(
94					"773539d4ac0e786233d90a233654ccee26a613d9eeeeeeeeeeeeeeeeeeeeeeee",
95				),
96				// subxt_signer::eth::dev::ethan()
97				array_bytes::hex_n_into_unchecked(
98					"ff64d3f6efe2317ee2807d223a0bdc4c0c49dfdbeeeeeeeeeeeeeeeeeeeeeeee",
99				),
100			])
101			.collect::<Vec<_>>()
102	}
103
104	/// Returns a development genesis config preset.
105	pub fn development_config_genesis() -> Value {
106		frame_support::build_struct_json_patch!(RuntimeGenesisConfig {
107			balances: BalancesConfig {
108				balances: well_known_accounts()
109					.into_iter()
110					.map(|id| (id, ENDOWMENT))
111					.collect::<Vec<_>>(),
112			},
113			sudo: SudoConfig { key: Some(Sr25519Keyring::Alice.to_account_id()) },
114		})
115	}
116
117	/// Get the set of the available genesis config presets.
118	pub fn get_preset(id: &PresetId) -> Option<Vec<u8>> {
119		let patch = match id.as_ref() {
120			sp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(),
121			_ => return None,
122		};
123		Some(
124			serde_json::to_string(&patch)
125				.expect("serialization to json is expected to work. qed.")
126				.into_bytes(),
127		)
128	}
129
130	/// List of supported presets.
131	pub fn preset_names() -> Vec<PresetId> {
132		vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
133	}
134}
135
136/// The runtime version.
137#[runtime_version]
138pub const VERSION: RuntimeVersion = RuntimeVersion {
139	spec_name: alloc::borrow::Cow::Borrowed("revive-dev-runtime"),
140	impl_name: alloc::borrow::Cow::Borrowed("revive-dev-runtime"),
141	authoring_version: 1,
142	spec_version: 0,
143	impl_version: 1,
144	apis: RUNTIME_API_VERSIONS,
145	transaction_version: 1,
146	system_version: 1,
147};
148
149/// The version information used to identify this runtime when compiled natively.
150#[cfg(feature = "std")]
151pub fn native_version() -> NativeVersion {
152	NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
153}
154
155/// The address format for describing accounts.
156pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
157/// Block type as expected by this runtime.
158pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
159/// The transaction extensions that are added to the runtime.
160type TxExtension = (
161	// Checks that the sender is not the zero address.
162	frame_system::CheckNonZeroSender<Runtime>,
163	// Checks that the runtime version is correct.
164	frame_system::CheckSpecVersion<Runtime>,
165	// Checks that the transaction version is correct.
166	frame_system::CheckTxVersion<Runtime>,
167	// Checks that the genesis hash is correct.
168	frame_system::CheckGenesis<Runtime>,
169	// Checks that the era is valid.
170	frame_system::CheckEra<Runtime>,
171	// Checks that the nonce is valid.
172	frame_system::CheckNonce<Runtime>,
173	// Checks that the weight is valid.
174	frame_system::CheckWeight<Runtime>,
175	// Ensures that the sender has enough funds to pay for the transaction
176	// and deducts the fee from the sender's account.
177	pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
178	// Needs to be done after all extensions that rely on a signed origin.
179	pallet_revive::evm::tx_extension::SetOrigin<Runtime>,
180	// Reclaim the unused weight from the block using post dispatch information.
181	// It must be last in the pipeline in order to catch the refund in previous transaction
182	// extensions
183	frame_system::WeightReclaim<Runtime>,
184);
185
186/// Default extensions applied to Ethereum transactions.
187#[derive(Clone, PartialEq, Eq, Debug)]
188pub struct EthExtraImpl;
189
190impl EthExtra for EthExtraImpl {
191	type Config = Runtime;
192	type Extension = TxExtension;
193
194	fn get_eth_extension(nonce: u32, tip: Balance) -> Self::Extension {
195		(
196			frame_system::CheckNonZeroSender::<Runtime>::new(),
197			frame_system::CheckSpecVersion::<Runtime>::new(),
198			frame_system::CheckTxVersion::<Runtime>::new(),
199			frame_system::CheckGenesis::<Runtime>::new(),
200			frame_system::CheckMortality::from(sp_runtime::generic::Era::Immortal),
201			frame_system::CheckNonce::<Runtime>::from(nonce),
202			frame_system::CheckWeight::<Runtime>::new(),
203			pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
204			pallet_revive::evm::tx_extension::SetOrigin::<Runtime>::new_from_eth_transaction(),
205			frame_system::WeightReclaim::<Runtime>::new(),
206		)
207	}
208}
209
210pub type UncheckedExtrinsic =
211	pallet_revive::evm::runtime::UncheckedExtrinsic<Address, Signature, EthExtraImpl>;
212
213type Executive = frame_executive::Executive<
214	Runtime,
215	Block,
216	frame_system::ChainContext<Runtime>,
217	Runtime,
218	AllPalletsWithSystem,
219>;
220
221// Composes the runtime by adding all the used pallets and deriving necessary types.
222#[frame_construct_runtime]
223mod runtime {
224	/// The main runtime type.
225	#[runtime::runtime]
226	#[runtime::derive(
227		RuntimeCall,
228		RuntimeEvent,
229		RuntimeError,
230		RuntimeOrigin,
231		RuntimeFreezeReason,
232		RuntimeHoldReason,
233		RuntimeSlashReason,
234		RuntimeLockId,
235		RuntimeTask,
236		RuntimeViewFunction
237	)]
238	pub struct Runtime;
239
240	/// Mandatory system pallet that should always be included in a FRAME runtime.
241	#[runtime::pallet_index(0)]
242	pub type System = frame_system::Pallet<Runtime>;
243
244	/// Provides a way for consensus systems to set and check the onchain time.
245	#[runtime::pallet_index(1)]
246	pub type Timestamp = pallet_timestamp::Pallet<Runtime>;
247
248	/// Provides the ability to keep track of balances.
249	#[runtime::pallet_index(2)]
250	pub type Balances = pallet_balances::Pallet<Runtime>;
251
252	/// Provides a way to execute privileged functions.
253	#[runtime::pallet_index(3)]
254	pub type Sudo = pallet_sudo::Pallet<Runtime>;
255
256	/// Provides the ability to charge for extrinsic execution.
257	#[runtime::pallet_index(4)]
258	pub type TransactionPayment = pallet_transaction_payment::Pallet<Runtime>;
259
260	/// Provides the ability to execute Smart Contracts.
261	#[runtime::pallet_index(5)]
262	pub type Revive = pallet_revive::Pallet<Runtime>;
263}
264
265/// We assume that ~10% of the block weight is consumed by `on_initialize` handlers.
266/// This is used to limit the maximal weight of a single extrinsic.
267const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
268/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used
269/// by  Operational  extrinsics.
270const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
271/// We allow for 2 seconds of compute with a 6 second average block time, with maximum proof size.
272const MAXIMUM_BLOCK_WEIGHT: Weight =
273	Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
274
275parameter_types! {
276	pub const Version: RuntimeVersion = VERSION;
277	pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
278		.base_block(BlockExecutionWeight::get())
279		.for_class(DispatchClass::all(), |weights| {
280			weights.base_extrinsic = ExtrinsicBaseWeight::get();
281		})
282		.for_class(DispatchClass::Normal, |weights| {
283			weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
284		})
285		.for_class(DispatchClass::Operational, |weights| {
286			weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
287			// Operational transactions have some extra reserved space, so that they
288			// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
289			weights.reserved = Some(
290				MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT
291			);
292		})
293		.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
294		.build_or_panic();
295}
296
297/// Implements the types required for the system pallet.
298#[derive_impl(frame_system::config_preludes::SolochainDefaultConfig)]
299impl frame_system::Config for Runtime {
300	type Block = Block;
301	type Version = Version;
302	type AccountId = AccountId;
303	type Hash = Hash;
304	type Nonce = Nonce;
305	type AccountData = pallet_balances::AccountData<<Runtime as pallet_balances::Config>::Balance>;
306}
307
308parameter_types! {
309	pub const ExistentialDeposit: Balance = CENTS;
310}
311
312// Implements the types required for the balances pallet.
313#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
314impl pallet_balances::Config for Runtime {
315	type AccountStore = System;
316	type Balance = Balance;
317	type ExistentialDeposit = ExistentialDeposit;
318}
319
320// Implements the types required for the sudo pallet.
321#[derive_impl(pallet_sudo::config_preludes::TestDefaultConfig)]
322impl pallet_sudo::Config for Runtime {}
323
324// Implements the types required for the sudo pallet.
325#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)]
326impl pallet_timestamp::Config for Runtime {}
327
328parameter_types! {
329	pub const TransactionByteFee: Balance = 10 * MILLICENTS;
330	pub FeeMultiplier: Multiplier = Multiplier::one();
331}
332
333// Implements the types required for the transaction payment pallet.
334#[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)]
335impl pallet_transaction_payment::Config for Runtime {
336	type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter<Balances, ()>;
337	type WeightToFee = BlockRatioFee<1, 1, Self, Balance>;
338	type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
339	type FeeMultiplierUpdate = ConstFeeMultiplier<FeeMultiplier>;
340}
341
342parameter_types! {
343	pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(30);
344}
345
346#[derive_impl(pallet_revive::config_preludes::TestDefaultConfig)]
347impl pallet_revive::Config for Runtime {
348	type AddressMapper = AccountId32Mapper<Self>;
349	type ChainId = ConstU64<420_420_420>;
350	type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
351	type Balance = Balance;
352	type Currency = Balances;
353	type NativeToEthRatio = ConstU32<1_000_000>;
354	type UploadOrigin = EnsureSigned<Self::AccountId>;
355	type InstantiateOrigin = EnsureSigned<Self::AccountId>;
356	type Time = Timestamp;
357	type FeeInfo = FeeInfo<Address, Signature, EthExtraImpl>;
358	type DebugEnabled = ConstBool<false>;
359	type GasScale = ConstU32<50000>;
360}
361
362pallet_revive::impl_runtime_apis_plus_revive_traits!(
363	Runtime,
364	Revive,
365	Executive,
366	EthExtraImpl,
367
368	impl apis::Core<Block> for Runtime {
369		fn version() -> RuntimeVersion {
370			VERSION
371		}
372
373		fn execute_block(block: <Block as BlockT>::LazyBlock) {
374			Executive::execute_block(block)
375		}
376
377		fn initialize_block(header: &Header) -> ExtrinsicInclusionMode {
378			Executive::initialize_block(header)
379		}
380	}
381
382	impl apis::Metadata<Block> for Runtime {
383		fn metadata() -> OpaqueMetadata {
384			OpaqueMetadata::new(Runtime::metadata().into())
385		}
386
387		fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
388			Runtime::metadata_at_version(version)
389		}
390
391		fn metadata_versions() -> Vec<u32> {
392			Runtime::metadata_versions()
393		}
394	}
395
396	impl apis::BlockBuilder<Block> for Runtime {
397		fn apply_extrinsic(extrinsic: ExtrinsicFor<Runtime>) -> ApplyExtrinsicResult {
398			Executive::apply_extrinsic(extrinsic)
399		}
400
401		fn finalize_block() -> HeaderFor<Runtime> {
402			Executive::finalize_block()
403		}
404
405		fn inherent_extrinsics(data: InherentData) -> Vec<ExtrinsicFor<Runtime>> {
406			data.create_extrinsics()
407		}
408
409		fn check_inherents(
410			block: <Block as BlockT>::LazyBlock,
411			data: InherentData,
412		) -> CheckInherentsResult {
413			data.check_extrinsics(&block)
414		}
415	}
416
417	impl apis::TaggedTransactionQueue<Block> for Runtime {
418		fn validate_transaction(
419			source: TransactionSource,
420			tx: ExtrinsicFor<Runtime>,
421			block_hash: <Runtime as frame_system::Config>::Hash,
422		) -> TransactionValidity {
423			Executive::validate_transaction(source, tx, block_hash)
424		}
425	}
426
427	impl apis::OffchainWorkerApi<Block> for Runtime {
428		fn offchain_worker(header: &HeaderFor<Runtime>) {
429			Executive::offchain_worker(header)
430		}
431	}
432
433	impl apis::SessionKeys<Block> for Runtime {
434		fn generate_session_keys(_seed: Option<Vec<u8>>) -> Vec<u8> {
435			Default::default()
436		}
437
438		fn decode_session_keys(
439			_encoded: Vec<u8>,
440		) -> Option<Vec<(Vec<u8>, apis::KeyTypeId)>> {
441			Default::default()
442		}
443	}
444
445	impl apis::AccountNonceApi<Block, AccountId, Nonce> for Runtime {
446		fn account_nonce(account: AccountId) -> Nonce {
447			System::account_nonce(account)
448		}
449	}
450
451	impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<
452		Block,
453		Balance,
454	> for Runtime {
455		fn query_info(uxt: ExtrinsicFor<Runtime>, len: u32) -> RuntimeDispatchInfo<Balance> {
456			TransactionPayment::query_info(uxt, len)
457		}
458		fn query_fee_details(uxt: ExtrinsicFor<Runtime>, len: u32) -> FeeDetails<Balance> {
459			TransactionPayment::query_fee_details(uxt, len)
460		}
461		fn query_weight_to_fee(weight: Weight) -> Balance {
462			TransactionPayment::weight_to_fee(weight)
463		}
464		fn query_length_to_fee(length: u32) -> Balance {
465			TransactionPayment::length_to_fee(length)
466		}
467	}
468
469	impl apis::GenesisBuilder<Block> for Runtime {
470		fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
471			build_state::<RuntimeGenesisConfig>(config)
472		}
473
474		fn get_preset(id: &Option<PresetId>) -> Option<Vec<u8>> {
475			get_preset::<RuntimeGenesisConfig>(id, self::genesis_config_presets::get_preset)
476		}
477
478		fn preset_names() -> Vec<PresetId> {
479			self::genesis_config_presets::preset_names()
480		}
481	}
482);