Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into lexnv/update-smoldot
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Apr 9, 2024
2 parents c8020c9 + 498bc7d commit 6c9088e
Show file tree
Hide file tree
Showing 28 changed files with 573 additions and 200 deletions.
40 changes: 28 additions & 12 deletions Cargo.lock

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

11 changes: 6 additions & 5 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 Expand Up @@ -88,7 +89,7 @@ primitive-types = { version = "0.12.2", default-features = false }
proc-macro-error = "1.0.4"
proc-macro2 = "1.0.79"
quote = "1.0.35"
regex = { version = "1.10.3", default-features = false }
regex = { version = "1.10.4", default-features = false }
scale-info = { version = "2.11.0", default-features = false }
scale-value = { version = "0.14.1", default-features = false }
scale-bits = { version = "0.5.0", default-features = false }
Expand All @@ -100,11 +101,11 @@ serde = { version = "1.0.197", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.114", default-features = false }
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
thiserror = "1.0.58"
tokio = { version = "1.36", default-features = false }
tokio = { version = "1.37", default-features = false }
tracing = { version = "0.1.40", default-features = false }
tracing-wasm = "0.2.1"
tracing-subscriber = "0.3.18"
trybuild = "1.0.90"
trybuild = "1.0.91"
url = "2.5.0"
wabt = "0.10.0"
wasm-bindgen-test = "0.3.24"
Expand All @@ -114,11 +115,11 @@ strip-ansi-escapes = "0.2.0"
# Light client support:
smoldot = { path = "/home/lexnv/workspace/smoldot/lib", default-features = false }
#smoldot = { version = "0.17.0", default-features = false }

#smoldot-light = { version = "0.15.0", default-features = false }
smoldot-light = { path = "/home/lexnv/workspace/smoldot/light-base", default-features = false }

tokio-stream = "0.1.14"
tokio-stream = "0.1.15"

futures-util = "0.3.30"
rand = "0.8.5"
pin-project = "1.1.5"
Expand Down
2 changes: 1 addition & 1 deletion core/src/custom_values/custom_value_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::dynamic::DecodedValueThunk;
use crate::metadata::DecodeWithMetadata;
use crate::utils::Yes;

/// This represents the address of a custom value in in the metadata.
/// This represents the address of a custom value in the metadata.
/// Anything, that implements the [CustomValueAddress] trait can be used, to fetch
/// custom values from the metadata.
/// The trait is implemented by [str] for dynamic loopup and [StaticAddress] for static queries.
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();
}
}
2 changes: 1 addition & 1 deletion subxt/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cfg_jsonrpsee! {

/// A URL is considered secure if it uses a secure scheme ("https" or "wss") or is referring to localhost.
///
/// Returns an error if the the string could not be parsed into a URL.
/// Returns an error if the string could not be parsed into a URL.
pub fn url_is_secure(url: &str) -> Result<bool, Error> {
let url = Url::parse(url).map_err(|e| Error::Rpc(RpcError::ClientError(Box::new(e))))?;

Expand Down
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
Loading

0 comments on commit 6c9088e

Please sign in to comment.