From 8e2ede41794d36c7a19ded73e1f9e2429fd44fca Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 2 Feb 2022 16:25:18 +0200 Subject: [PATCH 01/10] test-runtime: Fix README typo Signed-off-by: Alexandru Vasile --- test-runtime/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-runtime/README.md b/test-runtime/README.md index 9763412df2..07cc531360 100644 --- a/test-runtime/README.md +++ b/test-runtime/README.md @@ -3,8 +3,8 @@ The logic for this crate exists mainly in the `build.rs` file. At compile time, this crate will: -- Spin up a local `substrate` binary (set the `SUBSTRATE_NODE_PATH` env var to point to a custom binary, otehrwise it'll look for `substrate` on your PATH). +- Spin up a local `substrate` binary (set the `SUBSTRATE_NODE_PATH` env var to point to a custom binary, otherwise it'll look for `substrate` on your PATH). - Obtain metadata from this node. - Export the metadata and a `node_runtime` module which has been annotated using the `subxt` proc macro and is based off the above metadata. -The reason for doing this is that our integration tests (which also spin up a Substrate node) can then use the generated `subxt` types from the exact node being tested against, so that we don't have to worry about metadata getting out of sync with the binary under test. \ No newline at end of file +The reason for doing this is that our integration tests (which also spin up a Substrate node) can then use the generated `subxt` types from the exact node being tested against, so that we don't have to worry about metadata getting out of sync with the binary under test. From fbf2aaf8cbd0f40e52bc3c5eaf3e08eb56e0423d Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 2 Feb 2022 17:32:11 +0200 Subject: [PATCH 02/10] test-runtime: Explicit error handling for missing substrate binary Signed-off-by: Alexandru Vasile --- test-runtime/build.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test-runtime/build.rs b/test-runtime/build.rs index 71a075a37a..cddbaf5991 100644 --- a/test-runtime/build.rs +++ b/test-runtime/build.rs @@ -54,6 +54,10 @@ async fn run() { .spawn(); let mut cmd = match cmd { Ok(cmd) => KillOnDrop(cmd), + Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { + panic!("A substrate binary should be installed on your path for testing purposes. \ + See https://github.com/paritytech/subxt/tree/master#integration-testing") + } Err(e) => { panic!("Cannot spawn substrate command '{}': {}", substrate_bin, e) } From 6da76520646ade20e6d39cce40cbfdd1d7fabd87 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Wed, 2 Feb 2022 17:35:14 +0200 Subject: [PATCH 03/10] test-runtime: Fix documentation typo Signed-off-by: Alexandru Vasile --- test-runtime/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-runtime/build.rs b/test-runtime/build.rs index cddbaf5991..246e617b38 100644 --- a/test-runtime/build.rs +++ b/test-runtime/build.rs @@ -161,7 +161,7 @@ fn next_open_port() -> Option { } } -/// If the substrate process isn't explicilty killed on drop, +/// If the substrate process isn't explicitly killed on drop, /// it seems that panics that occur while the command is running /// will leave it running and block the build step from ever finishing. /// Wrapping it in this prevents this from happening. From 28c82326ed818939b37d3fee7e980b3ededacd6e Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 13:51:39 +0200 Subject: [PATCH 04/10] events: Test primitive decode_and_consume Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index d7f5fb9e93..af21e72bad 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -373,10 +373,13 @@ pub enum EventsDecodingError { mod tests { use super::*; use crate::{ + error::GenericError::EventsDecoding, + events::EventsDecodingError::UnsupportedPrimitive, Config, DefaultConfig, Phase, }; + use assert_matches::assert_matches; use codec::Encode; use frame_metadata::{ v14::{ @@ -643,4 +646,36 @@ mod tests { bitvec::bitvec![Msb0, u64; 0, 1, 1, 0, 1, 0, 1, 0, 0], ); } + + #[test] + fn decode_primitive() { + decode_and_consume_type_consumes_all_bytes(false); + decode_and_consume_type_consumes_all_bytes(true); + + let dummy_data = vec![0u8]; + let dummy_cursor = &mut &*dummy_data; + let (id, reg) = singleton_type_registry::(); + let res = decode_and_consume_type(id.id(), ®, dummy_cursor); + assert_matches!( + res, + Err(EventsDecoding(UnsupportedPrimitive(TypeDefPrimitive::Char))) + ); + + decode_and_consume_type_consumes_all_bytes("str".to_string()); + + decode_and_consume_type_consumes_all_bytes(1u8); + decode_and_consume_type_consumes_all_bytes(1i8); + + decode_and_consume_type_consumes_all_bytes(1u16); + decode_and_consume_type_consumes_all_bytes(1i16); + + decode_and_consume_type_consumes_all_bytes(1u32); + decode_and_consume_type_consumes_all_bytes(1i32); + + decode_and_consume_type_consumes_all_bytes(1u64); + decode_and_consume_type_consumes_all_bytes(1i64); + + decode_and_consume_type_consumes_all_bytes(1u128); + decode_and_consume_type_consumes_all_bytes(1i128); + } } From 5393d3bbbc38ffb479030144bdeefbb83a39d9cb Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 16:12:58 +0200 Subject: [PATCH 05/10] events: Test tuple decode_and_consume Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index af21e72bad..79fc607ca6 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -373,7 +373,10 @@ pub enum EventsDecodingError { mod tests { use super::*; use crate::{ - error::GenericError::EventsDecoding, + error::GenericError::{ + Codec, + EventsDecoding, + }, events::EventsDecodingError::UnsupportedPrimitive, Config, DefaultConfig, @@ -678,4 +681,32 @@ mod tests { decode_and_consume_type_consumes_all_bytes(1u128); decode_and_consume_type_consumes_all_bytes(1i128); } + + #[test] + fn decode_tuple() { + decode_and_consume_type_consumes_all_bytes(()); + + decode_and_consume_type_consumes_all_bytes((true,)); + + decode_and_consume_type_consumes_all_bytes((true, "str")); + + // Incomplete bytes for decoding + let dummy_data = false.encode(); + let dummy_cursor = &mut &*dummy_data; + let (id, reg) = singleton_type_registry::<(bool, &'static str)>(); + let res = decode_and_consume_type(id.id(), ®, dummy_cursor); + assert_matches!(res, Err(Codec(_))); + + // Incomplete bytes for decoding, with invalid char type + let dummy_data = (false, "str", 0u8).encode(); + let dummy_cursor = &mut &*dummy_data; + let (id, reg) = singleton_type_registry::<(bool, &'static str, char)>(); + let res = decode_and_consume_type(id.id(), ®, dummy_cursor); + assert_matches!( + res, + Err(EventsDecoding(UnsupportedPrimitive(TypeDefPrimitive::Char))) + ); + // The last byte (0x0 u8) should not be consumed + assert_eq!(dummy_cursor.len(), 1); + } } From 921dda4e242c88cfc6628cfaa68c05b1de2a4a74 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 16:40:24 +0200 Subject: [PATCH 06/10] events: Test array decode_and_consume Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index 79fc607ca6..9e0d94132d 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -709,4 +709,12 @@ mod tests { // The last byte (0x0 u8) should not be consumed assert_eq!(dummy_cursor.len(), 1); } + + #[test] + fn decode_array() { + decode_and_consume_type_consumes_all_bytes([0]); + decode_and_consume_type_consumes_all_bytes([1, 2, 3, 4, 5]); + decode_and_consume_type_consumes_all_bytes([0; 500]); + decode_and_consume_type_consumes_all_bytes(["str", "abc", "cde"]); + } } From 65d4a99d8d69a8118850ac7e767323878c20435d Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 16:44:59 +0200 Subject: [PATCH 07/10] events: Extend array with sequences Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index 9e0d94132d..6c6d4f5f1b 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -711,10 +711,14 @@ mod tests { } #[test] - fn decode_array() { + fn decode_array_and_seq() { decode_and_consume_type_consumes_all_bytes([0]); decode_and_consume_type_consumes_all_bytes([1, 2, 3, 4, 5]); decode_and_consume_type_consumes_all_bytes([0; 500]); decode_and_consume_type_consumes_all_bytes(["str", "abc", "cde"]); + + decode_and_consume_type_consumes_all_bytes(vec![0]); + decode_and_consume_type_consumes_all_bytes(vec![1, 2, 3, 4, 5]); + decode_and_consume_type_consumes_all_bytes(vec!["str", "abc", "cde"]); } } From 21d839cf83288dcbd0ba70c9bbc18f5c02252b11 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 18:03:38 +0200 Subject: [PATCH 08/10] events: Test variant decode_and_consume Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index 6c6d4f5f1b..18022e9138 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -376,6 +376,7 @@ mod tests { error::GenericError::{ Codec, EventsDecoding, + Other, }, events::EventsDecodingError::UnsupportedPrimitive, Config, @@ -721,4 +722,35 @@ mod tests { decode_and_consume_type_consumes_all_bytes(vec![1, 2, 3, 4, 5]); decode_and_consume_type_consumes_all_bytes(vec!["str", "abc", "cde"]); } + + #[test] + fn decode_variant() { + #[derive(Clone, Encode, TypeInfo)] + enum EnumVar { + A, + B((&'static str, u8)), + C { named: i16 }, + } + const INVALID_TYPE_ID: u32 = 1024; + + decode_and_consume_type_consumes_all_bytes(EnumVar::A); + decode_and_consume_type_consumes_all_bytes(EnumVar::B(("str", 1))); + decode_and_consume_type_consumes_all_bytes(EnumVar::C { named: 1 }); + + // Invalid variant index + let dummy_data = 3u8.encode(); + let dummy_cursor = &mut &*dummy_data; + let (id, reg) = singleton_type_registry::(); + let res = decode_and_consume_type(id.id(), ®, dummy_cursor); + assert_matches!(res, Err(Other(_))); + + // Valid index, incomplete data + let dummy_data = 2u8.encode(); + let dummy_cursor = &mut &*dummy_data; + let res = decode_and_consume_type(id.id(), ®, dummy_cursor); + assert_matches!(res, Err(Codec(_))); + + let res = decode_and_consume_type(INVALID_TYPE_ID, ®, dummy_cursor); + assert_matches!(res, Err(crate::error::GenericError::Metadata(_))); + } } From e912ed26f17d88c13559d598056ecc972f6a604f Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 18:19:52 +0200 Subject: [PATCH 09/10] events: Test composite decode_and_consume Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index 18022e9138..5698bffbd3 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -753,4 +753,54 @@ mod tests { let res = decode_and_consume_type(INVALID_TYPE_ID, ®, dummy_cursor); assert_matches!(res, Err(crate::error::GenericError::Metadata(_))); } + + #[test] + fn decode_composite() { + #[derive(Clone, Encode, TypeInfo)] + struct Composite {} + decode_and_consume_type_consumes_all_bytes(Composite {}); + + #[derive(Clone, Encode, TypeInfo)] + struct CompositeV2 { + id: u32, + name: String, + } + decode_and_consume_type_consumes_all_bytes(CompositeV2 { + id: 10, + name: "str".to_string(), + }); + + #[derive(Clone, Encode, TypeInfo)] + struct CompositeV3 { + id: u32, + extra: T, + } + decode_and_consume_type_consumes_all_bytes(CompositeV3 { + id: 10, + extra: vec![0, 1, 2], + }); + decode_and_consume_type_consumes_all_bytes(CompositeV3 { + id: 10, + extra: bitvec::bitvec![Lsb0, u8; 0, 1, 1, 0, 1], + }); + decode_and_consume_type_consumes_all_bytes(CompositeV3 { + id: 10, + extra: ("str", 1), + }); + decode_and_consume_type_consumes_all_bytes(CompositeV3 { + id: 10, + extra: CompositeV2 { + id: 2, + name: "str".to_string(), + }, + }); + + #[derive(Clone, Encode, TypeInfo)] + struct CompositeV4(u32, bool); + decode_and_consume_type_consumes_all_bytes(CompositeV4(1, true)); + + #[derive(Clone, Encode, TypeInfo)] + struct CompositeV5(u32); + decode_and_consume_type_consumes_all_bytes(CompositeV5(1)); + } } From 6d53081a8901c6d7baad30f57f19de515ead6828 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Thu, 3 Feb 2022 19:00:58 +0200 Subject: [PATCH 10/10] events: Test compact decode_and_consume Signed-off-by: Alexandru Vasile --- subxt/src/events.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/subxt/src/events.rs b/subxt/src/events.rs index 5698bffbd3..e637774d9a 100644 --- a/subxt/src/events.rs +++ b/subxt/src/events.rs @@ -803,4 +803,32 @@ mod tests { struct CompositeV5(u32); decode_and_consume_type_consumes_all_bytes(CompositeV5(1)); } + + #[test] + fn decode_compact() { + #[derive(Clone, Encode, TypeInfo)] + enum Compact { + A(#[codec(compact)] u32), + } + decode_and_consume_type_consumes_all_bytes(Compact::A(1)); + + #[derive(Clone, Encode, TypeInfo)] + struct CompactV2(#[codec(compact)] u32); + decode_and_consume_type_consumes_all_bytes(CompactV2(1)); + + #[derive(Clone, Encode, TypeInfo)] + struct CompactV3 { + #[codec(compact)] + val: u32, + } + decode_and_consume_type_consumes_all_bytes(CompactV3 { val: 1 }); + + #[derive(Clone, Encode, TypeInfo)] + struct CompactV4 { + #[codec(compact)] + val: T, + } + decode_and_consume_type_consumes_all_bytes(CompactV4 { val: 0u8 }); + decode_and_consume_type_consumes_all_bytes(CompactV4 { val: 1u16 }); + } }