lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

public_heads.rs (5260B)


      1 use radroots_event::envelope::event_head::{EventHeadCoordinate, EventHeadDecision};
      2 use radroots_nostr::public_ingress::{Error, Interpretation, ObservedHead};
      3 use radroots_protocol::advertisement::UnixSeconds;
      4 use serde_json::Value;
      5 
      6 fn fixture() -> Value {
      7     serde_json::from_str(include_str!("fixtures/public_heads.v1.json")).unwrap()
      8 }
      9 fn scope(value: &Value, kind: u32) -> EventHeadCoordinate {
     10     let pubkey = value["author"].as_str().unwrap().parse().unwrap();
     11     if kind == 30402 {
     12         EventHeadCoordinate::Addressable {
     13             kind,
     14             pubkey,
     15             d_tag: value["d"].as_str().unwrap().into(),
     16         }
     17     } else {
     18         EventHeadCoordinate::Replaceable { kind, pubkey }
     19     }
     20 }
     21 fn raw<'a>(value: &'a Value, name: &str) -> &'a str {
     22     value["vectors"]
     23         .as_array()
     24         .unwrap()
     25         .iter()
     26         .find(|r| r["name"] == name)
     27         .unwrap()["raw"]
     28         .as_str()
     29         .unwrap()
     30 }
     31 fn classify(value: Interpretation) -> &'static str {
     32     match value {
     33         Interpretation::Listing(_) => "listing",
     34         Interpretation::Capabilities(_) => "capabilities",
     35         Interpretation::Unsupported => "unsupported",
     36         Interpretation::Invalid => "invalid",
     37         Interpretation::Expired => "expired",
     38         Interpretation::ClockAhead => "clock_ahead",
     39     }
     40 }
     41 
     42 #[test]
     43 fn independently_signed_public_vectors_authenticate_before_interpretation() {
     44     let data = fixture();
     45     let now = UnixSeconds::new(data["observed_at"].as_u64().unwrap()).unwrap();
     46     for row in data["vectors"].as_array().unwrap() {
     47         let wire: Value = serde_json::from_str(row["raw"].as_str().unwrap()).unwrap();
     48         let mut head =
     49             ObservedHead::new(scope(&data, wire["kind"].as_u64().unwrap() as u32)).unwrap();
     50         let result = head.observe(row["raw"].as_str().unwrap());
     51         if row["expected"] == "unauthenticated" {
     52             assert_eq!(result.unwrap_err(), Error::Unauthenticated);
     53             assert!(head.raw_json().is_none());
     54         } else {
     55             assert!(
     56                 matches!(result, Ok(EventHeadDecision::Applied(_))),
     57                 "{}: {result:?}",
     58                 row["name"]
     59             );
     60             assert_eq!(
     61                 classify(head.interpret(now).unwrap()),
     62                 row["expected"].as_str().unwrap(),
     63                 "{}",
     64                 row["name"]
     65             );
     66             assert_eq!(head.raw_json(), row["raw"].as_str());
     67         }
     68     }
     69 }
     70 #[test]
     71 fn newer_nonactionable_heads_never_restore_an_older_supported_revision() {
     72     let data = fixture();
     73     let now = UnixSeconds::new(150).unwrap();
     74     for (older, newer, expected, kind) in [
     75         ("listing_fixed", "listing_unsupported", "unsupported", 30402),
     76         ("listing_fixed", "listing_unknown_field", "invalid", 30402),
     77         ("listing_fixed", "listing_duplicate_d", "invalid", 30402),
     78         ("listing_fixed", "listing_future", "clock_ahead", 30402),
     79         ("capability", "capability_unsupported", "unsupported", 18890),
     80         ("capability", "capability_expired", "expired", 18890),
     81         ("capability", "capability_future", "clock_ahead", 18890),
     82     ] {
     83         for reverse in [false, true] {
     84             let mut head = ObservedHead::new(scope(&data, kind)).unwrap();
     85             let pair = if reverse {
     86                 [newer, older]
     87             } else {
     88                 [older, newer]
     89             };
     90             for name in pair {
     91                 head.observe(raw(&data, name)).unwrap();
     92             }
     93             assert_eq!(head.raw_json(), Some(raw(&data, newer)));
     94             assert_eq!(classify(head.interpret(now).unwrap()), expected);
     95         }
     96     }
     97 }
     98 #[test]
     99 fn same_timestamp_lowest_identity_wins_and_invalid_signatures_cannot_replace() {
    100     let data = fixture();
    101     let mut pair = [raw(&data, "listing_fixed"), raw(&data, "listing_shuffled")];
    102     pair.sort_by_key(|raw| {
    103         serde_json::from_str::<Value>(raw).unwrap()["id"]
    104             .as_str()
    105             .unwrap()
    106             .to_owned()
    107     });
    108     for reverse in [false, true] {
    109         let mut head = ObservedHead::new(scope(&data, 30402)).unwrap();
    110         for value in if reverse { [pair[1], pair[0]] } else { pair } {
    111             head.observe(value).unwrap();
    112         }
    113         assert_eq!(head.raw_json(), Some(pair[0]));
    114         assert_eq!(
    115             head.observe(raw(&data, "bad_signature")),
    116             Err(Error::Unauthenticated)
    117         );
    118         assert_eq!(
    119             head.observe(raw(&data, "bad_id")),
    120             Err(Error::Unauthenticated)
    121         );
    122         assert_eq!(head.raw_json(), Some(pair[0]));
    123         assert_eq!(
    124             head.observe(pair[0]).unwrap(),
    125             EventHeadDecision::SkippedDuplicate
    126         );
    127     }
    128 }
    129 #[test]
    130 fn explicit_scopes_and_absence_never_fabricate_observations() {
    131     let data = fixture();
    132     let mut head = ObservedHead::new(scope(&data, 30402)).unwrap();
    133     assert!(head.raw_json().is_none());
    134     assert!(head.selected().is_none());
    135     assert!(head.interpret(UnixSeconds::new(0).unwrap()).is_none());
    136     assert_eq!(
    137         head.observe(raw(&data, "capability")),
    138         Err(Error::ScopeMismatch)
    139     );
    140     assert!(ObservedHead::new(scope(&data, 0)).is_err());
    141     assert!(head.raw_json().is_none());
    142 }