diff --git a/Cargo.toml b/Cargo.toml index 82ae63559a..c40d47f125 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "cli", "codegen", "examples", + "integration-tests", "macro", "metadata", "subxt", diff --git a/examples/examples/polkadot_metadata.scale b/artifacts/polkadot_metadata.scale similarity index 100% rename from examples/examples/polkadot_metadata.scale rename to artifacts/polkadot_metadata.scale diff --git a/examples/examples/balance_transfer.rs b/examples/examples/balance_transfer.rs index 5d5accda0a..21f64435ed 100644 --- a/examples/examples/balance_transfer.rs +++ b/examples/examples/balance_transfer.rs @@ -30,7 +30,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/balance_transfer_with_params.rs b/examples/examples/balance_transfer_with_params.rs index 41150e3d89..9d00eacaa8 100644 --- a/examples/examples/balance_transfer_with_params.rs +++ b/examples/examples/balance_transfer_with_params.rs @@ -35,7 +35,7 @@ use subxt::{ PolkadotExtrinsicParamsBuilder as Params, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/custom_config.rs b/examples/examples/custom_config.rs index a28eff9515..27adb892a1 100644 --- a/examples/examples/custom_config.rs +++ b/examples/examples/custom_config.rs @@ -31,7 +31,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} /// Custom [`Config`] impl where the default types for the target chain differ from the diff --git a/examples/examples/custom_type_derives.rs b/examples/examples/custom_type_derives.rs index 30373df604..dc971389af 100644 --- a/examples/examples/custom_type_derives.rs +++ b/examples/examples/custom_type_derives.rs @@ -19,8 +19,7 @@ #![allow(clippy::redundant_clone)] #[subxt::subxt( - runtime_metadata_path = "examples/polkadot_metadata.scale", - + runtime_metadata_path = "../artifacts/polkadot_metadata.scale", // We can add (certain) custom derives to the generated types by providing // a comma separated list to the below attribute. Most useful for adding `Clone`. // The derives that we can add ultimately is limited to the traits that the base diff --git a/examples/examples/fetch_all_accounts.rs b/examples/examples/fetch_all_accounts.rs index bffb6fb134..32c0818f34 100644 --- a/examples/examples/fetch_all_accounts.rs +++ b/examples/examples/fetch_all_accounts.rs @@ -28,7 +28,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/fetch_staking_details.rs b/examples/examples/fetch_staking_details.rs index eec0d85fce..180f210397 100644 --- a/examples/examples/fetch_staking_details.rs +++ b/examples/examples/fetch_staking_details.rs @@ -34,7 +34,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/metadata_compatibility.rs b/examples/examples/metadata_compatibility.rs index 7318582226..a660bb9d14 100644 --- a/examples/examples/metadata_compatibility.rs +++ b/examples/examples/metadata_compatibility.rs @@ -28,7 +28,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/rpc_call.rs b/examples/examples/rpc_call.rs index e24c07005d..414a1d2f25 100644 --- a/examples/examples/rpc_call.rs +++ b/examples/examples/rpc_call.rs @@ -28,7 +28,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/submit_and_watch.rs b/examples/examples/submit_and_watch.rs index a5e9cda9b6..c41d7541ef 100644 --- a/examples/examples/submit_and_watch.rs +++ b/examples/examples/submit_and_watch.rs @@ -31,7 +31,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} #[tokio::main] diff --git a/examples/examples/subscribe_all_events.rs b/examples/examples/subscribe_all_events.rs index 9dd0b48e07..304cf4bd59 100644 --- a/examples/examples/subscribe_all_events.rs +++ b/examples/examples/subscribe_all_events.rs @@ -32,7 +32,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} /// Subscribe to all events, and then manually look through them and diff --git a/examples/examples/subscribe_one_event.rs b/examples/examples/subscribe_one_event.rs index 4864825787..1d09071a25 100644 --- a/examples/examples/subscribe_one_event.rs +++ b/examples/examples/subscribe_one_event.rs @@ -32,7 +32,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} /// Subscribe to all events, and then manually look through them and diff --git a/examples/examples/subscribe_some_events.rs b/examples/examples/subscribe_some_events.rs index 4585d06314..2e253ad16c 100644 --- a/examples/examples/subscribe_some_events.rs +++ b/examples/examples/subscribe_some_events.rs @@ -32,7 +32,7 @@ use subxt::{ PolkadotExtrinsicParams, }; -#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")] +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] pub mod polkadot {} /// Subscribe to all events, and then manually look through them and diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml new file mode 100644 index 0000000000..3ae9303eed --- /dev/null +++ b/integration-tests/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "subxt-integration" +version = "0.20.0" +authors = ["Parity Technologies "] +edition = "2021" + +license = "GPL-3.0" +readme = "../README.md" +repository = "https://github.com/paritytech/subxt" +documentation = "https://docs.rs/subxt" +homepage = "https://www.parity.io/" +description = "Subxt integration tests that rely on the Substrate binary" + +[features] +default = ["subxt/integration-tests"] + +[dev-dependencies] +assert_matches = "1.5.0" +async-trait = "0.1.49" +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] } +env_logger = "0.9.0" +frame-metadata = "15.0.0" +futures = "0.3.13" +hex = "0.4.3" +log = "0.4.14" +scale-info = { version = "2.0.0", features = ["bit-vec"] } +sp-core = { version = "6.0.0", default-features = false } +sp-keyring = "6.0.0" +sp-runtime = "6.0.0" +subxt = { version = "0.20.0", path = "../subxt" } +test-runtime = { path = "../test-runtime" } +tokio = { version = "1.8", features = ["macros", "time"] } +wabt = "0.10.0" +which = "4.0.2" diff --git a/subxt/tests/integration/client.rs b/integration-tests/src/client/mod.rs similarity index 100% rename from subxt/tests/integration/client.rs rename to integration-tests/src/client/mod.rs diff --git a/subxt/tests/integration/codegen/mod.rs b/integration-tests/src/codegen/mod.rs similarity index 100% rename from subxt/tests/integration/codegen/mod.rs rename to integration-tests/src/codegen/mod.rs diff --git a/subxt/tests/integration/codegen/polkadot.rs b/integration-tests/src/codegen/polkadot.rs similarity index 100% rename from subxt/tests/integration/codegen/polkadot.rs rename to integration-tests/src/codegen/polkadot.rs diff --git a/subxt/tests/integration/events.rs b/integration-tests/src/events/mod.rs similarity index 100% rename from subxt/tests/integration/events.rs rename to integration-tests/src/events/mod.rs diff --git a/subxt/tests/integration/frame/balances.rs b/integration-tests/src/frame/balances.rs similarity index 100% rename from subxt/tests/integration/frame/balances.rs rename to integration-tests/src/frame/balances.rs diff --git a/subxt/tests/integration/frame/contracts.rs b/integration-tests/src/frame/contracts.rs similarity index 100% rename from subxt/tests/integration/frame/contracts.rs rename to integration-tests/src/frame/contracts.rs diff --git a/subxt/tests/integration/frame/mod.rs b/integration-tests/src/frame/mod.rs similarity index 100% rename from subxt/tests/integration/frame/mod.rs rename to integration-tests/src/frame/mod.rs diff --git a/subxt/tests/integration/frame/staking.rs b/integration-tests/src/frame/staking.rs similarity index 100% rename from subxt/tests/integration/frame/staking.rs rename to integration-tests/src/frame/staking.rs diff --git a/subxt/tests/integration/frame/sudo.rs b/integration-tests/src/frame/sudo.rs similarity index 100% rename from subxt/tests/integration/frame/sudo.rs rename to integration-tests/src/frame/sudo.rs diff --git a/subxt/tests/integration/frame/system.rs b/integration-tests/src/frame/system.rs similarity index 100% rename from subxt/tests/integration/frame/system.rs rename to integration-tests/src/frame/system.rs diff --git a/subxt/tests/integration/frame/timestamp.rs b/integration-tests/src/frame/timestamp.rs similarity index 100% rename from subxt/tests/integration/frame/timestamp.rs rename to integration-tests/src/frame/timestamp.rs diff --git a/subxt/tests/integration/main.rs b/integration-tests/src/lib.rs similarity index 94% rename from subxt/tests/integration/main.rs rename to integration-tests/src/lib.rs index e288745714..48cc48f071 100644 --- a/subxt/tests/integration/main.rs +++ b/integration-tests/src/lib.rs @@ -14,7 +14,9 @@ // You should have received a copy of the GNU General Public License // along with subxt. If not, see . +#[cfg(test)] mod codegen; +#[cfg(test)] mod utils; #[cfg(test)] @@ -24,10 +26,11 @@ mod events; #[cfg(test)] mod frame; #[cfg(test)] -#[cfg(integration_tests)] mod metadata; #[cfg(test)] mod storage; +#[cfg(test)] use test_runtime::node_runtime; +#[cfg(test)] use utils::*; diff --git a/subxt/tests/integration/metadata/mod.rs b/integration-tests/src/metadata/mod.rs similarity index 100% rename from subxt/tests/integration/metadata/mod.rs rename to integration-tests/src/metadata/mod.rs diff --git a/subxt/tests/integration/metadata/validation.rs b/integration-tests/src/metadata/validation.rs similarity index 100% rename from subxt/tests/integration/metadata/validation.rs rename to integration-tests/src/metadata/validation.rs diff --git a/subxt/tests/integration/storage.rs b/integration-tests/src/storage/mod.rs similarity index 100% rename from subxt/tests/integration/storage.rs rename to integration-tests/src/storage/mod.rs diff --git a/subxt/tests/integration/utils/context.rs b/integration-tests/src/utils/context.rs similarity index 100% rename from subxt/tests/integration/utils/context.rs rename to integration-tests/src/utils/context.rs diff --git a/subxt/tests/integration/utils/mod.rs b/integration-tests/src/utils/mod.rs similarity index 100% rename from subxt/tests/integration/utils/mod.rs rename to integration-tests/src/utils/mod.rs diff --git a/subxt/tests/integration/utils/node_proc.rs b/integration-tests/src/utils/node_proc.rs similarity index 98% rename from subxt/tests/integration/utils/node_proc.rs rename to integration-tests/src/utils/node_proc.rs index 66c5c3543a..16e5d8887f 100644 --- a/subxt/tests/integration/utils/node_proc.rs +++ b/integration-tests/src/utils/node_proc.rs @@ -37,7 +37,6 @@ use subxt::{ pub struct TestNodeProcess { proc: process::Child, client: Client, - #[cfg(integration_tests)] ws_url: String, } @@ -79,7 +78,6 @@ where } /// Returns the address to which the client is connected. - #[cfg(integration_tests)] pub fn ws_url(&self) -> &str { &self.ws_url } @@ -149,7 +147,6 @@ impl TestNodeProcessBuilder { Ok(TestNodeProcess { proc, client, - #[cfg(integration_tests)] ws_url, }) } diff --git a/metadata/Cargo.toml b/metadata/Cargo.toml index b51ac458eb..8657d2833b 100644 --- a/metadata/Cargo.toml +++ b/metadata/Cargo.toml @@ -21,7 +21,6 @@ sp-core = { version = "6.0.0" } bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] } criterion = "0.3" scale-info = { version = "2.0.0", features = ["bit-vec"] } -test-runtime = { path = "../test-runtime" } [lib] # Without this, libtest cli opts interfere with criteron benches: diff --git a/metadata/benches/bench.rs b/metadata/benches/bench.rs index 09c866a1b1..20ef71e600 100644 --- a/metadata/benches/bench.rs +++ b/metadata/benches/bench.rs @@ -26,6 +26,10 @@ use scale_info::{ TypeDef, TypeDefVariant, }; +use std::{ + fs, + path::Path, +}; use subxt_metadata::{ get_call_hash, get_constant_hash, @@ -35,7 +39,8 @@ use subxt_metadata::{ }; fn load_metadata() -> RuntimeMetadataV14 { - let bytes = test_runtime::METADATA; + let bytes = fs::read(Path::new("../artifacts/polkadot_metadata.scale")) + .expect("Cannot read metadata blob"); let meta: RuntimeMetadataPrefixed = Decode::decode(&mut &*bytes).expect("Cannot decode scale metadata"); diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index eb6a7f2cfa..24a698f16f 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -12,6 +12,12 @@ homepage = "https://www.parity.io/" description = "Submit extrinsics (transactions) to a substrate node via RPC" keywords = ["parity", "substrate", "blockchain"] +[features] +# Activate this to expose functionality only used for integration testing. +# The exposed functionality is subject to breaking changes at any point, +# and should not be relied upon. +integration-tests = [] + [dependencies] bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] } codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] } @@ -42,5 +48,4 @@ env_logger = "0.9.0" tempdir = "0.3.7" wabt = "0.10.0" which = "4.0.2" -test-runtime = { path = "../test-runtime" } sp-keyring = "6.0.0" diff --git a/subxt/src/client.rs b/subxt/src/client.rs index 5dcb8206d7..3072f674b4 100644 --- a/subxt/src/client.rs +++ b/subxt/src/client.rs @@ -88,7 +88,7 @@ impl ClientBuilder { /// Set the metadata. /// /// *Note:* Metadata will no longer be downloaded from the runtime node. - #[cfg(integration_tests)] + #[cfg(feature = "integration-tests")] pub fn set_metadata(mut self, metadata: Metadata) -> Self { self.metadata = Some(metadata); self diff --git a/subxt/src/metadata/metadata_type.rs b/subxt/src/metadata/metadata_type.rs index 2ceeddcfeb..7069423a6c 100644 --- a/subxt/src/metadata/metadata_type.rs +++ b/subxt/src/metadata/metadata_type.rs @@ -478,13 +478,64 @@ impl TryFrom for Metadata { mod tests { use super::*; use crate::StorageEntryKey; + use frame_metadata::{ + ExtrinsicMetadata, + PalletStorageMetadata, + StorageEntryModifier, + StorageEntryType, + }; + use scale_info::{ + meta_type, + TypeInfo, + }; fn load_metadata() -> Metadata { - let bytes = test_runtime::METADATA; - let meta: RuntimeMetadataPrefixed = - codec::Decode::decode(&mut &*bytes).expect("Cannot decode scale metadata"); + #[allow(dead_code)] + #[allow(non_camel_case_types)] + #[derive(TypeInfo)] + enum Call { + fill_block { param: u128 }, + } + let storage = PalletStorageMetadata { + prefix: "System", + entries: vec![StorageEntryMetadata { + name: "Account", + modifier: StorageEntryModifier::Optional, + ty: StorageEntryType::Plain(meta_type::()), + default: vec![0], + docs: vec![], + }], + }; + let constant = PalletConstantMetadata { + name: "BlockWeights", + ty: meta_type::(), + value: vec![1, 2, 3], + docs: vec![], + }; + let pallet = frame_metadata::PalletMetadata { + index: 0, + name: "System", + calls: Some(frame_metadata::PalletCallMetadata { + ty: meta_type::(), + }), + storage: Some(storage), + constants: vec![constant], + event: None, + error: None, + }; + + let metadata = RuntimeMetadataV14::new( + vec![pallet], + ExtrinsicMetadata { + ty: meta_type::<()>(), + version: 0, + signed_extensions: vec![], + }, + meta_type::<()>(), + ); + let prefixed = RuntimeMetadataPrefixed::from(metadata); - Metadata::try_from(meta) + Metadata::try_from(prefixed) .expect("Cannot translate runtime metadata to internal Metadata") } @@ -501,10 +552,10 @@ mod tests { hash ); - // Currently the caching does not take into account different pallets - // as the intended behavior is to use this method only once. - // Enforce this behavior into testing. - let hash_old = metadata.metadata_hash(&["Balances"]); + // The cache `metadata.inner.cached_metadata_hash` is already populated from + // the previous call. Therefore, changing the pallets argument must not + // change the methods behavior. + let hash_old = metadata.metadata_hash(&["no-pallet"]); assert_eq!(hash_old, hash); }