Skip to content

Commit

Permalink
testing: Prepare light client testing with substrate binary and add s…
Browse files Browse the repository at this point in the history
…ubxt-test macro (#1507)

* testing: Add long running light client flag and cfg aliases

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Expose clients depending on feature flags

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Use unstable backend for light client

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Disable flaky lightclient tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Add long runnnig step

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "subxt: Use unstable backend for light client"

This reverts commit ea6f3cc.

* ci: Long running tests for 60 mins

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Use 16 cores for light-client testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Isolate light-client testing to save CI minutes

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Retry on Tx::Dropped for lightclinet only

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Wait for more blocks for the lightclient init

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Use unstable backend for light client

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Disable legacy RPC tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Disable sudo and contracts tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Retry constructing lightclient on read-proof errors

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Disable tx dynamic test

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* proc-macro: Timeout for tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Add timeout 800 seconds

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* proc-macro/tests: Adjust subxt-test proc-macro

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* proc-macro: Rename crate to subxt-test-proc-macro

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Use default subxt-proc-macro timeout

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* light-client: Remove println

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Remove tokio as dependency, use it only for testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Chagne default timeout to 6 seconds

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* proc-macro: Add env timeout variable

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Add subxt env var for controling test timeouts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests/tx-retries: Retry on `Non node available` error

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Use unstable backend for testing lightclient

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Remove old lightclient object

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Adjust for the new interface

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* backend/rpc: Allow older version of the initialized event

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rpc/tests: Check initialized decodes correctly

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Reset workflow

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Apply cargo fmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove unused dep

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove gitmerge old file

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove unused dep

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* rename proc-macro to subxt-test-macro

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Remove txretries for lightclient

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Wait for 5 blocks for the lightclient full testing suite

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Group imports

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* macro: Rename const value

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Apr 8, 2024
1 parent 827a35d commit b31131d
Show file tree
Hide file tree
Showing 26 changed files with 554 additions and 181 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"testing/substrate-runner",
"testing/test-runtime",
"testing/integration-tests",
"testing/integration-tests/subxt-test-macro",
"testing/ui-tests",
"testing/generate-custom-metadata",
"macro",
Expand Down
55 changes: 52 additions & 3 deletions subxt/src/backend/unstable/rpc_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::config::BlockHash;
use crate::{Config, Error};
use derive_where::derive_where;
use futures::{Stream, StreamExt};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::{HashMap, VecDeque};
use std::task::Poll;

Expand Down Expand Up @@ -377,8 +377,7 @@ pub enum FollowEvent<Hash> {
///
/// This is the first event generated by the `follow` subscription
/// and is submitted only once.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Initialized<Hash> {
/// The hashes of the last finalized blocks.
pub finalized_block_hashes: Vec<Hash>,
Expand All @@ -391,6 +390,30 @@ pub struct Initialized<Hash> {
pub finalized_block_runtime: Option<RuntimeEvent>,
}

impl<'de, Hash: Deserialize<'de>> Deserialize<'de> for Initialized<Hash> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
// Custom struct that can deserialize both `finalizedBlockHash` and `finalizedBlockHashes`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
struct InitializedIR<Hash> {
finalized_block_hashes: Option<Vec<Hash>>,
finalized_block_hash: Option<Hash>,
finalized_block_runtime: Option<RuntimeEvent>,
}

let ir = InitializedIR::deserialize(deserializer)?;
let finalized_block_hashes = ir
.finalized_block_hashes
.or_else(|| ir.finalized_block_hash.map(|hash| vec![hash]))
.ok_or_else(|| serde::de::Error::custom("Missing finalized block hashes"))?;

Ok(Initialized {
finalized_block_hashes,
finalized_block_runtime: ir.finalized_block_runtime,
})
}
}

/// The runtime event generated if the `follow` subscription
/// has set the `with_runtime` flag.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
Expand Down Expand Up @@ -973,4 +996,30 @@ mod test {
let _ = serde_json::from_value::<Foo32>(from_err)
.expect_err("can't deser invalid num into u32");
}

#[test]
fn chain_head_initialized() {
// Latest format version.
let event = serde_json::json!({
"finalizedBlockHashes": ["0x1", "0x2"],
});
let decoded: Initialized<String> = serde_json::from_value(event).unwrap();
assert_eq!(
decoded.finalized_block_hashes,
vec!["0x1".to_string(), "0x2".to_string()]
);

// Old format.
let event = serde_json::json!({
"finalizedBlockHash": "0x1",
});
let decoded: Initialized<String> = serde_json::from_value(event).unwrap();
assert_eq!(decoded.finalized_block_hashes, vec!["0x1".to_string()]);

// Wrong format.
let event = serde_json::json!({
"finalizedBlockHash": ["0x1"],
});
let _ = serde_json::from_value::<Initialized<String>>(event).unwrap_err();
}
}
7 changes: 7 additions & 0 deletions testing/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ default = []
# Enable to run the tests with Light Client support.
unstable-light-client = ["subxt/unstable-light-client"]

# Enable to run the full-client tests with Light Client support.
unstable-light-client-long-running = ["subxt/unstable-light-client"]

# Enable this to use the unstable backend in tests _instead of_
# the default one which relies on the "old" RPC methods.
unstable-backend-client = []
Expand All @@ -43,3 +46,7 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
wabt = { workspace = true }
substrate-runner = { workspace = true }
subxt-test-macro = { path = "subxt-test-macro" }

[build-dependencies]
cfg_aliases = "0.2.0"
9 changes: 9 additions & 0 deletions testing/integration-tests/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use cfg_aliases::cfg_aliases;

fn main() {
// Setup cfg aliases
cfg_aliases! {
lightclient: { any(feature = "unstable-light-client", feature = "unstable-light-client-long-running") },
fullclient: { all(not(feature = "unstable-light-client"), not(feature = "unstable-light-client-long-running")) },
}
}
38 changes: 26 additions & 12 deletions testing/integration-tests/src/full_client/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use crate::{test_context, utils::node_runtime};
use crate::{subxt_test, test_context};
use codec::{Compact, Encode};
use futures::StreamExt;
use subxt::config::signed_extensions::{ChargeAssetTxPayment, CheckMortality, CheckNonce};
use subxt::config::DefaultExtrinsicParamsBuilder;
use subxt::config::SubstrateConfig;
use subxt::utils::Era;
use subxt_metadata::Metadata;

#[cfg(fullclient)]
use crate::utils::node_runtime;

#[cfg(fullclient)]
use subxt::{
config::{
signed_extensions::{ChargeAssetTxPayment, CheckMortality, CheckNonce},
DefaultExtrinsicParamsBuilder, SubstrateConfig,
},
utils::Era,
};

#[cfg(fullclient)]
use subxt_signer::sr25519::dev;

#[tokio::test]
use subxt_metadata::Metadata;

#[cfg(fullclient)]
#[subxt_test]
async fn block_subscriptions_are_consistent_with_eachother() -> Result<(), subxt::Error> {
let ctx = test_context().await;
let api = ctx.client();
Expand Down Expand Up @@ -76,7 +88,7 @@ async fn block_subscriptions_are_consistent_with_eachother() -> Result<(), subxt
Ok(())
}

#[tokio::test]
#[subxt_test]
async fn finalized_headers_subscription() -> Result<(), subxt::Error> {
let ctx = test_context().await;
let api = ctx.client();
Expand All @@ -93,7 +105,7 @@ async fn finalized_headers_subscription() -> Result<(), subxt::Error> {
Ok(())
}

#[tokio::test]
#[subxt_test]
async fn missing_block_headers_will_be_filled_in() -> Result<(), subxt::Error> {
use subxt::backend::legacy;

Expand Down Expand Up @@ -138,7 +150,7 @@ async fn missing_block_headers_will_be_filled_in() -> Result<(), subxt::Error> {
}

// Check that we can subscribe to non-finalized blocks.
#[tokio::test]
#[subxt_test]
async fn runtime_api_call() -> Result<(), subxt::Error> {
let ctx = test_context().await;
let api = ctx.client();
Expand All @@ -163,7 +175,8 @@ async fn runtime_api_call() -> Result<(), subxt::Error> {
Ok(())
}

#[tokio::test]
#[cfg(fullclient)]
#[subxt_test]
async fn fetch_block_and_decode_extrinsic_details() {
let ctx = test_context().await;
let api = ctx.client();
Expand Down Expand Up @@ -232,7 +245,8 @@ async fn fetch_block_and_decode_extrinsic_details() {
assert!(tx.is_signed());
}

#[tokio::test]
#[cfg(fullclient)]
#[subxt_test]
async fn decode_signed_extensions_from_blocks() {
let ctx = test_context().await;
let api = ctx.client();
Expand Down
30 changes: 15 additions & 15 deletions testing/integration-tests/src/full_client/client/legacy_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
//! Just sanity checking some of the legacy RPC methods to make
//! sure they don't error out and can decode their results OK.

use crate::test_context;
use crate::{subxt_test, test_context};

#[tokio::test]
#[subxt_test]
async fn chain_get_block_hash() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

rpc.chain_get_block_hash(None).await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn chain_get_block() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;
Expand All @@ -24,15 +24,15 @@ async fn chain_get_block() {
rpc.chain_get_block(hash).await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn chain_get_finalized_head() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

rpc.chain_get_finalized_head().await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn chain_subscribe_all_heads() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;
Expand All @@ -41,7 +41,7 @@ async fn chain_subscribe_all_heads() {
let _block_header = sub.next().await.unwrap().unwrap();
}

#[tokio::test]
#[subxt_test]
async fn chain_subscribe_finalized_heads() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;
Expand All @@ -50,7 +50,7 @@ async fn chain_subscribe_finalized_heads() {
let _block_header = sub.next().await.unwrap().unwrap();
}

#[tokio::test]
#[subxt_test]
async fn chain_subscribe_new_heads() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;
Expand All @@ -59,23 +59,23 @@ async fn chain_subscribe_new_heads() {
let _block_header = sub.next().await.unwrap().unwrap();
}

#[tokio::test]
#[subxt_test]
async fn genesis_hash() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

let _genesis_hash = rpc.genesis_hash().await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn state_get_metadata() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

let _metadata = rpc.state_get_metadata(None).await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn state_call() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;
Expand All @@ -86,39 +86,39 @@ async fn state_call() {
.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn system_health() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

let _ = rpc.system_health().await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn system_chain() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

let _ = rpc.system_chain().await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn system_name() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

let _ = rpc.system_name().await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn system_version() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;

let _ = rpc.system_version().await.unwrap();
}

#[tokio::test]
#[subxt_test]
async fn system_properties() {
let ctx = test_context().await;
let rpc = ctx.legacy_rpc_methods().await;
Expand Down
Loading

0 comments on commit b31131d

Please sign in to comment.