From 7bdfdb66a653f80e9609ddac856631d2ce9174d6 Mon Sep 17 00:00:00 2001 From: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:56:37 +0200 Subject: [PATCH] cleaner parachain matching (#25) --- src/build_upgrade.rs | 44 ++++++-------------------------------------- src/types.rs | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/build_upgrade.rs b/src/build_upgrade.rs index ebc3ede..dd023df 100644 --- a/src/build_upgrade.rs +++ b/src/build_upgrade.rs @@ -452,25 +452,9 @@ async fn construct_kusama_batch( let mut batch_calls = Vec::new(); for auth in para_calls { - match auth.network { - // Relays. This iterator should only have parachain calls. - Network::Kusama | Network::Polkadot => - panic!("para calls should not contain relay calls"), - - // Polkadot parachains - Network::PolkadotAssetHub - | Network::PolkadotCollectives - | Network::PolkadotBridgeHub => panic!("not kusama parachains"), - - // The rest. We could `_` it but we match explicitly to avoid footguns when adding new - // chains to a network. - Network::KusamaAssetHub - | Network::KusamaBridgeHub - | Network::KusamaCoretime - | Network::KusamaEncointer => { - let send_auth = send_as_superuser_from_kusama(&auth).await; - batch_calls.push(send_auth); - }, + if auth.network.is_kusama_para() { + let send_auth = send_as_superuser_from_kusama(&auth).await; + batch_calls.push(send_auth); } } if let Some(a) = additional { @@ -495,25 +479,9 @@ async fn construct_polkadot_batch( let mut batch_calls = Vec::new(); for auth in para_calls { - match auth.network { - // Relays. This iterator should only have parachain calls. - Network::Kusama | Network::Polkadot => - panic!("para calls should not contain relay calls"), - - // Kusama parachains - Network::KusamaAssetHub - | Network::KusamaBridgeHub - | Network::KusamaCoretime - | Network::KusamaEncointer => panic!("not polkadot parachains"), - - // The rest. We could `_` it but we match explicitly to avoid footguns when adding new - // chains to a network. - Network::PolkadotAssetHub - | Network::PolkadotCollectives - | Network::PolkadotBridgeHub => { - let send_auth = send_as_superuser_from_polkadot(&auth).await; - batch_calls.push(send_auth); - }, + if auth.network.is_polkadot_para() { + let send_auth = send_as_superuser_from_polkadot(&auth).await; + batch_calls.push(send_auth); } } if let Some(a) = additional { diff --git a/src/types.rs b/src/types.rs index b9685ad..f1ab086 100644 --- a/src/types.rs +++ b/src/types.rs @@ -73,20 +73,35 @@ pub(super) enum Network { } impl Network { + /// Return the `ParaId` of a given network. Returns an error if the network is not a parachain. pub(super) fn get_para_id(&self) -> Result { use Network::*; match &self { + // Kusama Kusama => Err("relay chain"), KusamaAssetHub => Ok(1_000), KusamaBridgeHub => Ok(1_002), KusamaCoretime => Ok(1_005), KusamaEncointer => Ok(1_001), + // Polkadot Polkadot => Err("relay chain"), PolkadotAssetHub => Ok(1_000), - PolkadotCollectives => Ok(1_001), PolkadotBridgeHub => Ok(1_002), + PolkadotCollectives => Ok(1_001), } } + + /// Returns `true` if the network is a Kusama _parachain_. + pub(super) fn is_kusama_para(&self) -> bool { + use Network::*; + matches!(self, KusamaAssetHub | KusamaBridgeHub | KusamaCoretime | KusamaEncointer) + } + + /// Returns `true` if the network is a Polkadot _parachain_. + pub(super) fn is_polkadot_para(&self) -> bool { + use Network::*; + matches!(self, PolkadotAssetHub | PolkadotCollectives | PolkadotBridgeHub) + } } // Info and preferences provided by the user for proposal submission.