Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PraetorP committed Mar 5, 2024
1 parent 9916a84 commit dc112d9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 60 deletions.
56 changes: 29 additions & 27 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ use governance::{
TreasurySpender,
};
use xcm_executor::traits::WeightBounds;
use xcm_payment_runtime_api::Error as XcmPaymentError;
use xcm_payment_runtime_api::Error as XcmPaymentApiError;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -219,7 +219,7 @@ pub struct OriginPrivilegeCmp;
impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
fn cmp_privilege(left: &OriginCaller, right: &OriginCaller) -> Option<Ordering> {
if left == right {
return Some(Ordering::Equal)
return Some(Ordering::Equal);
}

match (left, right) {
Expand Down Expand Up @@ -379,10 +379,12 @@ impl OpaqueKeys for OldSessionKeys {
<<Babe as BoundToRuntimeAppPublic>::Public>::ID => self.babe.as_ref(),
sp_core::crypto::key_types::IM_ONLINE => self.im_online.as_ref(),
<<Initializer as BoundToRuntimeAppPublic>::Public>::ID => self.para_validator.as_ref(),
<<ParaSessionInfo as BoundToRuntimeAppPublic>::Public>::ID =>
self.para_assignment.as_ref(),
<<AuthorityDiscovery as BoundToRuntimeAppPublic>::Public>::ID =>
self.authority_discovery.as_ref(),
<<ParaSessionInfo as BoundToRuntimeAppPublic>::Public>::ID => {
self.para_assignment.as_ref()
},
<<AuthorityDiscovery as BoundToRuntimeAppPublic>::Public>::ID => {
self.authority_discovery.as_ref()
},
<<Beefy as BoundToRuntimeAppPublic>::Public>::ID => self.beefy.as_ref(),
_ => &[],
}
Expand Down Expand Up @@ -860,19 +862,19 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
),
ProxyType::IdentityJudgement => matches!(
c,
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. }) |
RuntimeCall::Utility(..)
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. })
| RuntimeCall::Utility(..)
),
ProxyType::CancelProxy => {
matches!(c, RuntimeCall::Proxy(pallet_proxy::Call::reject_announcement { .. }))
},
ProxyType::Auction => matches!(
c,
RuntimeCall::Auctions { .. } |
RuntimeCall::Crowdloan { .. } |
RuntimeCall::Registrar { .. } |
RuntimeCall::Multisig(..) |
RuntimeCall::Slots { .. }
RuntimeCall::Auctions { .. }
| RuntimeCall::Crowdloan { .. }
| RuntimeCall::Registrar { .. }
| RuntimeCall::Multisig(..)
| RuntimeCall::Slots { .. }
),
ProxyType::Society => matches!(c, RuntimeCall::Society(..)),
ProxyType::OnDemandOrdering => matches!(c, RuntimeCall::OnDemandAssignmentProvider(..)),
Expand Down Expand Up @@ -1491,11 +1493,11 @@ pub mod migrations {
let now = frame_system::Pallet::<Runtime>::block_number();
let lease = slots::Pallet::<Runtime>::lease(para);
if lease.is_empty() {
return None
return None;
}
// Lease not yet started, ignore:
if lease.iter().any(Option::is_none) {
return None
return None;
}
let (index, _) =
<slots::Pallet<Runtime> as Leaser<BlockNumber>>::lease_period_index(now)?;
Expand Down Expand Up @@ -1557,7 +1559,7 @@ pub mod migrations {
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, sp_runtime::TryRuntimeError> {
if System::last_runtime_upgrade_spec_version() > UPGRADE_SESSION_KEYS_FROM_SPEC {
log::warn!(target: "runtime::session_keys", "Skipping session keys migration pre-upgrade check due to spec version (already applied?)");
return Ok(Vec::new())
return Ok(Vec::new());
}

log::info!(target: "runtime::session_keys", "Collecting pre-upgrade session keys state");
Expand Down Expand Up @@ -1586,7 +1588,7 @@ pub mod migrations {
fn on_runtime_upgrade() -> Weight {
if System::last_runtime_upgrade_spec_version() > UPGRADE_SESSION_KEYS_FROM_SPEC {
log::info!("Skipping session keys upgrade: already applied");
return <Runtime as frame_system::Config>::DbWeight::get().reads(1)
return <Runtime as frame_system::Config>::DbWeight::get().reads(1);
}
log::trace!("Upgrading session keys");
Session::upgrade_keys::<OldSessionKeys, _>(transform_session_keys);
Expand All @@ -1599,7 +1601,7 @@ pub mod migrations {
) -> Result<(), sp_runtime::TryRuntimeError> {
if System::last_runtime_upgrade_spec_version() > UPGRADE_SESSION_KEYS_FROM_SPEC {
log::warn!(target: "runtime::session_keys", "Skipping session keys migration post-upgrade check due to spec version (already applied?)");
return Ok(())
return Ok(());
}

let key_ids = SessionKeys::key_ids();
Expand Down Expand Up @@ -1780,33 +1782,33 @@ sp_api::impl_runtime_apis! {
}

impl xcm_payment_runtime_api::XcmPaymentApi<Block, RuntimeCall> for Runtime {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentError> {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
if !matches!(xcm_version, 3 | 4) {
return Err(XcmPaymentError::UnhandledXcmVersion);
return Err(XcmPaymentApiError::UnhandledXcmVersion);
}
Ok([VersionedAssetId::V4(xcm_config::TokenLocation::get().into())]
.into_iter()
.filter_map(|asset| asset.into_version(xcm_version).ok())
.collect())
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentError> {
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let local_asset = VersionedAssetId::V4(xcm_config::TokenLocation::get().into());
let asset = asset
.into_version(4)
.map_err(|_| XcmPaymentError::VersionedConversionFailed)?;
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;

if asset != local_asset { return Err(XcmPaymentError::AssetNotFound); }
if asset != local_asset { return Err(XcmPaymentApiError::AssetNotFound); }

Ok(WeightToFee::weight_to_fee(&weight))
}

fn query_xcm_weight(message: VersionedXcm<RuntimeCall>) -> Result<Weight, XcmPaymentError> {
fn query_xcm_weight(message: VersionedXcm<RuntimeCall>) -> Result<Weight, XcmPaymentApiError> {
let mut message = message
.try_into()
.map_err(|_| XcmPaymentError::VersionedConversionFailed)?;
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;
<xcm_config::XcmConfig as xcm_executor::Config>::Weigher::weight(&mut message)
.map_err(|_| XcmPaymentError::WeightNotComputable)
.map_err(|_| XcmPaymentApiError::WeightNotComputable)
}
}

Expand Down Expand Up @@ -2493,7 +2495,7 @@ mod remote_tests {
#[tokio::test]
async fn run_migrations() {
if var("RUN_MIGRATION_TESTS").is_err() {
return
return;
}

sp_tracing::try_init_simple();
Expand Down
69 changes: 36 additions & 33 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use xcm::{
use xcm_builder::PayOverXcm;

use xcm_executor::traits::WeightBounds;
use xcm_payment_runtime_api::Error as XcmPaymentError;
use xcm_payment_runtime_api::Error as XcmPaymentApiError;

pub use frame_system::Call as SystemCall;
pub use pallet_balances::Call as BalancesCall;
Expand Down Expand Up @@ -441,10 +441,12 @@ impl OpaqueKeys for OldSessionKeys {
<<Babe as BoundToRuntimeAppPublic>::Public>::ID => self.babe.as_ref(),
sp_core::crypto::key_types::IM_ONLINE => self.im_online.as_ref(),
<<Initializer as BoundToRuntimeAppPublic>::Public>::ID => self.para_validator.as_ref(),
<<ParaSessionInfo as BoundToRuntimeAppPublic>::Public>::ID =>
self.para_assignment.as_ref(),
<<AuthorityDiscovery as BoundToRuntimeAppPublic>::Public>::ID =>
self.authority_discovery.as_ref(),
<<ParaSessionInfo as BoundToRuntimeAppPublic>::Public>::ID => {
self.para_assignment.as_ref()
},
<<AuthorityDiscovery as BoundToRuntimeAppPublic>::Public>::ID => {
self.authority_discovery.as_ref()
},
<<Beefy as BoundToRuntimeAppPublic>::Public>::ID => self.beefy.as_ref(),
_ => &[],
}
Expand Down Expand Up @@ -1050,11 +1052,12 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
ProxyType::Staking => {
matches!(
c,
RuntimeCall::Staking(..) |
RuntimeCall::Session(..) | RuntimeCall::Utility(..) |
RuntimeCall::FastUnstake(..) |
RuntimeCall::VoterList(..) |
RuntimeCall::NominationPools(..)
RuntimeCall::Staking(..)
| RuntimeCall::Session(..)
| RuntimeCall::Utility(..)
| RuntimeCall::FastUnstake(..)
| RuntimeCall::VoterList(..)
| RuntimeCall::NominationPools(..)
)
},
ProxyType::NominationPools => {
Expand All @@ -1070,24 +1073,24 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
ProxyType::Governance => matches!(
c,
// OpenGov calls
RuntimeCall::ConvictionVoting(..) |
RuntimeCall::Referenda(..) |
RuntimeCall::Whitelist(..)
RuntimeCall::ConvictionVoting(..)
| RuntimeCall::Referenda(..)
| RuntimeCall::Whitelist(..)
),
ProxyType::IdentityJudgement => matches!(
c,
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. }) |
RuntimeCall::Utility(..)
RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. })
| RuntimeCall::Utility(..)
),
ProxyType::CancelProxy => {
matches!(c, RuntimeCall::Proxy(pallet_proxy::Call::reject_announcement { .. }))
},
ProxyType::Auction => matches!(
c,
RuntimeCall::Auctions(..) |
RuntimeCall::Crowdloan(..) |
RuntimeCall::Registrar(..) |
RuntimeCall::Slots(..)
RuntimeCall::Auctions(..)
| RuntimeCall::Crowdloan(..)
| RuntimeCall::Registrar(..)
| RuntimeCall::Slots(..)
),
}
}
Expand Down Expand Up @@ -1593,11 +1596,11 @@ pub mod migrations {
let now = frame_system::Pallet::<Runtime>::block_number();
let lease = slots::Pallet::<Runtime>::lease(para);
if lease.is_empty() {
return None
return None;
}
// Lease not yet started, ignore:
if lease.iter().any(Option::is_none) {
return None
return None;
}
let (index, _) =
<slots::Pallet<Runtime> as Leaser<BlockNumber>>::lease_period_index(now)?;
Expand All @@ -1619,7 +1622,7 @@ pub mod migrations {
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, sp_runtime::TryRuntimeError> {
if System::last_runtime_upgrade_spec_version() > UPGRADE_SESSION_KEYS_FROM_SPEC {
log::warn!(target: "runtime::session_keys", "Skipping session keys migration pre-upgrade check due to spec version (already applied?)");
return Ok(Vec::new())
return Ok(Vec::new());
}

log::info!(target: "runtime::session_keys", "Collecting pre-upgrade session keys state");
Expand Down Expand Up @@ -1648,7 +1651,7 @@ pub mod migrations {
fn on_runtime_upgrade() -> Weight {
if System::last_runtime_upgrade_spec_version() > UPGRADE_SESSION_KEYS_FROM_SPEC {
log::warn!("Skipping session keys upgrade: already applied");
return <Runtime as frame_system::Config>::DbWeight::get().reads(1)
return <Runtime as frame_system::Config>::DbWeight::get().reads(1);
}
log::info!("Upgrading session keys");
Session::upgrade_keys::<OldSessionKeys, _>(transform_session_keys);
Expand All @@ -1661,7 +1664,7 @@ pub mod migrations {
) -> Result<(), sp_runtime::TryRuntimeError> {
if System::last_runtime_upgrade_spec_version() > UPGRADE_SESSION_KEYS_FROM_SPEC {
log::warn!(target: "runtime::session_keys", "Skipping session keys migration post-upgrade check due to spec version (already applied?)");
return Ok(())
return Ok(());
}

let key_ids = SessionKeys::key_ids();
Expand Down Expand Up @@ -2257,33 +2260,33 @@ sp_api::impl_runtime_apis! {
}

impl xcm_payment_runtime_api::XcmPaymentApi<Block, RuntimeCall> for Runtime {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentError> {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
if !matches!(xcm_version, 3 | 4) {
return Err(XcmPaymentError::UnhandledXcmVersion);
return Err(XcmPaymentApiError::UnhandledXcmVersion);
}
Ok([VersionedAssetId::V4(xcm_config::TokenLocation::get().into())]
.into_iter()
.filter_map(|asset| asset.into_version(xcm_version).ok())
.collect())
}

fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentError> {
fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, XcmPaymentApiError> {
let local_asset = VersionedAssetId::V4(xcm_config::TokenLocation::get().into());
let asset = asset
.into_version(4)
.map_err(|_| XcmPaymentError::VersionedConversionFailed)?;
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;

if asset != local_asset { return Err(XcmPaymentError::AssetNotFound); }
if asset != local_asset { return Err(XcmPaymentApiError::AssetNotFound); }

Ok(WeightToFee::weight_to_fee(&weight))
}

fn query_xcm_weight(message: VersionedXcm<RuntimeCall>) -> Result<Weight, XcmPaymentError> {
fn query_xcm_weight(message: VersionedXcm<RuntimeCall>) -> Result<Weight, XcmPaymentApiError> {
let mut message = message
.try_into()
.map_err(|_| XcmPaymentError::VersionedConversionFailed)?;
.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?;
<xcm_config::XcmConfig as xcm_executor::Config>::Weigher::weight(&mut message)
.map_err(|_| XcmPaymentError::WeightNotComputable)
.map_err(|_| XcmPaymentApiError::WeightNotComputable)
}
}

Expand Down Expand Up @@ -2578,7 +2581,7 @@ mod remote_tests {
#[tokio::test]
async fn run_migrations() {
if var("RUN_MIGRATION_TESTS").is_err() {
return
return;
}

sp_tracing::try_init_simple();
Expand Down

0 comments on commit dc112d9

Please sign in to comment.