From 2afa6d601455b998ae488829a2eb3a36a408d324 Mon Sep 17 00:00:00 2001 From: suntzu93 Date: Sun, 13 Nov 2022 10:29:05 +0700 Subject: [PATCH] add api checking rpc --- README.md | 8 ++++--- manage_actions.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a0143c8..71bd76f 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ git pull ## 5. How to change data in `config.py` file with example >**config.py** -| Attempt | #Description | -| :---: | :---: | +| Attempt | #Description | +|:---------------------------------------:|:----------------------------------------------------------------------------------------------------------------:| | token | Connect wallet and generate token on graphindexer.io | | network | Choose the right network (mainnet / testnet) | | indexer_management_url | Indexer management API (default port 18000) | @@ -47,7 +47,8 @@ git pull | indexer_graph | indexer cli [Detail](https://github.com/graphprotocol/indexer) | | indexer_address | Indexer address | | graphman_cli | Graphman cli | -| graphman_config_file | Graphman config file [Detail](https://github.com/graphprotocol/graph-node/blob/master/docs/config.md) | +| graphman_config_file | Graphman config file [Detail](https://github.com/graphprotocol/graph-node/blob/master/docs/config.md) | +| rpc_list | To monitor rpc healthy , only support EVM rpc, format ["http://rpc1","http://rpc2"] | | host | Should be 0.0.0.0 to access from network | | port | Indexer script port (default 5502) | @@ -66,6 +67,7 @@ agent_log = "/root/agent_log.txt" agent_restart_cmd = "pm2 restart indexer_agent" graphman_cli = "/root/graph-node/build/release/graphman" graphman_config_file = "graphman_config.toml" +rpc_list=["http://127.0.0.1:8545","http://127.0.0.1:8554"] host = "0.0.0.0" port = 5502 ``` diff --git a/manage_actions.py b/manage_actions.py index 646740c..0d19c3d 100644 --- a/manage_actions.py +++ b/manage_actions.py @@ -270,6 +270,63 @@ def update_decision_basic_never(ipfsHash): logging.info(output) +@app.route('/checkRPCs', methods=['POST']) +def check_rpc(): + try: + token = request.form.get("token") + if token != config.token: + return const.TOKEN_ERROR + + if len(config.rpc_list) == 0: + return "ERROR" + + listRpcCheckingResult = [] + for rpc in config.rpc_list: + block_number = -1 + chain_id = -1 + peer_count = -1 + try: + logging.info("check rpc: " + rpc) + playload_eth_blocknumber = {"method": "eth_blockNumber", "params": [], "id": 1, "jsonrpc": "2.0"} + headers = { + "content-type": "application/json" + } + response = requests.post(url=rpc, + json=playload_eth_blocknumber, + headers=headers) + if response.ok: + response_json = response.json() + block_number = int(response_json["result"], 16) + playload_eth_chainid = {"method": "eth_chainId", "params": [], "id": 1, "jsonrpc": "2.0"} + response = requests.post(url=rpc, + json=playload_eth_chainid, + headers=headers) + if response.ok: + response_json = response.json() + chain_id = int(response_json["result"], 16) + playload_peer = {"jsonrpc": "2.0", "method": "net_peerCount", "params": [], "id": 67} + response = requests.post(url=rpc, + json=playload_peer, + headers=headers) + if response.ok: + response_json = response.json() + peer_count = int(response_json["result"], 16) + + rpcInfo = {"rpc": rpc, "block_number": block_number, "chain_id": chain_id, "peer_count": peer_count} + logging.info("check rpc result: " + json.dumps(rpcInfo)) + listRpcCheckingResult.append(rpcInfo) + except Exception as e: + print(e) + logging.error("error checking rpc: " + str(e)) + rpcInfo = {"rpc": rpc, "block_number": block_number, "chain_id": chain_id, "peer_count": peer_count} + listRpcCheckingResult.append(rpcInfo) + return json.dumps(listRpcCheckingResult) + except Exception as e: + print(e) + logging.error("check_rpc: " + str(e)) + return "ERROR" + + @app.route('/verify', methods=['POST']) def verify(): try: