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
//! Governance Continuity Tracking
//!
//! Provides hash-chained epoch transitions to ensure governance history
//! behaves like an append-only transparency log. Prevents governance replay
//! attacks, arbitrary rollbacks, and alternate history emergence.
use alloc::string::String;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
/// A cryptographically linked transition between two federation governance epochs.
///
/// Chaining `previous_transition_hash` guarantees that the sequence of governance
/// changes forms an immutable DAG (or linear chain).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct GovernanceTransition {
/// The epoch number being transitioned from.
pub previous_epoch: u64,
/// The new epoch number becoming active. Must be strictly greater than `previous_epoch`.
pub next_epoch: u64,
/// The canonical hash of the immediate predecessor `GovernanceTransition`.
/// Forms the cryptographic link preventing alternate governance histories.
pub previous_transition_hash: [u8; 32],
/// The canonical hash of this specific transition event.
pub transition_hash: [u8; 32],
/// The verifiers/authorities that approved this transition.
pub approved_by: Vec<String>,
/// The aggregate quorum signature authorizing the transition.
#[serde(with = "serde_bytes")]
pub quorum_signature: Vec<u8>,
}
impl GovernanceTransition {
/// Verifies that this transition correctly links to its predecessor.
///
/// # Errors
/// Returns an error if the transition violates monotonicity or hash linkage.
pub fn verify_linkage(&self, previous: &GovernanceTransition) -> Result<(), &'static str> {
if self.previous_epoch != previous.next_epoch {
return Err("epoch discontinuity detected");
}
if self.next_epoch <= self.previous_epoch {
return Err("epoch transition must be strictly monotonic");
}
if self.previous_transition_hash != previous.transition_hash {
return Err("governance transition hash chain broken");
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_transition_linkage() {
let t1 = GovernanceTransition {
previous_epoch: 1,
next_epoch: 2,
previous_transition_hash: [0x00; 32],
transition_hash: [0xAA; 32],
approved_by: vec![],
quorum_signature: vec![],
};
let t2 = GovernanceTransition {
previous_epoch: 2,
next_epoch: 3,
previous_transition_hash: [0xAA; 32],
transition_hash: [0xBB; 32],
approved_by: vec![],
quorum_signature: vec![],
};
assert!(t2.verify_linkage(&t1).is_ok());
}
#[test]
fn invalid_hash_linkage() {
let t1 = GovernanceTransition {
previous_epoch: 1,
next_epoch: 2,
previous_transition_hash: [0x00; 32],
transition_hash: [0xAA; 32],
approved_by: vec![],
quorum_signature: vec![],
};
let t2 = GovernanceTransition {
previous_epoch: 2,
next_epoch: 3,
previous_transition_hash: [0xBB; 32], // Mismatch!
transition_hash: [0xCC; 32],
approved_by: vec![],
quorum_signature: vec![],
};
assert!(t2.verify_linkage(&t1).is_err());
}
#[test]
fn invalid_epoch_monotonicity() {
let t1 = GovernanceTransition {
previous_epoch: 1,
next_epoch: 2,
previous_transition_hash: [0x00; 32],
transition_hash: [0xAA; 32],
approved_by: vec![],
quorum_signature: vec![],
};
let t2 = GovernanceTransition {
previous_epoch: 2,
next_epoch: 1, // Reverted!
previous_transition_hash: [0xAA; 32],
transition_hash: [0xBB; 32],
approved_by: vec![],
quorum_signature: vec![],
};
assert!(t2.verify_linkage(&t1).is_err());
}
}