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