From e8c475c6cbbc6056987ff026cf2ce22f471b6220 Mon Sep 17 00:00:00 2001 From: Daniel Savu Date: Mon, 9 May 2022 20:49:54 +0100 Subject: [PATCH 1/2] feat: fail to start on metadata mismatch --- runtime/src/error.rs | 2 ++ runtime/src/rpc.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/runtime/src/error.rs b/runtime/src/error.rs index a0a1df50c..04d89c31b 100644 --- a/runtime/src/error.rs +++ b/runtime/src/error.rs @@ -55,6 +55,8 @@ pub enum Error { InsufficientFunds, #[error("Client does not support spec_version: expected {0}, got {1}")] InvalidSpecVersion(u32, u32), + #[error("Client metadata is different from parachain metadata: expected {0}, got {1}")] + ParachainMetadataMismatch(String, String), #[error("Failed to load credentials from file: {0}")] KeyLoadingFailure(#[from] KeyLoadingError), #[error("Error serializing: {0}")] diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index 1641c398f..a9e143678 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -13,6 +13,7 @@ use codec::Encode; use futures::{future::join_all, stream::StreamExt, FutureExt, SinkExt}; use module_oracle_rpc_runtime_api::BalanceWrapper; use primitives::UnsignedFixedPoint; +use serde_json::{Map, Value}; use sp_runtime::FixedPointNumber; use std::{collections::BTreeSet, future::Future, sync::Arc, time::Duration}; use subxt::{ @@ -30,15 +31,19 @@ const TRANSACTION_TIMEOUT: Duration = Duration::from_secs(300); // 5 minute time cfg_if::cfg_if! { if #[cfg(feature = "standalone-metadata")] { const DEFAULT_SPEC_VERSION: u32 = 1; + const DEFAULT_SPEC_NAME: &str = "interbtc-standalone"; pub const SS58_PREFIX: u16 = 42; } else if #[cfg(feature = "parachain-metadata-interlay")] { const DEFAULT_SPEC_VERSION: u32 = 3; + const DEFAULT_SPEC_NAME: &str = "interlay-parachain"; pub const SS58_PREFIX: u16 = 2032; } else if #[cfg(feature = "parachain-metadata-kintsugi")] { const DEFAULT_SPEC_VERSION: u32 = 15; + const DEFAULT_SPEC_NAME: &str = "kintsugi-parachain"; pub const SS58_PREFIX: u16 = 2092; } else if #[cfg(feature = "parachain-metadata-testnet")] { const DEFAULT_SPEC_VERSION: u32 = 6; + const DEFAULT_SPEC_NAME: &str = "testnet-parachain"; pub const SS58_PREFIX: u16 = 42; } } @@ -69,6 +74,18 @@ impl InterBtcParachain { let api: RuntimeApi = ext_client.clone().to_runtime_api(); let runtime_version = ext_client.rpc().runtime_version(None).await?; + if let Some(spec_name) = runtime_version.other.get("specName") { + if spec_name == DEFAULT_SPEC_NAME { + log::info!("spec_name={}", spec_name); + } else { + return Err(Error::ParachainMetadataMismatch( + DEFAULT_SPEC_NAME.into(), + // The spec name is always expected to be a string if defined, so `unwrap()` never panics. + spec_name.as_str().unwrap().into(), + )); + } + } + if runtime_version.spec_version == DEFAULT_SPEC_VERSION { log::info!("spec_version={}", runtime_version.spec_version); log::info!("transaction_version={}", runtime_version.transaction_version); From 580617464a9daffb4add1a82d90c58efa6883221 Mon Sep 17 00:00:00 2001 From: Daniel Savu Date: Tue, 10 May 2022 14:07:32 +0100 Subject: [PATCH 2/2] chore: unwrap spec_name to default --- runtime/src/rpc.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index a9e143678..4ce2e8440 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -74,16 +74,15 @@ impl InterBtcParachain { let api: RuntimeApi = ext_client.clone().to_runtime_api(); let runtime_version = ext_client.rpc().runtime_version(None).await?; - if let Some(spec_name) = runtime_version.other.get("specName") { - if spec_name == DEFAULT_SPEC_NAME { - log::info!("spec_name={}", spec_name); - } else { - return Err(Error::ParachainMetadataMismatch( - DEFAULT_SPEC_NAME.into(), - // The spec name is always expected to be a string if defined, so `unwrap()` never panics. - spec_name.as_str().unwrap().into(), - )); - } + let default_spec_name = &Value::default(); + let spec_name = runtime_version.other.get("specName").unwrap_or(default_spec_name); + if spec_name == DEFAULT_SPEC_NAME { + log::info!("spec_name={}", spec_name); + } else { + return Err(Error::ParachainMetadataMismatch( + DEFAULT_SPEC_NAME.into(), + spec_name.as_str().unwrap_or_default().into(), + )); } if runtime_version.spec_version == DEFAULT_SPEC_VERSION {