1234567891011121314151617pub mod config;18pub mod construct_runtime;19pub mod dispatch;20pub mod ethereum;21pub mod instance;22pub mod maintenance;23pub mod runtime_apis;24pub mod xcm;2526#[cfg(feature = "scheduler")]27pub mod scheduler;2829pub mod sponsoring;30pub mod weights;3132#[cfg(test)]33pub mod tests;3435use sp_core::H160;36use frame_support::{37 traits::{Currency, OnUnbalanced, Imbalance},38 weights::Weight,39};40use sp_runtime::{41 generic,42 traits::{BlakeTwo256, BlockNumberProvider},43 impl_opaque_keys,44};45use sp_std::vec::Vec;4647#[cfg(feature = "std")]48use sp_version::NativeVersion;4950use crate::{51 Runtime, RuntimeCall, Balances, Treasury, Aura, Signature, AllPalletsWithSystem,52 InherentDataExt,53};54use up_common::types::{AccountId, BlockNumber};5556#[macro_export]57macro_rules! unsupported {58 () => {59 pallet_common::unsupported!($crate::Runtime)60 };61}626364pub type Address = sp_runtime::MultiAddress<AccountId, ()>;6566pub type Header = generic::Header<BlockNumber, BlakeTwo256>;6768pub type Block = generic::Block<Header, UncheckedExtrinsic>;6970pub type SignedBlock = generic::SignedBlock<Block>;7172pub type BlockId = generic::BlockId<Block>;7374impl_opaque_keys! {75 pub struct SessionKeys {76 pub aura: Aura,77 }78}798081#[cfg(feature = "std")]82pub fn native_version() -> NativeVersion {83 NativeVersion {84 runtime_version: crate::VERSION,85 can_author_with: Default::default(),86 }87}8889pub type ChargeTransactionPayment = pallet_charge_transaction::ChargeTransactionPayment<Runtime>;9091pub type SignedExtra = (92 frame_system::CheckSpecVersion<Runtime>,93 frame_system::CheckTxVersion<Runtime>,94 frame_system::CheckGenesis<Runtime>,95 frame_system::CheckEra<Runtime>,96 frame_system::CheckNonce<Runtime>,97 frame_system::CheckWeight<Runtime>,98 maintenance::CheckMaintenance,99 ChargeTransactionPayment,100 101 pallet_ethereum::FakeTransactionFinalizer<Runtime>,102);103104105pub type UncheckedExtrinsic =106 fp_self_contained::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;107108109pub type CheckedExtrinsic =110 fp_self_contained::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra, H160>;111112113pub type Executive = frame_executive::Executive<114 Runtime,115 Block,116 frame_system::ChainContext<Runtime>,117 Runtime,118 AllPalletsWithSystem,119 AuraToCollatorSelection,120>;121122type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;123124pub struct DealWithFees;125impl OnUnbalanced<NegativeImbalance> for DealWithFees {126 fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance>) {127 if let Some(fees) = fees_then_tips.next() {128 129 let mut split = fees.ration(100, 0);130 if let Some(tips) = fees_then_tips.next() {131 132 tips.ration_merge_into(100, 0, &mut split);133 }134 Treasury::on_unbalanced(split.0);135 136 }137 }138}139140pub struct RelayChainBlockNumberProvider<T>(sp_std::marker::PhantomData<T>);141142impl<T: cumulus_pallet_parachain_system::Config> BlockNumberProvider143 for RelayChainBlockNumberProvider<T>144{145 type BlockNumber = BlockNumber;146147 fn current_block_number() -> Self::BlockNumber {148 cumulus_pallet_parachain_system::Pallet::<T>::validation_data()149 .map(|d| d.relay_parent_number)150 .unwrap_or_default()151 }152}153154pub(crate) struct CheckInherents;155156impl cumulus_pallet_parachain_system::CheckInherents<Block> for CheckInherents {157 fn check_inherents(158 block: &Block,159 relay_state_proof: &cumulus_pallet_parachain_system::RelayChainStateProof,160 ) -> sp_inherents::CheckInherentsResult {161 let relay_chain_slot = relay_state_proof162 .read_slot()163 .expect("Could not read the relay chain slot from the proof");164165 let inherent_data =166 cumulus_primitives_timestamp::InherentDataProvider::from_relay_chain_slot_and_duration(167 relay_chain_slot,168 sp_std::time::Duration::from_secs(6),169 )170 .create_inherent_data()171 .expect("Could not create the timestamp inherent data");172173 inherent_data.check_extrinsics(block)174 }175}176177#[derive(codec::Encode, codec::Decode)]178pub enum XCMPMessage<XAccountId, XBalance> {179 180 TransferToken(XAccountId, XBalance),181}182183pub struct AuraToCollatorSelection;184impl frame_support::traits::OnRuntimeUpgrade for AuraToCollatorSelection {185 fn on_runtime_upgrade() -> Weight {186 #[cfg(feature = "collator-selection")]187 {188 use frame_support::{BoundedVec, storage::migration};189 use sp_runtime::{190 traits::{OpaqueKeys, Saturating},191 RuntimeAppPublic,192 };193 use pallet_session::SessionManager;194 use up_common::constants::{GENESIS_LICENSE_BOND, SESSION_LENGTH};195 use crate::config::pallets::collator_selection::MaxCollators;196197 let mut weight = <Runtime as frame_system::Config>::DbWeight::get().reads(1);198199 let version = migration::get_storage_value::<()>(200 b"AuraToCollatorSelection",201 b"StorageVersion",202 &[],203 );204205 let should_upgrade = match version {206 None => true,207 Some(_) => false,208 };209210 if should_upgrade {211 log::info!(212 target: "runtime::aura_to_collator_selection",213 "Running migration of Aura authorities to Collator Selection invulnerables"214 );215216 let invulnerables = pallet_aura::Pallet::<Runtime>::authorities()217 .iter()218 .cloned()219 .filter_map(|authority_id| {220 weight.saturating_accrue(<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1));221 let vec = authority_id.clone().to_raw_vec();222 let slice = vec.as_slice();223 let array: Option<[u8; 32]> = match slice.try_into() {224 Ok(a) => Some(a),225 Err(_) => {226 log::error!("Failed to convert an Aura authority to a Collator Selection invulnerable: {:?}", authority_id);227 None228 },229 };230 array.map(|a| (AccountId::from(a), authority_id))231 })232 .collect::<Vec<_>>();233234 let bounded_invulnerables = BoundedVec::<_, MaxCollators>::try_from(235 invulnerables236 .iter()237 .cloned()238 .map(|(acc, _)| acc)239 .collect::<Vec<_>>(),240 )241 .expect("Existing collators/invulnerables are more than MaxCollators");242243 <pallet_collator_selection::Invulnerables<Runtime>>::put(bounded_invulnerables);244 <pallet_collator_selection::KickThreshold<Runtime>>::put(SESSION_LENGTH);245 <pallet_collator_selection::DesiredCollators<Runtime>>::put(MaxCollators::get());246 <pallet_collator_selection::LicenseBond<Runtime>>::put(GENESIS_LICENSE_BOND);247248 let keys = invulnerables249 .into_iter()250 .map(|(acc, aura)| {251 (252 acc.clone(), 253 acc, 254 SessionKeys { aura: aura.clone() }, 255 )256 })257 .collect::<Vec<_>>();258259 for (account, val, keys) in keys.iter().cloned() {260 for id in <Runtime as pallet_session::Config>::Keys::key_ids() {261 <pallet_session::KeyOwner<Runtime>>::insert((*id, keys.get_raw(*id)), &val)262 }263 <pallet_session::NextKeys<Runtime>>::insert(&val, &keys);264 265 if frame_system::Pallet::<Runtime>::inc_consumers_without_limit(&account)266 .is_err()267 {268 log::warn!(269 "We have entered an error with incrementing consumers without limit during the migration"270 );271 272 273 274 275 frame_system::Pallet::<Runtime>::inc_providers(&account);276 }277 }278279 let initial_validators_0 =280 <Runtime as pallet_session::Config>::SessionManager::new_session(0)281 .unwrap_or_else(|| {282 frame_support::print(283 "No initial validator provided by `SessionManager`, use \284 session config keys to generate initial validator set.",285 );286 keys.iter().map(|x| x.1.clone()).collect()287 });288 289290291292293 let initial_validators_1 =294 <Runtime as pallet_session::Config>::SessionManager::new_session(1)295 .unwrap_or_else(|| initial_validators_0.clone());296 297298299300301 let queued_keys: Vec<_> = initial_validators_1302 .iter()303 .cloned()304 .map(|v| {305 (306 v.clone(),307 <pallet_session::NextKeys<Runtime>>::get(&v)308 .expect("Validator in session 1 missing keys!"),309 )310 })311 .collect();312313 314 315316 <pallet_session::Validators<Runtime>>::put(initial_validators_0);317 <pallet_session::QueuedKeys<Runtime>>::put(queued_keys);318319 <Runtime as pallet_session::Config>::SessionManager::start_session(0);320321 log::info!(322 target: "runtime::aura_to_collator_selection",323 "Migration of Aura authorities to Collator Selection invulnerables is complete."324 );325326 migration::put_storage_value::<()>(327 b"AuraToCollatorSelection",328 b"StorageVersion",329 &[],330 (),331 );332333 weight += <Runtime as frame_system::Config>::DbWeight::get().writes(1)334 } else {335 log::info!(336 target: "runtime::aura_to_collator_selection",337 "The storage migration has already been flagged as complete. No migration needs to be done.",338 );339 }340341 weight342 }343344 #[cfg(not(feature = "collator-selection"))]345 {346 Weight::zero()347 }348 }349}