Skip to main content

witness_core/
negative_unl.rs

1//! The Negative UNL object in ledger state.
2//!
3//! The network itself temporarily disables UNL validators that have not
4//! taken part in consensus for a long time, so their silence does not get in
5//! the way of reaching quorum. The list of disabled validators lives in the
6//! ledger as a single object with a fixed index and changes only in flag
7//! ledgers (every 256).
8//!
9//! Besides the disabled ones, the object can carry candidates: who will be
10//! disabled and who will be re-enabled at the next flag ledger. They do not
11//! affect the quorum of the current ledger but matter for history.
12
13use crate::stobject::{self, Error, fields as f};
14
15/// Index of the Negative UNL object, one per ledger.
16pub const INDEX: &str = "2E8A59AA9D3B5B186B0B9E0F62E6C02587CA74A4D778938E957B6357D364B244";
17
18/// Ledger entry type `NegativeUNL`: the letter N.
19const ENTRY_TYPE: u16 = 0x004E;
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Disabled {
23    /// Master key of the disabled validator.
24    pub master_key: Vec<u8>,
25    /// The ledger it has been disabled since.
26    pub since: u32,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Default)]
30pub struct NegativeUnl {
31    pub disabled: Vec<Disabled>,
32    pub to_disable: Option<Vec<u8>>,
33    pub to_re_enable: Option<Vec<u8>>,
34}
35
36impl NegativeUnl {
37    pub fn parse(raw: &[u8]) -> Result<NegativeUnl, Error> {
38        let fs = stobject::fields(raw)?;
39        let kind = stobject::find(&fs, f::LEDGER_ENTRY_TYPE)
40            .and_then(|v| <[u8; 2]>::try_from(v).ok())
41            .map(u16::from_be_bytes);
42        if kind != Some(ENTRY_TYPE) {
43            return Err(Error::Malformed("not a NegativeUNL object".into()));
44        }
45        let mut disabled = Vec::new();
46        if let Some(arr) = fs.iter().find(|x| x.id == f::DISABLED_VALIDATORS) {
47            for item in stobject::children(arr)? {
48                if item.id != f::DISABLED_VALIDATOR {
49                    return Err(Error::Malformed(
50                        "unexpected element in DisabledValidators".into(),
51                    ));
52                }
53                let inner = stobject::children(&item)?;
54                disabled.push(Disabled {
55                    master_key: stobject::find(&inner, f::PUBLIC_KEY)
56                        .ok_or_else(|| Error::Malformed("missing PublicKey".into()))?
57                        .to_vec(),
58                    since: stobject::find(&inner, f::FIRST_LEDGER_SEQUENCE)
59                        .and_then(stobject::u32_of)
60                        .ok_or_else(|| Error::Malformed("missing FirstLedgerSequence".into()))?,
61                });
62            }
63        }
64        Ok(NegativeUnl {
65            disabled,
66            to_disable: stobject::find(&fs, f::VALIDATOR_TO_DISABLE).map(<[u8]>::to_vec),
67            to_re_enable: stobject::find(&fs, f::VALIDATOR_TO_RE_ENABLE).map(<[u8]>::to_vec),
68        })
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    fn parse(h: &str) -> NegativeUnl {
77        NegativeUnl::parse(&hex::decode(h).unwrap()).unwrap()
78    }
79
80    // Real objects from mainnet history (s2.ripple.com, ledger_entry,
81    // binary), flag ledgers from a sample taken 2026-10-24.
82
83    #[test]
84    fn one_disabled_validator() {
85        // Ledger 68 999 936.
86        let n = parse(
87            "11004E2200000000F011E013201A041CDB007121EDA5AAE0DB134B809F8D664888F7EC4FCE98DC4D00B33301032424941C16C2F0F7E1F1",
88        );
89        assert_eq!(n.disabled.len(), 1);
90        assert_eq!(n.disabled[0].since, 0x041C_DB00);
91        assert_eq!(n.disabled[0].master_key[0], 0xED);
92        assert_eq!(n.to_disable, None);
93    }
94
95    #[test]
96    fn only_a_candidate_to_disable() {
97        // Ledger 70 999 808: nobody disabled, one candidate.
98        let n = parse(
99            "11004E2200000000701421ED2C1468B4A11D281F93EF337C95E4A08DF0000FDEFB6D0EA9BC05FBD5D61A1F5A",
100        );
101        assert!(n.disabled.is_empty());
102        assert_eq!(n.to_disable.unwrap().len(), 33);
103    }
104
105    #[test]
106    fn disabled_and_candidate_to_re_enable() {
107        // Ledger 102 999 808: the same validator is disabled and queued for re-enabling.
108        let n = parse(
109            "11004E2200000000250623A700554CA8ED5F00C7CCEA679D925E02397D5FCAEA6F12B24F996F990CB6D7488DBA36701521ED580C4282950CB3F7E0185F37F2CFB216882C5EDDD3BB1EE49C304A1AA3C5DB92F011E013201A0623A7007121ED580C4282950CB3F7E0185F37F2CFB216882C5EDDD3BB1EE49C304A1AA3C5DB92E1F1",
110        );
111        assert_eq!(n.disabled.len(), 1);
112        assert_eq!(
113            n.to_re_enable.as_deref(),
114            Some(&n.disabled[0].master_key[..])
115        );
116    }
117
118    #[test]
119    fn two_disabled_validators() {
120        // Ledger 105 999 872.
121        let n = parse(
122            "11004E22000000002506516E005560D246507BAD8B86574F5C59851CC8BECFD1AB1F3F15F90A7CC8D65EA87F24C5701521EDCF08053DFF0F00AC6E78B61F7B7FD187AF74052DEB5074207506D3A2CDCD9E5CF011E013201A065163007121EDCF08053DFF0F00AC6E78B61F7B7FD187AF74052DEB5074207506D3A2CDCD9E5CE1E013201A065169007121ED8252C2F91523126EEF9A21964C7E487A10D6D63D459139700DBC70D9F7BAD542E1F1",
123        );
124        assert_eq!(n.disabled.len(), 2);
125        assert_ne!(n.disabled[0].master_key, n.disabled[1].master_key);
126        assert_eq!(n.disabled[0].since, 0x0651_6300);
127        assert_eq!(n.disabled[1].since, 0x0651_6900);
128        // Flag ledgers are multiples of 256.
129        assert!(n.disabled.iter().all(|d| d.since % 256 == 0));
130    }
131
132    #[test]
133    fn other_entries_are_refused() {
134        // A validation is not a NegativeUNL object.
135        assert!(NegativeUnl::parse(&[0x22, 0, 0, 0, 1]).is_err());
136    }
137}