Merge remote-tracking branch 'origin/master' into lexnv/update-smoldot

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2024-04-09 13:53:20 +03:00
28 changed files with 574 additions and 201 deletions
+52 -3
View File
@@ -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;
@@ -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>,
@@ -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)]
@@ -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();
}
}
+1 -1
View File
@@ -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))))?;