Rust backend (#185)

This commit is contained in:
Maciej Hirsz
2019-11-07 10:52:38 +01:00
committed by GitHub
parent 31784131d6
commit a3b6f6a5a1
26 changed files with 3194 additions and 808 deletions
+120
View File
@@ -0,0 +1,120 @@
use actix::prelude::*;
use chrono::{DateTime, Utc};
use serde::Deserialize;
use serde::de::IgnoredAny;
use crate::node::{NodeDetails, NodeStats};
use crate::types::{Block, BlockNumber, BlockHash};
#[derive(Deserialize, Debug, Message)]
pub struct NodeMessage {
pub level: Level,
pub ts: DateTime<Utc>,
#[serde(flatten)]
pub details: Details,
}
#[derive(Deserialize, Debug)]
pub enum Level {
#[serde(rename = "INFO")]
Info,
}
#[derive(Deserialize, Debug)]
#[serde(tag = "msg")]
pub enum Details {
#[serde(rename = "node.start")]
NodeStart(Block),
#[serde(rename = "system.connected")]
SystemConnected(SystemConnected),
#[serde(rename = "system.interval")]
SystemInterval(SystemInterval),
#[serde(rename = "system.network_state")]
SystemNetworkState(IgnoredAny),
#[serde(rename = "block.import")]
BlockImport(Block),
#[serde(rename = "notify.finalized")]
NotifyFinalized(Finalized),
#[serde(rename = "txpool.import")]
TxPoolImport(IgnoredAny),
#[serde(rename = "afg.finalized")]
AfgFinalized(IgnoredAny),
#[serde(rename = "afg.received_precommit")]
AfgReceivedPrecommit(IgnoredAny),
#[serde(rename = "afg.received_prevote")]
AfgReceivedPrevote(IgnoredAny),
#[serde(rename = "afg.received_commit")]
AfgReceivedCommit(IgnoredAny),
#[serde(rename = "afg.authority_set")]
AfgAuthoritySet(IgnoredAny),
#[serde(rename = "aura.pre_sealed_block")]
AuraPreSealedBlock(IgnoredAny),
#[serde(rename = "prepared_block_for_proposing")]
PreparedBlockForProposing(IgnoredAny),
}
#[derive(Deserialize, Debug)]
pub struct SystemConnected {
pub chain: Box<str>,
#[serde(flatten)]
pub node: NodeDetails,
}
#[derive(Deserialize, Debug)]
pub struct SystemInterval {
#[serde(flatten)]
pub stats: NodeStats,
pub memory: Option<f32>,
pub cpu: Option<f32>,
pub bandwidth_upload: Option<f64>,
pub bandwidth_download: Option<f64>,
pub finalized_height: Option<BlockNumber>,
pub finalized_hash: Option<BlockHash>,
#[serde(flatten)]
pub block: Block,
pub network_state: Option<IgnoredAny>,
}
#[derive(Deserialize, Debug)]
pub struct Finalized {
#[serde(rename = "best")]
pub hash: BlockHash,
pub height: Box<str>,
}
impl Block {
pub fn zero() -> Self {
Block {
hash: BlockHash::from([0; 32]),
height: 0,
}
}
}
impl Details {
pub fn best_block(&self) -> Option<&Block> {
match self {
Details::BlockImport(block) | Details::SystemInterval(SystemInterval { block, .. }) => {
Some(block)
}
_ => None,
}
}
pub fn finalized_block(&self) -> Option<Block> {
match self {
Details::SystemInterval(ref interval) => {
Some(Block {
hash: interval.finalized_hash?,
height: interval.finalized_height?,
})
},
Details::NotifyFinalized(ref finalized) => {
Some(Block {
hash: finalized.hash,
height: finalized.height.parse().ok()?
})
},
_ => None
}
}
}