domain_runtime_primitives/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
19
20#[cfg(not(feature = "std"))]
21extern crate alloc;
22
23#[cfg(not(feature = "std"))]
24use alloc::string::String;
25#[cfg(not(feature = "std"))]
26use alloc::vec::Vec;
27pub use fp_account::{AccountId20, EthereumSignature as EVMSignature};
28use frame_support::dispatch::DispatchClass;
29use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight};
30use frame_system::limits::{BlockLength, BlockWeights};
31use parity_scale_codec::{Decode, Encode};
32use scale_info::TypeInfo;
33use serde::{Deserialize, Serialize};
34use sp_core::parameter_types;
35use sp_runtime::generic::{ExtensionVersion, Preamble, UncheckedExtrinsic};
36use sp_runtime::traits::transaction_extension::TransactionExtension;
37use sp_runtime::traits::{Convert, Dispatchable, IdentifyAccount, Verify};
38use sp_runtime::transaction_validity::TransactionValidityError;
39use sp_runtime::{MultiAddress, MultiSignature, Perbill, Perquintill};
40use sp_weights::Weight;
41use sp_weights::constants::WEIGHT_REF_TIME_PER_SECOND;
42pub use subspace_runtime_primitives::HoldIdentifier;
43use subspace_runtime_primitives::{MAX_BLOCK_LENGTH, SHANNON};
44
45pub type Signature = MultiSignature;
47
48pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
54
55pub type Balance = u128;
57
58pub type Nonce = u32;
60
61pub type Hash = sp_core::H256;
63
64pub type BlockNumber = u32;
66
67pub type Address = MultiAddress<AccountId, ()>;
69
70pub const SLOT_DURATION: u64 = 1000;
72
73pub type EVMChainId = u64;
75
76pub type EthereumSignature = EVMSignature;
78
79pub type EthereumAccountId = <<EthereumSignature as Verify>::Signer as IdentifyAccount>::AccountId;
85
86pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(65);
88
89pub const MAX_OUTGOING_MESSAGES: u32 = 10_000;
91
92pub fn maximum_domain_block_weight() -> Weight {
96 let consensus_maximum_normal_block_length =
97 *maximum_block_length().max.get(DispatchClass::Normal) as u64;
98 let weight =
99 NORMAL_DISPATCH_RATIO * Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), 0);
100 weight.set_proof_size(consensus_maximum_normal_block_length)
101}
102
103pub const ERR_EVM_NONCE_OVERFLOW: u8 = 100;
109pub const ERR_BALANCE_OVERFLOW: u8 = 200;
111pub const ERR_CONTRACT_CREATION_NOT_ALLOWED: u8 = 210;
114
115pub fn maximum_block_length() -> BlockLength {
118 BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO)
119}
120
121pub const DEFAULT_EXTENSION_VERSION: ExtensionVersion = 0;
124
125pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000_000 * SHANNON;
132
133parameter_types! {
134 pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
137}
138
139const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);
142
143pub fn block_weights() -> BlockWeights {
144 let maximum_block_weight = maximum_domain_block_weight();
152
153 let max_extrinsic_weight = maximum_block_weight
157 .checked_sub(&ExtrinsicBaseWeight::get())
158 .expect("Unable to produce blocks unless maximum block weight is greater than base extrinsic weight");
159
160 BlockWeights::builder()
161 .base_block(BlockExecutionWeight::get())
162 .for_class(DispatchClass::all(), |weights| {
163 weights.base_extrinsic = ExtrinsicBaseWeight::get();
164 weights.max_extrinsic = Some(max_extrinsic_weight);
167 weights.max_total = Some(maximum_block_weight);
169 })
170 .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
171 .build_or_panic()
172}
173
174pub trait Signer<AccountId, Lookup> {
178 fn signer(&self, lookup: &Lookup) -> Option<AccountId>;
180}
181
182impl<Address, AccountId, Call, Signature, Extra, Lookup> Signer<AccountId, Lookup>
183 for UncheckedExtrinsic<Address, Call, Signature, Extra>
184where
185 Address: Clone,
186 Call: Dispatchable,
187 Extra: TransactionExtension<Call>,
188 Lookup: sp_runtime::traits::Lookup<Source = Address, Target = AccountId>,
189{
190 fn signer(&self, lookup: &Lookup) -> Option<AccountId> {
191 match &self.preamble {
192 Preamble::Bare(_) => None,
193 Preamble::Signed(address, _, _) => lookup.lookup(address.clone()).ok(),
194 Preamble::General(_, _) => None,
195 }
196 }
197}
198
199#[derive(
201 Debug, Encode, Decode, Clone, Eq, PartialEq, TypeInfo, Serialize, Deserialize, Ord, PartialOrd,
202)]
203pub enum MultiAccountId {
204 AccountId32([u8; 32]),
206 AccountId20([u8; 20]),
208 Raw(Vec<u8>),
210}
211
212pub trait TryConvertBack<A, B>: Convert<A, B> {
214 fn try_convert_back(b: B) -> Option<A>;
216}
217
218pub struct AccountIdConverter;
220
221impl Convert<AccountId, MultiAccountId> for AccountIdConverter {
222 fn convert(account_id: AccountId) -> MultiAccountId {
223 MultiAccountId::AccountId32(account_id.into())
224 }
225}
226
227impl TryConvertBack<AccountId, MultiAccountId> for AccountIdConverter {
228 fn try_convert_back(multi_account_id: MultiAccountId) -> Option<AccountId> {
229 match multi_account_id {
230 MultiAccountId::AccountId32(acc) => Some(AccountId::new(acc)),
231 _ => None,
232 }
233 }
234}
235
236pub struct AccountId20Converter;
238
239impl Convert<AccountId20, MultiAccountId> for AccountId20Converter {
240 fn convert(account_id: AccountId20) -> MultiAccountId {
241 MultiAccountId::AccountId20(account_id.into())
242 }
243}
244
245impl TryConvertBack<AccountId20, MultiAccountId> for AccountId20Converter {
246 fn try_convert_back(multi_account_id: MultiAccountId) -> Option<AccountId20> {
247 match multi_account_id {
248 MultiAccountId::AccountId20(acc) => Some(AccountId20::from(acc)),
249 _ => None,
250 }
251 }
252}
253
254#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
255pub struct CheckExtrinsicsValidityError {
256 pub extrinsic_index: u32,
257 pub transaction_validity_error: TransactionValidityError,
258}
259
260#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
261pub struct DecodeExtrinsicError(pub String);
262
263pub const CHECK_EXTRINSICS_AND_DO_PRE_DISPATCH_METHOD_NAME: &str =
267 "DomainCoreApi_check_extrinsics_and_do_pre_dispatch";
268
269pub mod opaque {
276 use crate::BlockNumber;
277 #[cfg(not(feature = "std"))]
278 use alloc::vec::Vec;
279 pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
280 use sp_runtime::generic;
281 use sp_runtime::traits::BlakeTwo256;
282
283 pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
285 pub type Block = generic::Block<Header, UncheckedExtrinsic>;
287 pub type BlockId = generic::BlockId<Block>;
289 pub type AccountId = Vec<u8>;
291}
292
293#[cfg(test)]
294mod test {
295 use super::block_weights;
296
297 #[test]
298 fn test_block_weights() {
299 let _block_weights = block_weights();
301 }
302}