forked from casper-network/casper-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhow_to_query_a_node.py
More file actions
196 lines (156 loc) · 6.59 KB
/
how_to_query_a_node.py
File metadata and controls
196 lines (156 loc) · 6.59 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import argparse
import os
import pathlib
import typing
from pycspr import factory
from pycspr.api import NodeConnectionInfo
from pycspr.client import NodeClient
from pycspr.types import PublicKey
from pycspr.types import UnforgeableReference
USR1_PUB_KEY_HEX = pathlib.Path(os.getenv("NCTL")) / "assets" / "net-1" /\
"users" / "user-1" / "public_key_hex"
# CLI argument parser.
_ARGS = argparse.ArgumentParser("Demo illustrating how to execute native"
" transfers with pycspr.")
# CLI argument: path to cp2 account key - defaults to NCTL user 2.
_ARGS.add_argument(
"--account-key-path",
default=USR1_PUB_KEY_HEX,
dest="path_to_account_key",
help="Path to a test user's public_key_hex file.",
type=str,
)
# CLI argument: host address of target node - defaults to NCTL node 1.
_ARGS.add_argument(
"--node-host",
default="localhost",
dest="node_host",
help="Host address of target node.",
type=str,
)
# CLI argument: Node API REST port - defaults to 14101 @ NCTL node 1.
_ARGS.add_argument(
"--node-port-rest",
default=14101,
dest="node_port_rest",
help="Node API REST port. Typically 8888 on most nodes.",
type=int,
)
# CLI argument: Node API JSON-RPC port - defaults to 11101 @ NCTL node 1.
_ARGS.add_argument(
"--node-port-rpc",
default=11101,
dest="node_port_rpc",
help="Node API JSON-RPC port. Typically 7777 on most nodes.",
type=int,
)
# CLI argument: Node API SSE port - defaults to 18101 @ NCTL node 1.
_ARGS.add_argument(
"--node-port-sse",
default=18101,
dest="node_port_sse",
help="Node API SSE port. Typically 9999 on most nodes.",
type=int,
)
def _main(args: argparse.Namespace):
"""Main entry point.
:param args: Parsed command line arguments.
"""
# Set client.
client = _get_client(args)
# Set account key of test user.
user_public_key = factory.parse_public_key(args.path_to_account_key)
# Query 0.1: get_rpc_schema.
rpc_schema: typing.List[dict] = client.queries.get_rpc_schema()
assert isinstance(rpc_schema, dict)
print("SUCCESS :: Query 0.1: get_rpc_schema")
# Query 0.2: get_rpc_endpoints.
rpc_endpoints: typing.List[str] = client.queries.get_rpc_endpoints()
assert isinstance(rpc_endpoints, list)
print("SUCCESS :: Query 0.2: get_rpc_endpoints")
# Query 0.3: get_rpc_endpoint.
rpc_endpoint: dict = client.queries.get_rpc_endpoint("account_put_deploy")
assert isinstance(rpc_endpoint, dict)
print("SUCCESS :: Query 0.3: get_rpc_endpoint")
# Query 1.1: get_node_metrics.
node_metrics: typing.List[str] = client.queries.get_node_metrics()
assert isinstance(node_metrics, list)
print("SUCCESS :: Query 1.1: get_node_metrics")
# Query 1.2: get_node_metric.
node_metric: typing.List[str] = client.queries.get_node_metric(
"mem_deploy_gossiper")
assert isinstance(node_metric, list)
print("SUCCESS :: Query 1.2: get_node_metric")
# Query 1.3: get_node_peers.
node_peers: typing.List[dict] = client.queries.get_node_peers()
assert isinstance(node_peers, list)
print("SUCCESS :: Query 1.3: get_node_peers")
# Query 1.4: get_node_status.
node_status: typing.List[dict] = client.queries.get_node_status()
assert isinstance(node_status, dict)
print("SUCCESS :: Query 1.4: get_node_status")
# Query 2.1: get_state_root_hash - required for global state related
# queries.
state_root_hash: bytes = client.queries.get_state_root_hash()
assert isinstance(state_root_hash, bytes)
print("SUCCESS :: Query 2.1: get_state_root_hash")
# Query 2.2: get_account_info.
account_info = client.queries.get_account_info(user_public_key.account_key)
assert isinstance(account_info, dict)
print("SUCCESS :: Query 2.2: get_account_info")
# Query 2.3: get_account_main_purse_uref.
account_main_purse = client.queries.get_account_main_purse_uref(
user_public_key.account_key)
assert isinstance(account_main_purse, UnforgeableReference)
print("SUCCESS :: Query 2.3: get_account_main_purse_uref")
# Query 2.4: get_account_balance.
account_balance = client.queries.get_account_balance(account_main_purse,
state_root_hash)
assert isinstance(account_balance, int)
print("SUCCESS :: Query 2.4: get_account_balance")
# Query 3.1: get_block_at_era_switch - will poll until switch block.
print("POLLING :: Query 3.1: get_block_at_era_switch - may take some time")
block: dict = client.queries.get_block_at_era_switch()
assert isinstance(block, dict)
print("SUCCESS :: Query 3.1: get_block_at_era_switch")
# Query 3.2: get_block - by hash & by height.
assert client.queries.get_block(block["hash"]) == \
client.queries.get_block(block["header"]["height"])
print("SUCCESS :: Query 3.2: get_block - by hash & by height")
# Query 3.3: get_block_transfers - by hash & by height.
block_transfers = client.queries.get_block_transfers(block["hash"])
assert isinstance(block_transfers, tuple)
assert isinstance(block_transfers[0], str) # black hash
assert isinstance(block_transfers[1], list) # set of transfers
assert block_transfers == client.queries.get_block_transfers(
block["header"]["height"])
print("SUCCESS :: Query 3.3: get_block_transfers - by hash & by height")
# Query 4.1: get_auction_info.
auction_info = client.queries.get_auction_info()
assert isinstance(auction_info, dict)
print("SUCCESS :: Query 4.1: get_auction_info")
# Query 4.2: get_era_info - by switch block hash.
era_info = client.queries.get_era_info(block["hash"])
assert isinstance(era_info, dict)
print("SUCCESS :: Query 4.2: get_era_info - by switch block hash")
# Query 4.3: get_era_info - by switch block height.
assert client.queries.get_era_info(block["hash"]) == \
client.queries.get_era_info(block["header"]["height"])
print("SUCCESS :: Query 4.3: get_era_info - by switch block height")
def _get_client(args: argparse.Namespace) -> NodeClient:
"""Returns a pycspr client instance.
"""
connection = NodeConnectionInfo(
host=args.node_host,
port_rest=args.node_port_rest,
port_rpc=args.node_port_rpc,
port_sse=args.node_port_sse
)
return NodeClient(connection)
def _get_counter_parties(args: argparse.Namespace) -> PublicKey:
"""Returns the 2 counter-parties participating in the transfer.
"""
return factory.parse_public_key(args.path_to_cp2_account_key)
# Entry point.
if __name__ == '__main__':
_main(_ARGS.parse_args())