-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnear_multinode_rpc_provider.py
72 lines (58 loc) · 2.3 KB
/
near_multinode_rpc_provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
import base64
import json
from config import GlobalConfig
global_config = GlobalConfig()
class MultiNodeJsonProviderError(Exception):
pass
class MultiNodeJsonProvider(object):
def __init__(self):
nodes = global_config.rpc_url
best_height = 0
best_node = None
for node in nodes:
self._rpc_addr = node
node_status = self.ping_node()
print(node, node_status)
if not node_status['syncing'] and node_status['latest_block_height'] > best_height + 10:
best_height = node_status['latest_block_height']
best_node = node
if best_node is not None:
print("Choose near rpc node", best_node)
self._rpc_addr = best_node
else:
raise MultiNodeJsonProviderError("No available nodes")
def rpc_addr(self):
return self._rpc_addr
def json_rpc(self, method, params, timeout=2):
j = {
'method': method,
'params': params,
'id': 'dontcare',
'jsonrpc': '2.0'
}
r = requests.post(self.rpc_addr(), json=j, timeout=timeout)
r.raise_for_status()
content = json.loads(r.content)
if "error" in content:
raise MultiNodeJsonProviderError(content["error"])
return content["result"]
def get_status(self):
return self.json_rpc('status', [None])
def view_call(self, account_id, method_name, args, finality='optimistic'):
return self.json_rpc('query', {"request_type": "call_function", "account_id": account_id,
"method_name": method_name, "args_base64": base64.b64encode(args).decode('utf8'), "finality": finality})
def ping_node(self):
ret = {'latest_block_height': 0, 'syncing': True}
try:
status = self.get_status()
if "sync_info" in status:
ret['latest_block_height'] = status['sync_info']['latest_block_height']
ret['syncing'] = status['sync_info']['syncing']
except MultiNodeJsonProviderError as e:
print("ping node MultiNodeJsonProviderError: ", e)
except Exception as e:
print("ping node Exception: ", e)
return ret
if __name__ == "__main__":
print("")