Skip to content

Commit 421fd60

Browse files
committed
Compress and share transaction set responses across peers
Prefer the negotiated zstd tx-set response protocol and cache each set's encoded payload for fanout. Encode and decode off Tokio network workers, retain existing size and send-admission bounds, and preserve legacy peer compatibility. Port 125a248 onto the batched-getdata branch. Rust tests: 191 passed, 3 pre-existing ignored; cargo fmt passed.
1 parent 03a55f7 commit 421fd60

8 files changed

Lines changed: 576 additions & 106 deletions

File tree

Cargo.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

overlay/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ libp2p = { version = "0.54", features = ["tokio", "macros", "identify", "quic"]
2929
libp2p-stream = "0.2.0-alpha"
3030
futures = "0.3"
3131

32+
# TX set response compression
33+
zstd = { version = "0.13.3", default-features = false }
34+
3235
# Logging
3336
tracing = "0.1"
3437
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

overlay/src/flood/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod mempool;
99
mod pending_requests;
1010
mod tx_buffer;
1111
mod txset;
12+
pub(crate) mod txset_encoding;
1213

1314
pub use inv_batcher::InvBatcher;
1415
pub use inv_messages::{GetData, InvBatch, InvEntry, TxStreamMessage};
@@ -17,3 +18,4 @@ pub use mempool::Mempool;
1718
pub use pending_requests::PendingRequests;
1819
pub use tx_buffer::TxBuffer;
1920
pub use txset::{CachedTxSet, Hash256, TxSetCache};
21+
pub use txset_encoding::TxSetData;

overlay/src/flood/txset.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! TX set cache for peer request/response handling.
22
3+
use super::TxSetData;
34
use std::collections::{HashMap, VecDeque};
45
use std::sync::Arc;
56

@@ -11,9 +12,9 @@ pub type Hash256 = [u8; 32];
1112
pub struct CachedTxSet {
1213
/// The TX set hash (SHA256 of XDR)
1314
pub hash: Hash256,
14-
/// Immutable XDR shared by the cache and pending peer responses. Keep the
15-
/// Vec allocation when caching; fanout clones only the Arc.
16-
pub xdr: Arc<Vec<u8>>,
15+
/// Immutable XDR and lazily encoded response shared by the cache and
16+
/// pending peer responses. Fanout clones only the Arc.
17+
pub xdr: Arc<TxSetData>,
1718
/// Ledger sequence this was built for
1819
pub ledger_seq: u32,
1920
}
@@ -85,7 +86,7 @@ mod tests {
8586

8687
let tx_set = CachedTxSet {
8788
hash: [1u8; 32],
88-
xdr: vec![1, 2, 3].into(),
89+
xdr: Arc::new(vec![1, 2, 3].into()),
8990
ledger_seq: 100,
9091
};
9192

@@ -105,12 +106,12 @@ mod tests {
105106

106107
cache.insert(CachedTxSet {
107108
hash: [1u8; 32],
108-
xdr: vec![].into(),
109+
xdr: Arc::new(vec![].into()),
109110
ledger_seq: 100,
110111
});
111112
cache.insert(CachedTxSet {
112113
hash: [2u8; 32],
113-
xdr: vec![].into(),
114+
xdr: Arc::new(vec![].into()),
114115
ledger_seq: 200,
115116
});
116117

@@ -126,12 +127,12 @@ mod tests {
126127

127128
cache.insert(CachedTxSet {
128129
hash: [1u8; 32],
129-
xdr: vec![].into(),
130+
xdr: Arc::new(vec![].into()),
130131
ledger_seq: 100,
131132
});
132133
cache.insert(CachedTxSet {
133134
hash: [2u8; 32],
134-
xdr: vec![].into(),
135+
xdr: Arc::new(vec![].into()),
135136
ledger_seq: 101,
136137
});
137138

@@ -140,7 +141,7 @@ mod tests {
140141
// Insert 3rd - should evict one
141142
cache.insert(CachedTxSet {
142143
hash: [3u8; 32],
143-
xdr: vec![].into(),
144+
xdr: Arc::new(vec![].into()),
144145
ledger_seq: 102,
145146
});
146147

@@ -165,14 +166,14 @@ mod tests {
165166

166167
cache.insert(CachedTxSet {
167168
hash: [1u8; 32],
168-
xdr: vec![1, 2, 3].into(),
169+
xdr: Arc::new(vec![1, 2, 3].into()),
169170
ledger_seq: 100,
170171
});
171172

172173
// Insert with same hash but different data
173174
cache.insert(CachedTxSet {
174175
hash: [1u8; 32],
175-
xdr: vec![4, 5, 6].into(),
176+
xdr: Arc::new(vec![4, 5, 6].into()),
176177
ledger_seq: 200,
177178
});
178179

@@ -188,7 +189,7 @@ mod tests {
188189

189190
cache.insert(CachedTxSet {
190191
hash: [1u8; 32],
191-
xdr: vec![1, 2, 3].into(),
192+
xdr: Arc::new(vec![1, 2, 3].into()),
192193
ledger_seq: 100,
193194
});
194195
assert_eq!(cache.len(), 1, "Cache is full");
@@ -197,7 +198,7 @@ mod tests {
197198
// not evict the entry or shrink the cache below capacity.
198199
cache.insert(CachedTxSet {
199200
hash: [1u8; 32],
200-
xdr: vec![4, 5, 6].into(),
201+
xdr: Arc::new(vec![4, 5, 6].into()),
201202
ledger_seq: 102,
202203
});
203204

@@ -216,12 +217,12 @@ mod tests {
216217
// Insert, then overwrite the same hash with a newer ledger_seq.
217218
cache.insert(CachedTxSet {
218219
hash: [1u8; 32],
219-
xdr: vec![1, 2, 3].into(),
220+
xdr: Arc::new(vec![1, 2, 3].into()),
220221
ledger_seq: 100,
221222
});
222223
cache.insert(CachedTxSet {
223224
hash: [1u8; 32],
224-
xdr: vec![4, 5, 6].into(),
225+
xdr: Arc::new(vec![4, 5, 6].into()),
225226
ledger_seq: 200,
226227
});
227228

@@ -239,12 +240,12 @@ mod tests {
239240
// cache exactly at capacity (not one over).
240241
cache.insert(CachedTxSet {
241242
hash: [2u8; 32],
242-
xdr: vec![].into(),
243+
xdr: Arc::new(vec![].into()),
243244
ledger_seq: 300,
244245
});
245246
cache.insert(CachedTxSet {
246247
hash: [3u8; 32],
247-
xdr: vec![].into(),
248+
xdr: Arc::new(vec![].into()),
248249
ledger_seq: 400,
249250
});
250251

0 commit comments

Comments
 (0)