mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-04-24 20:38:01 +00:00
Split msg into JSON and internal variant, and other bits
This commit is contained in:
@@ -8,10 +8,16 @@ use serde::de::{self, Deserialize, Deserializer, Unexpected, Visitor, SeqAccess}
|
||||
const HASH_BYTES: usize = 32;
|
||||
|
||||
/// Newtype wrapper for 32-byte hash values, implementing readable `Debug` and `serde::Deserialize`.
|
||||
// We could use primitive_types::H256 here, but opted for a custom type to avoid more dependencies.
|
||||
/// This can deserialize from a JSON string or array.
|
||||
#[derive(Hash, PartialEq, Eq, Clone, Copy)]
|
||||
pub struct Hash([u8; HASH_BYTES]);
|
||||
|
||||
impl From<Hash> for crate::types::BlockHash {
|
||||
fn from(hash: Hash) -> Self {
|
||||
hash.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
struct HashVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for HashVisitor {
|
||||
@@ -0,0 +1,7 @@
|
||||
//! This module contains the types we need to deserialize JSON messages from nodes
|
||||
|
||||
mod hash;
|
||||
mod node_message;
|
||||
|
||||
pub use node_message::*;
|
||||
pub use hash::Hash;
|
||||
@@ -0,0 +1,192 @@
|
||||
//! The structs and enums defined in this module are largely identical to those
|
||||
//! we'll use elsewhere internally, but are kept separate so that the JSON structure
|
||||
//! is defined (almost) from just this file, and we don't have to worry about breaking
|
||||
//! compatibility with the input data when we make changes to our internal data
|
||||
//! structures (for example, to support bincode better).
|
||||
use super::hash::Hash;
|
||||
use serde::{Deserialize};
|
||||
|
||||
/// This struct represents a telemetry message sent from a node as
|
||||
/// a JSON payload. Since JSON is self describing, we can use attributes
|
||||
/// like serde(untagged) and serde(flatten) without issue.
|
||||
///
|
||||
/// Internally, we want to minimise the amount of data sent from shards to
|
||||
/// the core node. For that reason, we use a non-self-describing serialization
|
||||
/// format like bincode, which doesn't support things like `[serde(flatten)]` (which
|
||||
/// internally wants to serialize to a map of unknown length) or `[serde(tag/untagged)]`
|
||||
/// (which relies on the data to know which variant to deserialize to.)
|
||||
///
|
||||
/// So, this can be converted fairly cheaply into an enum we'll use internally
|
||||
/// which is compatible with formats like bincode.
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(untagged)]
|
||||
pub enum NodeMessage {
|
||||
V1 {
|
||||
#[serde(flatten)]
|
||||
payload: Payload,
|
||||
},
|
||||
V2 {
|
||||
id: ConnId,
|
||||
payload: Payload,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(tag = "msg")]
|
||||
pub enum Payload {
|
||||
#[serde(rename = "system.connected")]
|
||||
SystemConnected(SystemConnected),
|
||||
#[serde(rename = "system.interval")]
|
||||
SystemInterval(SystemInterval),
|
||||
#[serde(rename = "block.import")]
|
||||
BlockImport(Block),
|
||||
#[serde(rename = "notify.finalized")]
|
||||
NotifyFinalized(Finalized),
|
||||
#[serde(rename = "txpool.import")]
|
||||
TxPoolImport,
|
||||
#[serde(rename = "afg.finalized")]
|
||||
AfgFinalized(AfgFinalized),
|
||||
#[serde(rename = "afg.received_precommit")]
|
||||
AfgReceivedPrecommit(AfgReceivedPrecommit),
|
||||
#[serde(rename = "afg.received_prevote")]
|
||||
AfgReceivedPrevote(AfgReceivedPrevote),
|
||||
#[serde(rename = "afg.received_commit")]
|
||||
AfgReceivedCommit(AfgReceivedCommit),
|
||||
#[serde(rename = "afg.authority_set")]
|
||||
AfgAuthoritySet(AfgAuthoritySet),
|
||||
#[serde(rename = "afg.finalized_blocks_up_to")]
|
||||
AfgFinalizedBlocksUpTo,
|
||||
#[serde(rename = "aura.pre_sealed_block")]
|
||||
AuraPreSealedBlock,
|
||||
#[serde(rename = "prepared_block_for_proposing")]
|
||||
PreparedBlockForProposing,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SystemConnected {
|
||||
pub genesis_hash: Hash,
|
||||
#[serde(flatten)]
|
||||
pub node: NodeDetails,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct SystemInterval {
|
||||
pub peers: Option<u64>,
|
||||
pub txcount: Option<u64>,
|
||||
pub bandwidth_upload: Option<f64>,
|
||||
pub bandwidth_download: Option<f64>,
|
||||
pub finalized_height: Option<BlockNumber>,
|
||||
pub finalized_hash: Option<Hash>,
|
||||
#[serde(flatten)]
|
||||
pub block: Option<Block>,
|
||||
pub used_state_cache_size: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Finalized {
|
||||
#[serde(rename = "best")]
|
||||
pub hash: Hash,
|
||||
pub height: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct AfgAuthoritySet {
|
||||
pub authority_id: Box<str>,
|
||||
pub authorities: Box<str>,
|
||||
pub authority_set_id: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct AfgFinalized {
|
||||
pub finalized_hash: Hash,
|
||||
pub finalized_number: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct AfgReceived {
|
||||
pub target_hash: Hash,
|
||||
pub target_number: Box<str>,
|
||||
pub voter: Option<Box<str>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct AfgReceivedPrecommit {
|
||||
#[serde(flatten)]
|
||||
pub received: AfgReceived,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct AfgReceivedPrevote {
|
||||
#[serde(flatten)]
|
||||
pub received: AfgReceived,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct AfgReceivedCommit {
|
||||
#[serde(flatten)]
|
||||
pub received: AfgReceived,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Copy)]
|
||||
pub struct Block {
|
||||
#[serde(rename = "best")]
|
||||
pub hash: Hash,
|
||||
pub height: BlockNumber,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct NodeDetails {
|
||||
pub chain: Box<str>,
|
||||
pub name: Box<str>,
|
||||
pub implementation: Box<str>,
|
||||
pub version: Box<str>,
|
||||
pub validator: Option<Box<str>>,
|
||||
pub network_id: Option<Box<str>>,
|
||||
pub startup_time: Option<Box<str>>,
|
||||
}
|
||||
|
||||
type ConnId = u64;
|
||||
type BlockNumber = u64;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn message_v1() {
|
||||
let json = r#"{
|
||||
"msg":"notify.finalized",
|
||||
"level":"INFO",
|
||||
"ts":"2021-01-13T12:38:25.410794650+01:00",
|
||||
"best":"0x031c3521ca2f9c673812d692fc330b9a18e18a2781e3f9976992f861fd3ea0cb",
|
||||
"height":"50"
|
||||
}"#;
|
||||
assert!(
|
||||
matches!(
|
||||
serde_json::from_str::<NodeMessage>(json).unwrap(),
|
||||
NodeMessage::V1 { .. },
|
||||
),
|
||||
"message did not match variant V1",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_v2() {
|
||||
let json = r#"{
|
||||
"id":1,
|
||||
"ts":"2021-01-13T12:22:20.053527101+01:00",
|
||||
"payload":{
|
||||
"best":"0xcc41708573f2acaded9dd75e07dac2d4163d136ca35b3061c558d7a35a09dd8d",
|
||||
"height":"209",
|
||||
"msg":"notify.finalized"
|
||||
}
|
||||
}"#;
|
||||
assert!(
|
||||
matches!(
|
||||
serde_json::from_str::<NodeMessage>(json).unwrap(),
|
||||
NodeMessage::V2 { .. },
|
||||
),
|
||||
"message did not match variant V2",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@ pub mod shard;
|
||||
pub mod types;
|
||||
pub mod util;
|
||||
pub mod ws;
|
||||
pub mod json;
|
||||
+254
-93
@@ -1,15 +1,13 @@
|
||||
use crate::types::{Block, BlockHash, BlockNumber, ConnId, NodeDetails};
|
||||
use crate::util::{Hash, NullAny};
|
||||
use crate::json;
|
||||
|
||||
use actix::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::ser::Serializer;
|
||||
|
||||
#[derive(Deserialize, Debug, Message)]
|
||||
#[derive(Serialize, Deserialize, Debug, Message)]
|
||||
#[rtype(result = "()")]
|
||||
#[serde(untagged)]
|
||||
pub enum NodeMessage {
|
||||
V1 {
|
||||
#[serde(flatten)]
|
||||
payload: Payload,
|
||||
},
|
||||
V2 {
|
||||
@@ -36,64 +34,98 @@ impl From<NodeMessage> for Payload {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(tag = "msg")]
|
||||
pub enum Payload {
|
||||
#[serde(rename = "system.connected")]
|
||||
SystemConnected(SystemConnected),
|
||||
#[serde(rename = "system.interval")]
|
||||
SystemInterval(SystemInterval),
|
||||
#[serde(rename = "block.import")]
|
||||
BlockImport(Block),
|
||||
#[serde(rename = "notify.finalized")]
|
||||
NotifyFinalized(Finalized),
|
||||
#[serde(rename = "txpool.import")]
|
||||
TxPoolImport(NullAny),
|
||||
// #[serde(rename = "afg.finalized")]
|
||||
// AfgFinalized(AfgFinalized),
|
||||
// #[serde(rename = "afg.received_precommit")]
|
||||
// AfgReceivedPrecommit(AfgReceivedPrecommit),
|
||||
// #[serde(rename = "afg.received_prevote")]
|
||||
// AfgReceivedPrevote(AfgReceivedPrevote),
|
||||
// #[serde(rename = "afg.received_commit")]
|
||||
// AfgReceivedCommit(AfgReceivedCommit),
|
||||
// #[serde(rename = "afg.authority_set")]
|
||||
// AfgAuthoritySet(AfgAuthoritySet),
|
||||
// #[serde(rename = "afg.finalized_blocks_up_to")]
|
||||
// AfgFinalizedBlocksUpTo(NullAny),
|
||||
// #[serde(rename = "aura.pre_sealed_block")]
|
||||
// AuraPreSealedBlock(NullAny),
|
||||
#[serde(rename = "prepared_block_for_proposing")]
|
||||
PreparedBlockForProposing(NullAny),
|
||||
}
|
||||
|
||||
impl Serialize for Payload {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
use Payload::*;
|
||||
|
||||
match self {
|
||||
SystemConnected(val) => serializer.serialize_newtype_variant("Payload", 0, "system.connected", val),
|
||||
SystemInterval(val) => serializer.serialize_newtype_variant("Payload", 1, "system.interval", val),
|
||||
BlockImport(val) => serializer.serialize_newtype_variant("Payload", 3, "block.import", val),
|
||||
NotifyFinalized(val) => serializer.serialize_newtype_variant("Payload", 4, "notify.finalized", val),
|
||||
TxPoolImport(_) => serializer.serialize_unit_variant("Payload", 3, "txpool.import"),
|
||||
PreparedBlockForProposing(_) => serializer.serialize_unit_variant("Payload", 4, "prepared_block_for_proposing"),
|
||||
_ => unimplemented!()
|
||||
impl From<json::NodeMessage> for NodeMessage {
|
||||
fn from(msg: json::NodeMessage) -> Self {
|
||||
match msg {
|
||||
json::NodeMessage::V1 { payload } => {
|
||||
NodeMessage::V1 { payload: payload.into() }
|
||||
},
|
||||
json::NodeMessage::V2 { id, payload } => {
|
||||
NodeMessage::V2 { id, payload: payload.into() }
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum Payload {
|
||||
SystemConnected(SystemConnected),
|
||||
SystemInterval(SystemInterval),
|
||||
BlockImport(Block),
|
||||
NotifyFinalized(Finalized),
|
||||
TxPoolImport,
|
||||
AfgFinalized(AfgFinalized),
|
||||
AfgReceivedPrecommit(AfgReceived),
|
||||
AfgReceivedPrevote(AfgReceived),
|
||||
AfgReceivedCommit(AfgReceived),
|
||||
AfgAuthoritySet(AfgAuthoritySet),
|
||||
AfgFinalizedBlocksUpTo,
|
||||
AuraPreSealedBlock,
|
||||
PreparedBlockForProposing,
|
||||
}
|
||||
|
||||
impl From<json::Payload> for Payload {
|
||||
fn from(msg: json::Payload) -> Self {
|
||||
match msg {
|
||||
json::Payload::SystemConnected(m) => {
|
||||
Payload::SystemConnected(m.into())
|
||||
},
|
||||
json::Payload::SystemInterval(m) => {
|
||||
Payload::SystemInterval(m.into())
|
||||
},
|
||||
json::Payload::BlockImport(m) => {
|
||||
Payload::BlockImport(m.into())
|
||||
},
|
||||
json::Payload::NotifyFinalized(m) => {
|
||||
Payload::NotifyFinalized(m.into())
|
||||
},
|
||||
json::Payload::TxPoolImport => {
|
||||
Payload::TxPoolImport
|
||||
},
|
||||
json::Payload::AfgFinalized(m) => {
|
||||
Payload::AfgFinalized(m.into())
|
||||
},
|
||||
json::Payload::AfgReceivedPrecommit(m) => {
|
||||
Payload::AfgReceivedPrecommit(m.received.into())
|
||||
},
|
||||
json::Payload::AfgReceivedPrevote(m) => {
|
||||
Payload::AfgReceivedPrevote(m.received.into())
|
||||
},
|
||||
json::Payload::AfgReceivedCommit(m) => {
|
||||
Payload::AfgReceivedCommit(m.received.into())
|
||||
},
|
||||
json::Payload::AfgAuthoritySet(m) => {
|
||||
Payload::AfgAuthoritySet(m.into())
|
||||
},
|
||||
json::Payload::AfgFinalizedBlocksUpTo => {
|
||||
Payload::AfgFinalizedBlocksUpTo
|
||||
},
|
||||
json::Payload::AuraPreSealedBlock => {
|
||||
Payload::AuraPreSealedBlock
|
||||
},
|
||||
json::Payload::PreparedBlockForProposing => {
|
||||
Payload::PreparedBlockForProposing
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct SystemConnected {
|
||||
pub genesis_hash: Hash,
|
||||
#[serde(flatten)]
|
||||
pub genesis_hash: BlockHash,
|
||||
pub node: NodeDetails,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
impl From<json::SystemConnected> for SystemConnected {
|
||||
fn from(msg: json::SystemConnected) -> Self {
|
||||
SystemConnected {
|
||||
genesis_hash: msg.genesis_hash.into(),
|
||||
node: msg.node.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct SystemInterval {
|
||||
pub peers: Option<u64>,
|
||||
pub txcount: Option<u64>,
|
||||
@@ -101,54 +133,87 @@ pub struct SystemInterval {
|
||||
pub bandwidth_download: Option<f64>,
|
||||
pub finalized_height: Option<BlockNumber>,
|
||||
pub finalized_hash: Option<BlockHash>,
|
||||
#[serde(flatten)]
|
||||
pub block: Option<Block>,
|
||||
pub used_state_cache_size: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
impl From<json::SystemInterval> for SystemInterval {
|
||||
fn from(msg: json::SystemInterval) -> Self {
|
||||
SystemInterval {
|
||||
peers: msg.peers,
|
||||
txcount: msg.txcount,
|
||||
bandwidth_upload: msg.bandwidth_upload,
|
||||
bandwidth_download: msg.bandwidth_download,
|
||||
finalized_height: msg.finalized_height,
|
||||
finalized_hash: msg.finalized_hash.map(|h| h.into()),
|
||||
block: msg.block.map(|b| b.into()),
|
||||
used_state_cache_size: msg.used_state_cache_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Finalized {
|
||||
#[serde(rename = "best")]
|
||||
pub hash: BlockHash,
|
||||
pub height: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct AfgAuthoritySet {
|
||||
pub authority_id: Box<str>,
|
||||
pub authorities: Box<str>,
|
||||
pub authority_set_id: Box<str>,
|
||||
impl From<json::Finalized> for Finalized {
|
||||
fn from(msg: json::Finalized) -> Self {
|
||||
Finalized {
|
||||
hash: msg.hash.into(),
|
||||
height: msg.height,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct AfgFinalized {
|
||||
pub finalized_hash: BlockHash,
|
||||
pub finalized_number: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
impl From<json::AfgFinalized> for AfgFinalized {
|
||||
fn from(msg: json::AfgFinalized) -> Self {
|
||||
AfgFinalized {
|
||||
finalized_hash: msg.finalized_hash.into(),
|
||||
finalized_number: msg.finalized_number,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct AfgReceived {
|
||||
pub target_hash: BlockHash,
|
||||
pub target_number: Box<str>,
|
||||
pub voter: Option<Box<str>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
pub struct AfgReceivedPrecommit {
|
||||
#[serde(flatten)]
|
||||
pub received: AfgReceived,
|
||||
impl From<json::AfgReceived> for AfgReceived {
|
||||
fn from(msg: json::AfgReceived) -> Self {
|
||||
AfgReceived {
|
||||
target_hash: msg.target_hash.into(),
|
||||
target_number: msg.target_number,
|
||||
voter: msg.voter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
pub struct AfgReceivedPrevote {
|
||||
#[serde(flatten)]
|
||||
pub received: AfgReceived,
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct AfgAuthoritySet {
|
||||
pub authority_id: Box<str>,
|
||||
pub authorities: Box<str>,
|
||||
pub authority_set_id: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
pub struct AfgReceivedCommit {
|
||||
#[serde(flatten)]
|
||||
pub received: AfgReceived,
|
||||
impl From<json::AfgAuthoritySet> for AfgAuthoritySet {
|
||||
fn from(msg: json::AfgAuthoritySet) -> Self {
|
||||
AfgAuthoritySet {
|
||||
authority_id: msg.authority_id,
|
||||
authorities: msg.authorities,
|
||||
authority_set_id: msg.authority_set_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Payload {
|
||||
@@ -180,27 +245,123 @@ mod tests {
|
||||
use super::*;
|
||||
use bincode::Options;
|
||||
|
||||
// Without adding a derive macro and marker trait (and enforcing their use), we don't really
|
||||
// know whether things can (de)serialize to bincode or not at runtime without failing unless
|
||||
// we test the different types we want to (de)serialize ourselves. We just need to test each
|
||||
// type, not each variant.
|
||||
fn bincode_can_serialize_and_deserialize<'de, T>(item: T)
|
||||
where T: Serialize + serde::de::DeserializeOwned
|
||||
{
|
||||
let bytes = bincode::serialize(&item).expect("Serialization should work");
|
||||
let _: T = bincode::deserialize(&bytes).expect("Deserialization should work");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_v1() {
|
||||
let json = r#"{"msg":"notify.finalized","level":"INFO","ts":"2021-01-13T12:38:25.410794650+01:00","best":"0x031c3521ca2f9c673812d692fc330b9a18e18a2781e3f9976992f861fd3ea0cb","height":"50"}"#;
|
||||
assert!(
|
||||
matches!(
|
||||
serde_json::from_str::<NodeMessage>(json).unwrap(),
|
||||
NodeMessage::V1 { .. },
|
||||
),
|
||||
"message did not match variant V1",
|
||||
fn bincode_can_serialize_and_deserialize_node_message_system_connected() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::SystemConnected(SystemConnected {
|
||||
genesis_hash: BlockHash::zero(),
|
||||
node: NodeDetails {
|
||||
chain: "foo".into(),
|
||||
name: "foo".into(),
|
||||
implementation: "foo".into(),
|
||||
version: "foo".into(),
|
||||
validator: None,
|
||||
network_id: None,
|
||||
startup_time: None,
|
||||
},
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_v2() {
|
||||
let json = r#"{"id":1,"ts":"2021-01-13T12:22:20.053527101+01:00","payload":{"best":"0xcc41708573f2acaded9dd75e07dac2d4163d136ca35b3061c558d7a35a09dd8d","height":"209","msg":"notify.finalized"}}"#;
|
||||
assert!(
|
||||
matches!(
|
||||
serde_json::from_str::<NodeMessage>(json).unwrap(),
|
||||
NodeMessage::V2 { .. },
|
||||
),
|
||||
"message did not match variant V2",
|
||||
fn bincode_can_serialize_and_deserialize_node_message_system_interval() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::SystemInterval(SystemInterval {
|
||||
peers: None,
|
||||
txcount: None,
|
||||
bandwidth_upload: None,
|
||||
bandwidth_download: None,
|
||||
finalized_height: None,
|
||||
finalized_hash: None,
|
||||
block: None,
|
||||
used_state_cache_size: None,
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bincode_can_serialize_and_deserialize_node_message_block_import() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::BlockImport(Block {
|
||||
hash: BlockHash([0; 32]),
|
||||
height: 0,
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bincode_can_serialize_and_deserialize_node_message_notify_finalized() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::NotifyFinalized(Finalized {
|
||||
hash: BlockHash::zero(),
|
||||
height: "foo".into(),
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bincode_can_serialize_and_deserialize_node_message_tx_pool_import() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::TxPoolImport
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bincode_can_serialize_and_deserialize_node_message_afg_finalized() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::AfgFinalized(AfgFinalized {
|
||||
finalized_hash: BlockHash::zero(),
|
||||
finalized_number: "foo".into(),
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bincode_can_serialize_and_deserialize_node_message_afg_received() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::AfgReceivedPrecommit(AfgReceived {
|
||||
target_hash: BlockHash::zero(),
|
||||
target_number: "foo".into(),
|
||||
voter: None,
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bincode_can_serialize_and_deserialize_node_message_afg_authority_set() {
|
||||
bincode_can_serialize_and_deserialize(
|
||||
NodeMessage::V1 {
|
||||
payload: Payload::AfgAuthoritySet(AfgAuthoritySet {
|
||||
authority_id: "foo".into(),
|
||||
authorities: "foo".into(),
|
||||
authority_set_id: "foo".into(),
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ use serde::ser::{SerializeTuple, Serializer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::util::{now, MeanList};
|
||||
use crate::json;
|
||||
|
||||
pub type NodeId = usize;
|
||||
pub type ConnId = u64;
|
||||
@@ -21,6 +22,20 @@ pub struct NodeDetails {
|
||||
pub startup_time: Option<Box<str>>,
|
||||
}
|
||||
|
||||
impl From<json::NodeDetails> for NodeDetails {
|
||||
fn from(details: json::NodeDetails) -> Self {
|
||||
NodeDetails {
|
||||
chain: details.chain,
|
||||
name: details.name,
|
||||
implementation: details.implementation,
|
||||
version: details.version,
|
||||
validator: details.validator,
|
||||
network_id: details.network_id,
|
||||
startup_time: details.startup_time,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct NodeStats {
|
||||
pub peers: u64,
|
||||
@@ -34,11 +49,19 @@ pub struct NodeIO {
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
|
||||
pub struct Block {
|
||||
#[serde(rename = "best")]
|
||||
pub hash: BlockHash,
|
||||
pub height: BlockNumber,
|
||||
}
|
||||
|
||||
impl From<json::Block> for Block {
|
||||
fn from(block: json::Block) -> Self {
|
||||
Block {
|
||||
hash: block.hash.into(),
|
||||
height: block.height
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn zero() -> Self {
|
||||
Block {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
mod dense_map;
|
||||
mod hash;
|
||||
mod mean_list;
|
||||
mod null;
|
||||
mod num_stats;
|
||||
|
||||
pub use dense_map::DenseMap;
|
||||
pub use hash::Hash;
|
||||
pub use mean_list::MeanList;
|
||||
pub use null::NullAny;
|
||||
pub use num_stats::NumStats;
|
||||
|
||||
Reference in New Issue
Block a user