Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into lexnv/storage-sub
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv committed Nov 28, 2024
2 parents b14a4bd + 51c3e95 commit 9676499
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ linked-hash-map = { version = "0.5.4" }
linked_hash_set = { version = "0.1.4" }
linregress = { version = "0.5.1" }
lite-json = { version = "0.2.0", default-features = false }
litep2p = { version = "0.8.1", features = ["websocket"] }
litep2p = { version = "0.8.2", features = ["websocket"] }
log = { version = "0.4.22", default-features = false }
macro_magic = { version = "0.5.1" }
maplit = { version = "1.0.2" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use serde_json::{json, Value};
use std::{process::Command, str};

const WASM_FILE_PATH: &str =
"../../../../../target/release/wbuild/chain-spec-guide-runtime/chain_spec_guide_runtime.wasm";
fn wasm_file_path() -> &'static str {
chain_spec_guide_runtime::runtime::WASM_BINARY_PATH
.expect("chain_spec_guide_runtime wasm should exist. qed")
}

const CHAIN_SPEC_BUILDER_PATH: &str = "../../../../../target/release/chain-spec-builder";

Expand All @@ -26,7 +28,7 @@ fn list_presets() {
let output = Command::new(get_chain_spec_builder_path())
.arg("list-presets")
.arg("-r")
.arg(WASM_FILE_PATH)
.arg(wasm_file_path())
.output()
.expect("Failed to execute command");

Expand All @@ -50,7 +52,7 @@ fn get_preset() {
let output = Command::new(get_chain_spec_builder_path())
.arg("display-preset")
.arg("-r")
.arg(WASM_FILE_PATH)
.arg(wasm_file_path())
.arg("-p")
.arg("preset_2")
.output()
Expand Down Expand Up @@ -83,7 +85,7 @@ fn generate_chain_spec() {
.arg("/dev/stdout")
.arg("create")
.arg("-r")
.arg(WASM_FILE_PATH)
.arg(wasm_file_path())
.arg("named-preset")
.arg("preset_2")
.output()
Expand Down Expand Up @@ -140,7 +142,7 @@ fn generate_para_chain_spec() {
.arg("-p")
.arg("1000")
.arg("-r")
.arg(WASM_FILE_PATH)
.arg(wasm_file_path())
.arg("named-preset")
.arg("preset_2")
.output()
Expand Down
7 changes: 7 additions & 0 deletions prdoc/pr_6673.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: 'chain-spec-guide-runtime: path to wasm blob fixed'
doc:
- audience: Runtime Dev
description: In `chain-spec-guide-runtime` crate's tests, there was assumption that
release version of wasm blob exists. This PR uses `chain_spec_guide_runtime::runtime::WASM_BINARY_PATH`
const to use correct path to runtime blob.
crates: []
11 changes: 11 additions & 0 deletions prdoc/pr_6677.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: 'chore: Update litep2p to v0.8.2'
doc:
- audience: Node Dev
description: |-
This includes a critical fix for debug release versions of litep2p (which are running in Kusama as validators).

While at it, have stopped the oncall pain of alerts around `incoming_connections_total`. We can rethink the metric expose of litep2p in Q1.

crates:
- name: sc-network
bump: minor
11 changes: 10 additions & 1 deletion substrate/client/network/src/litep2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,15 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac

let direction = match endpoint {
Endpoint::Dialer { .. } => "out",
Endpoint::Listener { .. } => "in",
Endpoint::Listener { .. } => {
// Increment incoming connections counter.
//
// Note: For litep2p these are represented by established negotiated connections,
// while for libp2p (legacy) these represent not-yet-negotiated connections.
metrics.incoming_connections_total.inc();

"in"
},
};
metrics.connections_opened_total.with_label_values(&[direction]).inc();

Expand Down Expand Up @@ -1058,6 +1066,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
NegotiationError::ParseError(_) => "parse-error",
NegotiationError::IoError(_) => "io-error",
NegotiationError::WebSocket(_) => "webscoket-error",
NegotiationError::BadSignature => "bad-signature",
}
};

Expand Down
257 changes: 257 additions & 0 deletions substrate/client/rpc-spec-v2/src/archive/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,3 +1003,260 @@ async fn archive_storage_diff_invalid_params() {
ArchiveStorageDiffEvent::StorageDiffError(ref err) if err.error.contains("Header was not found")
);
}

#[tokio::test]
async fn archive_storage_diff_main_trie() {
let (client, api) = setup_api(MAX_PAGINATION_LIMIT, MAX_QUERIED_LIMIT);

let mut builder = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().genesis_hash)
.with_parent_block_number(0)
.build()
.unwrap();
builder.push_storage_change(b":A".to_vec(), Some(b"B".to_vec())).unwrap();
builder.push_storage_change(b":AA".to_vec(), Some(b"BB".to_vec())).unwrap();
let prev_block = builder.build().unwrap().block;
let prev_hash = format!("{:?}", prev_block.header.hash());
client.import(BlockOrigin::Own, prev_block.clone()).await.unwrap();

let mut builder = BlockBuilderBuilder::new(&*client)
.on_parent_block(prev_block.hash())
.with_parent_block_number(1)
.build()
.unwrap();
builder.push_storage_change(b":A".to_vec(), Some(b"11".to_vec())).unwrap();
builder.push_storage_change(b":AA".to_vec(), Some(b"22".to_vec())).unwrap();
builder.push_storage_change(b":AAA".to_vec(), Some(b"222".to_vec())).unwrap();
let block = builder.build().unwrap().block;
let block_hash = format!("{:?}", block.header.hash());
client.import(BlockOrigin::Own, block.clone()).await.unwrap();

// Search for items in the main trie:
// - values of keys under ":A"
// - hashes of keys under ":AA"
let items = vec![
ArchiveStorageDiffItem::<String> {
key: hex_string(b":A"),
return_type: ArchiveStorageDiffType::Value,
child_trie_key: None,
},
ArchiveStorageDiffItem::<String> {
key: hex_string(b":AA"),
return_type: ArchiveStorageDiffType::Hash,
child_trie_key: None,
},
];
let mut sub = api
.subscribe_unbounded(
"archive_unstable_storageDiff",
rpc_params![&block_hash, items.clone(), &prev_hash],
)
.await
.unwrap();

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(
ArchiveStorageDiffEvent::StorageDiff(ArchiveStorageDiffResult {
key: hex_string(b":A"),
result: StorageResultType::Value(hex_string(b"11")),
operation_type: ArchiveStorageDiffOperationType::Modified,
child_trie_key: None,
}),
event,
);

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(
ArchiveStorageDiffEvent::StorageDiff(ArchiveStorageDiffResult {
key: hex_string(b":AA"),
result: StorageResultType::Value(hex_string(b"22")),
operation_type: ArchiveStorageDiffOperationType::Modified,
child_trie_key: None,
}),
event,
);

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(
ArchiveStorageDiffEvent::StorageDiff(ArchiveStorageDiffResult {
key: hex_string(b":AA"),
result: StorageResultType::Hash(format!("{:?}", Blake2Hasher::hash(b"22"))),
operation_type: ArchiveStorageDiffOperationType::Modified,
child_trie_key: None,
}),
event,
);

// Added key.
let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(
ArchiveStorageDiffEvent::StorageDiff(ArchiveStorageDiffResult {
key: hex_string(b":AAA"),
result: StorageResultType::Value(hex_string(b"222")),
operation_type: ArchiveStorageDiffOperationType::Added,
child_trie_key: None,
}),
event,
);

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(
ArchiveStorageDiffEvent::StorageDiff(ArchiveStorageDiffResult {
key: hex_string(b":AAA"),
result: StorageResultType::Hash(format!("{:?}", Blake2Hasher::hash(b"222"))),
operation_type: ArchiveStorageDiffOperationType::Added,
child_trie_key: None,
}),
event,
);

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(ArchiveStorageDiffEvent::StorageDiffDone, event);
}

#[tokio::test]
async fn archive_storage_diff_no_changes() {
let (client, api) = setup_api(MAX_PAGINATION_LIMIT, MAX_QUERIED_LIMIT);

// Build 2 identical blocks.
let mut builder = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().genesis_hash)
.with_parent_block_number(0)
.build()
.unwrap();
builder.push_storage_change(b":A".to_vec(), Some(b"B".to_vec())).unwrap();
builder.push_storage_change(b":AA".to_vec(), Some(b"BB".to_vec())).unwrap();
builder.push_storage_change(b":B".to_vec(), Some(b"CC".to_vec())).unwrap();
builder.push_storage_change(b":BA".to_vec(), Some(b"CC".to_vec())).unwrap();
let prev_block = builder.build().unwrap().block;
let prev_hash = format!("{:?}", prev_block.header.hash());
client.import(BlockOrigin::Own, prev_block.clone()).await.unwrap();

let mut builder = BlockBuilderBuilder::new(&*client)
.on_parent_block(prev_block.hash())
.with_parent_block_number(1)
.build()
.unwrap();
builder.push_storage_change(b":A".to_vec(), Some(b"B".to_vec())).unwrap();
builder.push_storage_change(b":AA".to_vec(), Some(b"BB".to_vec())).unwrap();
let block = builder.build().unwrap().block;
let block_hash = format!("{:?}", block.header.hash());
client.import(BlockOrigin::Own, block.clone()).await.unwrap();

// Search for items in the main trie with keys prefixed with ":A".
let items = vec![ArchiveStorageDiffItem::<String> {
key: hex_string(b":A"),
return_type: ArchiveStorageDiffType::Value,
child_trie_key: None,
}];
let mut sub = api
.subscribe_unbounded(
"archive_unstable_storageDiff",
rpc_params![&block_hash, items.clone(), &prev_hash],
)
.await
.unwrap();

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(ArchiveStorageDiffEvent::StorageDiffDone, event);
}

#[tokio::test]
async fn archive_storage_diff_deleted_changes() {
let (client, api) = setup_api(MAX_PAGINATION_LIMIT, MAX_QUERIED_LIMIT);

// Blocks are imported as forks.
let mut builder = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().genesis_hash)
.with_parent_block_number(0)
.build()
.unwrap();
builder.push_storage_change(b":A".to_vec(), Some(b"B".to_vec())).unwrap();
builder.push_storage_change(b":AA".to_vec(), Some(b"BB".to_vec())).unwrap();
builder.push_storage_change(b":B".to_vec(), Some(b"CC".to_vec())).unwrap();
builder.push_storage_change(b":BA".to_vec(), Some(b"CC".to_vec())).unwrap();
let prev_block = builder.build().unwrap().block;
let prev_hash = format!("{:?}", prev_block.header.hash());
client.import(BlockOrigin::Own, prev_block.clone()).await.unwrap();

let mut builder = BlockBuilderBuilder::new(&*client)
.on_parent_block(client.chain_info().genesis_hash)
.with_parent_block_number(0)
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: AccountKeyring::Alice.into(),
to: AccountKeyring::Ferdie.into(),
amount: 41,
nonce: 0,
})
.unwrap();
builder.push_storage_change(b":A".to_vec(), Some(b"B".to_vec())).unwrap();
let block = builder.build().unwrap().block;
let block_hash = format!("{:?}", block.header.hash());
client.import(BlockOrigin::Own, block.clone()).await.unwrap();

// Search for items in the main trie with keys prefixed with ":A".
let items = vec![ArchiveStorageDiffItem::<String> {
key: hex_string(b":A"),
return_type: ArchiveStorageDiffType::Value,
child_trie_key: None,
}];

let mut sub = api
.subscribe_unbounded(
"archive_unstable_storageDiff",
rpc_params![&block_hash, items.clone(), &prev_hash],
)
.await
.unwrap();

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(
ArchiveStorageDiffEvent::StorageDiff(ArchiveStorageDiffResult {
key: hex_string(b":AA"),
result: StorageResultType::Value(hex_string(b"BB")),
operation_type: ArchiveStorageDiffOperationType::Deleted,
child_trie_key: None,
}),
event,
);

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_eq!(ArchiveStorageDiffEvent::StorageDiffDone, event);
}

#[tokio::test]
async fn archive_storage_diff_invalid_params() {
let invalid_hash = hex_string(&INVALID_HASH);
let (_, api) = setup_api(MAX_PAGINATION_LIMIT, MAX_QUERIED_LIMIT);

// Invalid shape for parameters.
let items: Vec<ArchiveStorageDiffItem<String>> = Vec::new();
let err = api
.subscribe_unbounded(
"archive_unstable_storageDiff",
rpc_params!["123", items.clone(), &invalid_hash],
)
.await
.unwrap_err();
assert_matches!(err,
Error::JsonRpc(ref err) if err.code() == crate::chain_head::error::json_rpc_spec::INVALID_PARAM_ERROR && err.message() == "Invalid params"
);

// The shape is right, but the block hash is invalid.
let items: Vec<ArchiveStorageDiffItem<String>> = Vec::new();
let mut sub = api
.subscribe_unbounded(
"archive_unstable_storageDiff",
rpc_params![&invalid_hash, items.clone(), &invalid_hash],
)
.await
.unwrap();

let event = get_next_event::<ArchiveStorageDiffEvent>(&mut sub).await;
assert_matches!(event,
ArchiveStorageDiffEvent::StorageDiffError(ref err) if err.error.contains("Header was not found")
);
}

0 comments on commit 9676499

Please sign in to comment.