use crate::ids::ShardId;
use crate::sync_codec::error::SyncError;
use crate::sync_codec::message::root::{SyncDecision, SyncStats};
use crate::tree::{Hash, Node};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeTransfer {
pub hash: Hash,
pub node: Node,
}
impl NodeTransfer {
#[must_use]
pub fn new(node: Node) -> Self {
Self {
hash: node.hash(),
node,
}
}
pub fn from_parts(hash: Hash, node: Node) -> Result<Self, SyncError> {
let actual = node.hash();
if actual != hash {
return Err(SyncError::HashMismatch {
expected: hash,
actual,
});
}
Ok(Self { hash, node })
}
#[must_use]
pub fn byte_len(&self) -> usize {
self.node.serialise().len()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PushResponse {
pub shard_id: ShardId,
pub source_root: Option<Hash>,
pub target_root: Option<Hash>,
pub transfers: Vec<NodeTransfer>,
pub stats: SyncStats,
}
impl PushResponse {
#[must_use]
pub fn new(
shard_id: ShardId,
source_root: Option<Hash>,
target_root: Option<Hash>,
transfers: Vec<NodeTransfer>,
mut stats: SyncStats,
) -> Self {
stats.nodes_transferred = transfers.len();
stats.bytes_transferred = transfers.iter().map(NodeTransfer::byte_len).sum();
Self {
shard_id,
source_root,
target_root,
transfers,
stats,
}
}
#[must_use]
pub const fn with_stats(
shard_id: ShardId,
source_root: Option<Hash>,
target_root: Option<Hash>,
transfers: Vec<NodeTransfer>,
stats: SyncStats,
) -> Self {
Self {
shard_id,
source_root,
target_root,
transfers,
stats,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingNodes {
pub shard_id: ShardId,
pub source_root: Option<Hash>,
pub target_root: Option<Hash>,
pub decision: SyncDecision,
pub transfers: Vec<NodeTransfer>,
pub stats: SyncStats,
}