tsoracle 2.3.2

Standalone timestamp oracle server
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
//
//  ░▀█▀░█▀▀░█▀█░█▀▄░█▀█░█▀▀░█░░░█▀▀
//  ░░█░░▀▀█░█░█░█▀▄░█▀█░█░░░█░░░█▀▀
//  ░░▀░░▀▀▀░▀▀▀░▀░▀░▀░▀░▀▀▀░▀▀▀░▀▀▀
//
//  tsoracle — Distributed Timestamp Oracle
//  https://www.tsoracle.rs
//
//  Copyright (c) 2026 Prisma Risk
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;

use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[command(name = "tsoracle", version, about = "Standalone timestamp oracle")]
pub struct Cli {
    #[command(subcommand)]
    pub cmd: Option<Cmd>,
    /// Default subcommand fields (mirrors `serve file`).
    #[command(flatten)]
    pub serve_file: FileArgs,
}

#[derive(Subcommand, Debug)]
pub enum Cmd {
    /// Run the timestamp oracle server with a selected driver.
    #[command(subcommand)]
    Serve(Box<ServeCmd>),
    /// Initialize a fresh file-driver state directory at a seeded high-water.
    Init(InitArgs),
    /// Administer cluster membership over the admin gRPC port.
    #[cfg(feature = "openraft")]
    #[command(subcommand)]
    Admin(AdminCmd),
}

#[cfg(feature = "openraft")]
#[derive(Subcommand, Debug)]
pub enum AdminCmd {
    /// List current members. With `--capabilities`, also gather and show each
    /// member's format-version capabilities.
    Members(MembersArgs),
    /// Add a non-voting learner.
    AddLearner(AddLearnerArgs),
    /// Promote a learner to voter.
    Promote(AdminIdArgs),
    /// Remove a node.
    Remove(AdminIdArgs),
    /// Initiate a format-version activation. Runs the all-members capability
    /// gate then proposes the bump via raft. Exit codes:
    /// 0=success, 2=gate-rejected (MEMBERS_BELOW_TARGET),
    /// 3=NOT_LEADER (this node is a follower),
    /// 4=local-range-rejected (TARGET_OUT_OF_RANGE — receiving node's
    /// MAX_READABLE_VERSION < target), 1=other failure.
    ActivateFormat(ActivateFormatArgs),
    /// Report every member's format-version capabilities (min/max readable and
    /// active write version). Read-only; answerable by any node. Use before
    /// `activate-format` to confirm the cluster is ready for a target version.
    Capabilities(CapabilitiesArgs),
}

#[cfg(feature = "openraft")]
#[derive(Parser, Debug, Clone)]
pub struct AdminClientTlsArgs {
    /// PEM client certificate to present to the admin gRPC server (admin mTLS).
    #[arg(long)]
    pub client_tls_cert: Option<std::path::PathBuf>,
    /// PEM private key for `--client-tls-cert`.
    #[arg(long)]
    pub client_tls_key: Option<std::path::PathBuf>,
    /// PEM CA to verify the admin server's certificate.
    #[arg(long)]
    pub client_tls_ca: Option<std::path::PathBuf>,
}

#[cfg(feature = "openraft")]
#[derive(Parser, Debug)]
pub struct MembersArgs {
    /// Any node's admin endpoint, e.g. `https://127.0.0.1:50561`.
    #[arg(long)]
    pub endpoint: String,
    /// Also gather and display each member's format-version capabilities.
    #[arg(long)]
    pub capabilities: bool,
    #[command(flatten)]
    pub tls: AdminClientTlsArgs,
}

#[cfg(feature = "openraft")]
#[derive(Parser, Debug)]
pub struct CapabilitiesArgs {
    /// Any node's admin endpoint, e.g. `https://127.0.0.1:50561`.
    #[arg(long)]
    pub endpoint: String,
    /// Emit the report as JSON instead of the aligned table.
    #[arg(long)]
    pub json: bool,
    #[command(flatten)]
    pub tls: AdminClientTlsArgs,
}

#[cfg(feature = "openraft")]
#[derive(Parser, Debug)]
pub struct AdminIdArgs {
    #[arg(long)]
    pub endpoint: String,
    #[arg(long)]
    pub id: u64,
    #[command(flatten)]
    pub tls: AdminClientTlsArgs,
}

#[cfg(feature = "openraft")]
#[derive(Parser, Debug)]
pub struct AddLearnerArgs {
    #[arg(long)]
    pub endpoint: String,
    #[arg(long)]
    pub id: u64,
    #[arg(long)]
    pub raft_addr: String,
    #[arg(long)]
    pub service_endpoint: String,
    #[arg(long)]
    pub admin_endpoint: String,
    #[command(flatten)]
    pub tls: AdminClientTlsArgs,
}

#[cfg(feature = "openraft")]
#[derive(Parser, Debug)]
pub struct ActivateFormatArgs {
    /// Admin endpoint of any cluster member. `NOT_LEADER` responses for
    /// activation carry an empty `leader_admin_endpoint` (the underlying
    /// `FormatActivationError::NotLeader` is a unit variant), so the
    /// existing `with_redirect` short-circuits and the CLI exits 3 rather
    /// than auto-redirecting. Re-issue against a known leader.
    #[arg(long)]
    pub endpoint: String,
    /// Target format version. Must be within the local binary's readable
    /// range and supported by every cluster member.
    #[arg(long)]
    pub target: u8,
    #[command(flatten)]
    pub tls: AdminClientTlsArgs,
}

#[derive(Subcommand, Debug)]
pub enum ServeCmd {
    /// Single-node, fsync-durable file driver (default).
    File(FileArgs),
    /// HA via openraft (3+ node cluster).
    Openraft(OpenraftArgs),
    /// HA via OmniPaxos (3+ node cluster).
    Paxos(PaxosArgs),
}

/// Knobs shared by every serve subcommand; feed the bin's `Server`, not `DriverConfig`.
#[derive(Parser, Debug, Clone)]
pub struct CommonServeArgs {
    /// Client-facing gRPC listen address.
    #[arg(long, default_value = "127.0.0.1:50551")]
    pub listen: SocketAddr,
    /// How far ahead to allocate windows.
    #[arg(long, value_parser = parse_duration, default_value = "3s")]
    pub window_ahead: Duration,
    /// Advance on leadership gain.
    #[arg(long, value_parser = parse_duration, default_value = "1s")]
    pub failover_advance: Duration,
    /// Server-enforced lower bound on requested lease TTLs.
    #[arg(long, value_parser = parse_duration, default_value = "5s")]
    pub lease_ttl_floor: Duration,
    /// Server-enforced upper bound on requested lease TTLs.
    #[arg(long, value_parser = parse_duration, default_value = "300s")]
    pub lease_ttl_ceiling: Duration,
    /// Interval between proof-of-life heartbeat log lines. Default 10s.
    /// Pass `0s` to disable.
    #[arg(long, value_parser = parse_duration, default_value = "10s")]
    pub heartbeat_interval: Duration,
    /// Log level.
    #[arg(long, default_value = "info")]
    pub log: String,
    /// Prometheus /metrics listen address.
    #[cfg(feature = "metrics")]
    #[arg(long, default_value = "127.0.0.1:9551")]
    pub metrics_listen: SocketAddr,
    /// Disable the Prometheus metrics exporter. Accepted in every build so
    /// scripts and test harnesses can pass it unconditionally; it is a no-op
    /// when the binary is compiled without the `metrics` feature.
    #[arg(long)]
    pub no_metrics: bool,
    /// PEM server certificate chain for the client gRPC API (enables TLS).
    #[arg(long)]
    pub tls_cert: Option<std::path::PathBuf>,
    /// PEM private key for `--tls-cert`.
    #[arg(long)]
    pub tls_key: Option<std::path::PathBuf>,
    /// PEM CA to verify CLIENT certificates (enables client mTLS on the API).
    #[arg(long)]
    pub tls_client_ca: Option<std::path::PathBuf>,
}

#[derive(Parser, Debug, Clone)]
pub struct FileArgs {
    #[command(flatten)]
    pub common: CommonServeArgs,
    /// Where to persist window state.
    #[arg(long, default_value = "./tsoracle-data")]
    pub state_dir: PathBuf,
}

#[derive(Parser, Debug, Clone)]
pub struct OpenraftArgs {
    #[command(flatten)]
    pub common: CommonServeArgs,
    /// This node's numeric raft id (unique across the cluster).
    #[arg(long)]
    pub id: u64,
    /// Address to listen for raft peer RPCs. Plaintext on loopback; routable bind requires --peer-tls-{cert,key,ca} or --allow-insecure-peer.
    #[arg(long)]
    pub raft_addr: SocketAddr,
    /// Directory for raft log + state-machine data.
    #[arg(long)]
    pub raft_dir: PathBuf,
    /// Initialize the cluster on exactly one node, first boot only.
    #[arg(long)]
    pub bootstrap: bool,
    /// Initial membership, ONLY with --bootstrap: `id=raft_host:port/service_host:port/admin_host:port,...`.
    #[arg(long)]
    pub members: Option<String>,
    #[arg(long, default_value = "250")]
    pub heartbeat_ms: u64,
    #[arg(long, default_value = "1000")]
    pub election_min_ms: u64,
    #[arg(long, default_value = "2000")]
    pub election_max_ms: u64,
    /// Bind address for the membership-admin gRPC server. UNAUTHENTICATED plaintext on loopback; non-loopback bind requires --admin-tls-{cert,key,ca}. Omit to serve no admin surface.
    #[arg(long)]
    pub admin_listen: Option<SocketAddr>,
    /// PEM server certificate for the admin gRPC server (enables admin mTLS; needs all three).
    #[arg(long)]
    pub admin_tls_cert: Option<std::path::PathBuf>,
    /// PEM private key for `--admin-tls-cert`.
    #[arg(long)]
    pub admin_tls_key: Option<std::path::PathBuf>,
    /// PEM CA (operator-dedicated, NOT the peer CA) to verify connecting admin clients.
    #[arg(long)]
    pub admin_tls_ca: Option<std::path::PathBuf>,
    /// PEM node certificate for the peer transport (enables peer mTLS; needs all three).
    #[arg(long)]
    pub peer_tls_cert: Option<std::path::PathBuf>,
    /// PEM private key for `--peer-tls-cert`.
    #[arg(long)]
    pub peer_tls_key: Option<std::path::PathBuf>,
    /// PEM CA (cluster-dedicated) to verify connecting peers.
    #[arg(long)]
    pub peer_tls_ca: Option<std::path::PathBuf>,
    /// Opt out of the peer-listener secure-by-default guard. Allows
    /// routable bind without --peer-tls-*. Matches the helm chart's
    /// tls.allowInsecurePeer. Intended for single-host dev or
    /// service-mesh-terminated mTLS only.
    #[arg(long)]
    pub allow_insecure_peer: bool,
}

#[derive(Parser, Debug, Clone)]
pub struct PaxosArgs {
    #[command(flatten)]
    pub common: CommonServeArgs,
    /// This node's OmniPaxos pid (unique across the cluster).
    #[arg(long)]
    pub node_id: u64,
    /// Address to listen for paxos peer RPCs. Plaintext on loopback; routable bind requires --peer-tls-{cert,key,ca} or --allow-insecure-peer.
    #[arg(long)]
    pub peer_listen: SocketAddr,
    /// Comma-separated `id=host:port` paxos peer addresses (required every start).
    #[arg(long)]
    pub peers: String,
    /// Comma-separated `id=host:port` tsoracle service addresses for LeaderHint redirect.
    #[arg(long)]
    pub tso_peers: String,
    /// Directory for the paxos log + meta.
    #[arg(long)]
    pub data_dir: PathBuf,
    #[arg(long, value_parser = parse_duration, default_value = "20ms")]
    pub tick_interval: Duration,
    /// PEM node certificate for the peer transport (enables peer mTLS; needs all three).
    #[arg(long)]
    pub peer_tls_cert: Option<std::path::PathBuf>,
    /// PEM private key for `--peer-tls-cert`.
    #[arg(long)]
    pub peer_tls_key: Option<std::path::PathBuf>,
    /// PEM CA (cluster-dedicated) to verify connecting peers.
    #[arg(long)]
    pub peer_tls_ca: Option<std::path::PathBuf>,
    /// Opt out of the peer-listener secure-by-default guard. Allows
    /// routable bind without --peer-tls-*. Matches the helm chart's
    /// tls.allowInsecurePeer. Intended for single-host dev or
    /// service-mesh-terminated mTLS only.
    #[arg(long)]
    pub allow_insecure_peer: bool,
}

#[derive(Parser, Debug)]
pub struct InitArgs {
    #[arg(long, default_value = "./tsoracle-data")]
    pub state_dir: PathBuf,
    #[arg(long)]
    pub seed_physical_ms: u64,
}

pub fn parse_duration(input: &str) -> Result<Duration, String> {
    humantime::parse_duration(input).map_err(|e| e.to_string())
}

#[cfg(test)]
mod common_serve_args_tests {
    use super::{Cli, Cmd, ServeCmd};
    use clap::Parser;
    use std::time::Duration;

    #[test]
    fn lease_ttl_flags_parse_with_defaults_and_override() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "serve",
            "file",
            "--state-dir",
            "/tmp/tsoracle-data",
        ])
        .unwrap();
        let Some(Cmd::Serve(serve)) = cli.cmd else {
            panic!("expected serve command");
        };
        let ServeCmd::File(args) = *serve else {
            panic!("expected file args");
        };
        assert_eq!(args.common.lease_ttl_floor, Duration::from_secs(5));
        assert_eq!(args.common.lease_ttl_ceiling, Duration::from_secs(300));

        let cli = Cli::try_parse_from([
            "tsoracle",
            "serve",
            "file",
            "--lease-ttl-floor",
            "10s",
            "--lease-ttl-ceiling",
            "120s",
            "--state-dir",
            "/tmp/tsoracle-data",
        ])
        .unwrap();
        let Some(Cmd::Serve(serve)) = cli.cmd else {
            panic!("expected serve command");
        };
        let ServeCmd::File(args) = *serve else {
            panic!("expected file args");
        };
        assert_eq!(args.common.lease_ttl_floor, Duration::from_secs(10));
        assert_eq!(args.common.lease_ttl_ceiling, Duration::from_secs(120));
    }
}

#[cfg(all(test, feature = "openraft"))]
mod admin_capabilities_parse_tests {
    use super::{AdminCmd, Cli, Cmd};
    use clap::Parser;

    #[test]
    fn capabilities_parses_endpoint() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "admin",
            "capabilities",
            "--endpoint",
            "http://127.0.0.1:51002",
        ])
        .unwrap();
        match cli.cmd {
            Some(Cmd::Admin(AdminCmd::Capabilities(args))) => {
                assert_eq!(args.endpoint, "http://127.0.0.1:51002");
                assert!(!args.json);
            }
            other => panic!("expected Capabilities, got {other:?}"),
        }
    }

    #[test]
    fn capabilities_json_flag_sets_true() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "admin",
            "capabilities",
            "--endpoint",
            "http://x",
            "--json",
        ])
        .unwrap();
        match cli.cmd {
            Some(Cmd::Admin(AdminCmd::Capabilities(args))) => assert!(args.json),
            other => panic!("expected Capabilities, got {other:?}"),
        }
    }

    #[test]
    fn members_capabilities_flag_sets_true() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "admin",
            "members",
            "--endpoint",
            "http://x",
            "--capabilities",
        ])
        .unwrap();
        match cli.cmd {
            Some(Cmd::Admin(AdminCmd::Members(args))) => {
                assert_eq!(args.endpoint, "http://x");
                assert!(args.capabilities);
            }
            other => panic!("expected Members, got {other:?}"),
        }
    }
}

#[cfg(all(test, feature = "metrics"))]
mod metrics_args_tests {
    use super::{Cli, Cmd, ServeCmd};
    use clap::Parser;
    use std::net::SocketAddr;

    #[test]
    fn serve_metrics_args_default_to_loopback_exporter() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "serve",
            "file",
            "--state-dir",
            "/tmp/tsoracle-data",
        ])
        .unwrap();
        let Some(Cmd::Serve(serve)) = cli.cmd else {
            panic!("expected serve command");
        };
        let ServeCmd::File(args) = *serve else {
            panic!("expected file args");
        };
        assert_eq!(
            args.common.metrics_listen,
            SocketAddr::from(([127, 0, 0, 1], 9551))
        );
        assert!(!args.common.no_metrics);
    }

    #[test]
    fn serve_metrics_args_allow_override_and_disable() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "serve",
            "file",
            "--metrics-listen",
            "0.0.0.0:9551",
            "--no-metrics",
            "--state-dir",
            "/tmp/tsoracle-data",
        ])
        .unwrap();
        let Some(Cmd::Serve(serve)) = cli.cmd else {
            panic!("expected serve command");
        };
        let ServeCmd::File(args) = *serve else {
            panic!("expected file args");
        };
        assert_eq!(
            args.common.metrics_listen,
            SocketAddr::from(([0, 0, 0, 0], 9551))
        );
        assert!(args.common.no_metrics);
    }
}

#[cfg(all(test, not(feature = "metrics")))]
mod no_metrics_flag_without_feature_tests {
    use super::{Cli, Cmd, ServeCmd};
    use clap::Parser;

    // The stress process harness (and other scripts) pass `--no-metrics`
    // unconditionally; it must parse even in builds compiled without the
    // `metrics` feature, where it is a documented no-op.
    #[test]
    fn serve_accepts_no_metrics_without_feature() {
        let cli = Cli::try_parse_from([
            "tsoracle",
            "serve",
            "file",
            "--no-metrics",
            "--state-dir",
            "/tmp/tsoracle-data",
        ])
        .unwrap();
        let Some(Cmd::Serve(serve)) = cli.cmd else {
            panic!("expected serve command");
        };
        let ServeCmd::File(args) = *serve else {
            panic!("expected file args");
        };
        assert!(args.common.no_metrics);
    }
}