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