blockchain-zc-parser 0.1.0

Zero-copy, allocation-free parser for Bitcoin/blockchain binary data
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Bitcoin transaction parsing — zero-copy, no alloc.
//!
//! All structures borrow from the original input buffer via lifetime `'a`.

use crate::{
    cursor::Cursor,
    error::{ParseError, ParseResult},
    hash::Hash32,
    script::Script,
};

/// Maximum number of inputs / outputs per transaction (protocol sanity limit).
pub const MAX_IO_COUNT: usize = 100_000;
/// Maximum witness items per input.
pub const MAX_WITNESS_ITEMS: usize = 500;
/// Maximum witness item size.
pub const MAX_WITNESS_ITEM_SIZE: usize = 520;

// ---------------------------------------------------------------------------
// Outpoint
// ---------------------------------------------------------------------------

/// Reference to a specific output of a previous transaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutPoint<'a> {
    /// TXID of the referenced transaction (32 bytes, zero-copy).
    pub txid: Hash32<'a>,
    /// Index of the output in that transaction.
    pub vout: u32,
}

impl<'a> OutPoint<'a> {
    /// Parse from cursor (36 bytes total).
    #[inline]
    pub fn parse(c: &mut Cursor<'a>) -> ParseResult<Self> {
        let txid = Hash32(c.read_array::<32>()?);
        let vout = c.read_u32_le()?;
        Ok(Self { txid, vout })
    }

    /// `true` for a coinbase input (all-zero txid, vout = 0xffffffff).
    #[inline]
    pub fn is_coinbase(&self) -> bool {
        self.txid.as_bytes().iter().all(|&b| b == 0) && self.vout == 0xffff_ffff
    }
}

// ---------------------------------------------------------------------------
// TxInput
// ---------------------------------------------------------------------------

/// A transaction input, borrowing from the original buffer.
#[derive(Debug, Clone, Copy)]
pub struct TxInput<'a> {
    /// Previous output being spent.
    pub previous_output: OutPoint<'a>,
    /// Unlocking script (scriptSig). Empty for SegWit inputs.
    pub script_sig: Script<'a>,
    /// Sequence number.
    pub sequence: u32,
}

impl<'a> TxInput<'a> {
    /// Parse one input from the cursor.
    #[inline]
    pub fn parse(c: &mut Cursor<'a>) -> ParseResult<Self> {
        let previous_output = OutPoint::parse(c)?;
        let script_sig = Script::parse(c)?;
        let sequence = c.read_u32_le()?;
        Ok(Self {
            previous_output,
            script_sig,
            sequence,
        })
    }

    /// `true` if this is a coinbase input.
    #[inline]
    pub fn is_coinbase(&self) -> bool {
        self.previous_output.is_coinbase()
    }

    /// `true` if this input opts into Replace-By-Fee (BIP 125).
    #[inline]
    pub fn is_rbf(&self) -> bool {
        self.sequence <= 0xffff_fffd
    }
}

// ---------------------------------------------------------------------------
// TxOutput
// ---------------------------------------------------------------------------

/// A transaction output.
#[derive(Debug, Clone, Copy)]
pub struct TxOutput<'a> {
    /// Satoshi value (unsigned).
    pub value: u64,
    /// Locking script (scriptPubKey).
    pub script_pubkey: Script<'a>,
}

impl<'a> TxOutput<'a> {
    /// Parse one output from the cursor.
    #[inline]
    pub fn parse(c: &mut Cursor<'a>) -> ParseResult<Self> {
        let value = c.read_u64_le()?;
        let script_pubkey = Script::parse(c)?;
        Ok(Self {
            value,
            script_pubkey,
        })
    }
}

// ---------------------------------------------------------------------------
// Witness
// ---------------------------------------------------------------------------

/// SegWit witness data for a single input — a sequence of byte-string items.
///
/// Stores a zero-copy slice covering the entire witness field (item count
/// varint + all items), so items can be iterated on demand.
#[derive(Debug, Clone, Copy)]
pub struct Witness<'a> {
    /// Raw witness bytes starting at the item-count varint.
    pub raw: &'a [u8],
    /// Number of items decoded from the item-count varint.
    pub item_count: usize,
}

impl<'a> Witness<'a> {
    /// Parse one witness from `data` (which should start at the item-count varint).
    ///
    /// Returns the parsed [`Witness`] and the number of bytes consumed, so the
    /// caller can advance its cursor by exactly that amount.
    pub(crate) fn parse(data: &'a [u8]) -> ParseResult<(Self, usize)> {
        let mut c = Cursor::new(data);
        let item_count_u64 = c.read_varint()?;
        let item_count: usize =
            item_count_u64
                .try_into()
                .map_err(|_| ParseError::IntegerTooLarge {
                    value: item_count_u64,
                })?;
        if item_count > MAX_WITNESS_ITEMS {
            return Err(ParseError::OversizedData {
                size: item_count,
                max: MAX_WITNESS_ITEMS,
            });
        }
        for _ in 0..item_count {
            c.read_var_bytes(MAX_WITNESS_ITEM_SIZE)?;
        }
        let consumed = c.position();
        // SAFETY: `consumed` bytes were validated by the reads above,
        // so `data[..consumed]` is a valid sub-slice.
        let raw = unsafe { data.get_unchecked(..consumed) };
        Ok((Witness { raw, item_count }, consumed))
    }

    /// Iterate over items in this witness.
    pub fn items(&self) -> WitnessIter<'a> {
        // Skip the item_count varint at the start of `raw`.
        let mut skip_cursor = Cursor::new(self.raw);
        let _ = skip_cursor.read_varint(); // can't fail — we validated on parse
        WitnessIter {
            cursor: Cursor::new(&self.raw[skip_cursor.position()..]),
            remaining: self.item_count,
        }
    }
}

/// Iterator over witness items.
pub struct WitnessIter<'a> {
    cursor: Cursor<'a>,
    remaining: usize,
}

impl<'a> Iterator for WitnessIter<'a> {
    type Item = ParseResult<&'a [u8]>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.remaining == 0 {
            return None;
        }
        self.remaining -= 1;
        Some(self.cursor.read_var_bytes(MAX_WITNESS_ITEM_SIZE))
    }
}

// ---------------------------------------------------------------------------
// Transaction
// ---------------------------------------------------------------------------

/// A fully parsed Bitcoin transaction.
///
/// **Note:** This struct requires the caller to provide backing storage for the
/// `inputs`, `outputs`, and `witnesses` slices (e.g. stack arrays or arena
/// buffers). For allocation-free use, prefer [`TransactionParser`] with its
/// closure API.
///
/// All fields borrow from the original parse buffer — zero allocations in the
/// parser itself.
#[allow(dead_code)]
pub struct Transaction<'a> {
    /// Serialised version (1 or 2).
    pub version: i32,
    /// Whether this transaction uses the SegWit format (BIP 141).
    pub is_segwit: bool,
    /// Transaction inputs.
    pub inputs: &'a [TxInput<'a>],
    /// Transaction outputs.
    pub outputs: &'a [TxOutput<'a>],
    /// Witness data, one per input (empty slice for non-segwit).
    pub witnesses: &'a [Witness<'a>],
    /// Lock time.
    pub locktime: u32,
    /// Zero-copy reference to the raw bytes of this transaction.
    pub raw: &'a [u8],
}

impl core::fmt::Debug for Transaction<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Transaction")
            .field("version", &self.version)
            .field("is_segwit", &self.is_segwit)
            .field("input_count", &self.inputs.len())
            .field("output_count", &self.outputs.len())
            .field("locktime", &self.locktime)
            .field("raw_len", &self.raw.len())
            .finish()
    }
}

// ---------------------------------------------------------------------------
// Streaming parser (no alloc)
// ---------------------------------------------------------------------------

/// Streaming, callback-based transaction parser for `no_std` / zero-alloc use.
///
/// Instead of collecting inputs/outputs into a slice, this parser calls user-
/// supplied closures for each element, allowing the caller to decide how to
/// store or discard them.
///
/// ```rust
/// # use blockchain_zc_parser::transaction::TransactionParser;
/// # let raw = &[0u8; 0]; // placeholder
/// let mut parser = TransactionParser::new(raw);
/// // parser.parse_with(|input| { ... }, |output| { ... });
/// ```
pub struct TransactionParser<'a> {
    cursor: Cursor<'a>,
}

impl<'a> TransactionParser<'a> {
    /// Create a new parser positioned at the start of a raw transaction.
    pub fn new(data: &'a [u8]) -> Self {
        Self {
            cursor: Cursor::new(data),
        }
    }

    /// How many bytes have been consumed so far.
    ///
    /// Call this after [`parse_with`](Self::parse_with) to advance an outer
    /// cursor by exactly the number of bytes this transaction occupied.
    #[inline]
    pub fn bytes_consumed(&self) -> usize {
        self.cursor.position()
    }

    /// Parse the transaction, calling `on_input` for each input and
    /// `on_output` for each output.
    ///
    /// Witness data is skipped unless you need it (saves work for indexers).
    ///
    /// Returns `(version, locktime, input_count, output_count)`.
    pub fn parse_with<FI, FO>(
        &mut self,
        mut on_input: FI,
        mut on_output: FO,
    ) -> ParseResult<(i32, u32, usize, usize)>
    where
        FI: FnMut(TxInput<'a>) -> ParseResult<()>,
        FO: FnMut(TxOutput<'a>) -> ParseResult<()>,
    {
        let c = &mut self.cursor;
        let version = c.read_i32_le()?;

        // Local helper for varint parsing from first byte
        #[inline]
        fn varint_from_first<'a>(c: &mut Cursor<'a>, first: u8) -> ParseResult<u64> {
            Ok(match first {
                0x00..=0xfc => first as u64,
                0xfd => c.read_u16_le()? as u64,
                0xfe => c.read_u32_le()? as u64,
                0xff => c.read_u64_le()?,
            })
        }

        // Detect SegWit marker and read input count
        let first_byte = c.read_u8()?;
        let is_segwit = if first_byte == 0x00 {
            // Potential SegWit marker. In legacy format this would mean 0 inputs,
            // which is invalid; we treat 0x00 0x01 as SegWit, otherwise error.
            let flag = c.read_u8()?;
            if flag != 0x01 {
                return Err(ParseError::InvalidSegwitFlag(flag));
            }
            true
        } else {
            false
        };

        let input_count_u64 = if is_segwit {
            c.read_varint()?
        } else {
            // `first_byte` is the first byte of the input-count varint.
            varint_from_first(c, first_byte)?
        };

        let input_count: usize =
            input_count_u64
                .try_into()
                .map_err(|_| ParseError::IntegerTooLarge {
                    value: input_count_u64,
                })?;

        if input_count == 0 {
            return Err(ParseError::InvalidInputCount);
        }

        if input_count > MAX_IO_COUNT {
            return Err(ParseError::OversizedData {
                size: input_count,
                max: MAX_IO_COUNT,
            });
        }

        for _ in 0..input_count {
            let input = TxInput::parse(c)?;
            on_input(input)?;
        }

        let output_count_u64 = c.read_varint()?;
        let output_count: usize =
            output_count_u64
                .try_into()
                .map_err(|_| ParseError::IntegerTooLarge {
                    value: output_count_u64,
                })?;
        if output_count > MAX_IO_COUNT {
            return Err(ParseError::OversizedData {
                size: output_count,
                max: MAX_IO_COUNT,
            });
        }

        for _ in 0..output_count {
            let output = TxOutput::parse(c)?;
            on_output(output)?;
        }

        // Parse (and discard) witness data, using Witness::parse so the logic
        // lives in one place.  Callers that need witness access can use the
        // returned Witness values by adding an `on_witness` callback in future.
        if is_segwit {
            for _ in 0..input_count {
                let (_, consumed) = Witness::parse(c.as_slice())?;
                c.skip(consumed)?;
            }
        }

        let locktime = c.read_u32_le()?;
        Ok((version, locktime, input_count, output_count))
    }
}

#[cfg(test)]
mod tests {
    extern crate std;
    use super::*;
    use std::vec::Vec;

    /// Minimal valid non-segwit coinbase transaction.
    fn coinbase_tx_raw() -> Vec<u8> {
        let mut tx = Vec::new();
        // version
        tx.extend_from_slice(&1i32.to_le_bytes());
        // input count: 1
        tx.push(1);
        // outpoint: 32 zero bytes + 0xffffffff
        tx.extend_from_slice(&[0u8; 32]);
        tx.extend_from_slice(&0xffff_ffffu32.to_le_bytes());
        // scriptSig length (4 bytes) + arbitrary data
        tx.push(4);
        tx.extend_from_slice(&[0xde, 0xad, 0xbe, 0xef]);
        // sequence
        tx.extend_from_slice(&0xffff_ffffu32.to_le_bytes());
        // output count: 1
        tx.push(1);
        // value: 50 BTC in satoshis
        tx.extend_from_slice(&(50u64 * 100_000_000).to_le_bytes());
        // scriptPubKey: empty (non-standard, ok for test)
        tx.push(0);
        // locktime
        tx.extend_from_slice(&0u32.to_le_bytes());
        tx
    }

    #[test]
    fn parse_coinbase_streaming() {
        let raw = coinbase_tx_raw();
        let mut parser = TransactionParser::new(&raw);
        let mut inputs = 0usize;
        let mut outputs = 0usize;
        let mut saw_coinbase = false;

        let (version, locktime, in_count, out_count) = parser
            .parse_with(
                |inp| {
                    inputs += 1;
                    if inp.is_coinbase() {
                        saw_coinbase = true;
                    }
                    Ok(())
                },
                |_out| {
                    outputs += 1;
                    Ok(())
                },
            )
            .unwrap();

        assert_eq!(version, 1);
        assert_eq!(locktime, 0);
        assert_eq!(in_count, 1);
        assert_eq!(out_count, 1);
        assert_eq!(inputs, 1);
        assert_eq!(outputs, 1);
        assert!(saw_coinbase);
    }

    #[test]
    fn outpoint_coinbase_detection() {
        let raw = [0u8; 36];
        let mut raw = raw.to_vec();
        raw[32..].copy_from_slice(&0xffff_ffffu32.to_le_bytes());
        let mut c = Cursor::new(&raw);
        let op = OutPoint::parse(&mut c).unwrap();
        assert!(op.is_coinbase());
    }
}