@@ -31,99 +31,117 @@ pub(crate) mod monitor;
31
31
pub ( crate ) mod test_packet;
32
32
pub ( crate ) mod tested_network;
33
33
34
- pub ( crate ) struct NetworkMonitorRunnables {
35
- monitor : Monitor ,
36
- packet_receiver : PacketReceiver ,
34
+ pub ( crate ) struct NetworkMonitorBuilder < ' a > {
35
+ config : & ' a Config ,
36
+ tested_network : TestedNetwork ,
37
+ node_status_storage : NodeStatusStorage ,
38
+ validator_cache : ValidatorCache ,
37
39
}
38
40
39
- impl NetworkMonitorRunnables {
40
- // TODO: note, that is not exactly doing what we want, because when
41
- // `ReceivedProcessor` is constructed, it already spawns a future
42
- // this needs to be refactored!
43
- pub ( crate ) fn spawn_tasks ( self ) {
44
- let mut packet_receiver = self . packet_receiver ;
45
- let mut monitor = self . monitor ;
46
- tokio:: spawn ( async move { packet_receiver. run ( ) . await } ) ;
47
- tokio:: spawn ( async move { monitor. run ( ) . await } ) ;
41
+ impl < ' a > NetworkMonitorBuilder < ' a > {
42
+ pub ( crate ) fn new (
43
+ config : & ' a Config ,
44
+ v4_topology : NymTopology ,
45
+ v6_topology : NymTopology ,
46
+ node_status_storage : NodeStatusStorage ,
47
+ validator_cache : ValidatorCache ,
48
+ ) -> Self {
49
+ let tested_network = TestedNetwork :: new_good ( v4_topology, v6_topology) ;
50
+
51
+ NetworkMonitorBuilder {
52
+ config,
53
+ tested_network,
54
+ node_status_storage,
55
+ validator_cache,
56
+ }
48
57
}
49
- }
50
58
51
- pub ( crate ) async fn new_monitor_runnables (
52
- config : & Config ,
53
- v4_topology : NymTopology ,
54
- v6_topology : NymTopology ,
55
- node_status_storage : NodeStatusStorage ,
56
- validator_cache : ValidatorCache ,
57
- ) -> NetworkMonitorRunnables {
58
- // TODO: in the future I guess this should somehow change to distribute the load
59
- let tested_mix_gateway = v4_topology. gateways ( ) [ 0 ] . clone ( ) ;
60
- info ! (
61
- "* gateway for testing mixnodes: {}" ,
62
- tested_mix_gateway. identity_key. to_base58_string( )
63
- ) ;
59
+ pub ( crate ) async fn build ( self ) -> NetworkMonitorRunnables {
60
+ // TODO: in the future I guess this should somehow change to distribute the load
61
+ let tested_mix_gateway = self . tested_network . main_v4_gateway ( ) . clone ( ) ;
62
+ info ! (
63
+ "* gateway for testing mixnodes: {}" ,
64
+ tested_mix_gateway. identity_key. to_base58_string( )
65
+ ) ;
64
66
65
- let tested_network = TestedNetwork :: new_good ( v4_topology, v6_topology) ;
67
+ // TODO: those keys change constant throughout the whole execution of the monitor.
68
+ // and on top of that, they are used with ALL the gateways -> presumably this should change
69
+ // in the future
70
+ let mut rng = rand:: rngs:: OsRng ;
66
71
67
- // TODO: those keys change constant throughout the whole execution of the monitor.
68
- // and on top of that, they are used with ALL the gateways -> presumably this should change
69
- // in the future
70
- let mut rng = rand:: rngs:: OsRng ;
72
+ let identity_keypair = Arc :: new ( identity:: KeyPair :: new ( & mut rng) ) ;
73
+ let encryption_keypair = Arc :: new ( encryption:: KeyPair :: new ( & mut rng) ) ;
71
74
72
- let identity_keypair = Arc :: new ( identity:: KeyPair :: new ( & mut rng) ) ;
73
- let encryption_keypair = Arc :: new ( encryption:: KeyPair :: new ( & mut rng) ) ;
75
+ let test_mixnode_sender = Recipient :: new (
76
+ * identity_keypair. public_key ( ) ,
77
+ * encryption_keypair. public_key ( ) ,
78
+ tested_mix_gateway. identity_key ,
79
+ ) ;
74
80
75
- let test_mixnode_sender = Recipient :: new (
76
- * identity_keypair. public_key ( ) ,
77
- * encryption_keypair. public_key ( ) ,
78
- tested_mix_gateway. identity_key ,
79
- ) ;
81
+ let ( gateway_status_update_sender, gateway_status_update_receiver) = mpsc:: unbounded ( ) ;
82
+ let ( received_processor_sender_channel, received_processor_receiver_channel) =
83
+ mpsc:: unbounded ( ) ;
80
84
81
- let ( gateway_status_update_sender, gateway_status_update_receiver) = mpsc:: unbounded ( ) ;
82
- let ( received_processor_sender_channel, received_processor_receiver_channel) =
83
- mpsc:: unbounded ( ) ;
85
+ let packet_preparer = new_packet_preparer (
86
+ self . validator_cache ,
87
+ self . tested_network . clone ( ) ,
88
+ test_mixnode_sender,
89
+ * identity_keypair. public_key ( ) ,
90
+ * encryption_keypair. public_key ( ) ,
91
+ ) ;
84
92
85
- let packet_preparer = new_packet_preparer (
86
- validator_cache,
87
- tested_network. clone ( ) ,
88
- test_mixnode_sender,
89
- * identity_keypair. public_key ( ) ,
90
- * encryption_keypair. public_key ( ) ,
91
- ) ;
93
+ let bandwidth_credential =
94
+ TEMPORARY_obtain_bandwidth_credential ( self . config , identity_keypair. public_key ( ) ) . await ;
92
95
93
- let bandwidth_credential =
94
- TEMPORARY_obtain_bandwidth_credential ( config, identity_keypair. public_key ( ) ) . await ;
96
+ let packet_sender = new_packet_sender (
97
+ self . config ,
98
+ gateway_status_update_sender,
99
+ Arc :: clone ( & identity_keypair) ,
100
+ bandwidth_credential,
101
+ self . config . get_gateway_sending_rate ( ) ,
102
+ ) ;
95
103
96
- let packet_sender = new_packet_sender (
97
- config,
98
- gateway_status_update_sender,
99
- Arc :: clone ( & identity_keypair) ,
100
- bandwidth_credential,
101
- config. get_gateway_sending_rate ( ) ,
102
- ) ;
104
+ let received_processor = new_received_processor (
105
+ received_processor_receiver_channel,
106
+ Arc :: clone ( & encryption_keypair) ,
107
+ ) ;
108
+ let summary_producer = new_summary_producer ( self . config . get_detailed_report ( ) ) ;
109
+ let packet_receiver = new_packet_receiver (
110
+ gateway_status_update_receiver,
111
+ received_processor_sender_channel,
112
+ ) ;
103
113
104
- let received_processor = new_received_processor (
105
- received_processor_receiver_channel ,
106
- Arc :: clone ( & encryption_keypair ) ,
107
- ) ;
108
- let summary_producer = new_summary_producer ( config . get_detailed_report ( ) ) ;
109
- let packet_receiver = new_packet_receiver (
110
- gateway_status_update_receiver ,
111
- received_processor_sender_channel ,
112
- ) ;
114
+ let monitor = monitor :: Monitor :: new (
115
+ self . config ,
116
+ packet_preparer ,
117
+ packet_sender ,
118
+ received_processor ,
119
+ summary_producer ,
120
+ self . node_status_storage ,
121
+ self . tested_network ,
122
+ ) ;
113
123
114
- let monitor = monitor:: Monitor :: new (
115
- config,
116
- packet_preparer,
117
- packet_sender,
118
- received_processor,
119
- summary_producer,
120
- node_status_storage,
121
- tested_network,
122
- ) ;
124
+ NetworkMonitorRunnables {
125
+ monitor,
126
+ packet_receiver,
127
+ }
128
+ }
129
+ }
123
130
124
- NetworkMonitorRunnables {
125
- monitor,
126
- packet_receiver,
131
+ pub ( crate ) struct NetworkMonitorRunnables {
132
+ monitor : Monitor ,
133
+ packet_receiver : PacketReceiver ,
134
+ }
135
+
136
+ impl NetworkMonitorRunnables {
137
+ // TODO: note, that is not exactly doing what we want, because when
138
+ // `ReceivedProcessor` is constructed, it already spawns a future
139
+ // this needs to be refactored!
140
+ pub ( crate ) fn spawn_tasks ( self ) {
141
+ let mut packet_receiver = self . packet_receiver ;
142
+ let mut monitor = self . monitor ;
143
+ tokio:: spawn ( async move { packet_receiver. run ( ) . await } ) ;
144
+ tokio:: spawn ( async move { monitor. run ( ) . await } ) ;
127
145
}
128
146
}
129
147
0 commit comments