subspace_test_runtime/
lib.rs

1// Copyright (C) 2021 Subspace Labs, Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#![cfg_attr(not(feature = "std"), no_std)]
18#![feature(variant_count)]
19// `generic_const_exprs` is an incomplete feature
20#![allow(incomplete_features)]
21// TODO: This feature is not actually used in this crate, but is added as a workaround for
22//  https://github.com/rust-lang/rust/issues/133199
23#![feature(generic_const_exprs)]
24// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
25#![recursion_limit = "256"]
26// TODO: remove when upstream issue is fixed
27#![allow(
28    non_camel_case_types,
29    reason = "https://github.com/rust-lang/rust-analyzer/issues/16514"
30)]
31
32extern crate alloc;
33
34// Make the WASM binary available.
35#[cfg(feature = "std")]
36include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
37
38use alloc::borrow::Cow;
39use core::mem;
40use core::num::NonZeroU64;
41use domain_runtime_primitives::opaque::Header as DomainHeader;
42use domain_runtime_primitives::{
43    AccountIdConverter, BlockNumber as DomainNumber, EthereumAccountId, Hash as DomainHash,
44    MAX_OUTGOING_MESSAGES,
45};
46use frame_support::genesis_builder_helper::{build_state, get_preset};
47use frame_support::inherent::ProvideInherent;
48use frame_support::traits::fungible::Inspect;
49#[cfg(feature = "runtime-benchmarks")]
50use frame_support::traits::fungible::Mutate;
51use frame_support::traits::tokens::WithdrawConsequence;
52use frame_support::traits::{
53    ConstU8, ConstU16, ConstU32, ConstU64, ConstU128, Currency, Everything, ExistenceRequirement,
54    Get, Imbalance, VariantCount, WithdrawReasons,
55};
56use frame_support::weights::constants::{ParityDbWeight, WEIGHT_REF_TIME_PER_SECOND};
57use frame_support::weights::{ConstantMultiplier, Weight};
58use frame_support::{PalletId, construct_runtime, parameter_types};
59use frame_system::limits::{BlockLength, BlockWeights};
60use frame_system::pallet_prelude::RuntimeCallFor;
61use pallet_balances::NegativeImbalance;
62use pallet_domains::staking::StakingSummary;
63pub use pallet_rewards::RewardPoint;
64pub use pallet_subspace::{AllowAuthoringBy, EnableRewardsAt};
65use pallet_transporter::EndpointHandler;
66use parity_scale_codec::{Compact, CompactLen, Decode, Encode, MaxEncodedLen};
67use scale_info::TypeInfo;
68use sp_api::impl_runtime_apis;
69use sp_consensus_slots::{Slot, SlotDuration};
70use sp_consensus_subspace::{ChainConstants, PotParameters, SignedVote, SolutionRanges, Vote};
71use sp_core::crypto::KeyTypeId;
72use sp_core::{H256, OpaqueMetadata};
73use sp_domains::bundle::{BundleVersion, OpaqueBundle, OpaqueBundles};
74use sp_domains::bundle_producer_election::BundleProducerElectionParams;
75use sp_domains::execution_receipt::{
76    ExecutionReceiptFor, ExecutionReceiptVersion, SealedSingletonReceipt,
77};
78use sp_domains::{
79    BundleAndExecutionReceiptVersion, DomainAllowlistUpdates, DomainId, DomainInstanceData,
80    EpochIndex, INITIAL_DOMAIN_TX_RANGE, OperatorId, OperatorPublicKey,
81    PermissionedActionAllowedBy,
82};
83use sp_domains_fraud_proof::fraud_proof::FraudProof;
84use sp_domains_fraud_proof::storage_proof::{
85    FraudProofStorageKeyProvider, FraudProofStorageKeyRequest,
86};
87use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId};
88use sp_messenger::messages::{
89    BlockMessagesQuery, ChainId, ChannelId, ChannelStateWithNonce, CrossDomainMessage, MessageId,
90    MessageKey, MessagesWithStorageKey, Nonce as XdmNonce,
91};
92use sp_messenger::{ChannelNonce, XdmId};
93use sp_messenger_host_functions::{StorageKeyRequest, get_storage_key};
94use sp_mmr_primitives::EncodableOpaqueLeaf;
95use sp_runtime::traits::{
96    AccountIdConversion, AccountIdLookup, BlakeTwo256, ConstBool, DispatchInfoOf, Keccak256,
97    NumberFor, PostDispatchInfoOf, Zero,
98};
99use sp_runtime::transaction_validity::{
100    InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
101};
102use sp_runtime::type_with_default::TypeWithDefault;
103use sp_runtime::{AccountId32, ApplyExtrinsicResult, ExtrinsicInclusionMode, Perbill, generic};
104use sp_std::collections::btree_map::BTreeMap;
105use sp_std::collections::btree_set::BTreeSet;
106use sp_std::marker::PhantomData;
107use sp_std::prelude::*;
108use sp_subspace_mmr::ConsensusChainMmrLeafProof;
109use sp_version::RuntimeVersion;
110use static_assertions::const_assert;
111use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping};
112use subspace_core_primitives::pieces::Piece;
113use subspace_core_primitives::segments::{
114    HistorySize, SegmentCommitment, SegmentHeader, SegmentIndex,
115};
116use subspace_core_primitives::solutions::SolutionRange;
117use subspace_core_primitives::{PublicKey, Randomness, SlotNumber, U256, hashes};
118pub use subspace_runtime_primitives::extension::BalanceTransferCheckExtension;
119use subspace_runtime_primitives::extension::{BalanceTransferChecks, MaybeBalancesCall};
120use subspace_runtime_primitives::utility::{
121    DefaultNonceProvider, MaybeMultisigCall, MaybeNestedCall, MaybeUtilityCall,
122};
123use subspace_runtime_primitives::{
124    AI3, AccountId, Balance, BlockHashFor, BlockNumber, ConsensusEventSegmentSize, ExtrinsicFor,
125    FindBlockRewardAddress, Hash, HeaderFor, HoldIdentifier, MAX_BLOCK_LENGTH,
126    MAX_CALL_RECURSION_DEPTH, MIN_REPLICATION_FACTOR, Moment, Nonce, SHANNON, Signature,
127    SlowAdjustingFeeUpdate, TargetBlockFullness, XdmAdjustedWeightToFee, XdmFeeMultipler,
128};
129
130sp_runtime::impl_opaque_keys! {
131    pub struct SessionKeys {
132    }
133}
134
135// Smaller value for testing purposes
136const MAX_PIECES_IN_SECTOR: u16 = 32;
137
138// To learn more about runtime versioning and what each of the following value means:
139//   https://substrate.dev/docs/en/knowledgebase/runtime/upgrades#runtime-versioning
140#[sp_version::runtime_version]
141pub const VERSION: RuntimeVersion = RuntimeVersion {
142    spec_name: Cow::Borrowed("subspace"),
143    impl_name: Cow::Borrowed("subspace"),
144    authoring_version: 1,
145    // The version of the runtime specification. A full node will not attempt to use its native
146    //   runtime in substitute for the on-chain Wasm runtime unless all of `spec_name`,
147    //   `spec_version`, and `authoring_version` are the same between Wasm and native.
148    // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
149    //   the compatible custom types.
150    spec_version: 100,
151    impl_version: 1,
152    apis: RUNTIME_API_VERSIONS,
153    transaction_version: 1,
154    system_version: 2,
155};
156
157// TODO: Many of below constants should probably be updatable but currently they are not
158
159/// Expected block time in milliseconds.
160///
161/// Since Subspace is probabilistic this is the average expected block time that
162/// we are targeting. Blocks will be produced at a minimum duration defined
163/// by `SLOT_DURATION`, but some slots will not be allocated to any
164/// farmer and hence no block will be produced. We expect to have this
165/// block time on average following the defined slot duration and the value
166/// of `c` configured for Subspace (where `1 - c` represents the probability of
167/// a slot being empty).
168/// This value is only used indirectly to define the unit constants below
169/// that are expressed in blocks. The rest of the code should use
170/// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the
171/// minimum period).
172///
173/// Based on:
174/// <https://research.web3.foundation/en/latest/polkadot/block-production/Babe.html#-6.-practical-results>
175pub const MILLISECS_PER_BLOCK: u64 = 2000;
176
177// NOTE: Currently it is not possible to change the slot duration after the chain has started.
178//       Attempting to do so will brick block production.
179pub const SLOT_DURATION: u64 = 2000;
180
181/// 1 in 6 slots (on average, not counting collisions) will have a block.
182/// Must match ratio between block and slot duration in constants above.
183const SLOT_PROBABILITY: (u64, u64) = (1, 1);
184/// Number of slots between slot arrival and when corresponding block can be produced.
185const BLOCK_AUTHORING_DELAY: SlotNumber = 2;
186
187/// Interval, in blocks, between blockchain entropy injection into proof of time chain.
188const POT_ENTROPY_INJECTION_INTERVAL: BlockNumber = 5;
189
190/// Interval, in entropy injection intervals, where to take entropy for injection from.
191const POT_ENTROPY_INJECTION_LOOKBACK_DEPTH: u8 = 2;
192
193/// Delay after block, in slots, when entropy injection takes effect.
194const POT_ENTROPY_INJECTION_DELAY: SlotNumber = 4;
195
196// Entropy injection interval must be bigger than injection delay or else we may end up in a
197// situation where we'll need to do more than one injection at the same slot
198const_assert!(POT_ENTROPY_INJECTION_INTERVAL as u64 > POT_ENTROPY_INJECTION_DELAY);
199// Entropy injection delay must be bigger than block authoring delay or else we may include
200// invalid future proofs in parent block, +1 ensures we do not have unnecessary reorgs that will
201// inevitably happen otherwise
202const_assert!(POT_ENTROPY_INJECTION_DELAY > BLOCK_AUTHORING_DELAY + 1);
203
204/// Era duration in blocks.
205const ERA_DURATION_IN_BLOCKS: BlockNumber = 2016;
206
207/// Any solution range is valid in the test environment.
208const INITIAL_SOLUTION_RANGE: SolutionRange = SolutionRange::MAX;
209
210/// A ratio of `Normal` dispatch class within block, for `BlockWeight` and `BlockLength`.
211const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
212
213/// The block weight for 2 seconds of compute
214const BLOCK_WEIGHT_FOR_2_SEC: Weight =
215    Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX);
216
217parameter_types! {
218    pub const Version: RuntimeVersion = VERSION;
219    pub const BlockHashCount: BlockNumber = 250;
220    /// We allow for 2 seconds of compute with a 6 second average block time.
221    pub SubspaceBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(BLOCK_WEIGHT_FOR_2_SEC, NORMAL_DISPATCH_RATIO);
222    /// We allow for 3.75 MiB for `Normal` extrinsic with 5 MiB maximum block length.
223    pub SubspaceBlockLength: BlockLength = BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO);
224}
225
226pub type SS58Prefix = ConstU16<6094>;
227
228// Configure FRAME pallets to include in runtime.
229
230impl frame_system::Config for Runtime {
231    /// The basic call filter to use in dispatchable.
232    ///
233    /// `Everything` is used here as we use the signed extension
234    /// `DisablePallets` as the actual call filter.
235    type BaseCallFilter = Everything;
236    /// Block & extrinsics weights: base values and limits.
237    type BlockWeights = SubspaceBlockWeights;
238    /// The maximum length of a block (in bytes).
239    type BlockLength = SubspaceBlockLength;
240    /// The identifier used to distinguish between accounts.
241    type AccountId = AccountId;
242    /// The aggregated dispatch type that is available for extrinsics.
243    type RuntimeCall = RuntimeCall;
244    /// The aggregated `RuntimeTask` type.
245    type RuntimeTask = RuntimeTask;
246    /// The lookup mechanism to get account ID from whatever is passed in dispatchers.
247    type Lookup = AccountIdLookup<AccountId, ()>;
248    /// The type for storing how many extrinsics an account has signed.
249    type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
250    /// The type for hashing blocks and tries.
251    type Hash = Hash;
252    /// The hashing algorithm used.
253    type Hashing = BlakeTwo256;
254    /// The block type.
255    type Block = Block;
256    /// The ubiquitous event type.
257    type RuntimeEvent = RuntimeEvent;
258    /// The ubiquitous origin type.
259    type RuntimeOrigin = RuntimeOrigin;
260    /// Maximum number of block number to block hash mappings to keep (oldest pruned first).
261    type BlockHashCount = BlockHashCount;
262    /// The weight of database operations that the runtime can invoke.
263    type DbWeight = ParityDbWeight;
264    /// Version of the runtime.
265    type Version = Version;
266    /// Converts a module to the index of the module in `construct_runtime!`.
267    ///
268    /// This type is being generated by `construct_runtime!`.
269    type PalletInfo = PalletInfo;
270    /// What to do if a new account is created.
271    type OnNewAccount = ();
272    /// What to do if an account is fully reaped from the system.
273    type OnKilledAccount = ();
274    /// The data to be stored in an account.
275    type AccountData = pallet_balances::AccountData<Balance>;
276    /// Weight information for the extrinsics of this pallet.
277    type SystemWeightInfo = frame_system::weights::SubstrateWeight<Runtime>;
278    /// This is used as an identifier of the chain.
279    type SS58Prefix = SS58Prefix;
280    /// The set code logic.
281    type OnSetCode = subspace_runtime_primitives::SetCode<Runtime, Domains>;
282    type SingleBlockMigrations = ();
283    type MultiBlockMigrator = ();
284    type PreInherents = ();
285    type PostInherents = ();
286    type PostTransactions = ();
287    type MaxConsumers = ConstU32<16>;
288    type ExtensionsWeightInfo = frame_system::ExtensionsWeight<Runtime>;
289    type EventSegmentSize = ConsensusEventSegmentSize;
290}
291
292parameter_types! {
293    pub const BlockAuthoringDelay: SlotNumber = BLOCK_AUTHORING_DELAY;
294    pub const PotEntropyInjectionInterval: BlockNumber = POT_ENTROPY_INJECTION_INTERVAL;
295    pub const PotEntropyInjectionLookbackDepth: u8 = POT_ENTROPY_INJECTION_LOOKBACK_DEPTH;
296    pub const PotEntropyInjectionDelay: SlotNumber = POT_ENTROPY_INJECTION_DELAY;
297    pub const EraDuration: BlockNumber = ERA_DURATION_IN_BLOCKS;
298    pub const SlotProbability: (u64, u64) = SLOT_PROBABILITY;
299    pub const ShouldAdjustSolutionRange: bool = false;
300    pub const ExpectedVotesPerBlock: u32 = 9;
301    pub const ConfirmationDepthK: u32 = 5;
302    pub const RecentSegments: HistorySize = HistorySize::new(NonZeroU64::new(5).unwrap());
303    pub const RecentHistoryFraction: (HistorySize, HistorySize) = (
304        HistorySize::new(NonZeroU64::new(1).unwrap()),
305        HistorySize::new(NonZeroU64::new(10).unwrap()),
306    );
307    pub const MinSectorLifetime: HistorySize = HistorySize::new(NonZeroU64::new(4).unwrap());
308    pub const BlockSlotCount: u32 = 6;
309    pub TransactionWeightFee: Balance = 100_000 * SHANNON;
310}
311
312impl pallet_subspace::Config for Runtime {
313    type RuntimeEvent = RuntimeEvent;
314    type SubspaceOrigin = pallet_subspace::EnsureSubspaceOrigin;
315    type BlockAuthoringDelay = BlockAuthoringDelay;
316    type PotEntropyInjectionInterval = PotEntropyInjectionInterval;
317    type PotEntropyInjectionLookbackDepth = PotEntropyInjectionLookbackDepth;
318    type PotEntropyInjectionDelay = PotEntropyInjectionDelay;
319    type EraDuration = EraDuration;
320    type InitialSolutionRange = ConstU64<INITIAL_SOLUTION_RANGE>;
321    type SlotProbability = SlotProbability;
322    type ConfirmationDepthK = ConfirmationDepthK;
323    type RecentSegments = RecentSegments;
324    type RecentHistoryFraction = RecentHistoryFraction;
325    type MinSectorLifetime = MinSectorLifetime;
326    type ExpectedVotesPerBlock = ExpectedVotesPerBlock;
327    type MaxPiecesInSector = ConstU16<{ MAX_PIECES_IN_SECTOR }>;
328    type ShouldAdjustSolutionRange = ShouldAdjustSolutionRange;
329    type EraChangeTrigger = pallet_subspace::NormalEraChange;
330    type WeightInfo = pallet_subspace::weights::SubstrateWeight<Runtime>;
331    type BlockSlotCount = BlockSlotCount;
332    type ExtensionWeightInfo = pallet_subspace::extensions::weights::SubstrateWeight<Runtime>;
333}
334
335impl pallet_timestamp::Config for Runtime {
336    /// A timestamp: milliseconds since the unix epoch.
337    type Moment = Moment;
338    type OnTimestampSet = ();
339    type MinimumPeriod = ConstU64<{ SLOT_DURATION / 2 }>;
340    type WeightInfo = pallet_timestamp::weights::SubstrateWeight<Runtime>;
341}
342
343#[derive(
344    PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
345)]
346pub struct HoldIdentifierWrapper(HoldIdentifier);
347
348impl pallet_domains::HoldIdentifier<Runtime> for HoldIdentifierWrapper {
349    fn staking_staked() -> Self {
350        Self(HoldIdentifier::DomainStaking)
351    }
352
353    fn domain_instantiation_id() -> Self {
354        Self(HoldIdentifier::DomainInstantiation)
355    }
356
357    fn storage_fund_withdrawal() -> Self {
358        Self(HoldIdentifier::DomainStorageFund)
359    }
360}
361
362impl pallet_messenger::HoldIdentifier<Runtime> for HoldIdentifierWrapper {
363    fn messenger_channel() -> Self {
364        Self(HoldIdentifier::MessengerChannel)
365    }
366}
367
368impl VariantCount for HoldIdentifierWrapper {
369    const VARIANT_COUNT: u32 = mem::variant_count::<HoldIdentifier>() as u32;
370}
371
372impl pallet_balances::Config for Runtime {
373    type RuntimeFreezeReason = RuntimeFreezeReason;
374    type MaxLocks = ConstU32<50>;
375    type MaxReserves = ();
376    type ReserveIdentifier = [u8; 8];
377    /// The type for recording an account's balance.
378    type Balance = Balance;
379    /// The ubiquitous event type.
380    type RuntimeEvent = RuntimeEvent;
381    type DustRemoval = ();
382    type ExistentialDeposit = ConstU128<{ 10_000_000_000_000 * SHANNON }>;
383    type AccountStore = System;
384    type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
385    type FreezeIdentifier = ();
386    type MaxFreezes = ();
387    type RuntimeHoldReason = HoldIdentifierWrapper;
388    type DoneSlashHandler = ();
389}
390
391pub struct CreditSupply;
392
393impl Get<Balance> for CreditSupply {
394    fn get() -> Balance {
395        Balances::total_issuance()
396    }
397}
398
399pub struct TotalSpacePledged;
400
401impl Get<u128> for TotalSpacePledged {
402    fn get() -> u128 {
403        // Operations reordered to avoid data loss, but essentially are:
404        // u64::MAX * SlotProbability / (solution_range / PIECE_SIZE)
405        u128::from(u64::MAX)
406            .saturating_mul(Piece::SIZE as u128)
407            .saturating_mul(u128::from(SlotProbability::get().0))
408            / u128::from(Subspace::solution_ranges().current)
409            / u128::from(SlotProbability::get().1)
410    }
411}
412
413pub struct BlockchainHistorySize;
414
415impl Get<u128> for BlockchainHistorySize {
416    fn get() -> u128 {
417        u128::from(Subspace::archived_history_size())
418    }
419}
420
421impl pallet_transaction_fees::Config for Runtime {
422    type RuntimeEvent = RuntimeEvent;
423    type MinReplicationFactor = ConstU16<MIN_REPLICATION_FACTOR>;
424    type CreditSupply = CreditSupply;
425    type TotalSpacePledged = TotalSpacePledged;
426    type BlockchainHistorySize = BlockchainHistorySize;
427    type Currency = Balances;
428    type FindBlockRewardAddress = Subspace;
429    type DynamicCostOfStorage = ConstBool<false>;
430    type WeightInfo = pallet_transaction_fees::weights::SubstrateWeight<Runtime>;
431}
432
433pub struct TransactionByteFee;
434
435impl Get<Balance> for TransactionByteFee {
436    fn get() -> Balance {
437        TransactionFees::transaction_byte_fee()
438    }
439}
440
441pub struct LiquidityInfo {
442    storage_fee: Balance,
443    imbalance: NegativeImbalance<Runtime>,
444}
445
446/// Implementation of [`pallet_transaction_payment::OnChargeTransaction`] that charges transaction
447/// fees and distributes storage/compute fees and tip separately.
448pub struct OnChargeTransaction;
449
450impl pallet_transaction_payment::OnChargeTransaction<Runtime> for OnChargeTransaction {
451    type LiquidityInfo = Option<LiquidityInfo>;
452    type Balance = Balance;
453
454    fn withdraw_fee(
455        who: &AccountId,
456        call: &RuntimeCall,
457        _info: &DispatchInfoOf<RuntimeCall>,
458        fee: Self::Balance,
459        tip: Self::Balance,
460    ) -> Result<Self::LiquidityInfo, TransactionValidityError> {
461        if fee.is_zero() {
462            return Ok(None);
463        }
464
465        let withdraw_reason = if tip.is_zero() {
466            WithdrawReasons::TRANSACTION_PAYMENT
467        } else {
468            WithdrawReasons::TRANSACTION_PAYMENT | WithdrawReasons::TIP
469        };
470
471        let withdraw_result =
472            Balances::withdraw(who, fee, withdraw_reason, ExistenceRequirement::KeepAlive);
473        let imbalance = withdraw_result.map_err(|_error| InvalidTransaction::Payment)?;
474
475        // Separate storage fee while we have access to the call data structure to calculate it.
476        let storage_fee = TransactionByteFee::get()
477            * Balance::try_from(call.encoded_size())
478                .expect("Size of the call never exceeds balance units; qed");
479
480        Ok(Some(LiquidityInfo {
481            storage_fee,
482            imbalance,
483        }))
484    }
485
486    fn correct_and_deposit_fee(
487        who: &AccountId,
488        _dispatch_info: &DispatchInfoOf<RuntimeCall>,
489        _post_info: &PostDispatchInfoOf<RuntimeCall>,
490        corrected_fee: Self::Balance,
491        tip: Self::Balance,
492        liquidity_info: Self::LiquidityInfo,
493    ) -> Result<(), TransactionValidityError> {
494        if let Some(LiquidityInfo {
495            storage_fee,
496            imbalance,
497        }) = liquidity_info
498        {
499            // Calculate how much refund we should return
500            let refund_amount = imbalance.peek().saturating_sub(corrected_fee);
501            // Refund to the account that paid the fees. If this fails, the account might have
502            // dropped below the existential balance. In that case we don't refund anything.
503            let refund_imbalance = Balances::deposit_into_existing(who, refund_amount)
504                .unwrap_or_else(|_| <Balances as Currency<AccountId>>::PositiveImbalance::zero());
505            // Merge the imbalance caused by paying the fees and refunding parts of it again.
506            let adjusted_paid = imbalance
507                .offset(refund_imbalance)
508                .same()
509                .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
510
511            // Split the tip from the total fee that ended up being paid.
512            let (tip, fee) = adjusted_paid.split(tip);
513            // Split paid storage and compute fees so that they can be distributed separately.
514            let (paid_storage_fee, paid_compute_fee) = fee.split(storage_fee);
515
516            TransactionFees::note_transaction_fees(
517                paid_storage_fee.peek(),
518                paid_compute_fee.peek(),
519                tip.peek(),
520            );
521        }
522        Ok(())
523    }
524
525    fn can_withdraw_fee(
526        who: &AccountId,
527        _call: &RuntimeCall,
528        _dispatch_info: &DispatchInfoOf<RuntimeCall>,
529        fee: Self::Balance,
530        _tip: Self::Balance,
531    ) -> Result<(), TransactionValidityError> {
532        if fee.is_zero() {
533            return Ok(());
534        }
535
536        match Balances::can_withdraw(who, fee) {
537            WithdrawConsequence::Success => Ok(()),
538            _ => Err(InvalidTransaction::Payment.into()),
539        }
540    }
541
542    #[cfg(feature = "runtime-benchmarks")]
543    fn endow_account(who: &AccountId, amount: Self::Balance) {
544        Balances::set_balance(who, amount);
545    }
546
547    #[cfg(feature = "runtime-benchmarks")]
548    fn minimum_balance() -> Self::Balance {
549        <Balances as Currency<AccountId>>::minimum_balance()
550    }
551}
552
553impl pallet_transaction_payment::Config for Runtime {
554    type RuntimeEvent = RuntimeEvent;
555    type OnChargeTransaction = OnChargeTransaction;
556    type OperationalFeeMultiplier = ConstU8<5>;
557    type WeightToFee = ConstantMultiplier<Balance, TransactionWeightFee>;
558    type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
559    type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime, TargetBlockFullness>;
560    type WeightInfo = pallet_transaction_payment::weights::SubstrateWeight<Runtime>;
561}
562
563impl pallet_utility::Config for Runtime {
564    type RuntimeEvent = RuntimeEvent;
565    type RuntimeCall = RuntimeCall;
566    type PalletsOrigin = OriginCaller;
567    type WeightInfo = pallet_utility::weights::SubstrateWeight<Runtime>;
568}
569
570impl MaybeBalancesCall<Runtime> for RuntimeCall {
571    fn maybe_balance_call(&self) -> Option<&pallet_balances::Call<Runtime>> {
572        match self {
573            RuntimeCall::Balances(call) => Some(call),
574            _ => None,
575        }
576    }
577}
578
579impl BalanceTransferChecks for Runtime {
580    fn is_balance_transferable() -> bool {
581        RuntimeConfigs::enable_balance_transfers()
582    }
583}
584
585impl MaybeMultisigCall<Runtime> for RuntimeCall {
586    /// If this call is a `pallet_multisig::Call<Runtime>` call, returns the inner call.
587    fn maybe_multisig_call(&self) -> Option<&pallet_multisig::Call<Runtime>> {
588        match self {
589            RuntimeCall::Multisig(call) => Some(call),
590            _ => None,
591        }
592    }
593}
594
595impl MaybeUtilityCall<Runtime> for RuntimeCall {
596    /// If this call is a `pallet_utility::Call<Runtime>` call, returns the inner call.
597    fn maybe_utility_call(&self) -> Option<&pallet_utility::Call<Runtime>> {
598        match self {
599            RuntimeCall::Utility(call) => Some(call),
600            _ => None,
601        }
602    }
603}
604
605impl MaybeNestedCall<Runtime> for RuntimeCall {
606    /// If this call is a nested runtime call, returns the inner call(s).
607    ///
608    /// Ignored calls (such as `pallet_utility::Call::__Ignore`) should be yielded themsevles, but
609    /// their contents should not be yielded.
610    fn maybe_nested_call(&self) -> Option<Vec<&RuntimeCallFor<Runtime>>> {
611        // We currently ignore privileged calls, because privileged users can already change
612        // runtime code. This includes sudo, collective, and scheduler nested `RuntimeCall`s,
613        // and democracy nested `BoundedCall`s.
614
615        // It is ok to return early, because each call can only belong to one pallet.
616        let calls = self.maybe_nested_utility_calls();
617        if calls.is_some() {
618            return calls;
619        }
620
621        let calls = self.maybe_nested_multisig_calls();
622        if calls.is_some() {
623            return calls;
624        }
625
626        None
627    }
628}
629
630impl pallet_sudo::Config for Runtime {
631    type RuntimeEvent = RuntimeEvent;
632    type RuntimeCall = RuntimeCall;
633    type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
634}
635
636parameter_types! {
637    pub SelfChainId: ChainId = ChainId::Consensus;
638}
639
640pub struct MmrProofVerifier;
641
642impl sp_subspace_mmr::MmrProofVerifier<mmr::Hash, NumberFor<Block>, Hash> for MmrProofVerifier {
643    fn verify_proof_and_extract_leaf(
644        mmr_leaf_proof: ConsensusChainMmrLeafProof<NumberFor<Block>, Hash, mmr::Hash>,
645    ) -> Option<mmr::Leaf> {
646        let mmr_root = SubspaceMmr::mmr_root_hash(mmr_leaf_proof.consensus_block_number)?;
647        Self::verify_proof_stateless(mmr_root, mmr_leaf_proof)
648    }
649
650    fn verify_proof_stateless(
651        mmr_root: mmr::Hash,
652        mmr_leaf_proof: ConsensusChainMmrLeafProof<NumberFor<Block>, Hash, mmr::Hash>,
653    ) -> Option<mmr::Leaf> {
654        let ConsensusChainMmrLeafProof {
655            opaque_mmr_leaf,
656            proof,
657            ..
658        } = mmr_leaf_proof;
659
660        pallet_mmr::verify_leaves_proof::<mmr::Hashing, _>(
661            mmr_root,
662            vec![mmr::DataOrHash::Data(
663                EncodableOpaqueLeaf(opaque_mmr_leaf.0.clone()).into_opaque_leaf(),
664            )],
665            proof,
666        )
667        .ok()?;
668
669        let leaf: mmr::Leaf = opaque_mmr_leaf.into_opaque_leaf().try_decode()?;
670
671        Some(leaf)
672    }
673}
674
675pub struct StorageKeys;
676
677impl sp_messenger::StorageKeys for StorageKeys {
678    fn confirmed_domain_block_storage_key(domain_id: DomainId) -> Option<Vec<u8>> {
679        Some(Domains::confirmed_domain_block_storage_key(domain_id))
680    }
681
682    fn outbox_storage_key(chain_id: ChainId, message_key: MessageKey) -> Option<Vec<u8>> {
683        get_storage_key(StorageKeyRequest::OutboxStorageKey {
684            chain_id,
685            message_key,
686        })
687    }
688
689    fn inbox_responses_storage_key(chain_id: ChainId, message_key: MessageKey) -> Option<Vec<u8>> {
690        get_storage_key(StorageKeyRequest::InboxResponseStorageKey {
691            chain_id,
692            message_key,
693        })
694    }
695}
696
697pub struct DomainRegistration;
698impl sp_messenger::DomainRegistration for DomainRegistration {
699    fn is_domain_registered(domain_id: DomainId) -> bool {
700        Domains::is_domain_registered(domain_id)
701    }
702}
703
704parameter_types! {
705    pub const ChannelReserveFee: Balance = AI3;
706    pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20);
707    pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES;
708}
709
710// ensure the max outgoing messages is not 0.
711const_assert!(MaxOutgoingMessages::get() >= 1);
712
713pub struct OnXDMRewards;
714
715impl sp_messenger::OnXDMRewards<Balance> for OnXDMRewards {
716    fn on_xdm_rewards(reward: Balance) {
717        if let Some(block_author) = Subspace::find_block_reward_address() {
718            let _ = Balances::deposit_creating(&block_author, reward);
719        }
720    }
721
722    fn on_chain_protocol_fees(chain_id: ChainId, fees: Balance) {
723        // on consensus chain, reward the domain operators
724        // balance is already on this consensus runtime
725        if let ChainId::Domain(domain_id) = chain_id {
726            Domains::reward_domain_operators(domain_id, fees)
727        }
728    }
729}
730
731impl pallet_messenger::Config for Runtime {
732    type RuntimeEvent = RuntimeEvent;
733    type SelfChainId = SelfChainId;
734
735    fn get_endpoint_handler(endpoint: &Endpoint) -> Option<Box<dyn EndpointHandlerT<MessageId>>> {
736        if endpoint == &Endpoint::Id(TransporterEndpointId::get()) {
737            Some(Box::new(EndpointHandler(PhantomData::<Runtime>)))
738        } else {
739            None
740        }
741    }
742
743    type Currency = Balances;
744    type WeightInfo = pallet_messenger::weights::SubstrateWeight<Runtime>;
745    type WeightToFee = ConstantMultiplier<Balance, TransactionWeightFee>;
746    type AdjustedWeightToFee = XdmAdjustedWeightToFee<Runtime>;
747    type FeeMultiplier = XdmFeeMultipler;
748    type OnXDMRewards = OnXDMRewards;
749    type MmrHash = mmr::Hash;
750    type MmrProofVerifier = MmrProofVerifier;
751    type StorageKeys = StorageKeys;
752    type DomainOwner = Domains;
753    type HoldIdentifier = HoldIdentifierWrapper;
754    type ChannelReserveFee = ChannelReserveFee;
755    type ChannelInitReservePortion = ChannelInitReservePortion;
756    type DomainRegistration = DomainRegistration;
757    type MaxOutgoingMessages = MaxOutgoingMessages;
758    type MessengerOrigin = pallet_messenger::EnsureMessengerOrigin;
759    type NoteChainTransfer = Transporter;
760    type ExtensionWeightInfo = pallet_messenger::extensions::weights::SubstrateWeight<
761        Runtime,
762        pallet_messenger::extensions::WeightsFromConsensus<Runtime>,
763        pallet_messenger::extensions::WeightsFromDomains<Runtime>,
764    >;
765}
766
767impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
768where
769    RuntimeCall: From<C>,
770{
771    type Extrinsic = UncheckedExtrinsic;
772    type RuntimeCall = RuntimeCall;
773}
774
775impl<C> subspace_runtime_primitives::CreateUnsigned<C> for Runtime
776where
777    RuntimeCall: From<C>,
778{
779    fn create_unsigned(call: Self::RuntimeCall) -> Self::Extrinsic {
780        create_unsigned_general_extrinsic(call)
781    }
782}
783
784parameter_types! {
785    pub const TransporterEndpointId: EndpointId = 1;
786    pub const MinimumTransfer: Balance = 1;
787}
788
789impl pallet_transporter::Config for Runtime {
790    type RuntimeEvent = RuntimeEvent;
791    type SelfChainId = SelfChainId;
792    type SelfEndpointId = TransporterEndpointId;
793    type Currency = Balances;
794    type Sender = Messenger;
795    type AccountIdConverter = AccountIdConverter;
796    type WeightInfo = pallet_transporter::weights::SubstrateWeight<Runtime>;
797    type MinimumTransfer = MinimumTransfer;
798}
799
800pub struct BlockTreePruningDepth;
801impl Get<BlockNumber> for BlockTreePruningDepth {
802    fn get() -> BlockNumber {
803        pallet_runtime_configs::DomainBlockPruningDepth::<Runtime>::get()
804    }
805}
806
807pub struct StakeWithdrawalLockingPeriod;
808impl Get<BlockNumber> for StakeWithdrawalLockingPeriod {
809    fn get() -> BlockNumber {
810        pallet_runtime_configs::StakingWithdrawalPeriod::<Runtime>::get()
811    }
812}
813
814parameter_types! {
815    pub const MaximumReceiptDrift: BlockNumber = 2;
816    pub const InitialDomainTxRange: u64 = INITIAL_DOMAIN_TX_RANGE;
817    pub const DomainTxRangeAdjustmentInterval: u64 = 100;
818    pub const MinOperatorStake: Balance = 100 * AI3;
819    pub const MinNominatorStake: Balance = AI3;
820    /// Use the consensus chain's `Normal` extrinsics block size limit as the domain block size limit
821    pub MaxDomainBlockSize: u32 = NORMAL_DISPATCH_RATIO * MAX_BLOCK_LENGTH;
822    /// Use the consensus chain's `Normal` extrinsics block weight limit as the domain block weight limit
823    pub MaxDomainBlockWeight: Weight = NORMAL_DISPATCH_RATIO * BLOCK_WEIGHT_FOR_2_SEC;
824    pub const DomainInstantiationDeposit: Balance = 100 * AI3;
825    pub const MaxDomainNameLength: u32 = 32;
826    pub const StakeEpochDuration: DomainNumber = 5;
827    pub TreasuryAccount: AccountId = PalletId(*b"treasury").into_account_truncating();
828    pub const MaxPendingStakingOperation: u32 = 512;
829    pub const DomainsPalletId: PalletId = PalletId(*b"domains_");
830    pub const MaxInitialDomainAccounts: u32 = 20;
831    pub const MinInitialDomainAccountBalance: Balance = AI3;
832    pub const BundleLongevity: u32 = 5;
833    pub const WithdrawalLimit: u32 = 32;
834    pub const CurrentBundleAndExecutionReceiptVersion: BundleAndExecutionReceiptVersion = BundleAndExecutionReceiptVersion {
835        bundle_version: BundleVersion::V0,
836        execution_receipt_version: ExecutionReceiptVersion::V0,
837    };
838    pub const OperatorActivationDelayInEpochs: EpochIndex = 5;
839}
840
841// `BlockSlotCount` must at least keep the slot for the current and the parent block, it also need to
842// keep enough block slot for bundle validation
843const_assert!(BlockSlotCount::get() >= 2 && BlockSlotCount::get() > BundleLongevity::get());
844
845// `BlockHashCount` must greater than `BlockSlotCount` because we need to use the block number found
846// with `BlockSlotCount` to get the block hash.
847const_assert!(BlockHashCount::get() > BlockSlotCount::get());
848
849// Minimum operator stake must be >= minimum nominator stake since operator is also a nominator.
850const_assert!(MinOperatorStake::get() >= MinNominatorStake::get());
851
852pub struct BlockSlot;
853
854impl pallet_domains::BlockSlot<Runtime> for BlockSlot {
855    fn future_slot(block_number: BlockNumber) -> Option<Slot> {
856        let block_slots = Subspace::block_slots();
857        block_slots
858            .get(&block_number)
859            .map(|slot| *slot + Slot::from(BlockAuthoringDelay::get()))
860    }
861
862    fn slot_produced_after(to_check: Slot) -> Option<BlockNumber> {
863        let block_slots = Subspace::block_slots();
864        for (block_number, slot) in block_slots.into_iter().rev() {
865            if to_check > slot {
866                return Some(block_number);
867            }
868        }
869        None
870    }
871}
872
873pub struct OnChainRewards;
874
875impl sp_domains::OnChainRewards<Balance> for OnChainRewards {
876    fn on_chain_rewards(chain_id: ChainId, reward: Balance) {
877        match chain_id {
878            ChainId::Consensus => {
879                if let Some(block_author) = Subspace::find_block_reward_address() {
880                    let _ = Balances::deposit_creating(&block_author, reward);
881                }
882            }
883            ChainId::Domain(domain_id) => Domains::reward_domain_operators(domain_id, reward),
884        }
885    }
886}
887
888impl pallet_domains::Config for Runtime {
889    type RuntimeEvent = RuntimeEvent;
890    type DomainOrigin = pallet_domains::EnsureDomainOrigin;
891    type DomainHash = DomainHash;
892    type Balance = Balance;
893    type DomainHeader = DomainHeader;
894    type ConfirmationDepthK = ConfirmationDepthK;
895    type Currency = Balances;
896    type Share = Balance;
897    type HoldIdentifier = HoldIdentifierWrapper;
898    type BlockTreePruningDepth = BlockTreePruningDepth;
899    type ConsensusSlotProbability = SlotProbability;
900    type MaxDomainBlockSize = MaxDomainBlockSize;
901    type MaxDomainBlockWeight = MaxDomainBlockWeight;
902    type MaxDomainNameLength = MaxDomainNameLength;
903    type DomainInstantiationDeposit = DomainInstantiationDeposit;
904    type WeightInfo = pallet_domains::weights::SubstrateWeight<Runtime>;
905    type InitialDomainTxRange = InitialDomainTxRange;
906    type DomainTxRangeAdjustmentInterval = DomainTxRangeAdjustmentInterval;
907    type MinOperatorStake = MinOperatorStake;
908    type MinNominatorStake = MinNominatorStake;
909    type StakeWithdrawalLockingPeriod = StakeWithdrawalLockingPeriod;
910    type StakeEpochDuration = StakeEpochDuration;
911    type TreasuryAccount = TreasuryAccount;
912    type MaxPendingStakingOperation = MaxPendingStakingOperation;
913    type Randomness = Subspace;
914    type PalletId = DomainsPalletId;
915    type StorageFee = TransactionFees;
916    type BlockTimestamp = pallet_timestamp::Pallet<Runtime>;
917    type BlockSlot = BlockSlot;
918    type DomainsTransfersTracker = Transporter;
919    type MaxInitialDomainAccounts = MaxInitialDomainAccounts;
920    type MinInitialDomainAccountBalance = MinInitialDomainAccountBalance;
921    type BundleLongevity = BundleLongevity;
922    type DomainBundleSubmitted = Messenger;
923    type OnDomainInstantiated = Messenger;
924    type MmrHash = mmr::Hash;
925    type MmrProofVerifier = MmrProofVerifier;
926    type FraudProofStorageKeyProvider = StorageKeyProvider;
927    type OnChainRewards = OnChainRewards;
928    type WithdrawalLimit = WithdrawalLimit;
929    type CurrentBundleAndExecutionReceiptVersion = CurrentBundleAndExecutionReceiptVersion;
930    type OperatorActivationDelayInEpochs = OperatorActivationDelayInEpochs;
931}
932
933parameter_types! {
934    pub const AvgBlockspaceUsageNumBlocks: BlockNumber = 100;
935    pub const ProposerTaxOnVotes: (u32, u32) = (1, 10);
936}
937
938impl pallet_rewards::Config for Runtime {
939    type RuntimeEvent = RuntimeEvent;
940    type Currency = Balances;
941    type AvgBlockspaceUsageNumBlocks = AvgBlockspaceUsageNumBlocks;
942    type TransactionByteFee = TransactionByteFee;
943    type MaxRewardPoints = ConstU32<20>;
944    type ProposerTaxOnVotes = ProposerTaxOnVotes;
945    type RewardsEnabled = Subspace;
946    type FindBlockRewardAddress = Subspace;
947    type FindVotingRewardAddresses = Subspace;
948    type WeightInfo = pallet_rewards::weights::SubstrateWeight<Runtime>;
949    type OnReward = ();
950}
951
952pub mod mmr {
953    use super::Runtime;
954    pub use pallet_mmr::primitives::*;
955
956    pub type Leaf = <<Runtime as pallet_mmr::Config>::LeafData as LeafDataProvider>::LeafData;
957    pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
958    pub type Hash = <Hashing as sp_runtime::traits::Hash>::Output;
959}
960
961pub struct BlockHashProvider;
962
963impl pallet_mmr::BlockHashProvider<BlockNumber, Hash> for BlockHashProvider {
964    fn block_hash(block_number: BlockNumber) -> Hash {
965        sp_subspace_mmr::subspace_mmr_runtime_interface::consensus_block_hash(block_number)
966            .expect("Hash must exist for a given block number.")
967    }
968}
969
970impl pallet_mmr::Config for Runtime {
971    const INDEXING_PREFIX: &'static [u8] = mmr::INDEXING_PREFIX;
972    type Hashing = Keccak256;
973    type LeafData = SubspaceMmr;
974    type OnNewRoot = SubspaceMmr;
975    type BlockHashProvider = BlockHashProvider;
976    type WeightInfo = ();
977    #[cfg(feature = "runtime-benchmarks")]
978    type BenchmarkHelper = ();
979}
980
981parameter_types! {
982    pub const MmrRootHashCount: u32 = 15;
983}
984
985impl pallet_subspace_mmr::Config for Runtime {
986    type MmrRootHash = mmr::Hash;
987    type MmrRootHashCount = MmrRootHashCount;
988}
989
990impl pallet_runtime_configs::Config for Runtime {
991    type WeightInfo = pallet_runtime_configs::weights::SubstrateWeight<Runtime>;
992}
993
994impl pallet_domains::extensions::DomainsCheck for Runtime {
995    fn is_domains_enabled() -> bool {
996        RuntimeConfigs::enable_domains()
997    }
998}
999
1000parameter_types! {
1001    pub const MaxSignatories: u32 = 100;
1002}
1003
1004macro_rules! deposit {
1005    ($name:ident, $item_fee:expr, $items:expr, $bytes:expr) => {
1006        pub struct $name;
1007
1008        impl Get<Balance> for $name {
1009            fn get() -> Balance {
1010                $item_fee.saturating_mul($items.into()).saturating_add(
1011                    TransactionFees::transaction_byte_fee().saturating_mul($bytes.into()),
1012                )
1013            }
1014        }
1015    };
1016}
1017
1018// One storage item; key size is 32; value is size 4+4+16+32 bytes = 56 bytes.
1019// Each multisig costs 20 AI3 + bytes_of_storge * TransactionByteFee
1020deposit!(DepositBaseFee, 20 * AI3, 1u32, 88u32);
1021
1022// Additional storage item size of 32 bytes.
1023deposit!(DepositFactor, 0u128, 0u32, 32u32);
1024
1025impl pallet_multisig::Config for Runtime {
1026    type RuntimeEvent = RuntimeEvent;
1027    type RuntimeCall = RuntimeCall;
1028    type Currency = Balances;
1029    type DepositBase = DepositBaseFee;
1030    type DepositFactor = DepositFactor;
1031    type MaxSignatories = MaxSignatories;
1032    type WeightInfo = pallet_multisig::weights::SubstrateWeight<Runtime>;
1033}
1034
1035construct_runtime!(
1036    pub struct Runtime {
1037        System: frame_system = 0,
1038        Timestamp: pallet_timestamp = 1,
1039
1040        Subspace: pallet_subspace = 2,
1041        Rewards: pallet_rewards = 9,
1042
1043        Balances: pallet_balances = 4,
1044        TransactionFees: pallet_transaction_fees = 12,
1045        TransactionPayment: pallet_transaction_payment = 5,
1046        Utility: pallet_utility = 8,
1047
1048        Domains: pallet_domains = 11,
1049        RuntimeConfigs: pallet_runtime_configs = 14,
1050
1051        Mmr: pallet_mmr = 30,
1052        SubspaceMmr: pallet_subspace_mmr = 31,
1053
1054        // messenger stuff
1055        // Note: Indexes should match with indexes on other chains and domains
1056        Messenger: pallet_messenger exclude_parts { Inherent } = 60,
1057        Transporter: pallet_transporter = 61,
1058
1059        // Multisig
1060        Multisig: pallet_multisig = 90,
1061
1062        // Reserve some room for other pallets as we'll remove sudo pallet eventually.
1063        Sudo: pallet_sudo = 100,
1064    }
1065);
1066
1067/// The address format for describing accounts.
1068pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
1069/// Block header type as expected by this runtime.
1070pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
1071/// Block type as expected by this runtime.
1072pub type Block = generic::Block<Header, UncheckedExtrinsic>;
1073/// The SignedExtension to the basic transaction logic.
1074pub type SignedExtra = (
1075    frame_system::CheckNonZeroSender<Runtime>,
1076    frame_system::CheckSpecVersion<Runtime>,
1077    frame_system::CheckTxVersion<Runtime>,
1078    frame_system::CheckGenesis<Runtime>,
1079    frame_system::CheckMortality<Runtime>,
1080    frame_system::CheckNonce<Runtime>,
1081    frame_system::CheckWeight<Runtime>,
1082    pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
1083    BalanceTransferCheckExtension<Runtime>,
1084    pallet_subspace::extensions::SubspaceExtension<Runtime>,
1085    pallet_domains::extensions::DomainsExtension<Runtime>,
1086    pallet_messenger::extensions::MessengerExtension<Runtime>,
1087);
1088/// Unchecked extrinsic type as expected by this runtime.
1089pub type UncheckedExtrinsic =
1090    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
1091/// Executive: handles dispatch to the various modules.
1092pub type Executive = frame_executive::Executive<
1093    Runtime,
1094    Block,
1095    frame_system::ChainContext<Runtime>,
1096    Runtime,
1097    AllPalletsWithSystem,
1098>;
1099/// The payload being signed in transactions.
1100pub type SignedPayload = generic::SignedPayload<RuntimeCall, SignedExtra>;
1101
1102impl pallet_subspace::extensions::MaybeSubspaceCall<Runtime> for RuntimeCall {
1103    fn maybe_subspace_call(&self) -> Option<&pallet_subspace::Call<Runtime>> {
1104        match self {
1105            RuntimeCall::Subspace(call) => Some(call),
1106            _ => None,
1107        }
1108    }
1109}
1110
1111impl pallet_domains::extensions::MaybeDomainsCall<Runtime> for RuntimeCall {
1112    fn maybe_domains_call(&self) -> Option<&pallet_domains::Call<Runtime>> {
1113        match self {
1114            RuntimeCall::Domains(call) => Some(call),
1115            _ => None,
1116        }
1117    }
1118}
1119
1120impl pallet_messenger::extensions::MaybeMessengerCall<Runtime> for RuntimeCall {
1121    fn maybe_messenger_call(&self) -> Option<&pallet_messenger::Call<Runtime>> {
1122        match self {
1123            RuntimeCall::Messenger(call) => Some(call),
1124            _ => None,
1125        }
1126    }
1127}
1128
1129fn extract_segment_headers(ext: &UncheckedExtrinsic) -> Option<Vec<SegmentHeader>> {
1130    match &ext.function {
1131        RuntimeCall::Subspace(pallet_subspace::Call::store_segment_headers { segment_headers }) => {
1132            Some(segment_headers.clone())
1133        }
1134        _ => None,
1135    }
1136}
1137
1138fn is_xdm_mmr_proof_valid(ext: &ExtrinsicFor<Block>) -> Option<bool> {
1139    match &ext.function {
1140        RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })
1141        | RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
1142            let ConsensusChainMmrLeafProof {
1143                consensus_block_number,
1144                opaque_mmr_leaf,
1145                proof,
1146                ..
1147            } = msg.proof.consensus_mmr_proof();
1148
1149            let mmr_root = SubspaceMmr::mmr_root_hash(consensus_block_number)?;
1150
1151            Some(
1152                pallet_mmr::verify_leaves_proof::<mmr::Hashing, _>(
1153                    mmr_root,
1154                    vec![mmr::DataOrHash::Data(
1155                        EncodableOpaqueLeaf(opaque_mmr_leaf.0.clone()).into_opaque_leaf(),
1156                    )],
1157                    proof,
1158                )
1159                .is_ok(),
1160            )
1161        }
1162        _ => None,
1163    }
1164}
1165
1166// This code must be kept in sync with `crates/subspace-runtime/src/object_mapping.rs`.
1167fn extract_utility_block_object_mapping(
1168    mut base_offset: u32,
1169    objects: &mut Vec<BlockObject>,
1170    call: &pallet_utility::Call<Runtime>,
1171    mut recursion_depth_left: u16,
1172) {
1173    if recursion_depth_left == 0 {
1174        return;
1175    }
1176
1177    recursion_depth_left -= 1;
1178
1179    // Add enum variant to the base offset.
1180    base_offset += 1;
1181
1182    match call {
1183        pallet_utility::Call::batch { calls }
1184        | pallet_utility::Call::batch_all { calls }
1185        | pallet_utility::Call::force_batch { calls } => {
1186            base_offset += Compact::compact_len(&(calls.len() as u32)) as u32;
1187
1188            for call in calls {
1189                extract_call_block_object_mapping(base_offset, objects, call, recursion_depth_left);
1190
1191                base_offset += call.encoded_size() as u32;
1192            }
1193        }
1194        pallet_utility::Call::as_derivative { index, call } => {
1195            base_offset += index.encoded_size() as u32;
1196
1197            extract_call_block_object_mapping(
1198                base_offset,
1199                objects,
1200                call.as_ref(),
1201                recursion_depth_left,
1202            );
1203        }
1204        pallet_utility::Call::dispatch_as { as_origin, call } => {
1205            base_offset += as_origin.encoded_size() as u32;
1206
1207            extract_call_block_object_mapping(
1208                base_offset,
1209                objects,
1210                call.as_ref(),
1211                recursion_depth_left,
1212            );
1213        }
1214        pallet_utility::Call::with_weight { call, .. } => {
1215            extract_call_block_object_mapping(
1216                base_offset,
1217                objects,
1218                call.as_ref(),
1219                recursion_depth_left,
1220            );
1221        }
1222        pallet_utility::Call::__Ignore(_, _) => {
1223            // Ignore.
1224        }
1225    }
1226}
1227
1228fn extract_call_block_object_mapping(
1229    mut base_offset: u32,
1230    objects: &mut Vec<BlockObject>,
1231    call: &RuntimeCall,
1232    recursion_depth_left: u16,
1233) {
1234    // Add RuntimeCall enum variant to the base offset.
1235    base_offset += 1;
1236
1237    match call {
1238        // Extract the actual object mappings.
1239        RuntimeCall::System(frame_system::Call::remark { remark }) => {
1240            objects.push(BlockObject {
1241                hash: hashes::blake3_hash(remark),
1242                // Add frame_system::Call enum variant to the base offset.
1243                offset: base_offset + 1,
1244            });
1245        }
1246        RuntimeCall::System(frame_system::Call::remark_with_event { remark }) => {
1247            objects.push(BlockObject {
1248                hash: hashes::blake3_hash(remark),
1249                // Add frame_system::Call enum variant to the base offset.
1250                offset: base_offset + 1,
1251            });
1252        }
1253
1254        // Recursively extract object mappings for the call.
1255        RuntimeCall::Utility(call) => {
1256            extract_utility_block_object_mapping(base_offset, objects, call, recursion_depth_left)
1257        }
1258        // Other calls don't contain object mappings.
1259        _ => {}
1260    }
1261}
1262
1263fn extract_block_object_mapping(block: Block) -> BlockObjectMapping {
1264    let mut block_object_mapping = BlockObjectMapping::default();
1265    let mut base_offset =
1266        block.header.encoded_size() + Compact::compact_len(&(block.extrinsics.len() as u32));
1267    for extrinsic in block.extrinsics {
1268        let preamble_size = extrinsic.preamble.encoded_size();
1269        // Extrinsic starts with vector length followed by preamble and
1270        // `function` encoding.
1271        let base_extrinsic_offset = base_offset
1272            + Compact::compact_len(&((preamble_size + extrinsic.function.encoded_size()) as u32))
1273            + preamble_size;
1274
1275        extract_call_block_object_mapping(
1276            base_extrinsic_offset as u32,
1277            block_object_mapping.objects_mut(),
1278            &extrinsic.function,
1279            MAX_CALL_RECURSION_DEPTH as u16,
1280        );
1281
1282        base_offset += extrinsic.encoded_size();
1283    }
1284
1285    block_object_mapping
1286}
1287
1288fn extract_successful_bundles(
1289    domain_id: DomainId,
1290    extrinsics: Vec<UncheckedExtrinsic>,
1291) -> OpaqueBundles<Block, DomainHeader, Balance> {
1292    let successful_bundles = Domains::successful_bundles(domain_id);
1293    extrinsics
1294        .into_iter()
1295        .filter_map(|uxt| match uxt.function {
1296            RuntimeCall::Domains(pallet_domains::Call::submit_bundle { opaque_bundle })
1297                if opaque_bundle.domain_id() == domain_id
1298                    && successful_bundles.contains(&opaque_bundle.hash()) =>
1299            {
1300                Some(opaque_bundle)
1301            }
1302            _ => None,
1303        })
1304        .collect()
1305}
1306
1307fn create_unsigned_general_extrinsic(call: RuntimeCall) -> UncheckedExtrinsic {
1308    let extra: SignedExtra = (
1309        frame_system::CheckNonZeroSender::<Runtime>::new(),
1310        frame_system::CheckSpecVersion::<Runtime>::new(),
1311        frame_system::CheckTxVersion::<Runtime>::new(),
1312        frame_system::CheckGenesis::<Runtime>::new(),
1313        frame_system::CheckMortality::<Runtime>::from(generic::Era::Immortal),
1314        // for unsigned extrinsic, nonce check will be skipped
1315        // so set a default value
1316        frame_system::CheckNonce::<Runtime>::from(0u32.into()),
1317        frame_system::CheckWeight::<Runtime>::new(),
1318        // for unsigned extrinsic, transaction fee check will be skipped
1319        // so set a default value
1320        pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0u128),
1321        BalanceTransferCheckExtension::<Runtime>::default(),
1322        pallet_subspace::extensions::SubspaceExtension::<Runtime>::new(),
1323        pallet_domains::extensions::DomainsExtension::<Runtime>::new(),
1324        pallet_messenger::extensions::MessengerExtension::<Runtime>::new(),
1325    );
1326
1327    UncheckedExtrinsic::new_transaction(call, extra)
1328}
1329
1330struct RewardAddress([u8; 32]);
1331
1332impl From<PublicKey> for RewardAddress {
1333    #[inline]
1334    fn from(public_key: PublicKey) -> Self {
1335        Self(*public_key)
1336    }
1337}
1338
1339impl From<RewardAddress> for AccountId32 {
1340    #[inline]
1341    fn from(reward_address: RewardAddress) -> Self {
1342        reward_address.0.into()
1343    }
1344}
1345
1346pub struct StorageKeyProvider;
1347impl FraudProofStorageKeyProvider<NumberFor<Block>> for StorageKeyProvider {
1348    fn storage_key(req: FraudProofStorageKeyRequest<NumberFor<Block>>) -> Vec<u8> {
1349        match req {
1350            FraudProofStorageKeyRequest::InvalidInherentExtrinsicData => {
1351                pallet_domains::BlockInherentExtrinsicData::<Runtime>::hashed_key().to_vec()
1352            }
1353            FraudProofStorageKeyRequest::SuccessfulBundles(domain_id) => {
1354                pallet_domains::SuccessfulBundles::<Runtime>::hashed_key_for(domain_id)
1355            }
1356            FraudProofStorageKeyRequest::DomainAllowlistUpdates(domain_id) => {
1357                Messenger::domain_allow_list_update_storage_key(domain_id)
1358            }
1359            FraudProofStorageKeyRequest::DomainRuntimeUpgrades => {
1360                pallet_domains::DomainRuntimeUpgrades::<Runtime>::hashed_key().to_vec()
1361            }
1362            FraudProofStorageKeyRequest::RuntimeRegistry(runtime_id) => {
1363                pallet_domains::RuntimeRegistry::<Runtime>::hashed_key_for(runtime_id)
1364            }
1365            FraudProofStorageKeyRequest::DomainSudoCall(domain_id) => {
1366                pallet_domains::DomainSudoCalls::<Runtime>::hashed_key_for(domain_id)
1367            }
1368            FraudProofStorageKeyRequest::EvmDomainContractCreationAllowedByCall(domain_id) => {
1369                pallet_domains::EvmDomainContractCreationAllowedByCalls::<Runtime>::hashed_key_for(
1370                    domain_id,
1371                )
1372            }
1373            FraudProofStorageKeyRequest::MmrRoot(block_number) => {
1374                pallet_subspace_mmr::MmrRootHashes::<Runtime>::hashed_key_for(block_number)
1375            }
1376        }
1377    }
1378}
1379
1380impl_runtime_apis! {
1381    impl sp_api::Core<Block> for Runtime {
1382        fn version() -> RuntimeVersion {
1383            VERSION
1384        }
1385
1386        fn execute_block(block: Block) {
1387            Executive::execute_block(block);
1388        }
1389
1390        fn initialize_block(header: &HeaderFor<Block>) -> ExtrinsicInclusionMode {
1391            Executive::initialize_block(header)
1392        }
1393    }
1394
1395    impl sp_api::Metadata<Block> for Runtime {
1396        fn metadata() -> OpaqueMetadata {
1397            OpaqueMetadata::new(Runtime::metadata().into())
1398        }
1399
1400        fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
1401            Runtime::metadata_at_version(version)
1402        }
1403
1404        fn metadata_versions() -> Vec<u32> {
1405            Runtime::metadata_versions()
1406        }
1407    }
1408
1409    impl sp_block_builder::BlockBuilder<Block> for Runtime {
1410        fn apply_extrinsic(extrinsic: ExtrinsicFor<Block>) -> ApplyExtrinsicResult {
1411            Executive::apply_extrinsic(extrinsic)
1412        }
1413
1414        fn finalize_block() -> HeaderFor<Block> {
1415            Executive::finalize_block()
1416        }
1417
1418        fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<ExtrinsicFor<Block>> {
1419            data.create_extrinsics()
1420        }
1421
1422        fn check_inherents(
1423            block: Block,
1424            data: sp_inherents::InherentData,
1425        ) -> sp_inherents::CheckInherentsResult {
1426            data.check_extrinsics(&block)
1427        }
1428    }
1429
1430    impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
1431        fn validate_transaction(
1432            source: TransactionSource,
1433            tx: ExtrinsicFor<Block>,
1434            block_hash: BlockHashFor<Block>,
1435        ) -> TransactionValidity {
1436            Executive::validate_transaction(source, tx, block_hash)
1437        }
1438    }
1439
1440    impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
1441        fn offchain_worker(header: &HeaderFor<Block>) {
1442            Executive::offchain_worker(header)
1443        }
1444    }
1445
1446    impl sp_objects::ObjectsApi<Block> for Runtime {
1447        fn extract_block_object_mapping(block: Block) -> BlockObjectMapping {
1448            extract_block_object_mapping(block)
1449        }
1450    }
1451
1452    impl sp_consensus_subspace::SubspaceApi<Block, PublicKey> for Runtime {
1453        fn pot_parameters() -> PotParameters {
1454            Subspace::pot_parameters()
1455        }
1456
1457        fn solution_ranges() -> SolutionRanges {
1458            Subspace::solution_ranges()
1459        }
1460
1461        fn submit_vote_extrinsic(
1462            signed_vote: SignedVote<NumberFor<Block>, BlockHashFor<Block>, PublicKey>,
1463        ) {
1464            let SignedVote { vote, signature } = signed_vote;
1465            let Vote::V0 {
1466                height,
1467                parent_hash,
1468                slot,
1469                solution,
1470                proof_of_time,
1471                future_proof_of_time,
1472            } = vote;
1473
1474            Subspace::submit_vote(SignedVote {
1475                vote: Vote::V0 {
1476                    height,
1477                    parent_hash,
1478                    slot,
1479                    solution: solution.into_reward_address_format::<RewardAddress, AccountId32>(),
1480                    proof_of_time,
1481                    future_proof_of_time,
1482                },
1483                signature,
1484            })
1485        }
1486
1487        fn history_size() -> HistorySize {
1488            <pallet_subspace::Pallet<Runtime>>::history_size()
1489        }
1490
1491        fn max_pieces_in_sector() -> u16 {
1492            MAX_PIECES_IN_SECTOR
1493        }
1494
1495        fn segment_commitment(segment_index: SegmentIndex) -> Option<SegmentCommitment> {
1496            Subspace::segment_commitment(segment_index)
1497        }
1498
1499        fn extract_segment_headers(ext: &ExtrinsicFor<Block>) -> Option<Vec<SegmentHeader >> {
1500            extract_segment_headers(ext)
1501        }
1502
1503        fn is_inherent(ext: &ExtrinsicFor<Block>) -> bool {
1504            match &ext.function {
1505                RuntimeCall::Subspace(call) => Subspace::is_inherent(call),
1506                RuntimeCall::Timestamp(call) => Timestamp::is_inherent(call),
1507                _ => false,
1508            }
1509        }
1510
1511        fn root_plot_public_key() -> Option<PublicKey> {
1512            Subspace::root_plot_public_key()
1513        }
1514
1515        fn should_adjust_solution_range() -> bool {
1516            Subspace::should_adjust_solution_range()
1517        }
1518
1519        fn chain_constants() -> ChainConstants {
1520            ChainConstants::V0 {
1521                confirmation_depth_k: ConfirmationDepthK::get(),
1522                block_authoring_delay: Slot::from(BlockAuthoringDelay::get()),
1523                era_duration: EraDuration::get(),
1524                slot_probability: SlotProbability::get(),
1525                slot_duration: SlotDuration::from_millis(SLOT_DURATION),
1526                recent_segments: RecentSegments::get(),
1527                recent_history_fraction: RecentHistoryFraction::get(),
1528                min_sector_lifetime: MinSectorLifetime::get(),
1529            }
1530        }
1531
1532        fn block_weight() -> Weight {
1533            System::block_weight().total()
1534        }
1535    }
1536
1537    impl sp_domains::DomainsApi<Block, DomainHeader> for Runtime {
1538        fn submit_bundle_unsigned(
1539            opaque_bundle: OpaqueBundle<NumberFor<Block>, BlockHashFor<Block>, DomainHeader, Balance>,
1540        ) {
1541            Domains::submit_bundle_unsigned(opaque_bundle)
1542        }
1543
1544        fn submit_receipt_unsigned(
1545            singleton_receipt: SealedSingletonReceipt<NumberFor<Block>, BlockHashFor<Block>, DomainHeader, Balance>,
1546        ) {
1547            Domains::submit_receipt_unsigned(singleton_receipt)
1548        }
1549
1550        fn extract_successful_bundles(
1551            domain_id: DomainId,
1552            extrinsics: Vec<ExtrinsicFor<Block>>,
1553        ) -> OpaqueBundles<Block, DomainHeader, Balance> {
1554            extract_successful_bundles(domain_id, extrinsics)
1555        }
1556
1557        fn extrinsics_shuffling_seed() -> Randomness {
1558            Randomness::from(Domains::extrinsics_shuffling_seed().to_fixed_bytes())
1559        }
1560
1561        fn domain_runtime_code(domain_id: DomainId) -> Option<Vec<u8>> {
1562            Domains::domain_runtime_code(domain_id)
1563        }
1564
1565        fn runtime_id(domain_id: DomainId) -> Option<sp_domains::RuntimeId> {
1566            Domains::runtime_id(domain_id)
1567        }
1568
1569        fn runtime_upgrades() -> Vec<sp_domains::RuntimeId> {
1570            Domains::runtime_upgrades()
1571        }
1572
1573        fn domain_instance_data(domain_id: DomainId) -> Option<(DomainInstanceData, NumberFor<Block>)> {
1574            Domains::domain_instance_data(domain_id)
1575        }
1576
1577        fn domain_timestamp() -> Moment {
1578            Domains::timestamp()
1579        }
1580
1581        fn consensus_transaction_byte_fee() -> Balance {
1582            Domains::consensus_transaction_byte_fee()
1583        }
1584
1585        fn domain_tx_range(_: DomainId) -> U256 {
1586            U256::MAX
1587        }
1588
1589        fn genesis_state_root(domain_id: DomainId) -> Option<H256> {
1590            Domains::domain_genesis_block_execution_receipt(domain_id)
1591                .map(|er| *er.final_state_root())
1592        }
1593
1594        fn head_receipt_number(domain_id: DomainId) -> DomainNumber {
1595            Domains::head_receipt_number(domain_id)
1596        }
1597
1598        fn oldest_unconfirmed_receipt_number(domain_id: DomainId) -> Option<DomainNumber> {
1599            Domains::oldest_unconfirmed_receipt_number(domain_id)
1600        }
1601
1602        fn domain_bundle_limit(domain_id: DomainId) -> Option<sp_domains::DomainBundleLimit> {
1603            Domains::domain_bundle_limit(domain_id).ok().flatten()
1604        }
1605
1606        fn non_empty_er_exists(domain_id: DomainId) -> bool {
1607            Domains::non_empty_er_exists(domain_id)
1608        }
1609
1610        fn domain_best_number(domain_id: DomainId) -> Option<DomainNumber> {
1611            Domains::domain_best_number(domain_id).ok()
1612        }
1613
1614        fn execution_receipt(receipt_hash: DomainHash) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>> {
1615            Domains::execution_receipt(receipt_hash)
1616        }
1617
1618        fn domain_operators(domain_id: DomainId) -> Option<(BTreeMap<OperatorId, Balance>, Vec<OperatorId>)> {
1619            Domains::domain_staking_summary(domain_id).map(|summary| {
1620                let next_operators = summary.next_operators.into_iter().collect();
1621                (summary.current_operators, next_operators)
1622            })
1623        }
1624
1625        fn receipt_hash(domain_id: DomainId, domain_number: DomainNumber) -> Option<DomainHash> {
1626            Domains::receipt_hash(domain_id, domain_number)
1627        }
1628
1629        fn latest_confirmed_domain_block(domain_id: DomainId) -> Option<(DomainNumber, DomainHash)>{
1630            Domains::latest_confirmed_domain_block(domain_id)
1631        }
1632
1633        fn is_bad_er_pending_to_prune(domain_id: DomainId, receipt_hash: DomainHash) -> bool {
1634            Domains::execution_receipt(receipt_hash).map(
1635                |er| Domains::is_bad_er_pending_to_prune(domain_id, *er.domain_block_number())
1636            )
1637            .unwrap_or(false)
1638        }
1639
1640        fn storage_fund_account_balance(operator_id: OperatorId) -> Balance {
1641            Domains::storage_fund_account_balance(operator_id)
1642        }
1643
1644        fn is_domain_runtime_upgraded_since(domain_id: DomainId, at: NumberFor<Block>) -> Option<bool> {
1645            Domains::is_domain_runtime_upgraded_since(domain_id, at)
1646        }
1647
1648        fn domain_sudo_call(domain_id: DomainId) -> Option<Vec<u8>> {
1649            Domains::domain_sudo_call(domain_id)
1650        }
1651
1652        fn evm_domain_contract_creation_allowed_by_call(domain_id: DomainId) -> Option<PermissionedActionAllowedBy<EthereumAccountId>> {
1653            Domains::evm_domain_contract_creation_allowed_by_call(domain_id)
1654        }
1655
1656        fn last_confirmed_domain_block_receipt(domain_id: DomainId) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>>{
1657            Domains::latest_confirmed_domain_execution_receipt(domain_id)
1658        }
1659
1660        fn current_bundle_and_execution_receipt_version() -> BundleAndExecutionReceiptVersion {
1661            Domains::current_bundle_and_execution_receipt_version()
1662        }
1663
1664        fn genesis_execution_receipt(domain_id: DomainId) -> Option<ExecutionReceiptFor<DomainHeader, Block, Balance>> {
1665            Domains::domain_genesis_block_execution_receipt(domain_id)
1666        }
1667
1668        fn nominator_position(
1669            operator_id: OperatorId,
1670            nominator_account: sp_runtime::AccountId32,
1671        ) -> Option<sp_domains::NominatorPosition<Balance, DomainNumber, Balance>> {
1672            Domains::nominator_position(operator_id, nominator_account)
1673        }
1674
1675        fn block_pruning_depth() -> NumberFor<Block> {
1676            BlockTreePruningDepth::get()
1677        }
1678    }
1679
1680    impl sp_domains::BundleProducerElectionApi<Block, Balance> for Runtime {
1681        fn bundle_producer_election_params(domain_id: DomainId) -> Option<BundleProducerElectionParams<Balance>> {
1682            Domains::bundle_producer_election_params(domain_id)
1683        }
1684
1685        fn operator(operator_id: OperatorId) -> Option<(OperatorPublicKey, Balance)> {
1686            Domains::operator(operator_id)
1687        }
1688    }
1689
1690    impl sp_session::SessionKeys<Block> for Runtime {
1691        fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
1692            SessionKeys::generate(seed)
1693        }
1694
1695        fn decode_session_keys(
1696            encoded: Vec<u8>,
1697        ) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {
1698            SessionKeys::decode_into_raw_public_keys(&encoded)
1699        }
1700    }
1701
1702    impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Nonce> for Runtime {
1703        fn account_nonce(account: AccountId) -> Nonce {
1704            *System::account_nonce(account)
1705        }
1706    }
1707
1708    impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {
1709        fn query_info(
1710            uxt: ExtrinsicFor<Block>,
1711            len: u32,
1712        ) -> pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo<Balance> {
1713            TransactionPayment::query_info(uxt, len)
1714        }
1715        fn query_fee_details(
1716            uxt: ExtrinsicFor<Block>,
1717            len: u32,
1718        ) -> pallet_transaction_payment::FeeDetails<Balance> {
1719            TransactionPayment::query_fee_details(uxt, len)
1720        }
1721        fn query_weight_to_fee(weight: Weight) -> Balance {
1722            TransactionPayment::weight_to_fee(weight)
1723        }
1724        fn query_length_to_fee(length: u32) -> Balance {
1725            TransactionPayment::length_to_fee(length)
1726        }
1727    }
1728
1729    impl sp_messenger::MessengerApi<Block, BlockNumber, BlockHashFor<Block>> for Runtime {
1730        fn is_xdm_mmr_proof_valid(
1731            extrinsic: &ExtrinsicFor<Block>
1732        ) -> Option<bool> {
1733            is_xdm_mmr_proof_valid(extrinsic)
1734        }
1735
1736        fn extract_xdm_mmr_proof(ext: &ExtrinsicFor<Block>) -> Option<ConsensusChainMmrLeafProof<BlockNumber, BlockHashFor<Block>, sp_core::H256>> {
1737            match &ext.function {
1738                RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })
1739                | RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
1740                    Some(msg.proof.consensus_mmr_proof())
1741                }
1742                _ => None,
1743            }
1744        }
1745
1746        fn batch_extract_xdm_mmr_proof(extrinsics: &Vec<ExtrinsicFor<Block>>) -> BTreeMap<u32, ConsensusChainMmrLeafProof<BlockNumber, BlockHashFor<Block>, sp_core::H256>> {
1747            let mut mmr_proofs = BTreeMap::new();
1748            for (index, ext) in extrinsics.iter().enumerate() {
1749                match &ext.function {
1750                    RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })
1751                    | RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
1752                        mmr_proofs.insert(index as u32, msg.proof.consensus_mmr_proof());
1753                    }
1754                    _ => {},
1755                }
1756            }
1757            mmr_proofs
1758        }
1759
1760        fn confirmed_domain_block_storage_key(domain_id: DomainId) -> Vec<u8> {
1761            Domains::confirmed_domain_block_storage_key(domain_id)
1762        }
1763
1764        fn outbox_storage_key(message_key: MessageKey) -> Vec<u8> {
1765            Messenger::outbox_storage_key(message_key)
1766        }
1767
1768        fn inbox_response_storage_key(message_key: MessageKey) -> Vec<u8> {
1769            Messenger::inbox_response_storage_key(message_key)
1770        }
1771
1772        fn domain_chains_allowlist_update(domain_id: DomainId) -> Option<DomainAllowlistUpdates>{
1773            Messenger::domain_chains_allowlist_update(domain_id)
1774        }
1775
1776        fn xdm_id(ext: &ExtrinsicFor<Block>) -> Option<XdmId> {
1777            match &ext.function {
1778                RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
1779                    Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
1780                }
1781                RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
1782                    Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
1783                }
1784                _ => None,
1785            }
1786        }
1787
1788        fn channel_nonce(chain_id: ChainId, channel_id: ChannelId) -> Option<ChannelNonce> {
1789            Messenger::channel_nonce(chain_id, channel_id)
1790        }
1791    }
1792
1793    impl sp_messenger::RelayerApi<Block, BlockNumber, BlockNumber, BlockHashFor<Block>> for Runtime {
1794        fn outbox_message_unsigned(msg: CrossDomainMessage<NumberFor<Block>, BlockHashFor<Block>, BlockHashFor<Block>>) -> Option<ExtrinsicFor<Block>> {
1795            Messenger::outbox_message_unsigned(msg)
1796        }
1797
1798        fn inbox_response_message_unsigned(msg: CrossDomainMessage<NumberFor<Block>, BlockHashFor<Block>, BlockHashFor<Block>>) -> Option<ExtrinsicFor<Block>> {
1799            Messenger::inbox_response_message_unsigned(msg)
1800        }
1801
1802        fn updated_channels() -> BTreeSet<(ChainId, ChannelId)> {
1803            Messenger::updated_channels()
1804        }
1805
1806        fn channel_storage_key(chain_id: ChainId, channel_id: ChannelId) -> Vec<u8> {
1807            Messenger::channel_storage_key(chain_id, channel_id)
1808        }
1809
1810        fn open_channels() -> BTreeSet<(ChainId, ChannelId)> {
1811            Messenger::open_channels()
1812        }
1813
1814        fn block_messages_with_query(query: BlockMessagesQuery) -> MessagesWithStorageKey {
1815            Messenger::get_block_messages(query)
1816        }
1817
1818        fn channels_and_state() -> Vec<(ChainId, ChannelId, ChannelStateWithNonce)> {
1819            Messenger::channels_and_states()
1820        }
1821
1822        fn first_outbox_message_nonce_to_relay(dst_chain_id: ChainId, channel_id: ChannelId, from_nonce: XdmNonce) -> Option<XdmNonce> {
1823            Messenger::first_outbox_message_nonce_to_relay(dst_chain_id, channel_id, from_nonce)
1824        }
1825
1826        fn first_inbox_message_response_nonce_to_relay(dst_chain_id: ChainId, channel_id: ChannelId, from_nonce: XdmNonce) -> Option<XdmNonce> {
1827            Messenger::first_inbox_message_response_nonce_to_relay(dst_chain_id, channel_id, from_nonce)
1828        }
1829    }
1830
1831    impl sp_domains_fraud_proof::FraudProofApi<Block, DomainHeader> for Runtime {
1832        fn submit_fraud_proof_unsigned(fraud_proof: FraudProof<NumberFor<Block>, BlockHashFor<Block>, DomainHeader, H256>) {
1833            Domains::submit_fraud_proof_unsigned(fraud_proof)
1834        }
1835
1836        fn fraud_proof_storage_key(req: FraudProofStorageKeyRequest<NumberFor<Block>>) -> Vec<u8> {
1837            <StorageKeyProvider as FraudProofStorageKeyProvider<NumberFor<Block>>>::storage_key(req)
1838        }
1839    }
1840
1841    impl mmr::MmrApi<Block, mmr::Hash, BlockNumber> for Runtime {
1842        fn mmr_root() -> Result<mmr::Hash, mmr::Error> {
1843            Ok(Mmr::mmr_root())
1844        }
1845
1846        fn mmr_leaf_count() -> Result<mmr::LeafIndex, mmr::Error> {
1847            Ok(Mmr::mmr_leaves())
1848        }
1849
1850        fn generate_proof(
1851            block_numbers: Vec<BlockNumber>,
1852            best_known_block_number: Option<BlockNumber>,
1853        ) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::LeafProof<mmr::Hash>), mmr::Error> {
1854            Mmr::generate_proof(block_numbers, best_known_block_number).map(
1855                |(leaves, proof)| {
1856                    (
1857                        leaves
1858                            .into_iter()
1859                            .map(|leaf| mmr::EncodableOpaqueLeaf::from_leaf(&leaf))
1860                            .collect(),
1861                        proof,
1862                    )
1863                },
1864            )
1865        }
1866
1867        fn verify_proof(leaves: Vec<mmr::EncodableOpaqueLeaf>, proof: mmr::LeafProof<mmr::Hash>)
1868            -> Result<(), mmr::Error>
1869        {
1870            let leaves = leaves.into_iter().map(|leaf|
1871                leaf.into_opaque_leaf()
1872                .try_decode()
1873                .ok_or(mmr::Error::Verify)).collect::<Result<Vec<mmr::Leaf>, mmr::Error>>()?;
1874            Mmr::verify_leaves(leaves, proof)
1875        }
1876
1877        fn verify_proof_stateless(
1878            root: mmr::Hash,
1879            leaves: Vec<mmr::EncodableOpaqueLeaf>,
1880            proof: mmr::LeafProof<mmr::Hash>
1881        ) -> Result<(), mmr::Error> {
1882            let nodes = leaves.into_iter().map(|leaf|mmr::DataOrHash::Data(leaf.into_opaque_leaf())).collect();
1883            pallet_mmr::verify_leaves_proof::<mmr::Hashing, _>(root, nodes, proof)
1884        }
1885    }
1886
1887    impl subspace_test_primitives::OnchainStateApi<Block, AccountId, Balance> for Runtime {
1888        fn free_balance(account_id: AccountId) -> Balance {
1889            Balances::free_balance(account_id)
1890        }
1891
1892        fn get_open_channel_for_chain(dst_chain_id: ChainId) -> Option<ChannelId> {
1893            Messenger::get_open_channel_for_chain(dst_chain_id)
1894        }
1895
1896        fn verify_proof_and_extract_leaf(mmr_leaf_proof: ConsensusChainMmrLeafProof<NumberFor<Block>, BlockHashFor<Block>, H256>) -> Option<mmr::Leaf> {
1897            <MmrProofVerifier as sp_subspace_mmr::MmrProofVerifier<_, _, _,>>::verify_proof_and_extract_leaf(mmr_leaf_proof)
1898        }
1899
1900        fn domain_balance(domain_id: DomainId) -> Balance {
1901            Transporter::domain_balances(domain_id)
1902        }
1903
1904        fn domain_stake_summary(domain_id: DomainId) -> Option<StakingSummary<OperatorId, Balance>>{
1905            Domains::domain_staking_summary(domain_id)
1906        }
1907    }
1908
1909    impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
1910        fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
1911            build_state::<RuntimeGenesisConfig>(config)
1912        }
1913
1914        fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
1915            get_preset::<RuntimeGenesisConfig>(id, |_| None)
1916        }
1917
1918        fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
1919            vec![]
1920        }
1921    }
1922}
1923
1924#[cfg(test)]
1925mod tests {
1926    use crate::Runtime;
1927    use pallet_domains::bundle_storage_fund::AccountType;
1928    use sp_domains::OperatorId;
1929    use sp_runtime::traits::AccountIdConversion;
1930
1931    #[test]
1932    fn test_bundle_storage_fund_account_uniqueness() {
1933        let _: <Runtime as frame_system::Config>::AccountId = <Runtime as pallet_domains::Config>::PalletId::get()
1934            .try_into_sub_account((AccountType::StorageFund, OperatorId::MAX))
1935            .expect(
1936                "The `AccountId` type must be large enough to fit the seed of the bundle storage fund account",
1937            );
1938    }
1939}