stratum-node-core 0.1.0-alpha.4

Zero-I/O core types for Stratum pod nodes: did:nostr principals, typed identifiers, a strict NIP-98 signed-request verifier and bootstrap claim bodies
Documentation
//! The offered grant's exact bytes (E3b.35: the client and the controller
//! must render the same hash) and the schedule's period maths (E3b.20).

use stratum_node_core::{
    check_schedule_secs, render_offered, ContainerGrant, DidNostr, OfferedGrant, Schedule,
    Sha256Digest, UnixTime,
};

const APP: &str = "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5";
const OWNER: &str = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";

fn grants() -> Vec<ContainerGrant> {
    serde_json::from_str(
        r#"[{"path":"reports-input/","modes":["Read"],"operations":["GET","HEAD"]},
            {"path":"reports/","modes":["Read","Write"],"operations":["GET","HEAD","PUT"]}]"#,
    )
    .expect("grants")
}

#[test]
fn e3b_35_offered_grant_bytes_and_hash_are_pinned() {
    let app = DidNostr::from_hex(APP).expect("app");
    let owner = DidNostr::from_hex(OWNER).expect("owner");
    let offered = render_offered(&app, &owner, &grants());
    let first = format!(
        "@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n\
         \n\
         <#app-{APP}> a acl:Authorization ;\n    \
         acl:agent <did:nostr:{APP}> ;\n    \
         acl:default </pods/{OWNER}/reports-input/> ;\n    \
         acl:mode acl:Read .\n"
    );
    let second = format!(
        "@prefix acl: <http://www.w3.org/ns/auth/acl#> .\n\
         \n\
         <#app-{APP}> a acl:Authorization ;\n    \
         acl:agent <did:nostr:{APP}> ;\n    \
         acl:default </pods/{OWNER}/reports/> ;\n    \
         acl:mode acl:Read, acl:Write .\n"
    );
    assert_eq!(offered.containers[0].turtle, first, "first container");
    assert_eq!(offered.containers[1].turtle, second, "second container");
    assert_eq!(
        offered.turtle,
        format!("{first}\n{second}"),
        "joined by one blank line"
    );
    assert_eq!(
        offered.containers[0].sha256,
        Sha256Digest::of(first.as_bytes()),
        "per-container hash"
    );
    assert_eq!(
        offered.sha256,
        Sha256Digest::of(offered.turtle.as_bytes()),
        "combined hash"
    );
    assert_eq!(
        offered.sha256.to_hex(),
        "8e09552742f1101896ff6e4fdf8e8fd3f5b2467fba73e96d4513988687d233c8",
        "combined hash is pinned"
    );
}

#[test]
fn e3b_35_the_combined_grant_is_recomputed_from_stored_rows() {
    let app = DidNostr::from_hex(APP).expect("app");
    let owner = DidNostr::from_hex(OWNER).expect("owner");
    let offered = render_offered(&app, &owner, &grants());
    let rebuilt = OfferedGrant::from_containers(offered.containers.clone());
    assert_eq!(
        rebuilt, offered,
        "rows in manifest order give the same hash"
    );
    let mut swapped = offered.containers.clone();
    swapped.reverse();
    assert_ne!(
        OfferedGrant::from_containers(swapped).sha256,
        offered.sha256,
        "order matters"
    );
    let json = serde_json::to_value(&offered).expect("serialise");
    assert_eq!(
        json["containers"][1]["path"], "reports/",
        "path on the wire"
    );
    assert_eq!(json["sha256"], offered.sha256.to_hex(), "hash as hex");
}

#[test]
fn e3b_20_schedule_secs_is_zero_or_300_to_604800() {
    for ok in [0, 300, 3600, 604_800] {
        assert!(check_schedule_secs(ok).is_ok(), "{ok}");
    }
    for bad in [1, 299, 604_801, u64::MAX] {
        assert!(check_schedule_secs(bad).is_err(), "{bad}");
        assert!(Schedule::new(bad, UnixTime::from_secs(0)).is_err(), "{bad}");
    }
}

#[test]
fn e3b_20_the_anchor_is_the_activation_rounded_down_to_the_interval() {
    let s = Schedule::new(3600, UnixTime::from_secs(1_764_720_123)).expect("schedule");
    assert_eq!(
        s.anchor(),
        Some(UnixTime::from_secs(1_764_720_000)),
        "anchor"
    );
    let (start, end) = s.period_bounds(2).expect("bounds");
    assert_eq!(start.as_secs(), 1_764_720_000 + 2 * 3600, "period 2 start");
    assert_eq!(end.as_secs(), 1_764_720_000 + 3 * 3600, "period 2 end");
    let exact = Schedule::new(300, UnixTime::from_secs(600)).expect("schedule");
    assert_eq!(
        exact.anchor(),
        Some(UnixTime::from_secs(600)),
        "already aligned"
    );
}

#[test]
fn e3b_20_a_period_becomes_eligible_when_it_ends() {
    let s = Schedule::new(300, UnixTime::from_secs(1_000)).expect("schedule");
    assert_eq!(
        s.latest_ended(UnixTime::from_secs(899)),
        None,
        "before the anchor"
    );
    assert_eq!(
        s.latest_ended(UnixTime::from_secs(1_199)),
        None,
        "period 0 not ended"
    );
    assert_eq!(
        s.latest_ended(UnixTime::from_secs(1_200)),
        Some(0),
        "period 0 ended"
    );
    assert_eq!(
        s.latest_ended(UnixTime::from_secs(1_499)),
        Some(0),
        "period 1 running"
    );
    assert_eq!(
        s.latest_ended(UnixTime::from_secs(1_500)),
        Some(1),
        "period 1 ended"
    );
}

#[test]
fn e3b_20_only_the_latest_ended_period_runs_and_the_rest_are_skipped() {
    let s = Schedule::new(300, UnixTime::from_secs(900)).expect("schedule");
    let now = UnixTime::from_secs(900 + 10 * 300 + 7);
    let due = s.ended_periods(Some(3), now).expect("due");
    assert_eq!(due.run, 9, "latest ended");
    assert_eq!(due.skip, 4..9, "the others since the last job");
    let first = s.ended_periods(None, now).expect("due");
    assert_eq!((first.run, first.skip), (9, 0..9), "nothing ran yet");
    assert_eq!(s.ended_periods(Some(9), now), None, "nothing new");
    assert_eq!(
        s.ended_periods(Some(12), now),
        None,
        "a manual run ahead of time"
    );
    let next = s.ended_periods(Some(8), now).expect("one due");
    assert_eq!((next.run, next.skip), (9, 9..9), "no skip");
}

#[test]
fn e3b_20_a_manual_schedule_never_ends_a_period() {
    let s = Schedule::new(0, UnixTime::from_secs(1_000)).expect("manual");
    assert!(s.is_manual(), "manual");
    assert_eq!(s.period_bounds(0), None, "no bounds");
    assert_eq!(
        s.ended_periods(None, UnixTime::from_secs(u64::MAX)),
        None,
        "never due"
    );
}

#[test]
fn e3b_20_period_arithmetic_never_overflows() {
    let s = Schedule::new(604_800, UnixTime::from_secs(u64::MAX - 1)).expect("schedule");
    assert_eq!(s.period_bounds(u64::MAX), None, "overflow is None");
    assert_eq!(
        s.ended_periods(Some(u64::MAX), UnixTime::from_secs(u64::MAX)),
        None,
        "no wrap"
    );
}