aboutsummaryrefslogtreecommitdiffstats
path: root/src/simtest/signatures.rs
blob: cc087a9a62a6ea99f10be5a689faf46c7267b1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Signed changes end to end (`document::signatures`): nodes under the identity layer,
//! whose node keys sign, write through the API as a user would, and every node comes
//! to see the writes verified, by signatures that came with the changes in sync
//! messages.

use std::time::Duration;

use serde_json::json;

use super::{Sim, path};
use crate::document::ActorId;
use crate::document::signatures::{Sign, SignEvery};
use crate::document::store;
use crate::state::AppState;
use crate::swarm::bootstrap::Start;

const SOON: Duration = Duration::from_secs(20);

/// Writes `value` at `at` on `node` as a write through the API does, signed at once if
/// `signed` (`?signed=true`); the change's actor and seq.
async fn write_as_user(
    node: &AppState,
    at: &str,
    value: serde_json::Value,
    signed: bool,
) -> (ActorId, u64) {
    let at = path(at);
    let sign = Sign::Write {
        path: at.clone(),
        asked: signed,
    };
    node.mutate_doc_for(Some("alice".to_owned()), sign, move |doc| {
        store::write_path(doc, &at, &value)
    })
    .await
    .expect("write");
    let replica = node.doc.read().await;
    let change = replica.doc.get_last_local_change().expect("the write");
    (change.actor_id().clone(), change.seq())
}

/// Whether a signature vouches for `actor`'s change `seq` on `node`.
async fn verified(node: &AppState, actor: &ActorId, seq: u64) -> bool {
    node.doc.read().await.signatures.verified(actor, seq)
}

/// A write no rule signs is signed as a sync message takes it (`--sign-every sent`, the
/// default), and the signature reaches every node with it.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn writes_leave_signed() {
    let sim = Sim::with_identity(61);
    let a = sim.start("a", &[], Start::Genesis);
    let b = sim.start("b", &["a"], Start::Join);
    let c = sim.start("c", &["a"], Start::Join);
    sim.wait_ready(&[&a, &b, &c], SOON).await;

    let (actor, seq) = write_as_user(&b, "metrics/cpu", json!(42), false).await;
    sim.wait_converged(&[&a, &b, &c], SOON).await;
    for node in [&a, &b, &c] {
        let actor = actor.clone();
        sim.wait_until("the write is verified", SOON, move || {
            let actor = actor.clone();
            async move { verified(node, &actor, seq).await }
        })
        .await;
    }
}

/// With `--sign-every none`, a write no rule signs stays unverified; the node's next
/// signed write vouches for it too, wherever its signature goes.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_signed_write_vouches_for_the_writes_before() {
    let sim = Sim::with_identity(62);
    let a = sim.start("a", &[], Start::Genesis);
    let b = sim.start_with("b", &["a"], Start::Join, |config| {
        config.sign_every = SignEvery::Never;
    });
    let c = sim.start("c", &["a"], Start::Join);
    sim.wait_ready(&[&a, &b, &c], SOON).await;

    let (actor, first) = write_as_user(&b, "metrics/cpu", json!(42), false).await;
    sim.wait_converged(&[&a, &b, &c], SOON).await;
    // None is made for it, nor sent. Unless b wrote protocol data since (always
    // signed), whose signature vouches for it too.
    tokio::time::sleep(Duration::from_secs(3)).await;
    let latest = b
        .doc
        .read()
        .await
        .doc
        .get_last_local_change()
        .map(|change| change.seq());
    if latest == Some(first) {
        for node in [&a, &b, &c] {
            assert!(!verified(node, &actor, first).await);
        }
    }

    let (same, second) = write_as_user(&b, "config/mode", json!("fast"), true).await;
    assert_eq!(same, actor);
    sim.wait_converged(&[&a, &b, &c], SOON).await;
    for node in [&a, &b, &c] {
        let actor = actor.clone();
        sim.wait_until("both writes are verified", SOON, move || {
            let actor = actor.clone();
            async move {
                verified(node, &actor, first).await && verified(node, &actor, second).await
            }
        })
        .await;
    }
}

/// A node writing on as its writer of before holds its writes until it synced once with a
/// ready neighbour (`AppState::hold_writes`): a write waits while the node is alone, and
/// goes once a neighbour came and the node is level with it, before the wait ran out.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_kept_writer_writes_once_it_synced_with_a_neighbour() {
    let sim = Sim::new(62);
    let a = sim.start("a", &[], Start::Genesis);
    sim.wait_ready(&[&a], SOON).await;
    a.hold_writes();
    let writing = tokio::spawn({
        let a = a.clone();
        async move { super::write(&a, "users/alice", json!(1)).await }
    });
    tokio::time::sleep(Duration::from_secs(1)).await;
    assert!(*a.writes_held.borrow(), "writes go while alone");
    assert!(
        !writing.is_finished(),
        "a write went while writes were held"
    );

    let b = sim.start("b", &["a"], Start::Join);
    sim.wait_until(
        "a synced with b",
        crate::state::KEPT_WRITER_WAIT - Duration::from_secs(2),
        || async { !*a.writes_held.borrow() },
    )
    .await;
    tokio::time::timeout(SOON, writing)
        .await
        .expect("the write went")
        .expect("the write task");
    sim.wait_converged(&[&a, &b], SOON).await;
    assert_eq!(super::read(&b, "users/alice").await, Some(json!(1)));
}