1#![cfg_attr(not(feature = "std"), no_std)]
19
20#[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
62pub 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 array_bytes::hex_n_into_unchecked(
81 "f24ff3a9cf04c71dbc94d0b566f7a27b94566caceeeeeeeeeeeeeeeeeeeeeeee",
82 ),
83 array_bytes::hex_n_into_unchecked(
85 "3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0eeeeeeeeeeeeeeeeeeeeeeee",
86 ),
87 array_bytes::hex_n_into_unchecked(
89 "798d4ba9baf0064ec19eb4f0a1a45785ae9d6dfceeeeeeeeeeeeeeeeeeeeeeee",
90 ),
91 array_bytes::hex_n_into_unchecked(
93 "773539d4ac0e786233d90a233654ccee26a613d9eeeeeeeeeeeeeeeeeeeeeeee",
94 ),
95 array_bytes::hex_n_into_unchecked(
97 "ff64d3f6efe2317ee2807d223a0bdc4c0c49dfdbeeeeeeeeeeeeeeeeeeeeeeee",
98 ),
99 ])
100 .collect::<Vec<_>>()
101 }
102
103 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 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 pub fn preset_names() -> Vec<PresetId> {
131 vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
132 }
133}
134
135#[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#[cfg(feature = "std")]
150pub fn native_version() -> NativeVersion {
151 NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
152}
153
154pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
156pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
158type TxExtension = (
160 frame_system::CheckNonZeroSender<Runtime>,
162 frame_system::CheckSpecVersion<Runtime>,
164 frame_system::CheckTxVersion<Runtime>,
166 frame_system::CheckGenesis<Runtime>,
168 frame_system::CheckEra<Runtime>,
170 frame_system::CheckNonce<Runtime>,
172 frame_system::CheckWeight<Runtime>,
174 pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
177 pallet_revive::evm::tx_extension::SetOrigin<Runtime>,
179 frame_system::WeightReclaim<Runtime>,
183);
184
185#[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#[frame_construct_runtime]
222mod runtime {
223 #[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 #[runtime::pallet_index(0)]
241 pub type System = frame_system::Pallet<Runtime>;
242
243 #[runtime::pallet_index(1)]
245 pub type Timestamp = pallet_timestamp::Pallet<Runtime>;
246
247 #[runtime::pallet_index(2)]
249 pub type Balances = pallet_balances::Pallet<Runtime>;
250
251 #[runtime::pallet_index(3)]
253 pub type Sudo = pallet_sudo::Pallet<Runtime>;
254
255 #[runtime::pallet_index(4)]
257 pub type TransactionPayment = pallet_transaction_payment::Pallet<Runtime>;
258
259 #[runtime::pallet_index(5)]
261 pub type Revive = pallet_revive::Pallet<Runtime>;
262}
263
264const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
267const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
270const 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 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#[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#[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#[derive_impl(pallet_sudo::config_preludes::TestDefaultConfig)]
321impl pallet_sudo::Config for Runtime {}
322
323#[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#[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);