sp_domains/
core_api.rs

1#[cfg(not(feature = "std"))]
2extern crate alloc;
3
4use crate::{BlockFees, DomainAllowlistUpdates, Transfers};
5#[cfg(not(feature = "std"))]
6use alloc::vec::Vec;
7use domain_runtime_primitives::{
8    opaque, Balance, CheckExtrinsicsValidityError, DecodeExtrinsicError,
9};
10use sp_runtime::generic::Era;
11use sp_runtime::traits::NumberFor;
12use sp_runtime::Digest;
13use sp_weights::Weight;
14use subspace_core_primitives::U256;
15use subspace_runtime_primitives::{ExtrinsicFor, Moment};
16
17sp_api::decl_runtime_apis! {
18    /// Base API that every domain runtime must implement.
19    #[api_version(2)]
20    // `allow(clippy::ptr_arg` is needed because Clippy complains to replace `&Vec<T>` with `&[T]`
21    // but the latter fails to compile.
22    #[allow(clippy::ptr_arg)]
23    pub trait DomainCoreApi {
24        /// Extracts the optional signer per extrinsic.
25        fn extract_signer(
26            extrinsics: Vec<ExtrinsicFor<Block>>,
27        ) -> Vec<(Option<opaque::AccountId>, Block::Extrinsic)>;
28
29        fn is_within_tx_range(
30            extrinsic: &Block::Extrinsic,
31            bundle_vrf_hash: &U256,
32            tx_range: &U256,
33        ) -> bool;
34
35        /// Extract signer of a given list of extrinsic if all of them are within the
36        /// tx range, otherwise return the index if the first tx not in the tx range.
37        fn extract_signer_if_all_within_tx_range(
38            extrinsic: &Vec<Block::Extrinsic>,
39            bundle_vrf_hash: &U256,
40            tx_range: &U256,
41        ) -> Result<Vec<Option<opaque::AccountId>> , u32>;
42
43        /// Returns the storage root after initializing the block.
44        fn initialize_block_with_post_state_root(header: &Block::Header) -> Vec<u8>;
45
46        /// Returns the storage root after applying the extrinsic.
47        fn apply_extrinsic_with_post_state_root(extrinsic: Block::Extrinsic) -> Vec<u8>;
48
49        /// Returns an encoded extrinsic aiming to upgrade the runtime using given code.
50        fn construct_set_code_extrinsic(code: Vec<u8>) -> Vec<u8>;
51
52        /// Returns an encoded extrinsic to set timestamp.
53        fn construct_timestamp_extrinsic(moment: Moment) -> Block::Extrinsic;
54
55        /// Returns an encoded extrinsic to set domain transaction byte fee.
56        fn construct_consensus_chain_byte_fee_extrinsic(consensus_chain_byte_fee: Balance) -> Block::Extrinsic;
57
58        /// Returns an extrinsic to update chain allowlist.
59        fn construct_domain_update_chain_allowlist_extrinsic(updates: DomainAllowlistUpdates) -> Block::Extrinsic;
60
61        /// Returns true if the extrinsic is an inherent extrinsic.
62        fn is_inherent_extrinsic(extrinsic: &Block::Extrinsic) -> bool;
63
64        /// Find the first inherent extrinsic
65        fn find_first_inherent_extrinsic(extrinsics: &Vec<Block::Extrinsic>) -> Option<u32>;
66
67        /// Checks the validity of array of extrinsics + pre_dispatch
68        /// returning failure on first extrinsic that fails runtime call.
69        /// IMPORTANT: Change `CHECK_EXTRINSICS_AND_DO_PRE_DISPATCH_METHOD_NAME` constant when this method name is changed
70        fn check_extrinsics_and_do_pre_dispatch(uxts: Vec<Block::Extrinsic>, block_number: NumberFor<Block>,
71            block_hash: Block::Hash) -> Result<(), CheckExtrinsicsValidityError>;
72
73        /// Decodes the domain specific extrinsic from the opaque extrinsic.
74        fn decode_extrinsic(
75            opaque_extrinsic: sp_runtime::OpaqueExtrinsic,
76        ) -> Result<Block::Extrinsic, DecodeExtrinsicError>;
77
78        /// Decodes a list of domain extrinsics from opaque extrinsics, returning all extrinsics
79        /// up to the first undecodable tx. (Or the full list if all are decodable.)
80        fn decode_extrinsics_prefix(
81            opaque_extrinsics: Vec<sp_runtime::OpaqueExtrinsic>,
82        ) -> Vec<Block::Extrinsic>;
83
84        /// Returns extrinsic Era if present.
85        fn extrinsic_era(
86          extrinsic: &Block::Extrinsic
87        ) -> Option<Era>;
88
89        /// Returns the extrinsic weight.
90        fn extrinsic_weight(ext: &Block::Extrinsic) -> Weight;
91
92        /// Returns the sum of a given set of extrinsics weight.
93        fn extrinsics_weight(ext: &Vec<Block::Extrinsic>) -> Weight;
94
95        /// The accumulated transaction fee of all transactions included in the block.
96        fn block_fees() -> BlockFees<Balance>;
97
98        /// Returns the block digest.
99        fn block_digest() -> Digest;
100
101        /// Returns the consumed weight of the block.
102        fn block_weight() -> Weight;
103
104        /// Returns the transfers for this domain in the block.
105        fn transfers() -> Transfers<Balance>;
106
107        /// Returns the storage key for the Transfers on Domain.
108        fn transfers_storage_key() -> Vec<u8>;
109
110        /// Returns the storage key for the `CollectedBlockFees` on Domain.
111        fn block_fees_storage_key() -> Vec<u8>;
112    }
113}