dig-slashing 0.1.0

Validator slashing, attestation participation, inactivity accounting, and fraud-proof appeals for the DIG Network L2 blockchain.
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! Per-ground appeal verifiers.
//!
//! Traces to: [SPEC.md §6](../../../docs/resources/SPEC.md),
//! catalogue rows
//! [DSL-034..054](../../../docs/requirements/domains/appeal/specs/).
//!
//! # Role
//!
//! Each verifier inspects the evidence + (optional) appeal witness
//! and returns an [`AppealVerdict`]. Grounds that re-read existing
//! evidence state ("HeadersIdentical", "SlotMismatch") are pure
//! functions of the envelope. Grounds that require external state
//! (ValidatorNotActiveAtEpoch, BlockActuallyValid) additionally
//! consult a trait handle passed at the dispatcher layer.
//!
//! # Scope (incremental)
//!
//! First commit lands DSL-034 only. Subsequent DSLs add one verifier
//! each; the dispatcher `verify_appeal` lands once enough grounds
//! exist to exercise it.

use chia_bls::Signature;
use dig_block::L2BlockHeader;
use dig_protocol::Bytes32;

use crate::appeal::ground::ProposerAppealGround;
use crate::appeal::verdict::{AppealRejectReason, AppealSustainReason, AppealVerdict};
use crate::constants::BLS_SIGNATURE_SIZE;
use crate::evidence::attester_slashing::AttesterSlashing;
use crate::evidence::indexed_attestation::IndexedAttestation;
use crate::evidence::invalid_block::InvalidBlockProof;
use crate::evidence::proposer_slashing::ProposerSlashing;
use crate::evidence::verify::block_signing_message;
use crate::traits::{ExecutionOutcome, InvalidBlockOracle, PublicKeyLookup, ValidatorView};

/// Verify `ProposerAppealGround::HeadersIdentical`.
///
/// Implements [DSL-034](../../../docs/requirements/domains/appeal/specs/DSL-034.md).
/// Traces to SPEC §6.2.
///
/// # Predicate
///
/// `evidence.signed_header_a.message == evidence.signed_header_b.message`
/// (byte-equal `L2BlockHeader` structs).
///
/// # Verdict
///
/// - `Sustained { HeadersIdentical }` iff headers byte-equal.
/// - `Rejected { GroundDoesNotHold }` otherwise.
///
/// # Scope
///
/// Evidence-only check — the appeal's `witness` bytes are ignored
/// (a byte-equality check over `L2BlockHeader` is self-contained).
/// Other grounds may consume witness bytes for external-state proofs;
/// this one does not.
#[must_use]
pub fn verify_proposer_appeal_headers_identical(evidence: &ProposerSlashing) -> AppealVerdict {
    if evidence.signed_header_a.message == evidence.signed_header_b.message {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::HeadersIdentical,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `ProposerAppealGround::ProposerIndexMismatch`.
///
/// Implements [DSL-035](../../../docs/requirements/domains/appeal/specs/DSL-035.md).
/// Traces to SPEC §6.2.
///
/// # Predicate
///
/// `evidence.signed_header_a.message.proposer_index !=
/// evidence.signed_header_b.message.proposer_index`
///
/// # Verdict
///
/// - `Sustained { ProposerIndexMismatch }` iff the indices differ.
/// - `Rejected { GroundDoesNotHold }` otherwise.
///
/// Evidence-only check; witness ignored.
#[must_use]
pub fn verify_proposer_appeal_proposer_index_mismatch(
    evidence: &ProposerSlashing,
) -> AppealVerdict {
    let a = evidence.signed_header_a.message.proposer_index;
    let b = evidence.signed_header_b.message.proposer_index;
    if a != b {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::ProposerIndexMismatch,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `ProposerAppealGround::SignatureAInvalid`.
///
/// Implements [DSL-036](../../../docs/requirements/domains/appeal/specs/DSL-036.md).
/// Traces to SPEC §6.2.
///
/// # Predicate
///
/// Re-runs `chia_bls::verify(sig_a, proposer_pubkey,
/// block_signing_message(...))` against header A. Sustains when the
/// verify returns `false` OR the signature bytes cannot be decoded
/// OR the proposer is not registered.
///
/// # Verdict
///
/// - `Sustained { SignatureAInvalid }` iff re-check fails.
/// - `Rejected { GroundDoesNotHold }` iff signature genuinely verifies.
///
/// # Determinism
///
/// `chia_bls::verify` is deterministic; the same (sig, pk, msg)
/// triple always produces the same verdict.
#[must_use]
pub fn verify_proposer_appeal_signature_a_invalid(
    evidence: &ProposerSlashing,
    validator_view: &dyn ValidatorView,
    network_id: &Bytes32,
) -> AppealVerdict {
    verify_proposer_appeal_signature_side(
        &evidence.signed_header_a.message,
        &evidence.signed_header_a.signature,
        validator_view,
        network_id,
        AppealSustainReason::SignatureAInvalid,
    )
}

/// Verify `ProposerAppealGround::SlotMismatch`.
///
/// Implements [DSL-038](../../../docs/requirements/domains/appeal/specs/DSL-038.md).
/// Traces to SPEC §6.2.
///
/// # Predicate
///
/// `evidence.signed_header_a.message.height !=
/// evidence.signed_header_b.message.height` — the L2 height field
/// serves as the "slot" coordinate for proposer equivocation.
///
/// # Verdict
///
/// - `Sustained { SlotMismatch }` iff heights differ.
/// - `Rejected { GroundDoesNotHold }` otherwise.
///
/// Evidence-only check; witness ignored.
#[must_use]
pub fn verify_proposer_appeal_slot_mismatch(evidence: &ProposerSlashing) -> AppealVerdict {
    let a = evidence.signed_header_a.message.height;
    let b = evidence.signed_header_b.message.height;
    if a != b {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::SlotMismatch,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `ProposerAppealGround::SignatureBInvalid`.
///
/// Implements [DSL-037](../../../docs/requirements/domains/appeal/specs/DSL-037.md).
/// Mirror of DSL-036 on `signed_header_b` — same shared helper,
/// different sustain reason.
#[must_use]
pub fn verify_proposer_appeal_signature_b_invalid(
    evidence: &ProposerSlashing,
    validator_view: &dyn ValidatorView,
    network_id: &Bytes32,
) -> AppealVerdict {
    verify_proposer_appeal_signature_side(
        &evidence.signed_header_b.message,
        &evidence.signed_header_b.signature,
        validator_view,
        network_id,
        AppealSustainReason::SignatureBInvalid,
    )
}

/// Verify `ProposerAppealGround::ValidatorNotActiveAtEpoch`.
///
/// Implements [DSL-039](../../../docs/requirements/domains/appeal/specs/DSL-039.md).
/// Traces to SPEC §6.2 + §15.1.
///
/// # Predicate
///
/// `!validator_view.get(proposer_index)?.is_active_at_epoch(header_a.epoch)`
/// — the accused was outside their active window at the claimed
/// offense epoch, so they could not have been the proposer.
///
/// # Verdict
///
/// - `Sustained { ValidatorNotActiveAtEpoch }` iff inactive at epoch
///   OR the validator is not registered (unknown validator → cannot
///   be active, same coarse handling as DSL-036).
/// - `Rejected { GroundDoesNotHold }` iff active.
///
/// Checks only header A — if both headers share the same
/// `proposer_index` (DSL-013 precondition 2), activation status at
/// header-A's epoch is dispositive. A verifier bug admitting
/// different epochs between A and B is separately catchable under
/// DSL-035 (ProposerIndexMismatch) or DSL-019 (InvalidBlock epoch
/// mismatch).
#[must_use]
pub fn verify_proposer_appeal_validator_not_active_at_epoch(
    evidence: &ProposerSlashing,
    validator_view: &dyn ValidatorView,
) -> AppealVerdict {
    let header = &evidence.signed_header_a.message;
    let sustain = AppealVerdict::Sustained {
        reason: AppealSustainReason::ValidatorNotActiveAtEpoch,
    };
    let Some(entry) = validator_view.get(header.proposer_index) else {
        return sustain;
    };
    if entry.is_active_at_epoch(header.epoch) {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    } else {
        sustain
    }
}

/// Shared helper: re-check one header's BLS signature. Returns
/// `Sustained { sustain_reason }` on verify-failure / decode-failure
/// / missing-pubkey, `Rejected { GroundDoesNotHold }` on successful
/// verify.
///
/// Reused by DSL-037 (`SignatureBInvalid`) — same shape, different
/// side.
fn verify_proposer_appeal_signature_side(
    header: &L2BlockHeader,
    sig_bytes: &[u8],
    validator_view: &dyn ValidatorView,
    network_id: &Bytes32,
    sustain_reason: AppealSustainReason,
) -> AppealVerdict {
    let sustain = AppealVerdict::Sustained {
        reason: sustain_reason,
    };

    // Decode sig bytes — wrong width or non-curve-point bytes both
    // collapse to sustain (the signature is not a valid BLS G2).
    let Ok(sig_arr) = <&[u8; BLS_SIGNATURE_SIZE]>::try_from(sig_bytes) else {
        return sustain;
    };
    let Ok(sig) = Signature::from_bytes(sig_arr) else {
        return sustain;
    };

    // Look up the proposer's pubkey. Absent validator → sustain: the
    // appeal proves the manager slashed a non-existent/unknown key.
    let Some(entry) = validator_view.get(header.proposer_index) else {
        return sustain;
    };
    let pk = entry.public_key();

    let msg = block_signing_message(
        network_id,
        header.epoch,
        &header.hash(),
        header.proposer_index,
    );
    if chia_bls::verify(&sig, pk, &msg) {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    } else {
        sustain
    }
}

/// Verify `AttesterAppealGround::NotSlashableByPredicate`.
///
/// Implements [DSL-042](../../../docs/requirements/domains/appeal/specs/DSL-042.md).
/// Traces to SPEC §6.3.
///
/// # Predicate
///
/// Mirrors DSL-017 rejection: sustains when NEITHER double-vote
/// nor surround-vote holds on the two `AttestationData`s.
///
/// ```text
/// double_vote ⟺ a.target.epoch == b.target.epoch AND a.data != b.data
/// surround_vote ⟺
///     (a.src < b.src AND a.tgt > b.tgt)
///     OR (b.src < a.src AND b.tgt > a.tgt)
/// sustain ⟺ !(double_vote || surround_vote)
/// ```
///
/// # Verdict
///
/// - `Sustained { NotSlashableByPredicate }` iff neither holds.
/// - `Rejected { GroundDoesNotHold }` otherwise.
///
/// Evidence-only; witness ignored.
#[must_use]
pub fn verify_attester_appeal_not_slashable_by_predicate(
    evidence: &AttesterSlashing,
) -> AppealVerdict {
    let a = &evidence.attestation_a.data;
    let b = &evidence.attestation_b.data;
    let double_vote = a.target.epoch == b.target.epoch && a != b;
    let surround_vote = (a.source.epoch < b.source.epoch && a.target.epoch > b.target.epoch)
        || (b.source.epoch < a.source.epoch && b.target.epoch > a.target.epoch);
    if !(double_vote || surround_vote) {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::NotSlashableByPredicate,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `AttesterAppealGround::EmptyIntersection`.
///
/// Implements [DSL-043](../../../docs/requirements/domains/appeal/specs/DSL-043.md).
/// Traces to SPEC §6.3.
///
/// # Predicate
///
/// `evidence.slashable_indices().is_empty()` — delegates to DSL-007
/// two-pointer sorted intersection. Sustains when no validator
/// participated in BOTH attestations (non-actionable evidence).
///
/// # Verdict
///
/// - `Sustained { EmptyIntersection }` iff intersection empty.
/// - `Rejected { GroundDoesNotHold }` otherwise.
///
/// Evidence-only; witness ignored.
#[must_use]
pub fn verify_attester_appeal_empty_intersection(evidence: &AttesterSlashing) -> AppealVerdict {
    if evidence.slashable_indices().is_empty() {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::EmptyIntersection,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `AttesterAppealGround::AttesterSignatureAInvalid`.
///
/// Implements [DSL-044](../../../docs/requirements/domains/appeal/specs/DSL-044.md).
/// Traces to SPEC §6.3, §15.2.
///
/// # Predicate
///
/// Re-runs `IndexedAttestation::verify_signature` (DSL-006) over
/// `evidence.attestation_a`. Any failure leg — malformed bytes,
/// bad G2 point, unknown attester index, cryptographic reject —
/// collapses to `Sustained{ AttesterSignatureAInvalid }`. This
/// matches the coarse DSL-006 handling of `BlsVerifyFailed` (SPEC
/// §15.2 does not distinguish "unknown validator" from "bad sig").
///
/// # Verdict
///
/// - `Sustained { AttesterSignatureAInvalid }` iff re-check fails.
/// - `Rejected { GroundDoesNotHold }` iff sig genuinely verifies.
///
/// # Determinism
///
/// `chia_bls::aggregate_verify` is deterministic; the same
/// (sig, pubkey-set, msg) triple always produces the same verdict.
#[must_use]
pub fn verify_attester_appeal_signature_a_invalid(
    evidence: &AttesterSlashing,
    pks: &dyn PublicKeyLookup,
    network_id: &Bytes32,
) -> AppealVerdict {
    verify_attester_appeal_signature_side(
        &evidence.attestation_a,
        pks,
        network_id,
        AppealSustainReason::AttesterSignatureAInvalid,
    )
}

/// Verify `AttesterAppealGround::AttesterSignatureBInvalid`.
///
/// Implements [DSL-045](../../../docs/requirements/domains/appeal/specs/DSL-045.md).
/// Mirror of DSL-044 on `attestation_b` — same shared helper,
/// different sustain reason. Traces to SPEC §6.3, §15.2.
#[must_use]
pub fn verify_attester_appeal_signature_b_invalid(
    evidence: &AttesterSlashing,
    pks: &dyn PublicKeyLookup,
    network_id: &Bytes32,
) -> AppealVerdict {
    verify_attester_appeal_signature_side(
        &evidence.attestation_b,
        pks,
        network_id,
        AppealSustainReason::AttesterSignatureBInvalid,
    )
}

/// Shared helper: re-check one `IndexedAttestation`'s aggregate
/// signature. Returns `Sustained { sustain_reason }` on any
/// verify failure (DSL-006 returns a single coarse `Err`
/// variant), `Rejected { GroundDoesNotHold }` on success.
///
/// Reused by DSL-045 (`AttesterSignatureBInvalid`) — same shape,
/// different side.
fn verify_attester_appeal_signature_side(
    attestation: &IndexedAttestation,
    pks: &dyn PublicKeyLookup,
    network_id: &Bytes32,
    sustain_reason: AppealSustainReason,
) -> AppealVerdict {
    if attestation.verify_signature(pks, network_id).is_err() {
        AppealVerdict::Sustained {
            reason: sustain_reason,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `AttesterAppealGround::InvalidIndexedAttestationStructure`.
///
/// Implements [DSL-046](../../../docs/requirements/domains/appeal/specs/DSL-046.md).
/// Traces to SPEC §6.3.
///
/// # Predicate
///
/// Sustains when `validate_structure()` (DSL-005) fails on either
/// attestation. Covers all five DSL-005 rejection legs:
/// - empty `attesting_indices`
/// - `attesting_indices.len() > MAX_VALIDATORS_PER_COMMITTEE`
/// - `signature.len() != BLS_SIGNATURE_SIZE`
/// - non-ascending indices (`w[0] > w[1]`)
/// - duplicate indices (`w[0] == w[1]`)
///
/// # Verdict
///
/// - `Sustained { InvalidIndexedAttestationStructure }` iff either
///   side fails structural validation.
/// - `Rejected { GroundDoesNotHold }` iff both sides are well-formed.
///
/// # Short-circuit order
///
/// Checks side A first; if A fails, B is not evaluated. Either-side
/// failure is sufficient to sustain, so the short-circuit is
/// verdict-preserving — only cost order matters.
///
/// Evidence-only; witness ignored.
#[must_use]
pub fn verify_attester_appeal_invalid_indexed_attestation_structure(
    evidence: &AttesterSlashing,
) -> AppealVerdict {
    if evidence.attestation_a.validate_structure().is_err()
        || evidence.attestation_b.validate_structure().is_err()
    {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::InvalidIndexedAttestationStructure,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `AttesterAppealGround::ValidatorNotInIntersection { validator_index }`.
///
/// Implements [DSL-047](../../../docs/requirements/domains/appeal/specs/DSL-047.md).
/// Traces to SPEC §6.3.
///
/// # Predicate
///
/// `!evidence.slashable_indices().contains(&validator_index)` —
/// the named validator is NOT in the two-pointer sorted
/// intersection (DSL-007). Used to rescue a falsely-included
/// validator from a buggy verifier admission without touching the
/// other slashed indices.
///
/// # Verdict
///
/// - `Sustained { ValidatorNotInIntersection }` iff the named
///   index is absent from the intersection.
/// - `Rejected { GroundDoesNotHold }` iff present.
///
/// # Per-validator scope
///
/// The verdict references only the index the appellant named. Any
/// other originally-slashed index MUST be appealed independently —
/// the adjudicator (DSL-064) credits back only the named index on
/// a sustain. Callers get this for free because the function is
/// parameterised on `validator_index`, not on the evidence alone.
///
/// Evidence-derived; witness ignored (the `validator_index` comes
/// from the ground variant, not witness bytes).
#[must_use]
pub fn verify_attester_appeal_validator_not_in_intersection(
    evidence: &AttesterSlashing,
    validator_index: u32,
) -> AppealVerdict {
    if evidence.slashable_indices().contains(&validator_index) {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    } else {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::ValidatorNotInIntersection,
        }
    }
}

/// Verify `AttesterAppealGround::AttestationsIdentical`.
///
/// Implements [DSL-041](../../../docs/requirements/domains/appeal/specs/DSL-041.md).
/// Traces to SPEC §6.3.
///
/// # Predicate
///
/// `evidence.attestation_a == evidence.attestation_b` (byte-wise
/// `IndexedAttestation` equality — includes attesting_indices, data,
/// signature).
///
/// # Verdict
///
/// - `Sustained { AttestationsIdentical }` iff byte-equal.
/// - `Rejected { GroundDoesNotHold }` otherwise.
///
/// Evidence-only; witness ignored.
#[must_use]
pub fn verify_attester_appeal_attestations_identical(evidence: &AttesterSlashing) -> AppealVerdict {
    if evidence.attestation_a == evidence.attestation_b {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::AttestationsIdentical,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

/// Verify `InvalidBlockAppealGround::ProposerSignatureInvalid`.
///
/// Implements [DSL-050](../../../docs/requirements/domains/appeal/specs/DSL-050.md).
/// Traces to SPEC §6.4.
///
/// # Predicate
///
/// Re-runs `chia_bls::verify(sig, proposer_pubkey,
/// block_signing_message(...))` over `evidence.signed_header`.
/// Pure BLS — no oracle needed. Mirrors DSL-018 (the inverse
/// admission check in the evidence verifier) and DSL-036 (the
/// proposer-equivocation variant).
///
/// Reuses the shared `verify_proposer_appeal_signature_side`
/// helper — same contract (decode-failure / unknown-validator /
/// crypto-reject all collapse to sustain) with a different sustain
/// reason tag. Keeps the behavior surface identical across every
/// "signature invalid" appeal ground in the crate.
///
/// # Verdict
///
/// - `Sustained { ProposerSignatureInvalid }` iff re-check fails.
/// - `Rejected { GroundDoesNotHold }` iff signature verifies.
#[must_use]
pub fn verify_invalid_block_appeal_proposer_signature_invalid(
    evidence: &InvalidBlockProof,
    validator_view: &dyn ValidatorView,
    network_id: &Bytes32,
) -> AppealVerdict {
    verify_proposer_appeal_signature_side(
        &evidence.signed_header.message,
        &evidence.signed_header.signature,
        validator_view,
        network_id,
        AppealSustainReason::ProposerSignatureInvalid,
    )
}

/// Verify `InvalidBlockAppealGround::BlockActuallyValid`.
///
/// Implements [DSL-049](../../../docs/requirements/domains/appeal/specs/DSL-049.md).
/// Traces to SPEC §6.4, §15.3.
///
/// # Predicate
///
/// Delegates to [`InvalidBlockOracle::re_execute`] (DSL-145). The
/// oracle re-runs full block validation with the caller-supplied
/// appeal witness (trie proofs, pre-state, parent refs). If
/// re-execution succeeds the original `InvalidBlockProof` was a
/// lie — the slash MUST be reverted.
///
/// # Witness passthrough
///
/// The appeal's own `witness: &[u8]` is passed through to the
/// oracle verbatim — NOT `evidence.failure_witness`. The appellant
/// may supply different replay material than the slasher; the
/// oracle adjudicates based on what the appeal asserts.
///
/// # Verdict
///
/// - `Sustained { BlockActuallyValid }` iff oracle returns
///   `Valid`.
/// - `Rejected { GroundDoesNotHold }` iff oracle returns
///   `Invalid(_)` (oracle disagrees with the appeal's assertion).
/// - `Rejected { MalformedWitness }` iff the oracle returns `Err`
///   (witness bytes did not decode / replay aborted). The
///   appellant failed to produce usable replay material — distinct
///   from "oracle says invalid".
/// - `Rejected { MissingOracle }` iff no oracle was supplied. The
///   appeal requires external state that the slashing crate does
///   not own (SPEC §15.3 bootstrap mode).
#[must_use]
pub fn verify_invalid_block_appeal_block_actually_valid(
    evidence: &InvalidBlockProof,
    appeal_witness: &[u8],
    oracle: Option<&dyn InvalidBlockOracle>,
) -> AppealVerdict {
    let Some(oracle) = oracle else {
        return AppealVerdict::Rejected {
            reason: AppealRejectReason::MissingOracle,
        };
    };
    match oracle.re_execute(&evidence.signed_header.message, appeal_witness) {
        Ok(ExecutionOutcome::Valid) => AppealVerdict::Sustained {
            reason: AppealSustainReason::BlockActuallyValid,
        },
        Ok(ExecutionOutcome::Invalid(_)) => AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        },
        Err(_) => AppealVerdict::Rejected {
            reason: AppealRejectReason::MalformedWitness,
        },
    }
}

/// Verify `InvalidBlockAppealGround::FailureReasonMismatch`.
///
/// Implements [DSL-051](../../../docs/requirements/domains/appeal/specs/DSL-051.md).
/// Traces to SPEC §6.4.
///
/// # Predicate
///
/// Calls `InvalidBlockOracle::re_execute`. The block IS invalid but
/// the classified reason (`ExecutionOutcome::Invalid(actual)`)
/// differs from `evidence.failure_reason`. This is a reporter
/// mis-classification — they slashed the proposer under the wrong
/// `InvalidBlockReason`.
///
/// # Verdict
///
/// - `Sustained { FailureReasonMismatch }` iff oracle returns
///   `Invalid(actual)` with `actual != evidence.failure_reason`.
/// - `Rejected { GroundDoesNotHold }` iff oracle returns
///   `Invalid(same)` (reason matches — evidence is correct) OR
///   `Valid` (DSL-049 is the correct ground for that case).
/// - `Rejected { MalformedWitness }` iff oracle returns `Err`.
/// - `Rejected { MissingOracle }` iff no oracle supplied.
///
/// # Witness forwarding
///
/// Forwards the APPEAL's witness to the oracle (not
/// `evidence.failure_witness`) — same rationale as DSL-049. The
/// appellant may supply different replay material than the slasher.
#[must_use]
pub fn verify_invalid_block_appeal_failure_reason_mismatch(
    evidence: &InvalidBlockProof,
    appeal_witness: &[u8],
    oracle: Option<&dyn InvalidBlockOracle>,
) -> AppealVerdict {
    let Some(oracle) = oracle else {
        return AppealVerdict::Rejected {
            reason: AppealRejectReason::MissingOracle,
        };
    };
    match oracle.re_execute(&evidence.signed_header.message, appeal_witness) {
        Ok(ExecutionOutcome::Invalid(actual)) if actual != evidence.failure_reason => {
            AppealVerdict::Sustained {
                reason: AppealSustainReason::FailureReasonMismatch,
            }
        }
        Ok(_) => AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        },
        Err(_) => AppealVerdict::Rejected {
            reason: AppealRejectReason::MalformedWitness,
        },
    }
}

/// Verify `InvalidBlockAppealGround::EvidenceEpochMismatch`.
///
/// Implements [DSL-052](../../../docs/requirements/domains/appeal/specs/DSL-052.md).
/// Traces to SPEC §6.4. Mirrors DSL-019 rejection inverted.
///
/// # Predicate
///
/// `evidence.signed_header.message.epoch != evidence_epoch` — the
/// envelope claims the offense happened at one epoch, but the
/// header inside the evidence carries a different epoch. One of
/// the two is wrong, so the slash was admitted on inconsistent
/// evidence.
///
/// # Verdict
///
/// - `Sustained { EvidenceEpochMismatch }` iff the two epochs
///   disagree.
/// - `Rejected { GroundDoesNotHold }` iff they match.
///
/// # Scope
///
/// Pure local check — no oracle, no cryptography. The
/// `evidence_epoch` argument is the enclosing
/// `SlashingEvidence::epoch` field; the dispatcher passes it in
/// rather than the appeal carrying its own copy (which would just
/// reintroduce the same inconsistency risk).
#[must_use]
pub fn verify_invalid_block_appeal_evidence_epoch_mismatch(
    evidence: &InvalidBlockProof,
    evidence_epoch: u64,
) -> AppealVerdict {
    if evidence.signed_header.message.epoch != evidence_epoch {
        AppealVerdict::Sustained {
            reason: AppealSustainReason::EvidenceEpochMismatch,
        }
    } else {
        AppealVerdict::Rejected {
            reason: AppealRejectReason::GroundDoesNotHold,
        }
    }
}

// Compile-time sanity: keep `ProposerAppealGround::HeadersIdentical`
// referenced from the verify module so the variant-to-verifier
// mapping remains visible in cross-references.
#[allow(dead_code)]
const _HEADERS_IDENTICAL_GROUND: ProposerAppealGround = ProposerAppealGround::HeadersIdentical;
#[allow(dead_code)]
const _PROPOSER_INDEX_MISMATCH_GROUND: ProposerAppealGround =
    ProposerAppealGround::ProposerIndexMismatch;
#[allow(dead_code)]
const _SIGNATURE_A_INVALID_GROUND: ProposerAppealGround = ProposerAppealGround::SignatureAInvalid;
#[allow(dead_code)]
const _SIGNATURE_B_INVALID_GROUND: ProposerAppealGround = ProposerAppealGround::SignatureBInvalid;
#[allow(dead_code)]
const _SLOT_MISMATCH_GROUND: ProposerAppealGround = ProposerAppealGround::SlotMismatch;
#[allow(dead_code)]
const _VALIDATOR_NOT_ACTIVE_GROUND: ProposerAppealGround =
    ProposerAppealGround::ValidatorNotActiveAtEpoch;