sidestr-core 0.4.3

sidestr user-activated sidechains beside a Bitcoin-family parent: the chain document, the parents table, signed blocks generic over the header family (BIP-325 challenge, no subsidy), the peg-in claim and peg-out burn rules, Knots' unified sighash, the block file and an in-memory validating chain. A port of siding by Melvin Carvalho.
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! The chain document (SPEC 3, 5): the overlay that defines a sidestr chain
//! beside its parent, and the chain's identity.
//!
//! The document is what `siding new` writes and what every validator reads.
//! It is the chain's identity: `genesisHash` is derived from it (the genesis
//! commits to the chain's alias, the pegs, `genesisTime` and the signer's
//! witness), and a validator refuses to proceed past a block 0 that does not
//! hash to it. Changing any sealed field is a new chain, never a
//! configuration edit.
//!
//! # Two names (SPEC 3 and 11, 0.0.5)
//!
//! Since spec 0.0.5 the document is published as a Nostr event of kind 3500,
//! regular and immutable, and **its event id is the chain's hash**: the one
//! value that names this chain and no other, which a tip announcement points
//! at, a client verifies the document against, and anything that must commit
//! to a chain uses (a peg-in tweak; a nested chain's `parent`). The event's
//! author is the signer, so the published document carries no `signer`
//! field; `genesisHash` stays as a cross-check, never the identity. A
//! document cannot contain its own hash, so inside it (the genesis's `chain`
//! field) and in everything written before the hash exists (tags, the
//! `pegin:` and `pegout:` `OP_RETURN`s, a rule document's `d`) the chain is
//! named by its **alias**, `id` = `sidestr:<name>`, which is a name for
//! people and not a proof. Building, reading and resolving the event is
//! `sidestr-nostr`'s `chain` module; this type is the document either way.
//!
//! A chain made before 0.0.5 (the live `sidestr:dreamlab` among them) keeps
//! its `chain.json` with `signer` and is resolved as before until its signer
//! publishes the event; [`ChainDocument::signer`] is therefore kept, and a
//! document read back from an event has it filled from the event's author,
//! so [`ChainDocument::validate`] still checks the challenge against it.
//!
//! Every field siding reads is parsed and checked here. Fields siding does not
//! read (an operator's `depth`, `containment`, `containmentDigest`, a
//! `comment`, a peg's `parentAddress`) are carried through untouched in
//! [`ChainDocument::extra`] so a document round-trips, and none of them alter
//! the genesis.
//!
//! ```
//! use sidestr_core::document::{ChainDocument, magic_for};
//!
//! let doc = ChainDocument::from_json(r#"{
//!   "id": "sidestr:trial", "name": "trial", "parent": "tbtc4",
//!   "challenge": "512098b4e74305dac5ce76d5bee8e57a71549a27618a0e51b3bada3074fcba02325b",
//!   "powLimit": "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
//!   "addressPrefix": "trl", "magic": "a8f6706f", "genesisTime": 1790076612, "pegs": [],
//!   "signer": "98b4e74305dac5ce76d5bee8e57a71549a27618a0e51b3bada3074fcba02325b",
//!   "depth": 0
//! }"#).unwrap();
//! assert_eq!(doc.parent().unwrap().alias, "tbtc4");
//! assert_eq!(doc.peg_confirmations, 6);           // the SPEC default
//! assert_eq!(magic_for("trial"), "a8f6706f");     // what `siding new` derived
//! assert_eq!(doc.extra["depth"], 0);              // an estate field, carried through
//! ```

use std::collections::BTreeMap;

use bitcoin::{CompactTarget, ScriptBuf, Target};
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::federation::Federation;
use crate::parents::{resolve_parent, resolve_parent_with, Family, Parent, ParentRef};

/// One optional consensus rule named by a chain document.
///
/// The wire form is either the original string (`"assets"`) or an object
/// (`{"name":"markets","from":120}`) when a running chain adopts a rule
/// from a particular height.  The object form is preserved when the document
/// is serialised again, including when `from` is zero.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RuleEntry {
    /// A rule active from genesis.
    Name(String),
    /// A rule adopted from `from` (inclusive).
    Activated {
        /// The rule's registered name.
        name: String,
        /// The first height at which the rule applies.
        #[serde(default)]
        from: u32,
    },
}

impl RuleEntry {
    /// The registered rule name.
    pub fn name(&self) -> &str {
        match self {
            Self::Name(name) | Self::Activated { name, .. } => name,
        }
    }

    /// The first height at which the rule applies; string entries start at
    /// genesis.
    pub fn from(&self) -> u32 {
        match self {
            Self::Name(_) => 0,
            Self::Activated { from, .. } => *from,
        }
    }
}

impl From<&str> for RuleEntry {
    fn from(value: &str) -> Self {
        Self::Name(value.to_owned())
    }
}

impl From<String> for RuleEntry {
    fn from(value: String) -> Self {
        Self::Name(value)
    }
}

impl PartialEq<str> for RuleEntry {
    fn eq(&self, other: &str) -> bool {
        self.name() == other
    }
}

impl PartialEq<&str> for RuleEntry {
    fn eq(&self, other: &&str) -> bool {
        self.name() == *other
    }
}

impl core::fmt::Display for RuleEntry {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(self.name())
    }
}

/// A peg output the chain starts from (SPEC 5): the genesis coinbase pays
/// `script` exactly `amount`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Peg {
    /// Parent txid of the peg output, display order.
    pub txid: String,
    /// Output index on the parent.
    pub vout: u32,
    /// Amount in sats.
    pub amount: u64,
    /// The sidechain output script (hex) the coins appear on.
    pub script: String,
    /// Anything else the document says about the peg (`parentAddress`, …).
    #[serde(flatten)]
    pub extra: BTreeMap<String, serde_json::Value>,
}

/// The chain document (SPEC 3). Field names are the wire names.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChainDocument {
    /// The chain's alias, `sidestr:<name>`: what tags, `OP_RETURN`s, rule
    /// documents and the genesis name the chain by. Not its identity, which
    /// is the chain's hash, the id of the document's kind-3500 event (SPEC
    /// 0.0.5).
    pub id: String,
    /// The short name, the alias without `sidestr:` (SPEC 3, 0.0.5: "a short
    /// alias for people and tags; not an identity"); the key file and state
    /// directory are named after it.
    pub name: String,
    /// The parent: a SPEC 3.2 alias or an accepted long id. SPEC 0.0.5 lets
    /// a nested chain name a sidestr chain's hash here. Upstream's
    /// `parents.mjs` (at `e8deb63`) resolves no hash, and neither do
    /// [`ChainDocument::parent`] and [`ChainDocument::validate`], which refuse
    /// such a document as [`Error::UnknownParent`] as siding does;
    /// [`ChainDocument::parent_with`] and [`ChainDocument::validate_nested`]
    /// resolve it through the parent's chain event, a recorded departure
    /// ([`crate::parents`]).
    pub parent: String,
    /// The challenge script (hex); a block is valid when its witness satisfies it.
    pub challenge: String,
    /// The proof-of-work limit as 64 hex characters; no retarget.
    pub pow_limit: String,
    /// The bech32m prefix, distinct from the parent's.
    pub address_prefix: String,
    /// The network magic `siding new` derives from the name; absent on the very first chains.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub magic: Option<String>,
    /// Parent confirmations before a peg-in may be claimed.
    #[serde(default = "default_peg_confirmations")]
    pub peg_confirmations: u32,
    /// The relative timelock on every peg output's refund path.
    #[serde(default = "default_refund_blocks")]
    pub refund_blocks: u32,
    /// Parent blocks within which a burn must be paid.
    #[serde(default = "default_pegout_blocks")]
    pub pegout_blocks: u32,
    /// The least a burn may carry, in sats.
    #[serde(default = "default_pegout_min")]
    pub pegout_min: u64,
    /// Producer policy, published so a wallet can compute it: sat/vB.
    #[serde(default = "default_min_fee_rate")]
    pub min_fee_rate: u64,
    /// The genesis block's time.
    pub genesis_time: u32,
    /// The peg outputs the chain starts from.
    #[serde(default)]
    pub pegs: Vec<Peg>,
    /// The signer's x-only public key (level 1). A document published as a
    /// chain event (SPEC 0.0.5) carries none, its event's author being the
    /// signer; read back from the event, it is filled from that author. A
    /// `chain.json` made before 0.0.5 carries it, and that is how such a
    /// chain is still resolved.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signer: Option<String>,
    /// The genesis hash, set once the genesis is sealed: a cross-check, not
    /// the chain's identity (SPEC 5, 0.0.5).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub genesis_hash: Option<String>,
    /// Rules the chain names beyond the core (`assets`, `pool`, `markets`,
    /// `evm`), optionally with an activation height; a validator must carry
    /// every one ([`ChainDocument::validate_with`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rules: Option<Vec<RuleEntry>>,
    /// Level 2: the signers' x-only public keys, in leaf order. With
    /// `threshold`, they derive the challenge ([`Federation::for_document`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signers: Option<Vec<String>>,
    /// Level 2: how many signers a block needs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub threshold: Option<u32>,
    /// Everything else, carried through untouched.
    #[serde(flatten)]
    pub extra: BTreeMap<String, serde_json::Value>,
}

fn default_peg_confirmations() -> u32 {
    6
}
fn default_refund_blocks() -> u32 {
    10_000
}
fn default_pegout_blocks() -> u32 {
    144
}
fn default_pegout_min() -> u64 {
    10_000
}
fn default_min_fee_rate() -> u64 {
    1
}

/// The network magic `siding new` derives from a chain name
/// (`siding/bin/siding.mjs`): a 32-bit polynomial hash of `sidestr:<name>`,
/// seed 7, multiplier 31, as eight hex characters.
pub fn magic_for(name: &str) -> String {
    let h = format!("sidestr:{name}")
        .bytes()
        .fold(7u32, |h, b| h.wrapping_mul(31).wrapping_add(u32::from(b)));
    format!("{h:08x}")
}

fn is_hex(s: &str, len: usize) -> bool {
    s.len() == len && s.bytes().all(|b| b.is_ascii_hexdigit())
}

impl ChainDocument {
    /// Parse and validate a document from its JSON text.
    pub fn from_json(text: &str) -> Result<Self> {
        Self::from_json_with(text, &[])
    }

    /// [`ChainDocument::from_json`] for a validator that carries the rules
    /// named in `carried` ([`ChainDocument::validate_with`]).
    pub fn from_json_with(text: &str, carried: &[&str]) -> Result<Self> {
        let doc: Self = serde_json::from_str(text)?;
        doc.validate_with(carried)?;
        Ok(doc)
    }

    /// The document as JSON, one-space indented as `siding new` writes it.
    pub fn to_json(&self) -> Result<String> {
        let mut out = Vec::new();
        let fmt = serde_json::ser::PrettyFormatter::with_indent(b" ");
        let mut ser = serde_json::Serializer::with_formatter(&mut out, fmt);
        self.serialize(&mut ser)?;
        let mut s = String::from_utf8(out).map_err(|e| Error::Encoding(e.to_string()))?;
        s.push('\n');
        Ok(s)
    }

    /// Check every field this validator reads. A document naming a rule or a
    /// level this crate does not carry is refused here, as `loadEngine` refuses
    /// it, so a validator never runs a chain it would misjudge. The parent's
    /// header family is not judged here: both families are carried, and a
    /// state instantiated for the other one refuses the document itself.
    ///
    /// The core rules are all this crate carries, so a document naming any
    /// rule is refused; [`ChainDocument::validate_with`] is the same check
    /// for a validator that carries further rules.
    pub fn validate(&self) -> Result<()> {
        self.validate_with(&[])
    }

    /// [`ChainDocument::validate`] for a validator that carries the rules
    /// named in `carried` beside the core (`["assets", "evm"]` with
    /// `sidestr-evm`): every rule the document names must be one of them
    /// (`siding/lib/overlays/index.mjs rulesFor`), and the pool rule needs the
    /// assets rule. A state built with rules
    /// ([`crate::state::StateOf::from_genesis_with_rules`]) checks its
    /// document this way with the names its rules answer to
    /// ([`crate::rules::BlockRule::name`]).
    ///
    /// ```
    /// use sidestr_core::document::ChainDocument;
    ///
    /// let mut doc: serde_json::Value = serde_json::from_str(include_str!("../fixtures/trial/chain.json")).unwrap();
    /// doc["rules"] = serde_json::json!(["evm"]);
    /// let doc: ChainDocument = serde_json::from_value(doc).unwrap();
    /// assert!(doc.validate().unwrap_err().to_string().contains("does not have"));
    /// assert!(doc.validate_with(&["assets", "evm"]).is_ok());
    /// ```
    pub fn validate_with(&self, carried: &[&str]) -> Result<()> {
        self.parent()?;
        self.validate_fields(carried)
    }

    /// [`ChainDocument::validate_with`] for a nested chain: the parent is
    /// resolved with [`ChainDocument::parent_with`], so a `parent` that is a
    /// sidestr chain's hash is accepted when `lookup` answers with that
    /// chain's document and its own parents resolve down to a row of the
    /// table. Every other check is [`ChainDocument::validate_with`]'s. A
    /// departure from siding at `e8deb63`, which refuses the hash
    /// ([`crate::parents`]).
    ///
    /// ```
    /// use sidestr_core::document::ChainDocument;
    ///
    /// let siding = ChainDocument::from_json(include_str!("../fixtures/trial/chain.json")).unwrap();
    /// let child = ChainDocument { parent: "5d".repeat(32), ..siding.clone() };
    /// assert!(child.validate().is_err()); // as siding refuses it
    /// assert!(child.validate_nested(&[], |_| Ok(siding.clone())).is_ok());
    /// ```
    pub fn validate_nested<F>(&self, carried: &[&str], lookup: F) -> Result<()>
    where
        F: FnMut(&str) -> Result<ChainDocument>,
    {
        self.parent_with(lookup)?;
        self.validate_fields(carried)
    }

    fn validate_fields(&self, carried: &[&str]) -> Result<()> {
        let bad = |m: String| Err(Error::Document(m));
        if self.id.is_empty() || self.name.is_empty() {
            return bad("id and name are required".into());
        }
        if self.challenge.len() < 2
            || self.challenge.len() % 2 != 0
            || !is_hex(&self.challenge, self.challenge.len())
        {
            return bad("challenge must be a non-empty hex script".into());
        }
        if !is_hex(&self.pow_limit, 64) {
            return bad("powLimit must be 64 hex characters".into());
        }
        if self.address_prefix.is_empty()
            || self.address_prefix.len() > 8
            || !self.address_prefix.bytes().all(|b| b.is_ascii_lowercase())
        {
            return bad("addressPrefix is 1 to 8 lower-case letters".into());
        }
        if let Some(m) = &self.magic {
            if !is_hex(m, 8) {
                return bad("magic must be 8 hex characters".into());
            }
        }
        if let Some(h) = &self.genesis_hash {
            if !is_hex(h, 64) {
                return bad("genesisHash must be 64 hex characters".into());
            }
        }
        for (i, p) in self.pegs.iter().enumerate() {
            if !is_hex(&p.txid, 64)
                || p.script.is_empty()
                || p.script.len() % 2 != 0
                || !is_hex(&p.script, p.script.len())
            {
                return bad(format!(
                    "peg {i}: txid must be 64 hex characters and script a hex script"
                ));
            }
        }
        if let Some(s) = &self.signer {
            if !is_hex(s, 64) {
                return bad("signer must be a 64-hex x-only public key".into());
            }
            let c = self.challenge.to_ascii_lowercase();
            if c.starts_with("5120") && c.len() == 68 && c[4..] != s.to_ascii_lowercase() {
                return bad("signer is not the key the challenge names".into());
            }
        }
        if let Some(rules) = &self.rules {
            if let Some(r) = rules
                .iter()
                .find(|r| !r.name().is_empty() && !carried.contains(&r.name()))
            {
                return bad(if carried.is_empty() {
                    format!("chain {} names rule \"{r}\", which this validator does not have (sidestr-core carries the core rules only)", self.id)
                } else {
                    format!(
                        "chain {} names rule \"{r}\", which this validator does not have (it carries {})",
                        self.id,
                        carried.join(", ")
                    )
                });
            }
            if rules.iter().any(|r| r == "pool") && !rules.iter().any(|r| r == "assets") {
                return bad("the pool rule needs the assets rule".into());
            }
            if rules.iter().any(|r| r == "markets") && !rules.iter().any(|r| r == "assets") {
                return bad("the markets rule needs the assets rule".into());
            }
        }
        // level 2: the challenge is named only through signers and threshold (overlay.mjs checkFederation)
        Federation::for_document(self)?;
        Ok(())
    }

    /// The parent's table row.
    pub fn parent(&self) -> Result<&'static Parent> {
        resolve_parent(&self.parent)
    }

    /// The header family the chain inherits.
    pub fn family(&self) -> Result<Family> {
        Ok(self.parent()?.family)
    }

    /// The parent, a row of the table or a sidestr chain by its hash
    /// ([`resolve_parent_with`]): `lookup` answers a hash with that chain's
    /// document, read from its verified chain event. A departure from
    /// siding at `e8deb63` for a hash ([`crate::parents`]); for an alias the
    /// same answer as [`ChainDocument::parent`].
    pub fn parent_with<F>(&self, lookup: F) -> Result<ParentRef>
    where
        F: FnMut(&str) -> Result<ChainDocument>,
    {
        resolve_parent_with(&self.parent, lookup)
    }

    /// The header family the chain inherits, through any number of nested
    /// parents ([`ChainDocument::parent_with`]): the proof-of-work root's.
    pub fn family_with<F>(&self, lookup: F) -> Result<Family>
    where
        F: FnMut(&str) -> Result<ChainDocument>,
    {
        Ok(self.parent_with(lookup)?.family())
    }

    /// The challenge as a script.
    pub fn challenge_script(&self) -> Result<ScriptBuf> {
        Ok(ScriptBuf::from_bytes(
            hex::decode(&self.challenge).map_err(|e| Error::Encoding(e.to_string()))?,
        ))
    }

    /// The script an EVM deposit pays (the `evm` rule, proposals/evm.md):
    /// the document's `evm.reserve`, else the challenge, as
    /// `siding/lib/overlays/evm.mjs` reads it
    /// (`(cfg.reserve ?? chain.challenge)`). An `evm` section that is not an
    /// object, or a `reserve` that is not a hex string, is
    /// [`Error::Document`]. The document need not name the rule for this to
    /// answer; whether a chain credits deposits is [`ChainDocument::rules`].
    ///
    /// ```
    /// use sidestr_core::document::ChainDocument;
    ///
    /// let mut doc: serde_json::Value = serde_json::from_str(include_str!("../fixtures/trial/chain.json")).unwrap();
    /// let challenge = doc["challenge"].as_str().unwrap().to_string();
    /// let read = |v: &serde_json::Value| serde_json::from_value::<ChainDocument>(v.clone()).unwrap().evm_reserve();
    /// assert_eq!(read(&doc).unwrap().to_hex_string(), challenge);
    /// doc["evm"] = serde_json::json!({ "chainId": 777, "reserve": "5120ABCD" });
    /// assert_eq!(read(&doc).unwrap().to_hex_string(), "5120abcd");
    /// doc["evm"] = serde_json::json!({ "reserve": 5 });
    /// assert!(read(&doc).is_err());
    /// ```
    pub fn evm_reserve(&self) -> Result<ScriptBuf> {
        let reserve = match self.extra.get("evm") {
            None | Some(serde_json::Value::Null) => None,
            Some(serde_json::Value::Object(m)) => m.get("reserve").filter(|v| !v.is_null()),
            Some(v) => return Err(Error::Document(format!("evm must be an object, not {v}"))),
        };
        match reserve {
            None => self.challenge_script(),
            Some(serde_json::Value::String(h)) => hex::decode(h)
                .map(ScriptBuf::from_bytes)
                .map_err(|e| Error::Document(format!("evm.reserve is not a hex script: {e}"))),
            Some(v) => Err(Error::Document(format!(
                "evm.reserve must be a hex script, not {v}"
            ))),
        }
    }

    /// `powLimit` as a 256-bit target.
    pub fn pow_limit_target(&self) -> Result<Target> {
        let bytes: [u8; 32] = hex::decode(&self.pow_limit)
            .map_err(|e| Error::Encoding(e.to_string()))?
            .try_into()
            .map_err(|_| Error::Document("powLimit must be 32 bytes".into()))?;
        Ok(Target::from_be_bytes(bytes))
    }

    /// The compact `bits` every block carries: `powLimit` encoded as Bitcoin
    /// Core's `GetCompact` does (`siding/lib/chain.mjs` sets it once per chain).
    pub fn bits(&self) -> Result<CompactTarget> {
        Ok(self.pow_limit_target()?.to_compact_lossy())
    }

    /// The magic the name derives to, whether or not the document carries it.
    pub fn derived_magic(&self) -> String {
        magic_for(&self.name)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const TRIAL: &str = include_str!("../fixtures/trial/chain.json");
    const DREAMLAB: &str = include_str!("../fixtures/dreamlab/chain.json");

    #[test]
    fn fixtures_parse_and_round_trip() {
        for (text, name, magic) in [
            (TRIAL, "trial", "a8f6706f"),
            (DREAMLAB, "dreamlab", "d981eab1"),
        ] {
            let doc = ChainDocument::from_json(text).unwrap();
            assert_eq!(doc.name, name);
            assert_eq!(doc.magic.as_deref(), Some(magic));
            assert_eq!(doc.derived_magic(), magic);
            assert_eq!(doc.bits().unwrap().to_consensus(), 0x207f_ffff);
            let again: serde_json::Value = serde_json::from_str(&doc.to_json().unwrap()).unwrap();
            assert_eq!(
                again,
                serde_json::from_str::<serde_json::Value>(text).unwrap()
            );
        }
        let d = ChainDocument::from_json(DREAMLAB).unwrap();
        assert_eq!(d.extra["containment"]["parent"], "tbtc4");
    }

    #[test]
    fn refusals() {
        let base: serde_json::Value = serde_json::from_str(TRIAL).unwrap();
        let with = |f: &dyn Fn(&mut serde_json::Value)| {
            let mut v = base.clone();
            f(&mut v);
            ChainDocument::from_json(&v.to_string())
        };
        // siding/test/rules-test.mjs: a document naming a rule this validator lacks is refused
        let e = with(&|v| v["rules"] = serde_json::json!(["assets", "oracle"]))
            .unwrap_err()
            .to_string();
        assert!(e.contains("does not have"), "{e}");
        // a validator carrying further rules accepts exactly the names it carries
        let named = |rules: serde_json::Value| {
            let mut v = base.clone();
            v["rules"] = rules;
            serde_json::from_value::<ChainDocument>(v).unwrap()
        };
        assert!(named(serde_json::json!(["evm"]))
            .validate_with(&["assets", "evm"])
            .is_ok());
        assert!(named(serde_json::json!(["assets", "evm"]))
            .validate_with(&["assets", "evm"])
            .is_ok());
        for (rules, want) in [
            (
                serde_json::json!(["evm", "pool"]),
                "\"pool\", which this validator does not have (it carries assets, evm)",
            ),
            (serde_json::json!(["desk"]), "\"desk\""),
            (serde_json::json!(["evm"]), "carries the core rules only"),
        ] {
            let carried: &[&str] = if want.contains("core rules") {
                &[]
            } else {
                &["assets", "evm"]
            };
            let e = named(rules).validate_with(carried).unwrap_err().to_string();
            assert!(e.contains(want), "{e}");
        }
        assert!(named(serde_json::json!(["pool"]))
            .validate_with(&["pool"])
            .unwrap_err()
            .to_string()
            .contains("needs the assets rule"));
        // a BLAKE2b parent is a valid document; which family a validator carries is the state's concern
        assert_eq!(
            with(&|v| v["parent"] = "txbt4".into())
                .unwrap()
                .family()
                .unwrap(),
            Family::Blake2b
        );
        assert!(matches!(
            with(&|v| v["parent"] = "doge".into()),
            Err(Error::UnknownParent(_))
        ));
        assert!(with(&|v| v["signers"] = serde_json::json!(["aa"])).is_err());
        assert!(with(&|v| {
            v["signers"] = serde_json::json!(["aa".repeat(32)]);
            v["threshold"] = serde_json::json!(1);
        })
        .unwrap_err()
        .to_string()
        .contains("is not the one 1 signers"));
        assert!(with(&|v| v["signer"] = serde_json::json!("00".repeat(32))).is_err());
        assert!(with(&|v| v["powLimit"] = serde_json::json!("ff")).is_err());
        assert!(with(&|v| v["addressPrefix"] = serde_json::json!("TRL")).is_err());
        assert!(with(&|v| v["genesisHash"] = serde_json::json!("zz")).is_err());
    }

    // SPEC 3.1 prose, a departure from parents.mjs at e8deb63 (crate::parents)
    #[test]
    fn a_nested_document_validates_only_through_its_parent_s_document() {
        let siding: ChainDocument = serde_json::from_str(TRIAL).unwrap();
        let siding = ChainDocument {
            parent: "txbt4".into(),
            ..siding
        };
        let hash = "5d".repeat(32);
        let child = ChainDocument {
            id: "sidestr:child".into(),
            name: "child".into(),
            parent: hash.clone(),
            ..siding.clone()
        };
        // siding's answer, unchanged
        assert!(matches!(child.parent(), Err(Error::UnknownParent(_))));
        assert!(matches!(child.validate(), Err(Error::UnknownParent(_))));
        // through the parent's document
        let lookup = |h: &str| {
            if h == hash {
                Ok(siding.clone())
            } else {
                Err(Error::Document(format!("no chain event {h}")))
            }
        };
        assert_eq!(child.family_with(lookup).unwrap(), Family::Blake2b);
        assert_eq!(child.parent_with(lookup).unwrap().depth(), 1);
        child.validate_nested(&[], lookup).unwrap();
        // the other checks still apply
        let mut ruled = child.clone();
        ruled.rules = Some(vec![RuleEntry::Name("evm".into())]);
        assert!(ruled.validate_nested(&[], lookup).is_err());
        assert!(ruled.validate_nested(&["assets", "evm"], lookup).is_ok());
        let other = ChainDocument {
            parent: "6e".repeat(32),
            ..child.clone()
        };
        assert!(other.validate_nested(&[], lookup).is_err());
        // an alias parent answers as validate_with, and never asks
        siding
            .validate_nested(&[], |_| panic!("no lookup for an alias"))
            .unwrap();
    }
}