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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! M4 acceptance battery: real multi-process integration over gRPC.
//!
//! Covers: N peers converging on every transaction; kill -9 of the
//! transactor mid-load with zero acked-transaction loss; gapless reconnect
//! backfill; deposed-transactor fencing (a paused process cannot publish);
//! TLS and bearer-token auth; and direct segment reads through the peer
//! segment cache.

use std::collections::BTreeSet;
use std::net::TcpListener;
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::time::{Duration, Instant};

use corium_core::IndexOrder;
use corium_peer::segment::SegmentSource;
use corium_peer::{Admin, ConnectConfig, Connection};
use corium_query::edn::{Edn, read_one};
use corium_store::{BlobStore, DbRoot, FsStore, RootStore};

const SCHEMA: &str = r"[{:db/ident :k/v :db/valueType :db.type/long
                          :db/cardinality :db.cardinality/one}]";

fn schema_forms() -> Vec<Edn> {
    match read_one(SCHEMA).expect("schema EDN") {
        Edn::Vector(items) => items,
        other => panic!("bad schema {other}"),
    }
}

fn free_port() -> u16 {
    TcpListener::bind("127.0.0.1:0")
        .expect("bind")
        .local_addr()
        .expect("addr")
        .port()
}

/// A transactor child process, killed on drop.
struct TransactorProc {
    child: Child,
    port: u16,
}

impl TransactorProc {
    fn spawn(data_dir: &Path, port: u16, extra: &[&str]) -> Self {
        let child = Command::new(env!("CARGO_BIN_EXE_corium"))
            .arg("transactor")
            .arg("--data-dir")
            .arg(data_dir)
            .arg("--listen")
            .arg(format!("127.0.0.1:{port}"))
            .args(extra)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn transactor");
        Self { child, port }
    }

    fn endpoint(&self) -> String {
        format!("http://127.0.0.1:{}", self.port)
    }

    fn pid(&self) -> u32 {
        self.child.id()
    }

    fn kill9(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }

    fn signal(&self, name: &str) {
        let status = Command::new("kill")
            .arg(format!("-{name}"))
            .arg(self.pid().to_string())
            .status()
            .expect("send signal");
        assert!(status.success(), "kill -{name} failed");
    }

    async fn wait_ready(&self) -> Admin {
        let deadline = Instant::now() + Duration::from_secs(20);
        loop {
            if let Ok(mut admin) = Admin::connect(&self.endpoint(), None, None).await {
                if admin.list_databases().await.is_ok() {
                    return admin;
                }
            }
            assert!(
                Instant::now() < deadline,
                "transactor on port {} never became ready",
                self.port
            );
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }
}

impl Drop for TransactorProc {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

fn add_value(value: i64) -> Vec<Edn> {
    vec![Edn::Vector(vec![
        Edn::keyword("db/add"),
        Edn::Str("e".into()),
        Edn::keyword("k/v"),
        Edn::Long(value),
    ])]
}

fn long_values(db: &corium_db::Db) -> Vec<i64> {
    db.datoms()
        .into_iter()
        .filter_map(|datom| match datom.v {
            corium_core::Value::Long(v) => Some(v),
            _ => None,
        })
        .collect()
}

#[tokio::test(flavor = "multi_thread")]
async fn n_peers_converge_and_reconnect_backfills_gaplessly() {
    let dir = tempfile::tempdir().expect("tempdir");
    let data = dir.path().join("data");
    let port = free_port();
    let mut proc = TransactorProc::spawn(&data, port, &["--index-interval-ms", "300"]);
    let mut admin = proc.wait_ready().await;
    assert!(
        admin
            .create_database("converge", &schema_forms())
            .await
            .expect("create db")
    );

    let mut peers = Vec::new();
    for _ in 0..3 {
        peers.push(
            Connection::connect(ConnectConfig::new(proc.endpoint(), "converge"))
                .await
                .expect("peer connects"),
        );
    }
    let mut reports = peers[1].tx_reports();
    for value in 0..30 {
        peers[0].transact(add_value(value)).await.expect("transact");
    }
    // Every peer converges on every transaction.
    for peer in &peers {
        let db = peer.sync().await.expect("sync");
        assert_eq!(db.basis_t(), 30);
        let mut values = long_values(&db);
        values.sort_unstable();
        assert_eq!(values, (0..30).collect::<Vec<_>>());
    }
    // The tx-report queue observed a gapless, ordered t sequence.
    let mut seen = Vec::new();
    while let Ok(report) = reports.try_recv() {
        seen.push(report.t);
    }
    assert_eq!(seen, (1..=30).collect::<Vec<_>>());

    // Kill -9, restart on the same port and directory: peers reconnect,
    // resubscribe from their basis, and the server backfills the gap.
    proc.kill9();
    let proc = TransactorProc::spawn(&data, port, &["--index-interval-ms", "300"]);
    let _admin = proc.wait_ready().await;
    // New transactions committed while peers are still backing off.
    let writer = Connection::connect(ConnectConfig::new(proc.endpoint(), "converge"))
        .await
        .expect("writer reconnects");
    for value in 30..40 {
        writer.transact(add_value(value)).await.expect("transact");
    }
    for peer in &peers {
        let deadline = Instant::now() + Duration::from_secs(20);
        loop {
            if peer.basis_t() >= 40 {
                break;
            }
            assert!(Instant::now() < deadline, "peer never caught up");
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
        let db = peer.db();
        let mut values = long_values(&db);
        values.sort_unstable();
        assert_eq!(values, (0..40).collect::<Vec<_>>(), "gap after reconnect");
        // Local record of every t with no holes.
        let ts: Vec<u64> = db.tx_range(0, None).into_iter().map(|(t, _)| t).collect();
        assert_eq!(ts, (1..=40).collect::<Vec<_>>());
    }

    // Segment cache: read the published index root directly from storage and
    // compare its EAVT keys with the converged peer value at that basis.
    let deadline = Instant::now() + Duration::from_secs(20);
    let source = SegmentSource::new(Arc::new(
        FsStore::open(data.join("store")).expect("open store"),
    ));
    let root = loop {
        if let Some(root) = source.index_root("converge").expect("root") {
            if root.roots.is_some() && root.index_basis_t >= 40 {
                break root;
            }
        }
        assert!(Instant::now() < deadline, "index never published");
        tokio::time::sleep(Duration::from_millis(100)).await;
    };
    let segment = source
        .segment(&root, IndexOrder::Eavt)
        .expect("segment loads")
        .expect("segment present");
    let keys = SegmentSource::<FsStore>::segment_keys(&segment).expect("keys decode");
    let expected: Vec<Vec<u8>> = peers[0]
        .db()
        .as_of(root.index_basis_t)
        .datoms()
        .iter()
        .map(|datom| datom.key(IndexOrder::Eavt))
        .collect();
    assert_eq!(keys, expected, "published segment diverges from peer state");
}

#[tokio::test(flavor = "multi_thread")]
async fn kill9_mid_load_loses_no_acked_transaction() {
    let dir = tempfile::tempdir().expect("tempdir");
    let data = dir.path().join("data");
    let port = free_port();
    let mut proc = TransactorProc::spawn(&data, port, &[]);
    let mut admin = proc.wait_ready().await;
    admin
        .create_database("main", &schema_forms())
        .await
        .expect("create db");
    let client = Connection::connect(ConnectConfig::new(proc.endpoint(), "main"))
        .await
        .expect("connect");

    // Load transactions; kill -9 fires mid-stream from another task.
    let killer = {
        let pid = proc.pid();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(400)).await;
            let _ = Command::new("kill")
                .arg("-KILL")
                .arg(pid.to_string())
                .status();
        })
    };
    let mut acked = Vec::new();
    for value in 0..10_000 {
        let forms = codec_encode(add_value(value));
        match client.transact_raw(forms).await {
            Ok(response) => {
                assert!(response.basis_t > 0);
                acked.push(value);
            }
            Err(_) => break,
        }
    }
    killer.await.expect("killer task");
    proc.kill9();
    assert!(
        !acked.is_empty(),
        "no transaction was acked before the kill"
    );

    // Restart and read back through a fresh peer.
    let proc = TransactorProc::spawn(&data, port, &[]);
    let _admin = proc.wait_ready().await;
    let peer = Connection::connect(ConnectConfig::new(proc.endpoint(), "main"))
        .await
        .expect("reconnect");
    let db = peer.sync().await.expect("sync");
    let committed = long_values(&db);
    let committed_set: BTreeSet<i64> = committed.iter().copied().collect();
    assert_eq!(
        committed.len(),
        committed_set.len(),
        "recovery duplicated a transaction"
    );
    for value in &acked {
        assert!(
            committed_set.contains(value),
            "acked transaction {value} was lost by kill -9"
        );
    }
    // At most one in-flight (durable but unacked) transaction may trail.
    assert!(committed.len() <= acked.len() + 1);
}

fn codec_encode(forms: Vec<Edn>) -> Vec<u8> {
    corium_protocol::codec::encode_edn(&Edn::Vector(forms))
}

#[tokio::test(flavor = "multi_thread")]
async fn deposed_transactor_cannot_publish_or_commit() {
    let dir = tempfile::tempdir().expect("tempdir");
    let data = dir.path().join("data");
    let port_a = free_port();
    let flags_a = [
        "--owner",
        "owner-a",
        "--lease-ttl-ms",
        "800",
        "--index-interval-ms",
        "200",
    ];
    let proc_a = TransactorProc::spawn(&data, port_a, &flags_a);
    let mut admin_a = proc_a.wait_ready().await;
    admin_a
        .create_database("fenced", &schema_forms())
        .await
        .expect("create db");
    let client_a = Connection::connect(ConnectConfig::new(proc_a.endpoint(), "fenced"))
        .await
        .expect("connect A");
    for value in 0..3 {
        client_a.transact(add_value(value)).await.expect("transact");
    }

    // Pause A (GC-pause stand-in) and let its lease expire.
    proc_a.signal("STOP");

    let port_b = free_port();
    let proc_b = TransactorProc::spawn(
        &data,
        port_b,
        &[
            "--owner",
            "owner-b",
            "--lease-ttl-ms",
            "800",
            "--lease-wait-ms",
            "20000",
            "--index-interval-ms",
            "200",
        ],
    );
    let _admin_b = proc_b.wait_ready().await;
    let client_b = Connection::connect(ConnectConfig::new(proc_b.endpoint(), "fenced"))
        .await
        .expect("connect B");
    client_b.transact(add_value(100)).await.expect("B commits");

    // Wait for B to publish an index under its (newer) lease version.
    let store = FsStore::open(data.join("store")).expect("open store");
    let deadline = Instant::now() + Duration::from_secs(20);
    let fenced_root = loop {
        let root = store
            .get_root(&corium_store::db_root_name("fenced"))
            .expect("read root")
            .as_deref()
            .and_then(DbRoot::decode)
            .expect("decodable root");
        if root.roots.is_some() && root.index_basis_t >= 4 {
            break root;
        }
        assert!(Instant::now() < deadline, "B never published");
        tokio::time::sleep(Duration::from_millis(100)).await;
    };
    assert!(
        fenced_root.lease_version >= 2,
        "B must fence with a newer lease"
    );

    // Wake the deposed transactor. It must refuse to commit and must not
    // regress the published root; the process shuts itself down.
    proc_a.signal("CONT");
    let refused = client_a.transact(add_value(999));
    let refused = tokio::time::timeout(Duration::from_secs(10), refused).await;
    if let Ok(Ok(result)) = refused {
        panic!("deposed transactor acked t={}", result.basis_t);
    }
    tokio::time::sleep(Duration::from_secs(2)).await;
    let root_after = store
        .get_root(&corium_store::db_root_name("fenced"))
        .expect("read root")
        .as_deref()
        .and_then(DbRoot::decode)
        .expect("decodable root");
    assert!(
        root_after.lease_version >= fenced_root.lease_version
            && root_after.index_basis_t >= fenced_root.index_basis_t,
        "deposed transactor regressed the published root"
    );
    // B remains healthy and its history contains no datoms from A's refused
    // transaction.
    let db = client_b.sync().await.expect("B sync");
    assert!(!long_values(&db).contains(&999));
}

#[tokio::test(flavor = "multi_thread")]
async fn tls_and_bearer_token_auth_guard_every_service() {
    let dir = tempfile::tempdir().expect("tempdir");
    let data = dir.path().join("data");
    // Self-signed certificate for localhost.
    let certified =
        rcgen::generate_simple_self_signed(vec!["localhost".into()]).expect("generate certificate");
    let cert_path = dir.path().join("cert.pem");
    let key_path = dir.path().join("key.pem");
    std::fs::write(&cert_path, certified.cert.pem()).expect("write cert");
    std::fs::write(&key_path, certified.key_pair.serialize_pem()).expect("write key");

    let port = free_port();
    let proc = TransactorProc::spawn(
        &data,
        port,
        &[
            "--serve-token",
            "secret-token",
            "--tls-cert",
            cert_path.to_str().expect("utf8"),
            "--tls-key",
            key_path.to_str().expect("utf8"),
        ],
    );
    let endpoint = format!("https://localhost:{port}");
    let tls =
        corium_protocol::auth::client_tls(Some(&cert_path), Some("localhost")).expect("client tls");

    // Correct token over TLS succeeds.
    let deadline = Instant::now() + Duration::from_secs(20);
    let mut admin = loop {
        if let Ok(mut admin) =
            Admin::connect(&endpoint, Some("secret-token".into()), Some(tls.clone())).await
        {
            if admin.list_databases().await.is_ok() {
                break admin;
            }
        }
        assert!(Instant::now() < deadline, "TLS transactor never ready");
        tokio::time::sleep(Duration::from_millis(100)).await;
    };
    assert!(
        admin
            .create_database("secure", &schema_forms())
            .await
            .expect("authorized create")
    );
    let mut config = ConnectConfig::new(endpoint.clone(), "secure");
    config.token = Some("secret-token".into());
    config.tls = Some(tls.clone());
    let peer = Connection::connect(config)
        .await
        .expect("TLS peer connects");
    peer.transact(add_value(1)).await.expect("TLS transact");

    // Wrong token is rejected.
    let mut admin = Admin::connect(&endpoint, Some("wrong".into()), Some(tls.clone()))
        .await
        .expect("channel still opens");
    let denied = admin.list_databases().await;
    match denied {
        Err(corium_peer::PeerError::Rpc(status)) => {
            assert_eq!(status.code(), tonic::Code::Unauthenticated);
        }
        other => panic!("wrong token was not rejected: {other:?}"),
    }

    // Plaintext to a TLS port fails outright.
    let plain = Admin::connect(&format!("http://localhost:{port}"), None, None).await;
    if let Ok(mut admin) = plain {
        assert!(admin.list_databases().await.is_err());
    }
    drop(proc);
}

#[tokio::test(flavor = "multi_thread")]
async fn cli_admin_commands_round_trip() {
    let dir = tempfile::tempdir().expect("tempdir");
    let data = dir.path().join("data");
    let port = free_port();
    let proc = TransactorProc::spawn(&data, port, &[]);
    let _admin = proc.wait_ready().await;
    let schema_path = dir.path().join("schema.edn");
    std::fs::write(&schema_path, SCHEMA).expect("write schema");

    let corium = env!("CARGO_BIN_EXE_corium");
    let run = |args: Vec<String>| {
        let output = Command::new(corium)
            .args(&args)
            .output()
            .expect("run corium");
        assert!(
            output.status.success(),
            "corium {args:?} failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        String::from_utf8_lossy(&output.stdout).into_owned()
    };
    let endpoint = proc.endpoint();
    let created = run(vec![
        "db".into(),
        "create".into(),
        "clidb".into(),
        "--schema".into(),
        schema_path.display().to_string(),
        "--transactor".into(),
        endpoint.clone(),
    ]);
    assert!(created.contains(":created true"), "{created}");
    let listed = run(vec![
        "db".into(),
        "list".into(),
        "--transactor".into(),
        endpoint.clone(),
    ]);
    assert!(listed.contains("clidb"), "{listed}");

    let peer = Connection::connect(ConnectConfig::new(endpoint.clone(), "clidb"))
        .await
        .expect("connect");
    peer.transact(add_value(7)).await.expect("transact");

    let stats = run(vec![
        "db".into(),
        "stats".into(),
        "clidb".into(),
        "--transactor".into(),
        endpoint.clone(),
    ]);
    assert!(
        stats.contains(":basis-t 1") && stats.contains(":datoms 1"),
        "{stats}"
    );

    // Offline log inspection sees the committed record.
    let logged = run(vec![
        "log".into(),
        "--data-dir".into(),
        data.display().to_string(),
        "--db".into(),
        "clidb".into(),
    ]);
    assert!(logged.contains(":t 1"), "{logged}");

    let deleted = run(vec![
        "db".into(),
        "delete".into(),
        "clidb".into(),
        "--transactor".into(),
        endpoint.clone(),
    ]);
    assert!(deleted.contains(":deleted true"), "{deleted}");
    // An explicit zero retention reaches the online GC path as an immediate
    // sweep rather than being confused with the omitted/default window.
    let store = FsStore::open(data.join("store")).expect("open store");
    let orphan = store.put(b"online-gc-orphan").expect("put orphan");
    let swept = run(vec![
        "gc".into(),
        "--transactor".into(),
        endpoint,
        "--window".into(),
        "0".into(),
    ]);
    assert!(swept.contains(":swept"), "{swept}");
    assert!(!store.contains(&orphan).expect("orphan swept"));
}