mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-27 08:08:15 -05:00
feat: tx and receipt compression utils for no-std config (#11112)
This commit is contained in:
committed by
GitHub
parent
d46f76264d
commit
6d0159eb70
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -10330,18 +10330,18 @@ checksum = "a38c90d48152c236a3ab59271da4f4ae63d678c5d7ad6b7714d7cb9760be5e4b"
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.63"
|
||||
version = "1.0.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724"
|
||||
checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.63"
|
||||
version = "1.0.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
|
||||
checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use alloc::vec::Vec;
|
||||
use core::cell::RefCell;
|
||||
use std::thread_local;
|
||||
use zstd::bulk::{Compressor, Decompressor};
|
||||
|
||||
/// Compression/Decompression dictionary for `Receipt`.
|
||||
@@ -10,7 +9,8 @@ pub static TRANSACTION_DICTIONARY: &[u8] = include_bytes!("./transaction_diction
|
||||
|
||||
// We use `thread_local` compressors and decompressors because dictionaries can be quite big, and
|
||||
// zstd-rs recommends to use one context/compressor per thread
|
||||
thread_local! {
|
||||
#[cfg(feature = "std")]
|
||||
std::thread_local! {
|
||||
/// Thread Transaction compressor.
|
||||
pub static TRANSACTION_COMPRESSOR: RefCell<Compressor<'static>> = RefCell::new(
|
||||
Compressor::with_dictionary(0, TRANSACTION_DICTIONARY)
|
||||
@@ -38,6 +38,33 @@ thread_local! {
|
||||
));
|
||||
}
|
||||
|
||||
/// Fn creates tx [`Compressor`]
|
||||
pub fn create_tx_compressor() -> Compressor<'static> {
|
||||
Compressor::with_dictionary(0, RECEIPT_DICTIONARY).expect("Failed to instantiate tx compressor")
|
||||
}
|
||||
|
||||
/// Fn creates tx [`Decompressor`]
|
||||
pub fn create_tx_decompressor() -> ReusableDecompressor {
|
||||
ReusableDecompressor::new(
|
||||
Decompressor::with_dictionary(TRANSACTION_DICTIONARY)
|
||||
.expect("Failed to instantiate tx decompressor"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Fn creates receipt [`Compressor`]
|
||||
pub fn create_receipt_compressor() -> Compressor<'static> {
|
||||
Compressor::with_dictionary(0, RECEIPT_DICTIONARY)
|
||||
.expect("Failed to instantiate receipt compressor")
|
||||
}
|
||||
|
||||
/// Fn creates receipt [`Decompressor`]
|
||||
pub fn create_receipt_decompressor() -> ReusableDecompressor {
|
||||
ReusableDecompressor::new(
|
||||
Decompressor::with_dictionary(RECEIPT_DICTIONARY)
|
||||
.expect("Failed to instantiate receipt decompressor"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Reusable decompressor that uses its own internal buffer.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct ReusableDecompressor {
|
||||
|
||||
@@ -956,15 +956,20 @@ impl reth_codecs::Compact for TransactionSignedNoHash {
|
||||
let zstd_bit = self.transaction.input().len() >= 32;
|
||||
|
||||
let tx_bits = if zstd_bit {
|
||||
crate::compression::TRANSACTION_COMPRESSOR.with(|compressor| {
|
||||
let mut compressor = compressor.borrow_mut();
|
||||
let mut tmp = Vec::with_capacity(256);
|
||||
let mut tmp = Vec::with_capacity(256);
|
||||
if cfg!(feature = "std") {
|
||||
crate::compression::TRANSACTION_COMPRESSOR.with(|compressor| {
|
||||
let mut compressor = compressor.borrow_mut();
|
||||
let tx_bits = self.transaction.to_compact(&mut tmp);
|
||||
buf.put_slice(&compressor.compress(&tmp).expect("Failed to compress"));
|
||||
tx_bits as u8
|
||||
})
|
||||
} else {
|
||||
let mut compressor = crate::compression::create_tx_compressor();
|
||||
let tx_bits = self.transaction.to_compact(&mut tmp);
|
||||
|
||||
buf.put_slice(&compressor.compress(&tmp).expect("Failed to compress"));
|
||||
|
||||
tx_bits as u8
|
||||
})
|
||||
}
|
||||
} else {
|
||||
self.transaction.to_compact(buf) as u8
|
||||
};
|
||||
@@ -984,17 +989,26 @@ impl reth_codecs::Compact for TransactionSignedNoHash {
|
||||
|
||||
let zstd_bit = bitflags >> 3;
|
||||
let (transaction, buf) = if zstd_bit != 0 {
|
||||
crate::compression::TRANSACTION_DECOMPRESSOR.with(|decompressor| {
|
||||
let mut decompressor = decompressor.borrow_mut();
|
||||
if cfg!(feature = "std") {
|
||||
crate::compression::TRANSACTION_DECOMPRESSOR.with(|decompressor| {
|
||||
let mut decompressor = decompressor.borrow_mut();
|
||||
|
||||
// TODO: enforce that zstd is only present at a "top" level type
|
||||
// TODO: enforce that zstd is only present at a "top" level type
|
||||
|
||||
let transaction_type = (bitflags & 0b110) >> 1;
|
||||
let (transaction, _) =
|
||||
Transaction::from_compact(decompressor.decompress(buf), transaction_type);
|
||||
|
||||
(transaction, buf)
|
||||
})
|
||||
} else {
|
||||
let mut decompressor = crate::compression::create_tx_decompressor();
|
||||
let transaction_type = (bitflags & 0b110) >> 1;
|
||||
let (transaction, _) =
|
||||
Transaction::from_compact(decompressor.decompress(buf), transaction_type);
|
||||
|
||||
(transaction, buf)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let transaction_type = bitflags >> 1;
|
||||
Transaction::from_compact(buf, transaction_type)
|
||||
|
||||
Reference in New Issue
Block a user