Bucket nodes by genesis hash (#335)

* Send unwrapped Payload to Chain + cargo fmt

* Read genesis_hash when connecting

* Group chains by genesis hashes

* Fix typo

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Fix grumbles

* Link up `Hash` for docs

* `hashes` -> `genesis_hashes`

* Typo :)

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Added a doc comment link

* Add comment about why H256 is not used

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Maciej Hirsz
2021-04-27 13:05:58 +02:00
committed by GitHub
parent 93e33fd3f7
commit 05b0afefd3
17 changed files with 439 additions and 237 deletions
+23 -25
View File
@@ -1,8 +1,9 @@
use actix::prelude::*;
use serde::Deserialize;
use serde::de::IgnoredAny;
use crate::node::NodeDetails;
use crate::types::{Block, BlockNumber, BlockHash, ConnId};
use crate::types::{Block, BlockHash, BlockNumber, ConnId};
use crate::util::Hash;
use actix::prelude::*;
use serde::de::IgnoredAny;
use serde::Deserialize;
#[derive(Deserialize, Debug, Message)]
#[rtype(result = "()")]
@@ -19,13 +20,6 @@ pub enum NodeMessage {
}
impl NodeMessage {
/// Returns a reference to the payload.
pub fn payload(&self) -> &Payload {
match self {
NodeMessage::V1 { payload, .. } | NodeMessage::V2 { payload, .. } => payload,
}
}
/// Returns the connection ID or 0 if there is no ID.
pub fn id(&self) -> ConnId {
match self {
@@ -35,6 +29,14 @@ impl NodeMessage {
}
}
impl From<NodeMessage> for Payload {
fn from(msg: NodeMessage) -> Payload {
match msg {
NodeMessage::V1 { payload, .. } | NodeMessage::V2 { payload, .. } => payload,
}
}
}
#[derive(Deserialize, Debug)]
#[serde(tag = "msg")]
pub enum Payload {
@@ -70,7 +72,7 @@ pub enum Payload {
#[derive(Deserialize, Debug)]
pub struct SystemConnected {
pub network_id: Option<Box<str>>,
pub genesis_hash: Hash,
#[serde(flatten)]
pub node: NodeDetails,
}
@@ -154,19 +156,15 @@ impl Payload {
pub fn finalized_block(&self) -> Option<Block> {
match self {
Payload::SystemInterval(ref interval) => {
Some(Block {
hash: interval.finalized_hash?,
height: interval.finalized_height?,
})
},
Payload::NotifyFinalized(ref finalized) => {
Some(Block {
hash: finalized.hash,
height: finalized.height.parse().ok()?
})
},
_ => None
Payload::SystemInterval(ref interval) => Some(Block {
hash: interval.finalized_hash?,
height: interval.finalized_height?,
}),
Payload::NotifyFinalized(ref finalized) => Some(Block {
hash: finalized.hash,
height: finalized.height.parse().ok()?,
}),
_ => None,
}
}
}