2
2
#![ deny( rust_2018_idioms) ]
3
3
#![ deny( clippy:: all) ]
4
4
5
- use futures:: future:: { FutureExt , TryFutureExt } ;
6
- use reqwest:: r#async:: Client ;
7
-
5
+ use adapter:: Adapter ;
8
6
use lazy_static:: lazy_static;
9
- use std:: sync:: Arc ;
10
7
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 } ;
16
8
17
9
lazy_static ! {
18
10
static ref CONFIG : Config = {
@@ -33,25 +25,96 @@ lazy_static! {
33
25
}
34
26
35
27
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
+
36
80
let sentry = SentryApi {
37
81
client : Client :: new ( ) ,
38
82
sentry_url : CONFIG . sentry_url . clone ( ) ,
39
83
} ;
40
84
41
- let channel_repository = Arc :: new ( ApiChannelRepository {
42
- sentry : sentry. clone ( ) ,
43
- } ) ;
85
+ let channel_repository = Arc :: new ( ApiChannelRepository { sentry } ) ;
44
86
45
87
let tick_worker = TickWorker {
46
88
leader : Leader { } ,
47
89
follower : Follower { } ,
48
90
channel_repository,
49
- identity : "0x2892f6C41E0718eeeDd49D98D648C789668cA67d" . to_string ( ) ,
91
+ identity : adapter . config ( ) . identity . to_string ( ) ,
50
92
} ;
51
93
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
+ } ;
53
99
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
+ }
55
118
}
56
119
57
120
struct Config {
0 commit comments