Skip to content

Commit

Permalink
Add runtime test for minimal dev chain spec
Browse files Browse the repository at this point in the history
Signed-off-by: EleisonC <[email protected]>
  • Loading branch information
EleisonC committed Nov 27, 2024
1 parent 0146534 commit 14a32f1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions templates/minimal/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ build = "build.rs"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
docify = { workspace = true }
clap = { features = ["derive"], workspace = true }
futures = { features = ["thread-pool"], workspace = true }
futures-timer = { workspace = true }
serde = { features = ["derive"], workspace = true, default-features = true }
jsonrpsee = { features = ["server"], workspace = true }
serde_json = { workspace = true, default-features = true }

Expand Down
84 changes: 84 additions & 0 deletions templates/minimal/node/tests/validate_dev_chain_spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use codec::Decode;
use sp_version::RuntimeVersion;
use polkadot_sdk::*;
use minimal_template_runtime::WASM_BINARY;
use sc_service::ChainType;
use sc_chain_spec::{
ChainSpecExtension,
ChainSpecGroup,
Properties,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use core::str;
use std::error::Error;

pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ChainSpecGroup, ChainSpecExtension)]
pub struct Extensions {
#[serde(alias = "relayChain", alias = "RelayChain")]
pub relay_chain: String,
#[serde(alias = "paraId", alias = "ParaId")]
pub para_id: u32,
}

impl Extensions {
pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
sc_chain_spec::get_extension(chain_spec.extensions())
}
}

fn decode_runtime_info(ch_spec_data: &ChainSpec) ->
Result<(String, u32, u32), Box<dyn Error>> {
let chain_spec_as_json = ch_spec_data.as_json(false)
.expect("Failed to serialize existing chain spec");
let json_value: Value = serde_json::from_str(&chain_spec_as_json)?; // Deserialize the chain spec JSON string into a usable serde_Json Value object.

let runtime_hx_cde = json_value
.get("genesis").expect("failed to get genesis")
.get("runtimeGenesis").expect("failed to get runtimeGenesis")
.get("code").expect("failed to get code")
.as_str().expect("failed to turn to str");

let clean_u8 = runtime_hx_cde.trim_start_matches("0x").as_bytes(); // normalize runtime hex cde remove '0x' and convert to bytes.
let version_info: RuntimeVersion = RuntimeVersion::decode(&mut &clean_u8[..])
.map_err(|_| "Failed to decode runtime version")?;

let rt_spec_name = version_info.spec_name.into_owned();
let rt_spec_version = version_info.spec_version;
let rt_authoring_version = version_info.authoring_version;

Ok((rt_spec_name, rt_spec_version, rt_authoring_version))
}

#[test]
fn test_minimal_dev_chain_spec_rt_validity() {
let mut properties = Properties::new();
properties.insert("tokenDecimals".to_string(), 12.into());
properties.insert("tokenSymbol".to_string(), "UNIT".into());
let current_wasm = WASM_BINARY.expect("Development wasm not available");

let test_chain_spec = ChainSpec::builder(
current_wasm,
Extensions {
relay_chain: "dev".into(),
para_id: 1000,
})
.with_name("test_Development")
.with_id("dev")
.with_chain_type(ChainType::Local)
.with_genesis_config_preset_name(sp_genesis_builder::DEV_RUNTIME_PRESET)
.with_properties(properties.clone())
.build();

let existing_chain_spec = ChainSpec::from_json_file(
"../dev_chain_spec.json".into()
).expect("failed to find development chain spec");

let test_chain_data:(String, u32, u32) = decode_runtime_info(&test_chain_spec).expect("failed to retrieve test chain runtime info");
let current_chain_data:(String, u32, u32) = decode_runtime_info(&existing_chain_spec).expect("failed to retrieve current chain runtime info");

assert_eq!(test_chain_data, current_chain_data);
}

0 comments on commit 14a32f1

Please sign in to comment.