bsv-tracker 0.6.0

A transaction evidence state machine with host supplied headers, proofs, hints and time
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! The compact byte form of one [`State`](crate::State) (bsv-stack-lean #92).
//!
//! Version 1, every field in order:
//!
//! | field | encoding |
//! | --- | --- |
//! | version | one byte, `1` |
//! | txid | an id |
//! | word | a tag byte (`0` unknown, `1` built, `2` announced, `3` seen, `4` mined, `5` stale, `6` rejected and its reason, `7` conflicted and its ids, `8` abandoned) |
//! | evidence | `0`, or `1` and the checked proof's txid (an id), path, header hash and root (ids), root (an id) and depth (a varint) |
//! | suspect | `0` or `1` |
//! | hints | a varint count, then each hint, the newest at the front |
//! | last hint time | `0`, or `1` and a varint |
//! | re-ask | `0`, or `1` and a tag byte (`0` reorg, `1` fork, `2` hint, `3` proof failed, `4` recheck, `5` age, `6` spend) and its payload |
//!
//! A varint is unsigned LEB128 in its shortest form. A string is a varint
//! length and its UTF-8 bytes. An id is `0` and 32 raw bytes when the text is
//! 64 lowercase hex digits, else `1` and a string. A hint is its source (a
//! string), its status (a tag byte in [`HintStatus`] declaration order and its
//! payload), its observed time (a varint) and its verdict (`0`, or `1` and the
//! status, the optional height, the optional reason and the competitor ids).
//! A path is its block height, a varint count of levels, and per level a
//! varint count of leaves, each an offset (a varint), a flag byte (`1`
//! duplicate, `2` txid, `4` a hash follows) and the hash (an id).
//!
//! The decoder accepts only the bytes the encoder writes: a non-canonical
//! varint, an id written as text that has the raw form, a flag outside its set
//! or trailing bytes are each refused.

use crate::{
    CheckError, CheckedProof, Header, Height, Hint, HintStatus, Proof, Reask, Timestamp, TxId,
    Verdict, Word,
};
use bsv_rs::transaction::{MerklePath, MerklePathLeaf};
use std::fmt;

/// The version byte [`State::to_compact`](crate::State::to_compact) writes.
pub const COMPACT_VERSION: u8 = 1;

/// Bytes [`State::from_compact`](crate::State::from_compact) refuses. A
/// refusal restores nothing; the host re-tracks the transaction.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CompactError {
    /// The version byte is not one this crate reads.
    UnknownVersion(u8),
    /// The bytes end inside a field.
    Truncated,
    /// Bytes remain after the last field.
    TrailingBytes,
    /// A tag, flag, varint, id or string the encoder never writes.
    Malformed(&'static str),
    /// The stored proof record does not hold together: its path does not
    /// reduce, it names another transaction, or its root differs from its
    /// header's. Nothing here consults the host's headers.
    Evidence(CheckError),
}
impl fmt::Display for CompactError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}
impl std::error::Error for CompactError {}

/// The fields of a state, as the codec reads and writes them.
pub(crate) struct Parts {
    pub txid: TxId,
    /// None is the mined word, backed by the evidence field.
    pub word: Option<Word>,
    pub evidence: Option<CheckedProof>,
    pub suspect: bool,
    pub hints: Vec<Hint>,
    pub last_hint_at: Option<Timestamp>,
    pub reask: Option<Reask>,
}

pub(crate) struct Fields<'a> {
    pub txid: &'a str,
    pub word: &'a Word,
    pub evidence: Option<&'a CheckedProof>,
    pub suspect: bool,
    pub hints: &'a [Hint],
    pub last_hint_at: Option<Timestamp>,
    pub reask: Option<&'a Reask>,
}

pub(crate) fn encode(fields: &Fields<'_>) -> Vec<u8> {
    let mut w = Writer(vec![COMPACT_VERSION]);
    w.id(fields.txid);
    w.word(fields.word);
    match fields.evidence {
        None => w.byte(0),
        Some(checked) => {
            w.byte(1);
            w.id(checked.proof().txid());
            w.path(checked.proof().path());
            w.id(&checked.header().hash);
            w.id(&checked.header().merkle_root);
            w.id(checked.root());
            w.varint(checked.depth());
        }
    }
    w.flag(fields.suspect);
    w.len(fields.hints.len());
    for hint in fields.hints {
        w.hint(hint);
    }
    w.option(fields.last_hint_at, Writer::varint);
    match fields.reask {
        None => w.byte(0),
        Some(reask) => {
            w.byte(1);
            w.reask(reask);
        }
    }
    w.0
}

pub(crate) fn decode(bytes: &[u8]) -> Result<Parts, CompactError> {
    let mut r = Reader(bytes);
    let version = r.byte()?;
    if version != COMPACT_VERSION {
        return Err(CompactError::UnknownVersion(version));
    }
    let txid = r.id()?;
    if txid != txid.to_ascii_lowercase() {
        return Err(CompactError::Malformed("txid not lowercase"));
    }
    let word = r.word()?;
    let evidence = if r.flag()? {
        let proof_txid = r.id()?;
        let path = r.path()?;
        let header = Header {
            hash: r.id()?,
            merkle_root: r.id()?,
        };
        let root = r.id()?;
        let depth = r.varint()?;
        let proof = Proof::new(proof_txid, path).map_err(CompactError::Evidence)?;
        if proof.txid() != txid {
            return Err(CompactError::Evidence(CheckError::TxidMismatch));
        }
        Some(CheckedProof::restored(proof, header, root, depth).map_err(CompactError::Evidence)?)
    } else {
        None
    };
    let suspect = r.flag()?;
    let count = r.count()?;
    let mut hints = Vec::with_capacity(count);
    for _ in 0..count {
        hints.push(r.hint()?);
    }
    let last_hint_at = r.option(Reader::varint)?;
    let reask = r.option(Reader::reask)?;
    if !r.0.is_empty() {
        return Err(CompactError::TrailingBytes);
    }
    Ok(Parts {
        txid,
        word,
        evidence,
        suspect,
        hints,
        last_hint_at,
        reask,
    })
}

struct Writer(Vec<u8>);
impl Writer {
    fn byte(&mut self, b: u8) {
        self.0.push(b);
    }
    fn flag(&mut self, b: bool) {
        self.byte(u8::from(b));
    }
    fn varint(&mut self, mut n: u64) {
        loop {
            // The low seven bits; the mask makes the truncation exact.
            #[allow(clippy::cast_possible_truncation)]
            let low = (n & 0x7f) as u8;
            n >>= 7;
            if n == 0 {
                self.byte(low);
                return;
            }
            self.byte(low | 0x80);
        }
    }
    fn len(&mut self, n: usize) {
        self.varint(n as u64);
    }
    fn height(&mut self, h: Height) {
        self.varint(u64::from(h));
    }
    fn string(&mut self, s: &str) {
        self.len(s.len());
        self.0.extend_from_slice(s.as_bytes());
    }
    fn id(&mut self, s: &str) {
        if let Some(raw) = raw_id(s) {
            self.byte(0);
            self.0.extend_from_slice(&raw);
        } else {
            self.byte(1);
            self.string(s);
        }
    }
    fn ids(&mut self, ids: &[TxId]) {
        self.len(ids.len());
        for id in ids {
            self.id(id);
        }
    }
    fn option<T>(&mut self, value: Option<T>, put: impl FnOnce(&mut Self, T)) {
        match value {
            None => self.byte(0),
            Some(v) => {
                self.byte(1);
                put(self, v);
            }
        }
    }
    fn word(&mut self, word: &Word) {
        match word {
            Word::Unknown => self.byte(0),
            Word::Built => self.byte(1),
            Word::Announced => self.byte(2),
            Word::Seen => self.byte(3),
            // The checked proof is the evidence field; a mined word shares it.
            Word::Mined(_) => self.byte(4),
            Word::Stale => self.byte(5),
            Word::Rejected { reason } => {
                self.byte(6);
                self.string(reason);
            }
            Word::Conflicted { competitors } => {
                self.byte(7);
                self.ids(competitors);
            }
            Word::Abandoned => self.byte(8),
        }
    }
    fn path(&mut self, path: &MerklePath) {
        self.height(path.block_height);
        self.len(path.path.len());
        for level in &path.path {
            self.len(level.len());
            for leaf in level {
                self.varint(leaf.offset);
                let flags = u8::from(leaf.duplicate)
                    | (u8::from(leaf.txid) << 1)
                    | (u8::from(leaf.hash.is_some()) << 2);
                self.byte(flags);
                if let Some(hash) = &leaf.hash {
                    self.id(hash);
                }
            }
        }
    }
    fn status(&mut self, status: &HintStatus) {
        match status {
            HintStatus::Accepted => self.byte(0),
            HintStatus::Seen => self.byte(1),
            HintStatus::Mined { height } => {
                self.byte(2);
                self.height(*height);
            }
            HintStatus::StaleBlock => self.byte(3),
            HintStatus::Rejected { reason } => {
                self.byte(4);
                self.string(reason);
            }
            HintStatus::DoubleSpend { competitors } => {
                self.byte(5);
                self.ids(competitors);
            }
            HintStatus::OrphanMempool => self.byte(6),
            HintStatus::Unknown => self.byte(7),
            HintStatus::MempoolPresent { source } => {
                self.byte(8);
                self.string(source);
            }
        }
    }
    fn hint(&mut self, hint: &Hint) {
        self.string(&hint.source);
        self.status(&hint.status);
        self.varint(hint.observed);
        self.option(hint.verdict.as_ref(), |w, v| {
            w.string(&v.status);
            w.option(v.height, Self::height);
            w.option(v.reason.as_deref(), Self::string);
            w.ids(&v.competitors);
        });
    }
    fn reask(&mut self, reask: &Reask) {
        match reask {
            Reask::Reorg { fork_height } => {
                self.byte(0);
                self.height(*fork_height);
            }
            Reask::Fork { height } => {
                self.byte(1);
                self.height(*height);
            }
            Reask::Hint(hint) => {
                self.byte(2);
                self.hint(hint);
            }
            Reask::ProofFailed { height } => {
                self.byte(3);
                self.height(*height);
            }
            Reask::Recheck => self.byte(4),
            Reask::Age => self.byte(5),
            Reask::Spend => self.byte(6),
        }
    }
}

struct Reader<'a>(&'a [u8]);
impl Reader<'_> {
    fn take(&mut self, n: usize) -> Result<&[u8], CompactError> {
        if n > self.0.len() {
            return Err(CompactError::Truncated);
        }
        let (head, rest) = self.0.split_at(n);
        self.0 = rest;
        Ok(head)
    }
    fn byte(&mut self) -> Result<u8, CompactError> {
        let (&b, rest) = self.0.split_first().ok_or(CompactError::Truncated)?;
        self.0 = rest;
        Ok(b)
    }
    fn flag(&mut self) -> Result<bool, CompactError> {
        match self.byte()? {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(CompactError::Malformed("flag")),
        }
    }
    fn varint(&mut self) -> Result<u64, CompactError> {
        let mut n = 0u64;
        for shift in (0..64).step_by(7) {
            let b = self.byte()?;
            let low = u64::from(b & 0x7f);
            if shift == 63 && low > 1 {
                return Err(CompactError::Malformed("varint overflow"));
            }
            n |= low << shift;
            if b & 0x80 == 0 {
                if b == 0 && shift > 0 {
                    return Err(CompactError::Malformed("varint not shortest"));
                }
                return Ok(n);
            }
        }
        Err(CompactError::Malformed("varint overflow"))
    }
    fn height(&mut self) -> Result<Height, CompactError> {
        Height::try_from(self.varint()?).map_err(|_| CompactError::Malformed("height"))
    }
    /// A count of items, each at least one byte, so a count beyond the
    /// remaining bytes is refused before anything is allocated.
    fn count(&mut self) -> Result<usize, CompactError> {
        let n = usize::try_from(self.varint()?).map_err(|_| CompactError::Truncated)?;
        if n > self.0.len() {
            return Err(CompactError::Truncated);
        }
        Ok(n)
    }
    fn string(&mut self) -> Result<String, CompactError> {
        let n = self.count()?;
        let bytes = self.take(n)?;
        String::from_utf8(bytes.to_vec()).map_err(|_| CompactError::Malformed("utf-8"))
    }
    fn id(&mut self) -> Result<String, CompactError> {
        match self.byte()? {
            0 => {
                const HEX: &[u8; 16] = b"0123456789abcdef";
                let mut s = String::with_capacity(64);
                for b in self.take(32)? {
                    s.push(char::from(HEX[usize::from(b >> 4)]));
                    s.push(char::from(HEX[usize::from(b & 0xf)]));
                }
                Ok(s)
            }
            1 => {
                let s = self.string()?;
                if raw_id(&s).is_some() {
                    return Err(CompactError::Malformed("id not in its raw form"));
                }
                Ok(s)
            }
            _ => Err(CompactError::Malformed("id tag")),
        }
    }
    fn ids(&mut self) -> Result<Vec<TxId>, CompactError> {
        let n = self.count()?;
        (0..n).map(|_| self.id()).collect()
    }
    fn option<T>(
        &mut self,
        get: impl FnOnce(&mut Self) -> Result<T, CompactError>,
    ) -> Result<Option<T>, CompactError> {
        if self.flag()? {
            get(self).map(Some)
        } else {
            Ok(None)
        }
    }
    /// None is the mined word; the state attaches the evidence field's proof.
    fn word(&mut self) -> Result<Option<Word>, CompactError> {
        Ok(Some(match self.byte()? {
            0 => Word::Unknown,
            1 => Word::Built,
            2 => Word::Announced,
            3 => Word::Seen,
            4 => return Ok(None),
            5 => Word::Stale,
            6 => Word::Rejected {
                reason: self.string()?,
            },
            7 => Word::Conflicted {
                competitors: self.ids()?,
            },
            8 => Word::Abandoned,
            _ => return Err(CompactError::Malformed("word tag")),
        }))
    }
    fn path(&mut self) -> Result<MerklePath, CompactError> {
        let block_height = self.height()?;
        let levels = self.count()?;
        let mut path = Vec::with_capacity(levels);
        for _ in 0..levels {
            let n = self.count()?;
            let mut level = Vec::with_capacity(n);
            for _ in 0..n {
                let offset = self.varint()?;
                let flags = self.byte()?;
                if flags & !0b111 != 0 {
                    return Err(CompactError::Malformed("leaf flags"));
                }
                let hash = if flags & 4 == 0 {
                    None
                } else {
                    Some(self.id()?)
                };
                level.push(MerklePathLeaf {
                    offset,
                    hash,
                    txid: flags & 2 != 0,
                    duplicate: flags & 1 != 0,
                });
            }
            path.push(level);
        }
        Ok(MerklePath { block_height, path })
    }
    fn status(&mut self) -> Result<HintStatus, CompactError> {
        Ok(match self.byte()? {
            0 => HintStatus::Accepted,
            1 => HintStatus::Seen,
            2 => HintStatus::Mined {
                height: self.height()?,
            },
            3 => HintStatus::StaleBlock,
            4 => HintStatus::Rejected {
                reason: self.string()?,
            },
            5 => HintStatus::DoubleSpend {
                competitors: self.ids()?,
            },
            6 => HintStatus::OrphanMempool,
            7 => HintStatus::Unknown,
            8 => HintStatus::MempoolPresent {
                source: self.string()?,
            },
            _ => return Err(CompactError::Malformed("hint status tag")),
        })
    }
    fn hint(&mut self) -> Result<Hint, CompactError> {
        let source = self.string()?;
        let status = self.status()?;
        let observed = self.varint()?;
        let verdict = self.option(|r| {
            Ok(Verdict {
                status: r.string()?,
                height: r.option(Self::height)?,
                reason: r.option(Self::string)?,
                competitors: r.ids()?,
            })
        })?;
        Ok(Hint {
            source,
            status,
            observed,
            verdict,
        })
    }
    fn reask(&mut self) -> Result<Reask, CompactError> {
        Ok(match self.byte()? {
            0 => Reask::Reorg {
                fork_height: self.height()?,
            },
            1 => Reask::Fork {
                height: self.height()?,
            },
            2 => Reask::Hint(self.hint()?),
            3 => Reask::ProofFailed {
                height: self.height()?,
            },
            4 => Reask::Recheck,
            5 => Reask::Age,
            6 => Reask::Spend,
            _ => return Err(CompactError::Malformed("re-ask tag")),
        })
    }
}

/// The 32 bytes of 64 lowercase hex digits, in text order.
fn raw_id(s: &str) -> Option<[u8; 32]> {
    fn nibble(c: u8) -> Option<u8> {
        match c {
            b'0'..=b'9' => Some(c - b'0'),
            b'a'..=b'f' => Some(c - b'a' + 10),
            _ => None,
        }
    }
    let bytes = s.as_bytes();
    if bytes.len() != 64 {
        return None;
    }
    let mut raw = [0u8; 32];
    for (out, pair) in raw.iter_mut().zip(bytes.as_chunks::<2>().0) {
        *out = (nibble(pair[0])? << 4) | nibble(pair[1])?;
    }
    Some(raw)
}