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 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
63pub 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 array_bytes::hex_n_into_unchecked(
82 "f24ff3a9cf04c71dbc94d0b566f7a27b94566caceeeeeeeeeeeeeeeeeeeeeeee",
83 ),
84 array_bytes::hex_n_into_unchecked(
86 "3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0eeeeeeeeeeeeeeeeeeeeeeee",
87 ),
88 array_bytes::hex_n_into_unchecked(
90 "798d4ba9baf0064ec19eb4f0a1a45785ae9d6dfceeeeeeeeeeeeeeeeeeeeeeee",
91 ),
92 array_bytes::hex_n_into_unchecked(
94 "773539d4ac0e786233d90a233654ccee26a613d9eeeeeeeeeeeeeeeeeeeeeeee",
95 ),
96 array_bytes::hex_n_into_unchecked(
98 "ff64d3f6efe2317ee2807d223a0bdc4c0c49dfdbeeeeeeeeeeeeeeeeeeeeeeee",
99 ),
100 ])
101 .collect::<Vec<_>>()
102 }
103
104 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 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 pub fn preset_names() -> Vec<PresetId> {
132 vec![PresetId::from(sp_genesis_builder::DEV_RUNTIME_PRESET)]
133 }
134}
135
136#[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#[cfg(feature = "std")]
151pub fn native_version() -> NativeVersion {
152 NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
153}
154
155pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
157pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
159type TxExtension = (
161 frame_system::CheckNonZeroSender<Runtime>,
163 frame_system::CheckSpecVersion<Runtime>,
165 frame_system::CheckTxVersion<Runtime>,
167 frame_system::CheckGenesis<Runtime>,
169 frame_system::CheckEra<Runtime>,
171 frame_system::CheckNonce<Runtime>,
173 frame_system::CheckWeight<Runtime>,
175 pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
178 pallet_revive::evm::tx_extension::SetOrigin<Runtime>,
180 frame_system::WeightReclaim<Runtime>,
184);
185
186#[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#[frame_construct_runtime]
223mod runtime {
224 #[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 #[runtime::pallet_index(0)]
242 pub type System = frame_system::Pallet<Runtime>;
243
244 #[runtime::pallet_index(1)]
246 pub type Timestamp = pallet_timestamp::Pallet<Runtime>;
247
248 #[runtime::pallet_index(2)]
250 pub type Balances = pallet_balances::Pallet<Runtime>;
251
252 #[runtime::pallet_index(3)]
254 pub type Sudo = pallet_sudo::Pallet<Runtime>;
255
256 #[runtime::pallet_index(4)]
258 pub type TransactionPayment = pallet_transaction_payment::Pallet<Runtime>;
259
260 #[runtime::pallet_index(5)]
262 pub type Revive = pallet_revive::Pallet<Runtime>;
263}
264
265const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
268const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
271const 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 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#[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#[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#[derive_impl(pallet_sudo::config_preludes::TestDefaultConfig)]
322impl pallet_sudo::Config for Runtime {}
323
324#[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#[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);