Skip to main content

witness_core/
skiplist.rs

1//! Skip lists: the `LedgerHashes` objects the network keeps in ledger state.
2//!
3//! Every ledger updates two of them before its transactions run (they are not
4//! in any transaction's metadata):
5//! - the short list, key SHA-512Half(`0x0073`), holds the hashes of the
6//!   previous 256 ledgers;
7//! - when the previous ledger's index is a multiple of 256, its hash is
8//!   appended to the long list of its group of 65536 ledgers, key
9//!   SHA-512Half(`0x0073`, index >> 16 as u32). A group's list is never
10//!   removed, so the latest state holds the hash of every 256th ledger back to
11//!   the start of history.
12//!
13//! Any hash in either list is the hash of an earlier ledger of the same chain:
14//! only the protocol writes these objects, and only with parent hashes. A
15//! state proof of a list in a confirmed ledger therefore confirms every ledger
16//! whose hash is in it, and a header chain of at most 255 ledgers reaches any
17//! ledger from the next multiple of 256.
18
19use crate::sha512_half;
20use crate::shamap::Hash;
21use crate::stobject::{self, FieldId, types};
22
23/// `LedgerEntryType` of a skip list: `h`.
24pub const LEDGER_HASHES: u16 = 0x0068;
25/// The `Hashes` field (Vector256, code 2).
26pub const HASHES: FieldId = FieldId::new(types::VECTOR256, 2);
27/// Namespace of skip list keys: `s`.
28const SPACE: [u8; 2] = [0x00, 0x73];
29
30/// Key of the short list (the previous 256 ledgers).
31pub fn short_key() -> Hash {
32    sha512_half(&SPACE)
33}
34
35/// Key of the long list that holds the hash of ledger `seq` when `seq` is a
36/// multiple of 256.
37pub fn long_key(seq: u32) -> Hash {
38    let mut b = SPACE.to_vec();
39    b.extend_from_slice(&(seq >> 16).to_be_bytes());
40    sha512_half(&b)
41}
42
43/// The first multiple of 256 at or after `seq`: the ledger whose hash a long
44/// list holds and from which a chain reaches `seq`.
45pub fn covering(seq: u32) -> u32 {
46    seq.div_ceil(256) * 256
47}
48
49/// The hashes of a serialized `LedgerHashes` object.
50pub fn hashes(object: &[u8]) -> Result<Vec<Hash>, String> {
51    let fields = stobject::fields(object).map_err(|e| e.to_string())?;
52    let kind = stobject::find(&fields, stobject::fields::LEDGER_ENTRY_TYPE)
53        .and_then(|v| v.try_into().ok())
54        .map(u16::from_be_bytes);
55    if kind != Some(LEDGER_HASHES) {
56        return Err("object is not a LedgerHashes entry".into());
57    }
58    let v = stobject::find(&fields, HASHES).ok_or("LedgerHashes has no Hashes field")?;
59    match v.as_chunks::<32>() {
60        (hashes, []) => Ok(hashes.to_vec()),
61        _ => Err("Hashes is not a list of 32-byte hashes".into()),
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn keys_and_covering_ledger() {
71        // Keys as xrpld computes them (keylet::skip), checked against mainnet state.
72        assert_eq!(
73            hex::encode_upper(short_key()),
74            "B4979A36CDC7F3D3D5C31A4EAE2AC7D7209DDA877588B9AFC66799692AB0D66B"
75        );
76        assert_eq!(long_key(87_000_064), long_key(86_966_272));
77        assert_ne!(long_key(87_000_064), long_key(87_031_808));
78        assert_eq!(covering(86_994_501), 86_994_688);
79        assert_eq!(covering(86_994_688), 86_994_688);
80    }
81
82    #[test]
83    fn hashes_of_an_object() {
84        let mut o = vec![0x11, 0x00, 0x68];
85        o.extend([0x02, 0x13, 64]);
86        o.extend([1u8; 32]);
87        o.extend([2u8; 32]);
88        assert_eq!(hashes(&o).unwrap(), vec![[1u8; 32], [2u8; 32]]);
89        o[2] = 0x61;
90        assert!(hashes(&o).is_err());
91    }
92}