types: add BlockCompleteEntry

This commit is contained in:
hinto.janai 2024-07-14 17:23:23 -04:00
parent e43e95f863
commit 705f72b266
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 162 additions and 1 deletions

4
Cargo.lock generated
View file

@ -800,8 +800,12 @@ version = "0.0.0"
name = "cuprate-types"
version = "0.0.0"
dependencies = [
"bytes",
"cuprate-epee-encoding",
"cuprate-fixed-bytes",
"curve25519-dalek",
"monero-serai",
"serde",
]
[[package]]

View file

@ -9,11 +9,18 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/types"
keywords = ["cuprate", "types"]
[features]
default = ["blockchain"]
default = ["blockchain", "epee", "serde"]
blockchain = []
epee = ["dep:cuprate-epee-encoding"]
serde = ["dep:serde"]
[dependencies]
cuprate-epee-encoding = { path = "../net/epee-encoding", optional = true }
cuprate-fixed-bytes = { path = "../net/fixed-bytes" }
bytes = { workspace = true }
curve25519-dalek = { workspace = true }
monero-serai = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }
[dev-dependencies]

View file

@ -0,0 +1,147 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
#[cfg(feature = "epee")]
use bytes::Bytes;
use cuprate_fixed_bytes::ByteArray;
#[cfg(feature = "epee")]
use cuprate_epee_encoding::{
epee_object,
macros::bytes::{Buf, BufMut},
EpeeValue, InnerMarker,
};
//---------------------------------------------------------------------------------------------------- BlockCompleteEntry
/// A block that can contain transactions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlockCompleteEntry {
/// `true` if transaction data is pruned.
pub pruned: bool,
/// The block.
pub block: Bytes,
/// The block weight/size.
pub block_weight: u64,
/// The block's transactions.
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,
);
//---------------------------------------------------------------------------------------------------- TransactionBlobs
/// TODO
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TransactionBlobs {
/// TODO
Pruned(Vec<PrunedTxBlobEntry>),
/// TODO
Normal(Vec<Bytes>),
/// TODO
None,
}
impl TransactionBlobs {
/// TODO
pub fn take_pruned(self) -> Option<Vec<PrunedTxBlobEntry>> {
match self {
Self::Normal(_) => None,
Self::Pruned(txs) => Some(txs),
Self::None => Some(vec![]),
}
}
/// TODO
pub fn take_normal(self) -> Option<Vec<Bytes>> {
match self {
Self::Normal(txs) => Some(txs),
Self::Pruned(_) => None,
Self::None => Some(vec![]),
}
}
/// TODO
pub fn len(&self) -> usize {
match self {
Self::Normal(txs) => txs.len(),
Self::Pruned(txs) => txs.len(),
Self::None => 0,
}
}
/// TODO
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// TODO
#[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(),
)),
}
}
/// TODO
#[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(())
}
/// TODO
#[cfg(feature = "epee")]
fn should_write_tx_blobs(&self) -> bool {
!self.is_empty()
}
}
//---------------------------------------------------------------------------------------------------- PrunedTxBlobEntry
/// A pruned transaction with the hash of the missing prunable data
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PrunedTxBlobEntry {
/// The transaction.
pub tx: Bytes,
/// The prunable transaction hash.
pub prunable_hash: ByteArray<32>,
}
#[cfg(feature = "epee")]
epee_object!(
PrunedTxBlobEntry,
tx: Bytes,
prunable_hash: ByteArray<32>,
);
//---------------------------------------------------------------------------------------------------- Import
#[cfg(test)]
mod tests {}

View file

@ -86,7 +86,10 @@
//
// Documentation for each module is located in the respective file.
mod block_complete_entry;
mod types;
pub use block_complete_entry::{BlockCompleteEntry, PrunedTxBlobEntry, TransactionBlobs};
pub use types::{
ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation, VerifiedTransactionInformation,
};