From a15fb0d63274eae9a0dbbc04e5327d52d26612fa Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 1 Dec 2021 13:00:45 +0000 Subject: [PATCH 1/3] Add timestamp pallet test (#340) * Add timestamp pallet test * Remove println * Make timestamp test more reliable --- tests/integration/frame/mod.rs | 1 + tests/integration/frame/timestamp.rs | 48 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/integration/frame/timestamp.rs diff --git a/tests/integration/frame/mod.rs b/tests/integration/frame/mod.rs index 8d18e46748..c7ec95aa03 100644 --- a/tests/integration/frame/mod.rs +++ b/tests/integration/frame/mod.rs @@ -21,3 +21,4 @@ mod contracts; mod staking; mod sudo; mod system; +mod timestamp; diff --git a/tests/integration/frame/timestamp.rs b/tests/integration/frame/timestamp.rs new file mode 100644 index 0000000000..301305783f --- /dev/null +++ b/tests/integration/frame/timestamp.rs @@ -0,0 +1,48 @@ +// Copyright 2019-2021 Parity Technologies (UK) Ltd. +// This file is part of subxt. +// +// subxt is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// subxt is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with subxt. If not, see . + +use crate::test_context; +use std::time::{ + SystemTime, + UNIX_EPOCH, +}; + +#[async_std::test] +async fn storage_get_current_timestamp() { + let sys_timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis() as u64; + let cxt = test_context().await; + + // wait until blocks are produced to get the timestamp + let mut sub = cxt.client().rpc().subscribe_blocks().await.unwrap(); + let block_hash = loop { + if let Ok(Some(block)) = sub.next().await { + break block.hash() + } + }; + + let timestamp = cxt + .api + .storage() + .timestamp() + .now(Some(block_hash)) + .await + .unwrap(); + + assert!(timestamp > sys_timestamp) +} From a4f1cbd9a1a8fdebb6818822686ec57c562e2795 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 2 Dec 2021 11:51:12 +0000 Subject: [PATCH 2/3] one nightly CI run a day, and link to it in issue template (#344) --- .github/issue_templates/nightly_run_failed.md | 4 +++- .github/workflows/nightly.yml | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/issue_templates/nightly_run_failed.md b/.github/issue_templates/nightly_run_failed.md index 96566ddd86..5743a84d6f 100644 --- a/.github/issue_templates/nightly_run_failed.md +++ b/.github/issue_templates/nightly_run_failed.md @@ -2,4 +2,6 @@ title: Subxt integration tests failed against latest Substrate build. --- -The nightly CI run which downloads the latest version of Substrate ran into test failures, which likely means that there are breaking changes that need fixing in Subxt. \ No newline at end of file +The nightly CI run which downloads the latest version of Substrate ran into test failures, which likely means that there are breaking changes that need fixing in Subxt. + +Go to https://github.com/paritytech/subxt/actions/workflows/nightly.yml to see details about the failure. \ No newline at end of file diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index b47dcf6acc..4a52fa81a2 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -3,7 +3,7 @@ name: Daily compatibility check against latest substrate on: schedule: # Run at 8am every day - - cron: "* 8 * * *" + - cron: "0 8 * * *" env: From e05d24bd8a4751c24ed14e0b93f78e705ece779d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 2 Dec 2021 17:02:11 +0000 Subject: [PATCH 3/3] Make system properties an arbitrary JSON object, plus CI fixes (#349) * Make system properties an arbitrary JSON object * Add comment * Make timestamp test more reliable * Fmt * Update src/client.rs Co-authored-by: David * Update src/client.rs Co-authored-by: David * Fix clippy errors and warnings * Fix tests Co-authored-by: David --- cli/src/main.rs | 4 ++-- codegen/src/ir.rs | 1 + codegen/src/types/tests.rs | 12 ++++++------ src/client.rs | 9 ++++++++- src/metadata.rs | 5 +++++ src/rpc.rs | 13 ++----------- tests/integration/frame/timestamp.rs | 26 ++------------------------ 7 files changed, 26 insertions(+), 44 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 271a720903..44a7189eae 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -120,7 +120,7 @@ fn main() -> color_eyre::Result<()> { }); let (_, bytes) = fetch_metadata(&url)?; codegen(&mut &bytes[..])?; - return Ok(()) + Ok(()) } } } @@ -139,7 +139,7 @@ fn fetch_metadata(url: &url::Url) -> color_eyre::Result<(String, Vec)> { let hex_data = json["result"] .as_str() .map(ToString::to_string) - .ok_or(eyre::eyre!("metadata result field should be a string"))?; + .ok_or_else(|| eyre::eyre!("metadata result field should be a string"))?; let bytes = hex::decode(hex_data.trim_start_matches("0x"))?; Ok((hex_data, bytes)) diff --git a/codegen/src/ir.rs b/codegen/src/ir.rs index f214241fda..779fee4545 100644 --- a/codegen/src/ir.rs +++ b/codegen/src/ir.rs @@ -73,6 +73,7 @@ impl ItemMod { } #[derive(Debug, PartialEq, Eq)] +#[allow(clippy::large_enum_variant)] pub enum Item { Rust(syn::Item), Subxt(SubxtItem), diff --git a/codegen/src/types/tests.rs b/codegen/src/types/tests.rs index dac5bf7c62..21364f429e 100644 --- a/codegen/src/types/tests.rs +++ b/codegen/src/types/tests.rs @@ -22,7 +22,7 @@ use scale_info::{ TypeInfo, }; -const MOD_PATH: &'static [&'static str] = &["subxt_codegen", "types", "tests"]; +const MOD_PATH: &[&str] = &["subxt_codegen", "types", "tests"]; fn get_mod<'a>(module: &'a Module, path_segs: &[&'static str]) -> Option<&'a Module<'a>> { let (mod_name, rest) = path_segs.split_first()?; @@ -790,7 +790,7 @@ fn generics_with_alias_adds_phantom_data_marker() { #[test] fn modules() { - mod modules { + mod m { pub mod a { #[allow(unused)] #[derive(scale_info::TypeInfo)] @@ -815,7 +815,7 @@ fn modules() { } let mut registry = Registry::new(); - registry.register_type(&meta_type::()); + registry.register_type(&meta_type::()); let portable_types: PortableRegistry = registry.into(); let type_gen = TypeGenerator::new( @@ -832,7 +832,7 @@ fn modules() { quote! { pub mod tests { use super::root; - pub mod modules { + pub mod m { use super::root; pub mod a { use super::root; @@ -842,7 +842,7 @@ fn modules() { #[derive(::subxt::codec::Encode, ::subxt::codec::Decode)] pub struct Bar { - pub a: root::subxt_codegen::types::tests::modules::a::Foo, + pub a: root::subxt_codegen::types::tests::m::a::Foo, } } @@ -855,7 +855,7 @@ fn modules() { #[derive(::subxt::codec::Encode, ::subxt::codec::Decode)] pub struct Foo { - pub a: root::subxt_codegen::types::tests::modules::a::b::Bar, + pub a: root::subxt_codegen::types::tests::m::a::b::Bar, } } } diff --git a/src/client.rs b/src/client.rs index c243d0fc91..b2bd582616 100644 --- a/src/client.rs +++ b/src/client.rs @@ -157,7 +157,14 @@ impl Client { &self.metadata } - /// Returns the system properties + /// Returns the properties defined in the chain spec as a JSON object. + /// + /// # Note + /// + /// Many chains use this to define common properties such as `token_decimals` and `token_symbol` + /// required for UIs, but this is merely a convention. It is up to the library user to + /// deserialize the JSON into the appropriate type or otherwise extract the properties defined + /// in the target chain's spec. pub fn properties(&self) -> &SystemProperties { &self.properties } diff --git a/src/metadata.rs b/src/metadata.rs index f8f4e2ad01..51af03f282 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -143,6 +143,11 @@ pub struct PalletMetadata { } impl PalletMetadata { + /// Get the name of the pallet. + pub fn name(&self) -> &str { + &self.name + } + /// Encode a call based on this pallet metadata. pub fn encode_call(&self, call: &C) -> Result where diff --git a/src/rpc.rs b/src/rpc.rs index 1543a6b228..1e43c14cf6 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -141,17 +141,8 @@ impl From for BlockNumber { } } -/// System properties for a Substrate-based runtime -#[derive(serde::Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)] -#[serde(rename_all = "camelCase")] -pub struct SystemProperties { - /// The address format - pub ss58_format: u8, - /// The number of digits after the decimal point in the native token - pub token_decimals: u8, - /// The symbol of the native token - pub token_symbol: String, -} +/// Arbitrary properties defined in the chain spec as a JSON object. +pub type SystemProperties = serde_json::Map; /// Possible transaction status events. /// diff --git a/tests/integration/frame/timestamp.rs b/tests/integration/frame/timestamp.rs index 301305783f..1aa9f78dd9 100644 --- a/tests/integration/frame/timestamp.rs +++ b/tests/integration/frame/timestamp.rs @@ -15,34 +15,12 @@ // along with subxt. If not, see . use crate::test_context; -use std::time::{ - SystemTime, - UNIX_EPOCH, -}; #[async_std::test] async fn storage_get_current_timestamp() { - let sys_timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH) - .unwrap() - .as_millis() as u64; let cxt = test_context().await; - // wait until blocks are produced to get the timestamp - let mut sub = cxt.client().rpc().subscribe_blocks().await.unwrap(); - let block_hash = loop { - if let Ok(Some(block)) = sub.next().await { - break block.hash() - } - }; + let timestamp = cxt.api.storage().timestamp().now(None).await; - let timestamp = cxt - .api - .storage() - .timestamp() - .now(Some(block_hash)) - .await - .unwrap(); - - assert!(timestamp > sys_timestamp) + assert!(timestamp.is_ok()) }