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