Skip to content

Commit 1b35dd3

Browse files
authored
Issue #68 Validator cli options (#69)
* validator - Cargo.toml - add `clap` & our `adapter` * validator - Add `clap` CLI options + fix a bug with workers * validator - infra - worker - pass `ticks_wait_time` `Duration` to `InfiniteWorker`
1 parent d7ef701 commit 1b35dd3

File tree

4 files changed

+178
-20
lines changed

4 files changed

+178
-20
lines changed

Cargo.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

validator/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ path = "src/lib.rs"
1111
[dependencies]
1212
# Domain
1313
domain = { version = "0.1", path = "../domain", features = ["repositories"] }
14+
adapter = { version = "0.1", path = "../adapter", features = ["dummy-adapter"] }
1415
# Futures
1516
futures-preview = { version = "=0.3.0-alpha.16", features = ["compat", "io-compat"] }
1617
futures_legacy = { version = "0.1", package = "futures" }
@@ -23,4 +24,6 @@ lazy_static = "1.3"
2324
dotenv = "0.14"
2425
# (De)Serialization
2526
serde = { version = "^1.0", features = ['derive'] }
26-
serde_json = "1.0"
27+
serde_json = "1.0"
28+
# CLI
29+
clap = "2.33.0"

validator/src/infrastructure/worker.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ pub mod single {
66

77
use futures::future::FutureExt;
88

9+
use domain::channel::SpecValidator;
10+
use domain::Channel;
11+
912
use crate::domain::channel::ChannelRepository;
1013
use crate::domain::validator::Validator;
1114
use crate::domain::{Worker, WorkerFuture};
1215
use crate::infrastructure::validator::follower::Follower;
1316
use crate::infrastructure::validator::leader::Leader;
14-
use domain::channel::SpecValidator;
15-
use domain::Channel;
1617

1718
#[derive(Clone)]
1819
pub struct TickWorker {
@@ -80,6 +81,7 @@ pub mod infinite {
8081
#[derive(Clone)]
8182
pub struct InfiniteWorker {
8283
pub tick_worker: TickWorker,
84+
pub ticks_wait_time: Duration,
8385
}
8486

8587
/// Infinite tick worker
@@ -88,8 +90,7 @@ pub mod infinite {
8890
let handle = self.clone();
8991
loop {
9092
let future = handle.clone().tick_worker.tick();
91-
// let tick_future = Delay::new(Instant::now().add(CONFIG.ticks_wait_time));
92-
let tick_future = Delay::new(Instant::now().add(Duration::from_millis(5000)));
93+
let tick_future = Delay::new(Instant::now().add(self.ticks_wait_time));
9394

9495
let joined = join(future, tick_future.compat());
9596

validator/src/main.rs

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22
#![deny(rust_2018_idioms)]
33
#![deny(clippy::all)]
44

5-
use futures::future::{FutureExt, TryFutureExt};
6-
use reqwest::r#async::Client;
7-
5+
use adapter::Adapter;
86
use lazy_static::lazy_static;
9-
use std::sync::Arc;
107
use std::time::Duration;
11-
use validator::domain::worker::Worker;
12-
use validator::infrastructure::persistence::channel::api::ApiChannelRepository;
13-
use validator::infrastructure::sentry::SentryApi;
14-
use validator::infrastructure::validator::{Follower, Leader};
15-
use validator::infrastructure::worker::{InfiniteWorker, TickWorker};
168

179
lazy_static! {
1810
static ref CONFIG: Config = {
@@ -33,25 +25,96 @@ lazy_static! {
3325
}
3426

3527
fn main() {
28+
use adapter::dummy::DummyAdapter;
29+
use adapter::ConfigBuilder;
30+
use clap::{App, Arg, SubCommand};
31+
use std::collections::HashMap;
32+
33+
let matches = App::new("Validator worker")
34+
.version("0.2")
35+
.arg(
36+
Arg::with_name("single-tick")
37+
.short("s")
38+
.help("Runs the validator in single-tick mode"),
39+
)
40+
.subcommand(
41+
SubCommand::with_name("dummy")
42+
.about("Runs the validator with the Dummy adapter")
43+
.arg(
44+
Arg::with_name("IDENTITY")
45+
.help("The dummy identity to be used for the validator")
46+
.required(true)
47+
.index(1),
48+
),
49+
)
50+
.get_matches();
51+
52+
let is_single_tick = matches.is_present("single-tick");
53+
54+
let adapter = match matches.subcommand_matches("dummy") {
55+
Some(dummy_matches) => {
56+
let identity = dummy_matches.value_of("IDENTITY").unwrap();
57+
58+
DummyAdapter {
59+
config: ConfigBuilder::new(identity).build(),
60+
participants: HashMap::default(),
61+
}
62+
}
63+
None => panic!("We don't have any other adapters implemented yet!"),
64+
};
65+
66+
run(is_single_tick, adapter);
67+
}
68+
69+
fn run(is_single_tick: bool, adapter: impl Adapter) {
70+
use futures::future::{FutureExt, TryFutureExt};
71+
use reqwest::r#async::Client;
72+
73+
use std::sync::Arc;
74+
use validator::domain::worker::Worker;
75+
use validator::infrastructure::persistence::channel::api::ApiChannelRepository;
76+
use validator::infrastructure::sentry::SentryApi;
77+
use validator::infrastructure::validator::{Follower, Leader};
78+
use validator::infrastructure::worker::{InfiniteWorker, TickWorker};
79+
3680
let sentry = SentryApi {
3781
client: Client::new(),
3882
sentry_url: CONFIG.sentry_url.clone(),
3983
};
4084

41-
let channel_repository = Arc::new(ApiChannelRepository {
42-
sentry: sentry.clone(),
43-
});
85+
let channel_repository = Arc::new(ApiChannelRepository { sentry });
4486

4587
let tick_worker = TickWorker {
4688
leader: Leader {},
4789
follower: Follower {},
4890
channel_repository,
49-
identity: "0x2892f6C41E0718eeeDd49D98D648C789668cA67d".to_string(),
91+
identity: adapter.config().identity.to_string(),
5092
};
5193

52-
let worker = InfiniteWorker { tick_worker };
94+
if !is_single_tick {
95+
let worker = InfiniteWorker {
96+
tick_worker,
97+
ticks_wait_time: CONFIG.ticks_wait_time,
98+
};
5399

54-
tokio::run(worker.run().boxed().compat());
100+
tokio::run(
101+
async move {
102+
await!(worker.run()).unwrap();
103+
}
104+
.unit_error()
105+
.boxed()
106+
.compat(),
107+
);
108+
} else {
109+
tokio::run(
110+
async move {
111+
await!(tick_worker.run()).unwrap();
112+
}
113+
.unit_error()
114+
.boxed()
115+
.compat(),
116+
);
117+
}
55118
}
56119

57120
struct Config {

0 commit comments

Comments
 (0)