minerva 0.2.0

Causal ordering for distributed systems
//! Example tests for the node store: deterministic scripts, each pinning one
//! documented behavior. The delta builders here compose bottom-up, the way a
//! caller assembles a write delta (build the child, insert it into a page
//! fragment, hand the fragment to [`Composer::compose`]).

use crate::metis::{Anchor, Dot, DotSet, Dotted, Kind, Locus};

use super::{Author, TestNode, rank};

mod construction;
mod depth;
mod dissolution;
mod kinds;
mod reassignment;

/// A one-write block delta: `kind` tagged under `dot`, nothing else.
fn tag_block(dot: Dot, kind: Kind) -> TestNode {
    let mut block = TestNode::new();
    assert!(block.write_tag(dot, kind));
    block
}

/// A one-write block delta: `value` registered under `dot`.
fn reg_block(dot: Dot, value: &'static str) -> TestNode {
    let mut block = TestNode::new();
    assert!(block.write_register(dot, value));
    block
}

/// A one-write block delta: `dot` woven at the origin with rank `n`.
fn seq_block(dot: Dot, n: u64) -> TestNode {
    let mut block = TestNode::new();
    assert!(block.weave(
        dot,
        Locus {
            anchor: Anchor::Origin,
            rank: rank(n),
        }
    ));
    block
}

/// A page delta: `block` under `key`, nothing else.
fn page_with(key: u8, block: TestNode) -> TestNode {
    let mut page = TestNode::new();
    assert!(page.insert_child(key, block));
    page
}

/// One anti-entropy step: ship `to` exactly what it is owed and absorb it.
fn sync(from: &Author, to: &mut Author) {
    let delta = from.owed_to(to.state().context());
    let _ = to.absorb(from.station(), &delta);
}

/// A pure-add write through the facade: build the delta store off the minted
/// dot, supersede nothing, return the delta for shipping.
fn add(author: &mut Author, build: impl FnOnce(Dot) -> TestNode) -> Dotted<TestNode> {
    let (_, delta) = author.compose(|dot| (build(dot), DotSet::new()));
    delta
}