cuprate_types/
block_complete_entry.rsuse bytes::Bytes;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use cuprate_fixed_bytes::ByteArray;
#[cfg(feature = "epee")]
use cuprate_epee_encoding::{
epee_object,
macros::bytes::{Buf, BufMut},
EpeeValue, InnerMarker,
};
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BlockCompleteEntry {
pub pruned: bool,
pub block: Bytes,
pub block_weight: u64,
pub txs: TransactionBlobs,
}
#[cfg(feature = "epee")]
epee_object!(
BlockCompleteEntry,
pruned: bool = false,
block: Bytes,
block_weight: u64 = 0_u64,
txs: TransactionBlobs = TransactionBlobs::None =>
TransactionBlobs::tx_blob_read,
TransactionBlobs::tx_blob_write,
TransactionBlobs::should_write_tx_blobs,
);
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TransactionBlobs {
Pruned(Vec<PrunedTxBlobEntry>),
Normal(Vec<Bytes>),
#[default]
None,
}
impl TransactionBlobs {
pub fn take_pruned(self) -> Option<Vec<PrunedTxBlobEntry>> {
match self {
Self::Normal(_) => None,
Self::Pruned(txs) => Some(txs),
Self::None => Some(vec![]),
}
}
pub fn take_normal(self) -> Option<Vec<Bytes>> {
match self {
Self::Normal(txs) => Some(txs),
Self::Pruned(_) => None,
Self::None => Some(vec![]),
}
}
pub fn len(&self) -> usize {
match self {
Self::Normal(txs) => txs.len(),
Self::Pruned(txs) => txs.len(),
Self::None => 0,
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[cfg(feature = "epee")]
fn tx_blob_read<B: Buf>(b: &mut B) -> cuprate_epee_encoding::Result<Self> {
let marker = cuprate_epee_encoding::read_marker(b)?;
match marker.inner_marker {
InnerMarker::Object => Ok(Self::Pruned(Vec::read(b, &marker)?)),
InnerMarker::String => Ok(Self::Normal(Vec::read(b, &marker)?)),
_ => Err(cuprate_epee_encoding::Error::Value(
"Invalid marker for tx blobs".to_string(),
)),
}
}
#[cfg(feature = "epee")]
fn tx_blob_write<B: BufMut>(
self,
field_name: &str,
w: &mut B,
) -> cuprate_epee_encoding::Result<()> {
if self.should_write_tx_blobs() {
match self {
Self::Normal(bytes) => {
cuprate_epee_encoding::write_field(bytes, field_name, w)?;
}
Self::Pruned(obj) => {
cuprate_epee_encoding::write_field(obj, field_name, w)?;
}
Self::None => (),
}
}
Ok(())
}
#[cfg(feature = "epee")]
fn should_write_tx_blobs(&self) -> bool {
!self.is_empty()
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PrunedTxBlobEntry {
pub blob: Bytes,
pub prunable_hash: ByteArray<32>,
}
#[cfg(feature = "epee")]
epee_object!(
PrunedTxBlobEntry,
blob: Bytes,
prunable_hash: ByteArray<32>,
);
#[cfg(test)]
mod tests {}