1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! BIP 331: Package Relay
//!
//! Specification: https://github.com/bitcoin/bips/blob/master/bip-0331.mediawiki
//!
//! Package relay allows nodes to relay and validate groups of transactions together.
//! This is particularly useful for:
//! - Fee-bumping (RBF) transactions (parent + child)
//! - CPFP (Child Pays For Parent) scenarios
//! - Atomic transaction sets
//!
//! Benefits:
//! - Better fee rate calculation for package
//! - Reduces orphan transactions in mempool
//! - More efficient validation (package as unit)
use crate::network::txhash::calculate_wtxid;
use blvm_protocol::block::calculate_tx_id;
use blvm_protocol::{Hash, Transaction};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use tracing::{debug, info, warn};
/// Package relay manager
pub struct PackageRelay {
/// Pending package requests
pending_packages: HashMap<PackageId, PackageState>,
/// Package validator
validator: PackageValidator,
}
/// Package ID (combined hash of all transactions)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PackageId(pub Hash);
/// Transaction package
#[derive(Debug, Clone)]
pub struct TransactionPackage {
/// Transactions in package (ordered: parents first)
pub transactions: Vec<Transaction>,
/// Package ID
pub package_id: PackageId,
/// Combined fee (sum of all transaction fees)
pub combined_fee: u64,
/// Combined weight (for fee rate calculation)
pub combined_weight: usize,
}
/// Package state
#[derive(Debug, Clone)]
struct PackageState {
/// Package data
package: TransactionPackage,
/// When package was received
received_at: u64,
/// Package status
status: PackageStatus,
}
/// Package status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageStatus {
/// Pending validation
Pending,
/// Validated and accepted
Accepted,
/// Rejected (validation failed)
Rejected { reason: PackageRejectReason },
}
/// Package rejection reason
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageRejectReason {
/// Package too large (transaction count)
TooManyTransactions,
/// Package weight exceeds limit
WeightExceedsLimit,
/// Invalid fee rate (below minimum)
FeeRateTooLow,
/// Transactions not properly ordered (parent before child)
InvalidOrder,
/// Duplicate transactions
DuplicateTransactions,
/// Invalid package structure
InvalidStructure,
}
/// Package validator
#[derive(Debug, Clone)]
pub struct PackageValidator {
/// Maximum transactions in package (BIP 331: 25)
pub max_package_size: usize,
/// Maximum package weight in WU (BIP 331: 404000)
pub max_package_weight: usize,
/// Minimum package fee rate (sat/vB)
pub min_fee_rate: u64,
}
impl Default for PackageValidator {
fn default() -> Self {
Self {
max_package_size: 25,
max_package_weight: 404_000, // 404k WU = ~101k vB
min_fee_rate: 1000, // 1 sat/vB minimum
}
}
}
impl PackageId {
/// Calculate package ID per BIP331: SHA256 of wtxids concatenated in lexicographical order.
/// When witnesses is None (typical for packages), wtxid == txid for non-SegWit transactions.
pub fn from_transactions(
transactions: &[Transaction],
witnesses: Option<&[Option<Vec<Vec<u8>>>]>,
) -> Self {
let mut wtxids: Vec<Hash> = transactions
.iter()
.enumerate()
.map(|(i, tx)| {
let w = witnesses.and_then(|w| w.get(i)).and_then(|o| o.as_ref());
match w {
Some(v) => {
let arr = [v.clone()];
calculate_wtxid(tx, Some(&arr))
}
None => calculate_wtxid(tx, None),
}
})
.collect();
wtxids.sort(); // Lexicographical order (Hash is [u8; 32])
let mut hasher = Sha256::new();
for w in &wtxids {
hasher.update(w);
}
let hash_bytes = hasher.finalize();
let mut package_hash = [0u8; 32];
package_hash.copy_from_slice(&hash_bytes);
PackageId(package_hash)
}
}
impl TransactionPackage {
/// Create a new transaction package
pub fn new(transactions: Vec<Transaction>) -> Result<Self, PackageError> {
Self::new_with_utxo_set(transactions, None)
}
/// Create a new transaction package with UTXO set for fee calculation
pub fn new_with_utxo_set(
transactions: Vec<Transaction>,
utxo_set: Option<&blvm_protocol::UtxoSet>,
) -> Result<Self, PackageError> {
if transactions.is_empty() {
return Err(PackageError::EmptyPackage);
}
// Validate ordering (parents before children)
Self::validate_ordering(&transactions)?;
// Calculate package ID per BIP331: SHA256 of wtxids in lexicographical order
let package_id = PackageId::from_transactions(&transactions, None);
// Calculate combined fee from UTXO set if provided
let combined_fee = if let Some(utxo_set) = utxo_set {
// Calculate fee for each transaction: sum(inputs) - sum(outputs)
transactions
.iter()
.map(|tx| {
let input_total: u64 = tx
.inputs
.iter()
.filter_map(|inp| utxo_set.get(&inp.prevout))
.map(|utxo| utxo.value as u64)
.sum();
let output_total: u64 = tx.outputs.iter().map(|out| out.value as u64).sum();
input_total.saturating_sub(output_total)
})
.sum()
} else {
0 // Fee calculation requires UTXO set
};
// Calculate combined weight
let combined_weight: usize = transactions
.iter()
.map(|tx| {
// Simplified weight calculation
// In real implementation, would use proper witness weight
tx.inputs.len() * 68 + tx.outputs.len() * 31 + 10
})
.sum();
Ok(Self {
transactions,
package_id,
combined_fee,
combined_weight,
})
}
/// Validate transaction ordering (parents before children)
fn validate_ordering(transactions: &[Transaction]) -> Result<(), PackageError> {
// Build index of txids to position
let mut idx = std::collections::HashMap::new();
for (i, tx) in transactions.iter().enumerate() {
idx.insert(calculate_tx_id(tx), i);
}
// Check each transaction: inputs that reference in-package parents must be earlier
for (i, tx) in transactions.iter().enumerate() {
for input in &tx.inputs {
if let Some(&parent_pos) = idx.get(&input.prevout.hash) {
if parent_pos >= i {
return Err(PackageError::InvalidOrder);
}
}
}
}
Ok(())
}
/// Calculate package fee rate (sat/vB)
pub fn fee_rate(&self) -> f64 {
if self.combined_weight == 0 {
return 0.0;
}
// Convert weight to virtual bytes (weight / 4)
let vbytes = self.combined_weight as f64 / 4.0;
if vbytes == 0.0 {
return 0.0;
}
self.combined_fee as f64 / vbytes
}
}
impl Default for PackageRelay {
fn default() -> Self {
Self::new()
}
}
impl PackageRelay {
/// Create a new package relay manager
pub fn new() -> Self {
Self {
pending_packages: HashMap::new(),
validator: PackageValidator::default(),
}
}
/// Create package from transactions
pub fn create_package(
&self,
transactions: Vec<Transaction>,
) -> Result<TransactionPackage, PackageError> {
TransactionPackage::new(transactions)
}
/// Validate package against limits
pub fn validate_package(
&self,
package: &TransactionPackage,
) -> Result<(), PackageRejectReason> {
// Check package size
if package.transactions.len() > self.validator.max_package_size {
return Err(PackageRejectReason::TooManyTransactions);
}
// Check package weight
if package.combined_weight > self.validator.max_package_weight {
return Err(PackageRejectReason::WeightExceedsLimit);
}
// Check fee rate (if fee calculated)
if package.combined_fee > 0 {
let fee_rate = package.fee_rate();
if fee_rate < self.validator.min_fee_rate as f64 {
return Err(PackageRejectReason::FeeRateTooLow);
}
}
// Check for duplicates by txid
let mut seen = std::collections::HashSet::new();
for tx in &package.transactions {
let txid = calculate_tx_id(tx);
if !seen.insert(txid) {
return Err(PackageRejectReason::DuplicateTransactions);
}
}
// Validate ordering
TransactionPackage::validate_ordering(&package.transactions)
.map_err(|_| PackageRejectReason::InvalidOrder)?;
Ok(())
}
/// Register package for relay
pub fn register_package(
&mut self,
package: TransactionPackage,
) -> Result<PackageId, PackageError> {
// Validate package
self.validate_package(&package)
.map_err(PackageError::ValidationFailed)?;
let package_id = package.package_id;
let tx_count = package.transactions.len();
let now = crate::utils::current_timestamp();
let state = PackageState {
package,
received_at: now,
status: PackageStatus::Pending,
};
self.pending_packages.insert(package_id, state);
debug!(
"Registered package {} with {} transactions",
hex::encode(package_id.0),
tx_count
);
Ok(package_id)
}
/// Get package by ID
pub fn get_package(&self, package_id: &PackageId) -> Option<&TransactionPackage> {
self.pending_packages.get(package_id).map(|s| &s.package)
}
/// Mark package as accepted
pub fn mark_accepted(&mut self, package_id: &PackageId) {
if let Some(state) = self.pending_packages.get_mut(package_id) {
state.status = PackageStatus::Accepted;
info!("Package {} accepted", hex::encode(package_id.0));
}
}
/// Mark package as rejected
pub fn mark_rejected(&mut self, package_id: &PackageId, reason: PackageRejectReason) {
if let Some(state) = self.pending_packages.get_mut(package_id) {
state.status = PackageStatus::Rejected { reason };
warn!(
"Package {} rejected: {:?}",
hex::encode(package_id.0),
reason
);
}
}
/// Clean up old packages
pub fn cleanup_old_packages(&mut self, max_age: u64) {
let now = crate::utils::current_timestamp();
let expired: Vec<PackageId> = self
.pending_packages
.iter()
.filter(|(_, state)| now - state.received_at > max_age)
.map(|(id, _)| *id)
.collect();
for id in expired {
self.pending_packages.remove(&id);
debug!("Cleaned up expired package {}", hex::encode(id.0));
}
}
}
/// Package error
#[derive(Debug, thiserror::Error)]
pub enum PackageError {
#[error("Empty package (no transactions)")]
EmptyPackage,
#[error("Invalid transaction ordering (children before parents)")]
InvalidOrder,
#[error("Package validation failed: {0:?}")]
ValidationFailed(PackageRejectReason),
#[error("Package not found")]
PackageNotFound,
}