File tree Expand file tree Collapse file tree 2 files changed +64
-1
lines changed Expand file tree Collapse file tree 2 files changed +64
-1
lines changed Original file line number Diff line number Diff line change
1
+ from http .server import BaseHTTPRequestHandler , HTTPServer
2
+ import json
3
+
4
+
5
+ class DummyRPCHandler (BaseHTTPRequestHandler ):
6
+ def _set_response (self , status_code = 200 ):
7
+ self .send_response (status_code )
8
+ self .send_header ('Content-type' , 'application/json' )
9
+ self .end_headers ()
10
+
11
+ def do_POST (self ):
12
+ content_length = int (self .headers ['Content-Length' ])
13
+ request_body = self .rfile .read (content_length )
14
+ rpc_request = json .loads (request_body )
15
+
16
+ if rpc_request ['method' ] == 'eth_blockNumber' :
17
+ response = {
18
+ "jsonrpc" : "2.0" ,
19
+ "id" : rpc_request ['id' ],
20
+ "result" : "0x123456" # Dummy block number
21
+ }
22
+ else :
23
+ response = {
24
+ "jsonrpc" : "2.0" ,
25
+ "id" : rpc_request ['id' ],
26
+ "error" : {
27
+ "code" : - 32601 ,
28
+ "message" : "Method not found"
29
+ }
30
+ }
31
+
32
+ self ._set_response ()
33
+ self .wfile .write (json .dumps (response ).encode ())
34
+
35
+ def do_GET (self ):
36
+ self ._set_response (404 )
37
+ self .wfile .write (b'Not found' )
38
+
39
+
40
+ def run_server ():
41
+ server_address = ('localhost' , 8000 )
42
+ httpd = HTTPServer (server_address , DummyRPCHandler )
43
+ print ('Dummy RPC server is running on http://localhost:8000...' )
44
+ httpd .serve_forever ()
45
+
46
+
47
+ if __name__ == '__main__' :
48
+ run_server ()
Original file line number Diff line number Diff line change 1
1
use url:: Url ;
2
2
use std:: thread:: sleep;
3
+ use std:: time:: Instant ;
3
4
4
5
use tokio:: time:: Duration ;
5
6
use serde:: { Deserialize , Serialize } ;
@@ -233,9 +234,23 @@ impl RpcConnection {
233
234
let mut new_blocknumber = blocknumber. clone ( ) ;
234
235
println ! ( "Listening for new blocks from block {}..." , hex_to_decimal( & blocknumber) . unwrap( ) ) ;
235
236
237
+ // Start timer for the *heartbeat*
238
+ let mut start_time = Instant :: now ( ) ;
239
+
236
240
while blocknumber == new_blocknumber {
237
- // sleep for 1 second
241
+ // sleep for set duration
238
242
sleep ( Duration :: from_millis ( time) ) ;
243
+
244
+ // Add this as a *heartbeat* so users are less confused if nothing is happening
245
+ let elapsed_time = start_time. elapsed ( ) ;
246
+
247
+ if elapsed_time >= Duration :: from_secs ( 60 ) {
248
+ println ! ( "!!! \x1b [93mNo new blocks have been detected in 60 seconds! Check your node(s)\x1b [0m !!!" ) ;
249
+ println ! ( "Still listening..." ) ;
250
+ start_time = Instant :: now ( ) ;
251
+ }
252
+
253
+
239
254
new_blocknumber = self . block_number ( ) . await ?
240
255
}
241
256
You can’t perform that action at this time.
0 commit comments