Skip to content

Commit

Permalink
add api checking rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
suntzu93 committed Nov 13, 2022
1 parent aa8ccdd commit 2afa6d6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://graphindexer.co" target="_blank">graphindexer.io</a> |
| network | Choose the right network (mainnet / testnet) |
| indexer_management_url | Indexer management API (default port 18000) |
Expand All @@ -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) |

Expand All @@ -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
```
Expand Down
57 changes: 57 additions & 0 deletions manage_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2afa6d6

Please sign in to comment.