use bee_block::payload::milestone::{MilestoneId, MilestoneIndex};
use packable::{
error::{UnpackError, UnpackErrorExt},
packer::Packer,
unpacker::Unpacker,
Packable,
};
use crate::{error::Error, snapshot::SnapshotKind};
const SNAPSHOT_VERSION: u8 = 2;
#[derive(Clone)]
pub struct SnapshotHeader {
kind: SnapshotKind,
timestamp: u32,
network_id: u64,
sep_index: MilestoneIndex,
ledger_index: MilestoneIndex,
}
impl SnapshotHeader {
pub const LENGTH: usize = 26;
pub fn kind(&self) -> SnapshotKind {
self.kind
}
pub fn timestamp(&self) -> u32 {
self.timestamp
}
pub fn network_id(&self) -> u64 {
self.network_id
}
pub fn sep_index(&self) -> MilestoneIndex {
self.sep_index
}
pub fn ledger_index(&self) -> MilestoneIndex {
self.ledger_index
}
}
impl Packable for SnapshotHeader {
type UnpackError = Error;
type UnpackVisitor = ();
fn pack<P: Packer>(&self, packer: &mut P) -> Result<(), P::Error> {
SNAPSHOT_VERSION.pack(packer)?;
self.kind.pack(packer)?;
self.timestamp.pack(packer)?;
self.network_id.pack(packer)?;
self.sep_index.pack(packer)?;
self.ledger_index.pack(packer)?;
Ok(())
}
fn unpack<U: Unpacker, const VERIFY: bool>(
unpacker: &mut U,
visitor: &Self::UnpackVisitor,
) -> Result<Self, UnpackError<Self::UnpackError, U::Error>> {
let version = u8::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
if VERIFY && SNAPSHOT_VERSION != version {
return Err(UnpackError::Packable(Error::UnsupportedVersion(
SNAPSHOT_VERSION,
version,
)));
}
let kind = SnapshotKind::unpack::<_, VERIFY>(unpacker, visitor)?;
let timestamp = u32::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
let network_id = u64::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
let sep_index = MilestoneIndex::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
let ledger_index = MilestoneIndex::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
Ok(Self {
kind,
timestamp,
network_id,
sep_index,
ledger_index,
})
}
}
#[derive(Clone, Packable)]
pub struct FullSnapshotHeader {
sep_count: u64,
output_count: u64,
milestone_diff_count: u64,
treasury_output_milestone_id: MilestoneId,
treasury_output_amount: u64,
}
impl FullSnapshotHeader {
pub fn sep_count(&self) -> u64 {
self.sep_count
}
pub fn output_count(&self) -> u64 {
self.output_count
}
pub fn milestone_diff_count(&self) -> u64 {
self.milestone_diff_count
}
pub fn treasury_output_milestone_id(&self) -> &MilestoneId {
&self.treasury_output_milestone_id
}
pub fn treasury_output_amount(&self) -> u64 {
self.treasury_output_amount
}
}
#[derive(Clone, Packable)]
pub struct DeltaSnapshotHeader {
sep_count: u64,
milestone_diff_count: u64,
}
impl DeltaSnapshotHeader {
pub fn sep_count(&self) -> u64 {
self.sep_count
}
pub fn milestone_diff_count(&self) -> u64 {
self.milestone_diff_count
}
}