mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-11-16 15:58:14 +00:00
Merge branch 'types' into rpc-serde
This commit is contained in:
commit
6099110a9d
8 changed files with 14 additions and 122 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -702,6 +702,7 @@ dependencies = [
|
|||
"cuprate-p2p-core",
|
||||
"cuprate-pruning",
|
||||
"cuprate-test-utils",
|
||||
"cuprate-types",
|
||||
"cuprate-wire",
|
||||
"dashmap",
|
||||
"futures",
|
||||
|
@ -818,6 +819,7 @@ dependencies = [
|
|||
"cuprate-epee-encoding",
|
||||
"cuprate-fixed-bytes",
|
||||
"cuprate-levin",
|
||||
"cuprate-types",
|
||||
"hex",
|
||||
"thiserror",
|
||||
]
|
||||
|
|
|
@ -14,6 +14,7 @@ tracing = ["cuprate-levin/tracing"]
|
|||
cuprate-levin = { path = "../levin" }
|
||||
cuprate-epee-encoding = { path = "../epee-encoding" }
|
||||
cuprate-fixed-bytes = { path = "../fixed-bytes" }
|
||||
cuprate-types = { path = "../../types", default-features = false, features = ["epee"] }
|
||||
|
||||
bitflags = { workspace = true, features = ["std"] }
|
||||
bytes = { workspace = true, features = ["std"] }
|
||||
|
|
|
@ -16,10 +16,9 @@
|
|||
//! Common types that are used across multiple messages.
|
||||
|
||||
use bitflags::bitflags;
|
||||
use bytes::{Buf, BufMut, Bytes};
|
||||
|
||||
use cuprate_epee_encoding::{epee_object, EpeeValue, InnerMarker};
|
||||
use cuprate_fixed_bytes::ByteArray;
|
||||
use cuprate_epee_encoding::epee_object;
|
||||
pub use cuprate_types::{BlockCompleteEntry, PrunedTxBlobEntry, TransactionBlobs};
|
||||
|
||||
use crate::NetworkAddress;
|
||||
|
||||
|
@ -168,113 +167,6 @@ epee_object! {
|
|||
rpc_credits_per_hash: u32 = 0_u32,
|
||||
}
|
||||
|
||||
/// A pruned tx with the hash of the missing prunable data
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PrunedTxBlobEntry {
|
||||
/// The Tx
|
||||
pub tx: Bytes,
|
||||
/// The Prunable Tx Hash
|
||||
pub prunable_hash: ByteArray<32>,
|
||||
}
|
||||
|
||||
epee_object!(
|
||||
PrunedTxBlobEntry,
|
||||
tx: Bytes,
|
||||
prunable_hash: ByteArray<32>,
|
||||
);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum TransactionBlobs {
|
||||
Pruned(Vec<PrunedTxBlobEntry>),
|
||||
Normal(Vec<Bytes>),
|
||||
None,
|
||||
}
|
||||
|
||||
impl TransactionBlobs {
|
||||
pub fn take_pruned(self) -> Option<Vec<PrunedTxBlobEntry>> {
|
||||
match self {
|
||||
TransactionBlobs::Normal(_) => None,
|
||||
TransactionBlobs::Pruned(txs) => Some(txs),
|
||||
TransactionBlobs::None => Some(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take_normal(self) -> Option<Vec<Bytes>> {
|
||||
match self {
|
||||
TransactionBlobs::Normal(txs) => Some(txs),
|
||||
TransactionBlobs::Pruned(_) => None,
|
||||
TransactionBlobs::None => Some(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
TransactionBlobs::Normal(txs) => txs.len(),
|
||||
TransactionBlobs::Pruned(txs) => txs.len(),
|
||||
TransactionBlobs::None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// A Block that can contain transactions
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct BlockCompleteEntry {
|
||||
/// True if tx data is pruned
|
||||
pub pruned: bool,
|
||||
/// The Block
|
||||
pub block: Bytes,
|
||||
/// The Block Weight/Size
|
||||
pub block_weight: u64,
|
||||
/// The blocks txs
|
||||
pub txs: TransactionBlobs,
|
||||
}
|
||||
|
||||
epee_object!(
|
||||
BlockCompleteEntry,
|
||||
pruned: bool = false,
|
||||
block: Bytes,
|
||||
block_weight: u64 = 0_u64,
|
||||
txs: TransactionBlobs = TransactionBlobs::None => tx_blob_read, tx_blob_write, should_write_tx_blobs,
|
||||
);
|
||||
|
||||
fn tx_blob_read<B: Buf>(b: &mut B) -> cuprate_epee_encoding::Result<TransactionBlobs> {
|
||||
let marker = cuprate_epee_encoding::read_marker(b)?;
|
||||
match marker.inner_marker {
|
||||
InnerMarker::Object => Ok(TransactionBlobs::Pruned(Vec::read(b, &marker)?)),
|
||||
InnerMarker::String => Ok(TransactionBlobs::Normal(Vec::read(b, &marker)?)),
|
||||
_ => Err(cuprate_epee_encoding::Error::Value(
|
||||
"Invalid marker for tx blobs".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn tx_blob_write<B: BufMut>(
|
||||
val: TransactionBlobs,
|
||||
field_name: &str,
|
||||
w: &mut B,
|
||||
) -> cuprate_epee_encoding::Result<()> {
|
||||
if should_write_tx_blobs(&val) {
|
||||
match val {
|
||||
TransactionBlobs::Normal(bytes) => {
|
||||
cuprate_epee_encoding::write_field(bytes, field_name, w)?
|
||||
}
|
||||
TransactionBlobs::Pruned(obj) => {
|
||||
cuprate_epee_encoding::write_field(obj, field_name, w)?
|
||||
}
|
||||
TransactionBlobs::None => (),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn should_write_tx_blobs(val: &TransactionBlobs) -> bool {
|
||||
!val.is_empty()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
|
|
|
@ -16,14 +16,14 @@
|
|||
//! This module defines Monero protocol messages
|
||||
//!
|
||||
//! Protocol message requests don't have to be responded to in order unlike
|
||||
//! admin messages.
|
||||
//! admin messages.
|
||||
|
||||
use bytes::Bytes;
|
||||
|
||||
use cuprate_epee_encoding::{container_as_blob::ContainerAsBlob, epee_object};
|
||||
use cuprate_fixed_bytes::{ByteArray, ByteArrayVec};
|
||||
|
||||
use super::common::BlockCompleteEntry;
|
||||
use crate::p2p::common::BlockCompleteEntry;
|
||||
|
||||
/// A block that SHOULD have transactions
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
|
@ -13,6 +13,7 @@ cuprate-address-book = { path = "../address-book" }
|
|||
cuprate-pruning = { path = "../../pruning" }
|
||||
cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false }
|
||||
cuprate-async-buffer = { path = "../async-buffer" }
|
||||
cuprate-types = { path = "../../types", default-features = false }
|
||||
|
||||
monero-serai = { workspace = true, features = ["std"] }
|
||||
|
||||
|
|
|
@ -26,10 +26,8 @@ use cuprate_p2p_core::{
|
|||
ProtocolResponse,
|
||||
};
|
||||
use cuprate_pruning::PruningSeed;
|
||||
use cuprate_wire::{
|
||||
common::{BlockCompleteEntry, TransactionBlobs},
|
||||
protocol::{ChainResponse, GetObjectsResponse},
|
||||
};
|
||||
use cuprate_types::{BlockCompleteEntry, TransactionBlobs};
|
||||
use cuprate_wire::protocol::{ChainResponse, GetObjectsResponse};
|
||||
|
||||
use crate::{
|
||||
block_downloader::{download_blocks, BlockDownloaderConfig, ChainSvcRequest, ChainSvcResponse},
|
||||
|
|
|
@ -25,10 +25,8 @@ use tower::Service;
|
|||
use cuprate_p2p_core::{
|
||||
client::InternalPeerID, BroadcastMessage, ConnectionDirection, NetworkZone,
|
||||
};
|
||||
use cuprate_wire::{
|
||||
common::{BlockCompleteEntry, TransactionBlobs},
|
||||
protocol::{NewFluffyBlock, NewTransactions},
|
||||
};
|
||||
use cuprate_types::{BlockCompleteEntry, TransactionBlobs};
|
||||
use cuprate_wire::protocol::{NewFluffyBlock, NewTransactions};
|
||||
|
||||
use crate::constants::{
|
||||
DIFFUSION_FLUSH_AVERAGE_SECONDS_INBOUND, DIFFUSION_FLUSH_AVERAGE_SECONDS_OUTBOUND,
|
||||
|
|
|
@ -6,6 +6,6 @@ This crate is a kitchen-sink for data types that are shared across Cuprate.
|
|||
# Features flags
|
||||
| Feature flag | Does what |
|
||||
|--------------|-----------|
|
||||
| `blockchain` | Enables the [`blockchain`] module, containing the blockchain database request/response types
|
||||
| `blockchain` | Enables the `blockchain` module, containing the blockchain database request/response types
|
||||
| `serde` | Enables `serde` on types where applicable
|
||||
| `epee` | Enables `cuprate-epee-encoding` on types where applicable
|
||||
| `epee` | Enables `cuprate-epee-encoding` on types where applicable
|
||||
|
|
Loading…
Reference in a new issue