@@ -123,9 +123,6 @@ fn sha1_digest(input: &str) -> String {
123123}
124124
125125async fn start_miner ( device : Device ) -> Result < ( ) , MinerError > {
126- let heatup_duration: u64 = rand:: thread_rng ( ) . gen_range ( 0 ..10000 ) ;
127- tokio:: time:: sleep ( Duration :: from_millis ( heatup_duration) ) . await ;
128-
129126 let mut stream = TcpStream :: connect (
130127 format ! ( "{}:{}" , device. host, device. port) ) . await . map_err ( |_| MinerError :: Connection ) ?;
131128
@@ -169,10 +166,9 @@ async fn start_miner(device: Device) -> Result<(), MinerError> {
169166 let expected_duration = expected_interval * duco_numeric_result as u128 ;
170167
171168 if duration < expected_duration {
172- let wait_multiplier: u64 = rand:: thread_rng ( ) . gen_range ( 95 ..105 ) ;
173- let wait_duration = ( expected_duration - duration) as u64 * wait_multiplier / 100 ;
169+ let wait_duration = ( expected_duration - duration) as u64 ;
174170 tokio:: time:: sleep ( Duration :: from_micros ( wait_duration) ) . await ;
175- info ! ( "Waited {} micro sec" , wait_duration) ;
171+ info ! ( "waited {} micro sec" , wait_duration) ;
176172 } else {
177173 warn ! ( "system too slow, lag {} micro sec" , duration - expected_duration) ;
178174 }
@@ -191,12 +187,15 @@ async fn start_miner(device: Device) -> Result<(), MinerError> {
191187 let n = stream. read ( & mut cmd_in) . await . map_err ( |_| MinerError :: RecvCommand ) ?;
192188 let resp = std:: str:: from_utf8 ( & cmd_in[ ..n] ) . map_err ( |_| MinerError :: InvalidUTF8 ) ?. trim ( ) ;
193189
194- if resp != "GOOD" {
190+ if resp == "GOOD" {
191+ info ! ( "result good, result: {}, rate: {:.2}, real: {:.2}" ,
192+ duco_numeric_result, emu_rate, real_rate) ;
193+ } else if resp == "BLOCK" {
194+ info ! ( "FOUND BLOCK!, result: {}, rate: {:.2}, real: {:.2}" ,
195+ duco_numeric_result, emu_rate, real_rate) ;
196+ } else {
195197 warn ! ( "resp: {}, result: {}, rate: {:.2}, real: {:.2}" ,
196198 resp, duco_numeric_result, emu_rate, real_rate) ;
197- } else {
198- info ! ( "resp: {}, result: {}, rate: {:.2}, real: {:.2}" ,
199- resp, duco_numeric_result, emu_rate, real_rate) ;
200199 }
201200
202201 break ;
@@ -205,17 +204,30 @@ async fn start_miner(device: Device) -> Result<(), MinerError> {
205204 }
206205}
207206
208- async fn start_miners ( devices : Vec < Device > ) -> Result < ( ) , MinerError > {
207+ async fn start_miner_with_watchdog ( device : Device ) {
208+ loop {
209+ let heatup_duration: u64 = rand:: thread_rng ( ) . gen_range ( 0 ..10000 ) ;
210+ tokio:: time:: sleep ( Duration :: from_millis ( heatup_duration) ) . await ;
211+
212+ match start_miner ( device. clone ( ) ) . await {
213+ Ok ( _) => error ! ( "exited without error" ) ,
214+ Err ( e) => error ! ( "exited with error: {:?}" , e) ,
215+ }
216+
217+ let hiatus_duration: u64 = rand:: thread_rng ( ) . gen_range ( 30 ..200 ) ;
218+ tokio:: time:: sleep ( Duration :: from_secs ( hiatus_duration) ) . await ;
219+ }
220+ }
221+
222+ async fn start_miners ( devices : Vec < Device > ) {
209223 let mut futures_vec = Vec :: new ( ) ;
210224
211225 for device in devices {
212- let f = start_miner ( device) ;
226+ let f = start_miner_with_watchdog ( device) ;
213227 futures_vec. push ( f) ;
214228 }
215229
216- futures:: future:: try_join_all ( futures_vec) . await ?;
217-
218- Ok ( ( ) )
230+ futures:: future:: join_all ( futures_vec) . await ;
219231}
220232
221233#[ tokio:: main]
@@ -232,17 +244,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
232244 let c_serial = tokio:: fs:: read_to_string ( opts. config_file ) . await ?;
233245 let c: Config = serde_yaml:: from_str ( c_serial. as_str ( ) ) ?;
234246
235- info ! ( "Running with {} miners" , c. devices. len( ) ) ;
247+ info ! ( "running with {} miners" , c. devices. len( ) ) ;
236248
237- loop {
238- match start_miners ( c. devices . clone ( ) ) . await {
239- Ok ( _) => break ,
240- Err ( e) => {
241- error ! ( "Exited with error: {:?}" , e) ;
242- tokio:: time:: sleep ( Duration :: from_secs ( 300u64 ) ) . await ;
243- }
244- }
245- }
249+ start_miners ( c. devices ) . await ;
246250 }
247251 }
248252
0 commit comments