1234567891011121314151617pub mod config;18pub mod construct_runtime;19pub mod dispatch;20pub mod ethereum;21pub mod instance;22pub mod runtime_apis;23pub mod scheduler;24pub mod sponsoring;25pub mod weights;2627use sp_core::H160;28use frame_support::{29 traits::{Currency, OnUnbalanced, Imbalance},30 weights::Weight,31};32use sp_runtime::{33 generic,34 traits::{BlakeTwo256, BlockNumberProvider},35 impl_opaque_keys,36};37use sp_std::vec::Vec;3839#[cfg(feature = "std")]40use sp_version::NativeVersion;4142use crate::{43 Runtime, Call, Balances, Treasury, Aura, Signature, AllPalletsReversedWithSystemFirst,44 InherentDataExt,45};46use up_common::types::{AccountId, BlockNumber};4748#[macro_export]49macro_rules! unsupported {50 () => {51 pallet_common::unsupported!($crate::Runtime)52 };53}545556pub type Address = sp_runtime::MultiAddress<AccountId, ()>;5758pub type Header = generic::Header<BlockNumber, BlakeTwo256>;5960pub type Block = generic::Block<Header, UncheckedExtrinsic>;6162pub type SignedBlock = generic::SignedBlock<Block>;6364pub type BlockId = generic::BlockId<Block>;6566impl_opaque_keys! {67 pub struct SessionKeys {68 pub aura: Aura,69 }70}717273#[cfg(feature = "std")]74pub fn native_version() -> NativeVersion {75 NativeVersion {76 runtime_version: crate::VERSION,77 can_author_with: Default::default(),78 }79}8081pub type ChargeTransactionPayment = pallet_charge_transaction::ChargeTransactionPayment<Runtime>;8283pub type SignedExtra = (84 frame_system::CheckSpecVersion<Runtime>,85 86 frame_system::CheckGenesis<Runtime>,87 frame_system::CheckEra<Runtime>,88 frame_system::CheckNonce<Runtime>,89 frame_system::CheckWeight<Runtime>,90 ChargeTransactionPayment,91 92 pallet_ethereum::FakeTransactionFinalizer<Runtime>,93);949596pub type UncheckedExtrinsic =97 fp_self_contained::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;9899100pub type CheckedExtrinsic = fp_self_contained::CheckedExtrinsic<AccountId, Call, SignedExtra, H160>;101102103pub type Executive = frame_executive::Executive<104 Runtime,105 Block,106 frame_system::ChainContext<Runtime>,107 Runtime,108 AllPalletsReversedWithSystemFirst,109 AuraToCollatorSelection,110>;111112type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;113114pub struct DealWithFees;115impl OnUnbalanced<NegativeImbalance> for DealWithFees {116 fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance>) {117 if let Some(fees) = fees_then_tips.next() {118 119 let mut split = fees.ration(100, 0);120 if let Some(tips) = fees_then_tips.next() {121 122 tips.ration_merge_into(100, 0, &mut split);123 }124 Treasury::on_unbalanced(split.0);125 126 }127 }128}129130pub struct RelayChainBlockNumberProvider<T>(sp_std::marker::PhantomData<T>);131132impl<T: cumulus_pallet_parachain_system::Config> BlockNumberProvider133 for RelayChainBlockNumberProvider<T>134{135 type BlockNumber = BlockNumber;136137 fn current_block_number() -> Self::BlockNumber {138 cumulus_pallet_parachain_system::Pallet::<T>::validation_data()139 .map(|d| d.relay_parent_number)140 .unwrap_or_default()141 }142}143144pub(crate) struct CheckInherents;145146impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {147 fn check_inherents(148 block: &Block,149 relay_state_proof: &cumulus_pallet_parachain_system::RelayChainStateProof,150 ) -> sp_inherents::CheckInherentsResult {151 let relay_chain_slot = relay_state_proof152 .read_slot()153 .expect("Could not read the relay chain slot from the proof");154155 let inherent_data =156 cumulus_primitives_timestamp::InherentDataProvider::from_relay_chain_slot_and_duration(157 relay_chain_slot,158 sp_std::time::Duration::from_secs(6),159 )160 .create_inherent_data()161 .expect("Could not create the timestamp inherent data");162163 inherent_data.check_extrinsics(block)164 }165}166167#[derive(codec::Encode, codec::Decode)]168pub enum XCMPMessage<XAccountId, XBalance> {169 170 TransferToken(XAccountId, XBalance),171}172173pub struct AuraToCollatorSelection;174impl frame_support::traits::OnRuntimeUpgrade for AuraToCollatorSelection {175 fn on_runtime_upgrade() -> Weight {176 use frame_support::{BoundedVec, storage::migration};177 use sp_runtime::{178 traits::{OpaqueKeys, Saturating},179 RuntimeAppPublic,180 };181 use pallet_session::SessionManager;182 use up_common::constants::EXISTENTIAL_DEPOSIT;183 use crate::config::substrate::MaxInvulnerables;184185 let mut weight = <Runtime as frame_system::Config>::DbWeight::get().reads(1);186187 let version =188 migration::get_storage_value::<()>(b"AuraToCollatorSelection", b"StorageVersion", &[]);189190 let should_upgrade = match version {191 None => true,192 Some(_) => false,193 };194195 if should_upgrade {196 log::info!(197 target: "runtime::aura_to_collator_selection",198 "Running migration of Aura authorities to Collator Selection invulnerables"199 );200201 let invulnerables = pallet_aura::Pallet::<Runtime>::authorities()202 .iter()203 .cloned()204 .filter_map(|authority_id| {205 weight.saturating_accrue(<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1));206 let vec = authority_id.clone().to_raw_vec();207 let slice = vec.as_slice();208 let array: Option<[u8; 32]> = match slice.try_into() {209 Ok(a) => Some(a),210 Err(_) => {211 log::error!("Failed to convert an Aura authority to a Collator Selection invulnerable: {:?}", authority_id);212 None213 },214 };215 array.map(|a| (AccountId::from(a), authority_id))216 })217 .collect::<Vec<_>>();218219 let bounded_invulnerables = BoundedVec::<_, MaxInvulnerables>::try_from(220 invulnerables221 .iter()222 .cloned()223 .map(|(acc, _)| acc)224 .collect::<Vec<_>>(),225 )226 .expect("Existing collators/invulnerables are more than MaxInvulnerables");227 228 <pallet_collator_selection::Invulnerables<Runtime>>::put(bounded_invulnerables);229 <pallet_collator_selection::DesiredCandidates<Runtime>>::put(0);230 <pallet_collator_selection::CandidacyBond<Runtime>>::put(EXISTENTIAL_DEPOSIT * 16);231232 let keys = invulnerables233 .into_iter()234 .map(|(acc, aura)| {235 (236 acc.clone(), 237 acc, 238 SessionKeys { aura: aura.clone() }, 239 )240 })241 .collect::<Vec<_>>();242243 for (account, val, keys) in keys.iter().cloned() {244 for id in <Runtime as pallet_session::Config>::Keys::key_ids() {245 <pallet_session::KeyOwner<Runtime>>::insert((*id, keys.get_raw(*id)), &val)246 }247 <pallet_session::NextKeys<Runtime>>::insert(&val, &keys);248 249 if frame_system::Pallet::<Runtime>::inc_consumers_without_limit(&account).is_err() {250 log::warn!(251 "We have entered an error with incrementing consumers without limit"252 );253 254 255 256 257 frame_system::Pallet::<Runtime>::inc_providers(&account);258 }259 }260261 let initial_validators_0 =262 <Runtime as pallet_session::Config>::SessionManager::new_session(0).unwrap_or_else(263 || {264 frame_support::print(265 "No initial validator provided by `SessionManager`, use \266 session config keys to generate initial validator set.",267 );268 keys.iter().map(|x| x.1.clone()).collect()269 },270 );271 272273274275276 let initial_validators_1 =277 <Runtime as pallet_session::Config>::SessionManager::new_session(1)278 .unwrap_or_else(|| initial_validators_0.clone());279 280281282283284 let queued_keys: Vec<_> = initial_validators_1285 .iter()286 .cloned()287 .map(|v| {288 (289 v.clone(),290 <pallet_session::NextKeys<Runtime>>::get(&v)291 .expect("Validator in session 1 missing keys!"),292 )293 })294 .collect();295296 297 298299 <pallet_session::Validators<Runtime>>::put(initial_validators_0);300 <pallet_session::QueuedKeys<Runtime>>::put(queued_keys);301302 <Runtime as pallet_session::Config>::SessionManager::start_session(0);303304 log::info!(305 target: "runtime::aura_to_collator_selection",306 "Migration of Aura authorities to Collator Selection invulnerables is complete."307 );308309 migration::put_storage_value::<()>(310 b"AuraToCollatorSelection",311 b"StorageVersion",312 &[],313 (),314 );315316 weight += <Runtime as frame_system::Config>::DbWeight::get().writes(1)317 } else {318 log::info!(319 target: "runtime::aura_to_collator_selection",320 "The storage migration has already been flagged as complete. No migration needs to be done.",321 );322 }323324 weight325 }326}