forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 59
/
feature_part_zmq_test.py
executable file
·207 lines (168 loc) · 7.35 KB
/
feature_part_zmq_test.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
#!/usr/bin/env python3
# Copyright (c) 2015-2016 The Bitcoin Core developers
# Copyright (c) 2018-2019 The Particl Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.z
"""Test the ZMQ API."""
import configparser
import os
import struct
import time
import base64
from test_framework.test_particl import ParticlTestFramework
from test_framework.test_framework import SkipTest
class ZMQTest(ParticlTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def setup_nodes(self):
# Try to import python3-zmq. Skip this test if the import fails.
try:
import zmq
except ImportError:
raise SkipTest("python3-zmq module not available.")
# Check that particl has been built with ZMQ enabled
config = configparser.ConfigParser()
if not self.options.configfile:
self.options.configfile = os.path.dirname(__file__) + "/../config.ini"
config.read_file(open(self.options.configfile))
if not config["components"].getboolean("ENABLE_ZMQ"):
raise SkipTest("particld has not been built with zmq enabled.")
self.zmq = zmq
self.zmqContext = zmq.Context()
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
self.zmqSubSocket.set(zmq.RCVTIMEO, 60000)
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawblock")
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawtx")
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashwtx")
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"smsg")
public_key, secret_key = self.zmq.curve_keypair()
self.zmqSubSocket.setsockopt(zmq.CURVE_PUBLICKEY, public_key)
self.zmqSubSocket.setsockopt(zmq.CURVE_SECRETKEY, secret_key)
self.zmqSubSocket.setsockopt(zmq.CURVE_SERVERKEY, b"hn%V}2&Z$vWw!ugnb@[#)Lzfsiz(IY+U(EOTl#n&")
ip_address = "tcp://127.0.0.1:28332"
self.zmqSubSocket.connect(ip_address)
server_secret = base64.b64encode(b"p%ymQKFW%l[45CJa}+y&<B%R]Q(MZ4G!lH3^H+y2").decode("utf-8")
self.extra_args = [[
'-wallet=wallet_test',
'-serverkeyzmq=%s' % server_secret,
'-zmqpubhashblock=%s' % ip_address, '-zmqpubhashtx=%s' % ip_address,
'-zmqpubrawblock=%s' % ip_address, '-zmqpubrawtx=%s' % ip_address,
'-zmqpubsmsg=%s' % ip_address,
'-zmqpubhashwtx=%s' % ip_address],
[]]
self.add_nodes(self.num_nodes, self.extra_args)
self.start_nodes()
def run_test(self):
try:
self._zmq_test()
finally:
# Destroy the zmq context
self.log.debug("Destroying zmq context")
self.zmqContext.destroy(linger=None)
def waitForZmqSmsg(self, msgid):
for count in range(0, 100):
try:
msg = self.zmqSubSocket.recv_multipart(self.zmq.NOBLOCK)
except self.zmq.ZMQError:
time.sleep(0.25)
continue
topic = msg[0].decode('utf-8')
if topic == 'smsg':
zmqhash = msg[1].hex()
assert (zmqhash[:4] == '0300') # version 3.0
assert (zmqhash[4:] == msgid)
return True
return False
def _zmq_test(self):
nodes = self.nodes
for i in range(len(nodes)):
nodes[i].reservebalance(True, 10000000)
nodes[0].extkeyimportmaster(nodes[0].mnemonic('new')['master'])
self.import_genesis_coins_a(nodes[1])
addrTo = nodes[0].getnewaddress()
txnHash = nodes[1].sendtoaddress(addrTo, 10)
self.sync_all()
self.log.info("Wait for tx")
fFound = False
fFoundWtx = False
fFoundRawTx = False
for count in range(0, 100):
try:
msg = self.zmqSubSocket.recv_multipart(self.zmq.NOBLOCK)
except self.zmq.ZMQError:
time.sleep(0.5)
continue
topic = msg[0].decode('utf-8')
msgSequence = struct.unpack('<I', msg[-1])[-1]
if topic == 'hashtx' and msgSequence == 1:
fFound = True
zmqhash = msg[1].hex()
assert (zmqhash == txnHash)
elif topic == 'rawtx' and msgSequence == 1:
fFoundRawTx = True
#body = msg[1]
# Check that the rawtx hashes to the hashtx
#assert_equal(hash256(body), txnHash)
#CTransaction.deserialize
elif topic == 'hashwtx' and msgSequence == 0:
fFoundWtx = True
zmqhash = msg[1][0:32].hex()
assert (zmqhash == txnHash)
walletName = msg[1][32:].decode('utf-8')
assert (walletName == 'wallet_test')
if fFound and fFoundRawTx and fFoundWtx:
break
assert (fFound)
assert (fFoundRawTx)
assert (fFoundWtx)
self.stakeBlocks(1, nStakeNode=1)
self.log.info("Wait for block")
fFound = False
for count in range(0, 100):
try:
msg = self.zmqSubSocket.recv_multipart(self.zmq.NOBLOCK)
except self.zmq.ZMQError:
time.sleep(0.5)
continue
topic = msg[0].decode('utf-8')
msgSequence = struct.unpack('<I', msg[-1])[-1]
if topic == 'hashblock' and msgSequence == 0:
fFound = True
blkhash = msg[1].hex()
besthash = nodes[1].getbestblockhash()
assert (blkhash == besthash)
break
assert (fFound)
address0 = nodes[0].getnewaddress() # Will be different each run
address1 = nodes[1].getnewaddress()
assert (address1 == 'pX9N6S76ZtA5BfsiJmqBbjaEgLMHpt58it')
ro = nodes[0].smsglocalkeys()
assert (len(ro['wallet_keys']) == 0)
ro = nodes[0].smsgaddlocaladdress(address0) # Listen on address0
assert ('Receiving messages enabled for address' in ro['result'])
ro = nodes[0].smsglocalkeys()
assert (len(ro['wallet_keys']) == 1)
ro = nodes[1].smsgaddaddress(address0, ro['wallet_keys'][0]['public_key'])
assert (ro['result'] == 'Public key added to db.')
ro = nodes[1].smsgsend(address1, address0, "['data':'test','value':1]", True, 4)
msgid = ro['msgid']
assert (ro['result'] == 'Sent.')
self.stakeBlocks(1, nStakeNode=1)
self.waitForSmsgExchange(1, 1, 0)
assert (self.waitForZmqSmsg(msgid))
ro = nodes[0].getnewzmqserverkeypair()
assert (len(ro['server_secret_key']) == 40)
assert (len(ro['server_public_key']) == 40)
assert (len(ro['server_secret_key_b64']) > 40)
ro = nodes[0].smsgzmqpush()
assert (ro['numsent'] == 1)
assert (self.waitForZmqSmsg(msgid))
ro = nodes[0].smsgzmqpush({"timefrom": int(time.time()) + 1})
assert (ro['numsent'] == 0)
if __name__ == '__main__':
ZMQTest().main()