Skip to content

Commit

Permalink
Add heartbeat check
Browse files Browse the repository at this point in the history
  • Loading branch information
makemake-kbo committed Jul 2, 2023
1 parent fa0973f commit b4e0478
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
48 changes: 48 additions & 0 deletions dummy_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import json


class DummyRPCHandler(BaseHTTPRequestHandler):
def _set_response(self, status_code=200):
self.send_response(status_code)
self.send_header('Content-type', 'application/json')
self.end_headers()

def do_POST(self):
content_length = int(self.headers['Content-Length'])
request_body = self.rfile.read(content_length)
rpc_request = json.loads(request_body)

if rpc_request['method'] == 'eth_blockNumber':
response = {
"jsonrpc": "2.0",
"id": rpc_request['id'],
"result": "0x123456" # Dummy block number
}
else:
response = {
"jsonrpc": "2.0",
"id": rpc_request['id'],
"error": {
"code": -32601,
"message": "Method not found"
}
}

self._set_response()
self.wfile.write(json.dumps(response).encode())

def do_GET(self):
self._set_response(404)
self.wfile.write(b'Not found')


def run_server():
server_address = ('localhost', 8000)
httpd = HTTPServer(server_address, DummyRPCHandler)
print('Dummy RPC server is running on http://localhost:8000...')
httpd.serve_forever()


if __name__ == '__main__':
run_server()
17 changes: 16 additions & 1 deletion src/rpc/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use url::Url;
use std::thread::sleep;
use std::time::Instant;

use tokio::time::Duration;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -233,9 +234,23 @@ impl RpcConnection {
let mut new_blocknumber = blocknumber.clone();
println!("Listening for new blocks from block {}...", hex_to_decimal(&blocknumber).unwrap());

// Start timer for the *heartbeat*
let mut start_time = Instant::now();

while blocknumber == new_blocknumber {
// sleep for 1 second
// sleep for set duration
sleep(Duration::from_millis(time));

// Add this as a *heartbeat* so users are less confused if nothing is happening
let elapsed_time = start_time.elapsed();

if elapsed_time >= Duration::from_secs(60) {
println!("!!! \x1b[93mNo new blocks have been detected in 60 seconds! Check your node(s)\x1b[0m !!!");
println!("Still listening...");
start_time = Instant::now();
}


new_blocknumber = self.block_number().await?
}

Expand Down

0 comments on commit b4e0478

Please sign in to comment.