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, DecodeWithMemTracking, 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 const DEFAULT_EVM_CHAIN_ID: EVMChainId = 870;
78
79pub type EthereumSignature = EVMSignature;
81
82pub type EthereumAccountId = <<EthereumSignature as Verify>::Signer as IdentifyAccount>::AccountId;
88
89pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(65);
91
92pub const MAX_OUTGOING_MESSAGES: u32 = 10_000;
94
95pub fn maximum_domain_block_weight() -> Weight {
99 let consensus_maximum_normal_block_length =
100 *maximum_block_length().max.get(DispatchClass::Normal) as u64;
101 let weight =
102 NORMAL_DISPATCH_RATIO * Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), 0);
103 weight.set_proof_size(consensus_maximum_normal_block_length)
104}
105
106pub const ERR_EVM_NONCE_OVERFLOW: u8 = 100;
112pub const ERR_BALANCE_OVERFLOW: u8 = 200;
114pub const ERR_CONTRACT_CREATION_NOT_ALLOWED: u8 = 210;
117
118pub fn maximum_block_length() -> BlockLength {
121 BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO)
122}
123
124pub const DEFAULT_EXTENSION_VERSION: ExtensionVersion = 0;
127
128pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000_000 * SHANNON;
135
136parameter_types! {
137 pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
140}
141
142const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);
145
146pub fn block_weights() -> BlockWeights {
147 let maximum_block_weight = maximum_domain_block_weight();
155
156 let max_extrinsic_weight = maximum_block_weight
160 .checked_sub(&ExtrinsicBaseWeight::get())
161 .expect("Unable to produce blocks unless maximum block weight is greater than base extrinsic weight");
162
163 BlockWeights::builder()
164 .base_block(BlockExecutionWeight::get())
165 .for_class(DispatchClass::all(), |weights| {
166 weights.base_extrinsic = ExtrinsicBaseWeight::get();
167 weights.max_extrinsic = Some(max_extrinsic_weight);
170 weights.max_total = Some(maximum_block_weight);
172 })
173 .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
174 .build_or_panic()
175}
176
177pub trait Signer<AccountId, Lookup> {
181 fn signer(&self, lookup: &Lookup) -> Option<AccountId>;
183}
184
185impl<Address, AccountId, Call, Signature, Extra, Lookup> Signer<AccountId, Lookup>
186 for UncheckedExtrinsic<Address, Call, Signature, Extra>
187where
188 Address: Clone,
189 Call: Dispatchable,
190 Extra: TransactionExtension<Call>,
191 Lookup: sp_runtime::traits::Lookup<Source = Address, Target = AccountId>,
192{
193 fn signer(&self, lookup: &Lookup) -> Option<AccountId> {
194 match &self.preamble {
195 Preamble::Bare(_) => None,
196 Preamble::Signed(address, _, _) => lookup.lookup(address.clone()).ok(),
197 Preamble::General(_, _) => None,
198 }
199 }
200}
201
202#[derive(
204 Debug,
205 Encode,
206 Decode,
207 Clone,
208 Eq,
209 PartialEq,
210 TypeInfo,
211 Serialize,
212 Deserialize,
213 Ord,
214 PartialOrd,
215 DecodeWithMemTracking,
216)]
217pub enum MultiAccountId {
218 AccountId32([u8; 32]),
220 AccountId20([u8; 20]),
222 Raw(Vec<u8>),
224}
225
226pub trait TryConvertBack<A, B>: Convert<A, B> {
228 fn try_convert_back(b: B) -> Option<A>;
230}
231
232pub struct AccountIdConverter;
234
235impl Convert<AccountId, MultiAccountId> for AccountIdConverter {
236 fn convert(account_id: AccountId) -> MultiAccountId {
237 MultiAccountId::AccountId32(account_id.into())
238 }
239}
240
241impl TryConvertBack<AccountId, MultiAccountId> for AccountIdConverter {
242 fn try_convert_back(multi_account_id: MultiAccountId) -> Option<AccountId> {
243 match multi_account_id {
244 MultiAccountId::AccountId32(acc) => Some(AccountId::new(acc)),
245 _ => None,
246 }
247 }
248}
249
250pub struct AccountId20Converter;
252
253impl Convert<AccountId20, MultiAccountId> for AccountId20Converter {
254 fn convert(account_id: AccountId20) -> MultiAccountId {
255 MultiAccountId::AccountId20(account_id.into())
256 }
257}
258
259impl TryConvertBack<AccountId20, MultiAccountId> for AccountId20Converter {
260 fn try_convert_back(multi_account_id: MultiAccountId) -> Option<AccountId20> {
261 match multi_account_id {
262 MultiAccountId::AccountId20(acc) => Some(AccountId20::from(acc)),
263 _ => None,
264 }
265 }
266}
267
268#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
269pub struct CheckExtrinsicsValidityError {
270 pub extrinsic_index: u32,
271 pub transaction_validity_error: TransactionValidityError,
272}
273
274#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
275pub struct DecodeExtrinsicError(pub String);
276
277pub const CHECK_EXTRINSICS_AND_DO_PRE_DISPATCH_METHOD_NAME: &str =
281 "DomainCoreApi_check_extrinsics_and_do_pre_dispatch";
282
283pub mod opaque {
290 use crate::BlockNumber;
291 #[cfg(not(feature = "std"))]
292 use alloc::vec::Vec;
293 pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
294 use sp_runtime::generic;
295 use sp_runtime::traits::BlakeTwo256;
296
297 pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
299 pub type Block = generic::Block<Header, UncheckedExtrinsic>;
301 pub type BlockId = generic::BlockId<Block>;
303 pub type AccountId = Vec<u8>;
305}
306
307#[cfg(test)]
308mod test {
309 use super::block_weights;
310
311 #[test]
312 fn test_block_weights() {
313 let _block_weights = block_weights();
315 }
316}