-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_node.py
267 lines (224 loc) · 10 KB
/
test_node.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import time
from enum import Enum
from framework.util import create_config_file, get_project_root, run_command, get_ckb_configs
from framework.config import get_tmp_path, CKB_DEFAULT_CONFIG, CKB_MINER_CONFIG
from framework.rpc import RPCClient
import shutil
import telnetlib
from websocket import create_connection, WebSocket
class CkbNodeConfigPath(Enum):
CURRENT_TEST = (
"source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/ckb/v112/specs/dev.toml",
"download/develop"
)
CURRENT_MAIN = ("source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/specs/mainnet.toml.j2",
"download/develop")
v114 = (
"source/template/ckb/v114/ckb.toml.j2",
"source/template/ckb/v114/ckb-miner.toml.j2",
"source/template/ckb/v114/specs/dev.toml",
"download/0.114.0"
)
V113 = (
"source/template/ckb/v113/ckb.toml.j2",
"source/template/ckb/v113/ckb-miner.toml.j2",
"source/template/ckb/v113/specs/dev.toml",
"download/0.113.1"
)
V112 = (
"source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/ckb/v112/specs/dev.toml",
"download/0.112.1"
)
V112_MAIN = (
"source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/specs/mainnet.toml.j2",
"download/0.112.1"
)
V111 = (
"source/template/ckb/v111/ckb.toml.j2",
"source/template/ckb/v111/ckb-miner.toml.j2",
"source/template/ckb/v111/specs/dev.toml",
"download/0.111.0"
)
V110 = (
"source/template/ckb/v110/ckb.toml.j2",
"source/template/ckb/v110/ckb-miner.toml.j2",
"source/template/ckb/v110/specs/dev.toml",
"download/0.110.2"
)
V110_MAIN = (
"source/template/ckb/v110/ckb.toml.j2",
"source/template/ckb/v110/ckb-miner.toml.j2",
"source/template/specs/mainnet.toml.j2",
"download/0.110.2"
)
V110_TEST = (
"source/template/ckb/v110/ckb.toml.j2",
"source/template/ckb/v110/ckb-miner.toml.j2",
"source/template/specs/testnet.toml.j2",
"download/0.110.2"
)
V109 = ("source/template/ckb/v109/ckb.toml.j2", "source/template/ckb/v109/ckb-miner.toml.j2",
"source/template/ckb/v109/specs/dev.toml", "download/0.109.0")
V109_MAIN = ("source/template/ckb/v109/ckb.toml.j2", "source/template/ckb/v109/ckb-miner.toml.j2",
"source/template/specs/mainnet.toml.j2", "download/0.109.0")
v108 = ("", "", "", "")
def __init__(self, ckb_config_path, ckb_miner_config_path, ckb_spec_path, ckb_bin_path):
self.ckb_config_path = ckb_config_path
self.ckb_miner_config_path = ckb_miner_config_path
self.ckb_spec_path = ckb_spec_path
self.ckb_bin_path = ckb_bin_path
def __str__(self):
return self.ckb_bin_path.split("/")[-1]
class CkbNode:
@classmethod
def init_dev_by_port(cls, ckb_node_path_enum: CkbNodeConfigPath, dec_dir, rpc_port, p2p_port):
ckb_config, ckb_miner_config, ckb_specs_config = get_ckb_configs(p2p_port, rpc_port)
return CkbNode(ckb_node_path_enum, dec_dir, ckb_config, ckb_miner_config, ckb_specs_config)
def __init__(self, ckb_node_path_enum: CkbNodeConfigPath,
dec_dir,
ckb_config=CKB_DEFAULT_CONFIG,
ckb_miner_config=CKB_MINER_CONFIG,
ckb_specs_config={},
):
self.ckb_config_path = ckb_node_path_enum
self.dec_path = ckb_config
self.ckb_config = ckb_config.copy()
self.ckb_miner_config = ckb_miner_config
self.ckb_specs_config = ckb_specs_config
self.ckb_dir = "{tmp}/{ckb_dir}".format(tmp=get_tmp_path(), ckb_dir=dec_dir)
self.ckb_bin_path = f"{self.ckb_dir}/ckb"
self.ckb_toml_path = f"{self.ckb_dir}/ckb.toml"
self.ckb_miner_toml_path = f"{self.ckb_dir}/ckb-miner.toml"
self.ckb_specs_config_path = f"{self.ckb_dir}/dev.toml"
self.ckb_pid = -1
self.ckb_miner_pid = -1
self.rpcUrl = "http://{url}".format(url=self.ckb_config.get("ckb_rpc_listen_address", "127.0.0.1:8114"))
self.client = RPCClient(self.rpcUrl)
def __str__(self):
return self.ckb_config_path
def get_peer_id(self):
return self.client.local_node_info()["node_id"]
def get_peer_address(self):
info = self.client.local_node_info()
return info["addresses"][0]['address'].replace("0.0.0.0", "127.0.0.1")
def get_connected_count(self):
return int(self.getClient().local_node_info()["connections"], 16)
def connected(self, node):
peer_id = node.get_peer_id()
peer_address = node.get_peer_address()
print("add node response:", self.getClient().add_node(peer_id, peer_address))
def getClient(self) -> RPCClient:
return self.client
def restart(self, config={}, clean_data=False):
self.stop()
self.stop_miner()
if clean_data:
# rm -rf indexer
run_command(f"cd {self.ckb_dir} && rm -rf data/indexer")
# rm -rf network
run_command(f"cd {self.ckb_dir} && rm -rf data/network")
# rm -rf tx_pool
run_command(f"cd {self.ckb_dir} && rm -rf data/tx_pool")
# rm -rf tmp
run_command(f"cd {self.ckb_dir} && rm -rf data/tmp")
self.start()
def start(self):
self.ckb_pid = run_command(
"cd {ckb_dir} && ./ckb run --indexer --skip-spec-check > node.log 2>&1 &".format(ckb_dir=self.ckb_dir))
# //todo replace by rpc
time.sleep(3)
def stop(self):
self.stop_miner()
# run_command("kill {pid}".format(pid=self.ckb_pid))
# self.ckb_pid = -1
port = self.rpcUrl.split(":")[-1]
run_command(f"kill $(lsof -t -i:{port})", check_exit_code=False)
self.ckb_pid = -1
time.sleep(3)
def prepare(self, other_ckb_config={}, other_ckb_miner_config={}, other_ckb_spec_config={}, check_file=False):
self.ckb_config.update(other_ckb_config)
self.ckb_miner_config.update(other_ckb_miner_config)
self.ckb_specs_config.update(other_ckb_spec_config)
# check file exist
create_config_file(self.ckb_config, self.ckb_config_path.ckb_config_path,
self.ckb_toml_path)
create_config_file(self.ckb_miner_config, self.ckb_config_path.ckb_miner_config_path, self.ckb_miner_toml_path)
if ".j2" in self.ckb_config_path.ckb_spec_path:
create_config_file(self.ckb_specs_config, self.ckb_config_path.ckb_spec_path, self.ckb_specs_config_path)
else:
shutil.copy("{root_path}/{spec_path}".format(root_path=get_project_root(),
spec_path=self.ckb_config_path.ckb_spec_path), self.ckb_dir)
shutil.copy("{root_path}/{ckb_bin_path}/ckb".format(root_path=get_project_root(),
ckb_bin_path=self.ckb_config_path.ckb_bin_path),
self.ckb_dir)
shutil.copy("{root_path}/{ckb_bin_path}/ckb-cli".format(root_path=get_project_root(),
ckb_bin_path=self.ckb_config_path.ckb_bin_path),
self.ckb_dir)
shutil.copy("{root_path}/source/template/ckb/default.db-options".format(root_path=get_project_root()),
self.ckb_dir)
def clean(self):
run_command("rm -rf {ckb_dir}".format(ckb_dir=self.ckb_dir))
def start_miner(self):
if self.ckb_miner_pid != -1:
return
self.ckb_miner_pid = run_command(
"cd {ckb_dir} && ./ckb miner > ckb.miner.log 2>&1 &".format(ckb_dir=self.ckb_dir))
# replace check height upper
time.sleep(3)
def stop_miner(self):
if self.ckb_miner_pid == -1:
return
try:
run_command("kill {pid}".format(pid=self.ckb_miner_pid))
self.ckb_miner_pid = -1
except:
self.ckb_miner_pid = -1
def version(self):
pass
def subscribe_telnet(self, topic, other_url=None) -> telnetlib.Telnet:
# new_tip_header | new_tip_block | new_transaction | proposed_transaction | rejected_transaction
if "ckb_tcp_listen_address" not in self.ckb_config.keys():
raise Exception("not set ckb_ws_listen_address")
ckb_tcp_listen_address = self.ckb_config['ckb_tcp_listen_address']
if other_url is not None:
ckb_tcp_listen_address = other_url
# get host
host = ckb_tcp_listen_address.split(":")[0]
# get port
port = ckb_tcp_listen_address.split(":")[1]
# new telnet
tn = telnetlib.Telnet(host, int(port))
print("----")
topic_str = '{"id": 2, "jsonrpc": "2.0", "method": "subscribe", "params": ["' + topic + '"]}'
tn.write(topic_str.encode('utf-8') + b"\n")
data = tn.read_until(b'}\n')
if data:
output = data.decode('utf-8')
print("telnet read:", output)
return tn
def subscribe_websocket(self, topic, other_url=None) -> WebSocket:
if other_url is None and "ckb_ws_listen_address" not in self.ckb_config.keys():
raise Exception("not set ckb_ws_listen_address")
print("subscribe_websocket")
if other_url is not None:
ckb_ws_listen_address = other_url
else:
ckb_ws_listen_address = self.ckb_config['ckb_ws_listen_address']
print(ckb_ws_listen_address)
ws = create_connection(f"ws://{ckb_ws_listen_address}")
topic_str = '{"id": 2, "jsonrpc": "2.0", "method": "subscribe", "params": ["' + topic + '"]}'
ws.send(topic_str)
print("Sent")
print("Receiving...")
result = ws.recv()
print(result)
# ws.settimeout(1)
return ws