-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparticl-test-env.py
executable file
·338 lines (261 loc) · 11.2 KB
/
particl-test-env.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Bootstrap the test environment by running:
python3 particl-test-env.py
particld version 0.17.x (if 0.17, re-enable [regtest] in writeConfig)
Copyright & credits go to tecnovert.
"""
import os
import shutil
import shlex
import subprocess
import time
import decimal
import sys
import json
import hashlib
import threading
import re
import traceback
import signal
def toBool(s):
return s.lower() in ["1", "true"]
# use the bins/download-particl-core.sh script to download the latest core to ./bins/particl-core
PARTICL_BINDIR = os.path.expanduser(os.getenv("PARTICL_BINDIR", "./bins/particl-core"))
PARTICLD = os.getenv("PARTICLD", "particld")
PARTICL_CLI = os.getenv("PARTICL_CLI", "particl-cli")
PARTICL_TX = os.getenv("PARTICL_TX", "particl-tx")
RESET_DATA = toBool(os.getenv("RESET_DATA", "True"))
DEBUG_MODE = toBool(os.getenv("DEBUG_MODE", "True"))
NUM_NODES = 3
DATADIRS = './tmp/particl'
BASE_PORT = 14792
BASE_RPC_PORT = 19792
mxLog = threading.Lock()
def getIndexAtProperty(arr, name, value):
for i, o in enumerate(arr):
try:
if o[name] == value:
return i
except:
continue
return -1
def jsonDecimal(obj):
if isinstance(obj, decimal.Decimal):
return str(obj)
raise TypeError
def dumpj(jin):
return json.dumps(jin, indent=4, default=jsonDecimal)
def dumpje(jin):
return json.dumps(jin, default=jsonDecimal).replace('"', '\\"')
def writeConfig(datadir, nodeId, rpcPort, port):
filePath = os.path.join(datadir, str(nodeId) + '/particl.conf')
if os.path.exists(filePath):
return
with open(filePath, 'w+') as fp:
fp.write('regtest=1\n')
fp.write('[regtest]\n') # > 0.16 only
fp.write('port=' + str(port) + "\n")
fp.write('rpcport=' + str(rpcPort) + "\n")
fp.write('rpcuser=rpcuser' + str(nodeId) + "\n")
fp.write('rpcpassword=rpcpass' + str(nodeId) + "\n")
fp.write('daemon=1\n')
fp.write('server=1\n')
fp.write('discover=0\n')
fp.write('listenonion=0\n')
fp.write('bind=127.0.0.1\n')
fp.write('findpeers=0\n')
if DEBUG_MODE:
fp.write('debug=1\n')
fp.write('debugexclude=libevent\n')
fp.write('displaylocaltime=1\n')
fp.write('acceptnonstdtxn=0\n')
fp.write('minstakeinterval=10\n')
for i in range(0, NUM_NODES):
if nodeId == i:
continue
fp.write('addnode=127.0.0.1:%d\n' % (BASE_PORT + i))
def prepareDir(datadir, nodeId):
nodeDir = os.path.join(datadir, str(nodeId))
if not os.path.exists(nodeDir):
os.makedirs(nodeDir)
writeConfig(datadir, nodeId, BASE_RPC_PORT + nodeId, BASE_PORT + nodeId)
def logd(fp, s):
mxLog.acquire()
try:
print(s)
fp.write(time.strftime("%y-%m-%d_%H-%M-%S", time.localtime()) + "\n" + s + "\n")
fp.flush()
finally:
mxLog.release()
logsep = "------------------------------------------------------------------------------"
def logc(fp, s):
mxLog.acquire()
try:
print(s)
fp.write('/*'+logsep + "\n " + s + "\n" + logsep + "*/\n")
fp.flush()
finally:
mxLog.release()
def loge(fp, s, tag=''):
mxLog.acquire()
try:
print(s)
fp.write(tag + s + "\n")
fp.flush()
finally:
mxLog.release()
def callrpc3(nodeId, bindir, cmd):
nodeDir = os.path.join(DATADIRS, str(nodeId))
logd(fpHandle, nodeDir)
command_cli = os.path.join(bindir, PARTICL_CLI)
args = command_cli + ' -datadir=' + nodeDir + ' ' + cmd
logd(fpHandle, args)
p = subprocess.Popen(args,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out = p.communicate()
if len(out[1]) > 0:
print('error ', out[1])
return [out[0], out[1]]
def callrpc2(nodeId, fp, bindir, cmd):
logd(fp, str(nodeId) + ' - ' + cmd)
r, re = callrpc3(nodeId, bindir, cmd)
if re and len(re) > 0:
fp.write("Error " + str(re) + "\n")
elif r is None:
fp.write("None\n")
else:
try:
ro = json.loads(r)
fp.write(json.dumps(ro, indent=4, default=jsonDecimal) + "\n")
r = ro
except:
r = r.decode('utf-8').strip()
fp.write(str(r) + "\n")
fp.write("\n")
fp.flush()
return r
def callrpc(nodeId, fp, cmd):
return callrpc2(nodeId, fp, PARTICL_BINDIR, cmd)
def calltx2(bindir, cmd):
print("cmd ", cmd)
command_cli = os.path.join(bindir, PARTICL_TX)
args = command_cli + ' ' + cmd
p = subprocess.Popen(args,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out = p.communicate()
if len(out[1]) > 0:
print('error ', out[1])
return [out[0], out[1]]
def calltx(fp, cmd):
logd(fp, PARTICL_TX + cmd)
r, re = calltx2(PARTICL_BINDIR, cmd)
if re and len(re) > 0:
fp.write("Error " + str(re) + "\n")
elif r is None:
fp.write("None\n")
else:
try:
ro = json.loads(r)
fp.write(json.dumps(ro, indent=4, default=jsonDecimal) + "\n")
except:
r = r.decode('utf-8').strip()
fp.write(str(r) + "\n")
fp.write("\n")
fp.flush()
return r
def startDaemon(nodeId, fp, bindir):
nodeDir = os.path.join(DATADIRS, str(nodeId))
command_cli = os.path.join(bindir, PARTICLD)
args = [command_cli, "-datadir="+nodeDir]
logd(fp, PARTICLD + ' '+"-datadir="+nodeDir+"\n")
p = subprocess.Popen(args,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = [p.stdout.read(), p.stderr.read()]
if len(out[1]) > 0:
print("error ", out[1])
return [out[0], out[1], p]
def startNodes(fp, resetData):
if resetData:
for i in range(NUM_NODES):
dirname = os.path.join(DATADIRS, str(i))
if os.path.isdir(dirname):
loge(fp, 'Removing' + dirname)
shutil.rmtree(dirname)
loge(fp, '')
logc(fp, 'Prepare the network')
global processes
processes = []
for i in range(NUM_NODES):
prepareDir(DATADIRS, i)
processes.append(startDaemon(i, fp, PARTICL_BINDIR)[2])
logc(fp, 'Process launched, checking')
# wait until responding
for k in range(5):
time.sleep(1)
try:
logc(fp, 'Trying getnetwork info!')
r = callrpc3(i, PARTICL_BINDIR, 'getnetworkinfo')
if len(r[1]) > 0:
raise ValueError('rpc error.')
except:
continue
break
callrpc(i, fp, 'walletsettings stakingoptions "{\\"stakecombinethreshold\\":\\"100\\",\\"stakesplitthreshold\\":200}"')
callrpc(i, fp, 'reservebalance true 1000')
callrpc(0, fp, 'extkeygenesisimport "abandon baby cabbage dad eager fabric gadget habit ice kangaroo lab absorb"')
callrpc(1, fp, 'extkeygenesisimport "pact mammal barrel matrix local final lecture chunk wasp survey bid various book strong spread fall ozone daring like topple door fatigue limb olympic" "" true')
callrpc(2, fp, 'extkeygenesisimport "てつづき いくぶん ちょさくけん たんご でんち おじぎ てくび やっぱり たんさん むろん いちりゅう たりょう"')
"""
callrpc(3, fp, 'extkeygenesisimport "面 闷 摄 旁 鸭 障 晚 偷 奔 凉 仰 树 铝 必 企 莫 理 飞 精 乎 合 互 江 巧" "江巧"')
callrpc(4, fp, 'extkeygenesisimport "matar misa bambú vinagre abierto faja válido lista saber jugo dulce perico"')
"""
for i in range(2):
callrpc(0, fp, 'sendtoaddress "pdYHpMumE3JfEyhLhxi9qmua1aDEJXzWv5" 100')
callrpc(0, fp, 'getnewstealthaddress') # TetXU1bNXEn4obs3iaDt5uup4gXz1XCwgButPoDZFcxkv7nD6S6o6vkqDNDQMmGz2MC9BMy4r3QrRKSb4RgzKQi2HSG1rYuBXSYc8A
callrpc(1, fp, 'getnewstealthaddress') # TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt
# Mixin inputs
callrpc(0, fp, 'sendtypeto part anon "[{\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":0.005}, {\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":0.005}, {\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":0.005}, {\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":0.005},{\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":0.005},{\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":0.005}]"')
callrpc(0, fp, 'sendtypeto part anon "[{\\"address\\":\\"TetXU1bNXEn4obs3iaDt5uup4gXz1XCwgButPoDZFcxkv7nD6S6o6vkqDNDQMmGz2MC9BMy4r3QrRKSb4RgzKQi2HSG1rYuBXSYc8A\\",\\"amount\\":20},{\\"address\\":\\"TetXU1bNXEn4obs3iaDt5uup4gXz1XCwgButPoDZFcxkv7nD6S6o6vkqDNDQMmGz2MC9BMy4r3QrRKSb4RgzKQi2HSG1rYuBXSYc8A\\",\\"amount\\":20},{\\"address\\":\\"TetXU1bNXEn4obs3iaDt5uup4gXz1XCwgButPoDZFcxkv7nD6S6o6vkqDNDQMmGz2MC9BMy4r3QrRKSb4RgzKQi2HSG1rYuBXSYc8A\\",\\"amount\\":20}, {\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":20}, {\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":20}, {\\"address\\":\\"TetbX4FkzdAFp1jSgo8jBr7DbRbA8fevy2Xhobhzr8iH171UPjYjmxBKdztpTPspccEKiAyK4u5vXcAtSFWbYb98RTGUxTVjF5qAZt\\",\\"amount\\":20}]"')
def exitNodes(sig, frame):
loge(fpHandle, 'Stopping nodes.')
for i in range(NUM_NODES):
if processes[i] is not None:
callrpc(i, fpHandle, 'stop')
# wait until stopped
for i in range(NUM_NODES):
if processes[i] is not None:
for i in range(2000):
if i > 1000:
logd(fp, 'Error: Node %d not stopping.' % (i))
break
return_code = processes[i].poll()
if return_code is None:
continue
break
print('You pressed Ctrl+C!')
sys.exit(0)
def main():
if not os.path.exists(DATADIRS):
os.makedirs(DATADIRS)
with open(os.path.join(DATADIRS,'test-log.txt'), 'w') as fp:
"""
Make a global fpHandle for the sigint
"""
global fpHandle
fpHandle = fp
signal.signal(signal.SIGINT, exitNodes)
logd(fp, os.path.basename(sys.argv[0]) + "\n\n")
startNodes(fp, RESET_DATA)
while 1:
"""
cmd = input("command> ")
rpcCmd = cmd.split(' ', maxsplit=1)
try:
prevout = callrpc(int(rpcCmd[0]), fp, str(rpcCmd[1]))
logc(fp, prevout)
except:
logc(fp, "Meh it broke :(")
"""
input("Press Ctrl+C to stop the nodes...")
print('Done.')
if __name__ == '__main__':
main()