/
p2p_txexpiringsoon.py
executable file
·215 lines (168 loc) · 8.43 KB
/
p2p_txexpiringsoon.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
#!/usr/bin/env python
# Copyright (c) 2018 The Zcash developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
from test_framework.authproxy import JSONRPCException
from test_framework.mininode import NodeConn, NetworkThread, CInv, \
msg_mempool, msg_getdata, msg_tx, mininode_lock, OVERWINTER_PROTO_VERSION
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, connect_nodes_bi, fail, \
initialize_chain_clean, p2p_port, start_nodes, sync_blocks, sync_mempools
from tx_expity_helper import TestNode, create_transaction
from binascii import hexlify
class TxExpiringSoonTest(BitcoinTestFramework):
def setup_chain(self):
print "Initializing test directory " + self.options.tmpdir
initialize_chain_clean(self.options.tmpdir, 3)
def setup_network(self):
self.nodes = start_nodes(3, self.options.tmpdir,
extra_args=[['-nuparams=5ba81b19:10']] * 3)
connect_nodes_bi(self.nodes, 0, 1)
# We don't connect node 2
def send_transaction(self, testnode, block, address, expiry_height):
tx = create_transaction(self.nodes[0],
block,
address,
10.0,
expiry_height)
testnode.send_message(msg_tx(tx))
# Sync up with node after p2p messages delivered
testnode.sync_with_ping()
# Sync nodes 0 and 1
sync_blocks(self.nodes[:2])
sync_mempools(self.nodes[:2])
return tx
def verify_inv(self, testnode, tx):
# Make sure we are synced before sending the mempool message
testnode.sync_with_ping()
# Send p2p message "mempool" to receive contents from zcashd node in "inv" message
with mininode_lock:
testnode.last_inv = None
testnode.send_message(msg_mempool())
# Sync up with node after p2p messages delivered
testnode.sync_with_ping()
with mininode_lock:
msg = testnode.last_inv
assert_equal(len(msg.inv), 1)
assert_equal(tx.sha256, msg.inv[0].hash)
def send_data_message(self, testnode, tx):
# Send p2p message "getdata" to verify tx gets sent in "tx" message
getdatamsg = msg_getdata()
getdatamsg.inv = [CInv(1, tx.sha256)]
with mininode_lock:
testnode.last_notfound = None
testnode.last_tx = None
testnode.send_message(getdatamsg)
def verify_last_tx(self, testnode, tx):
# Sync up with node after p2p messages delivered
testnode.sync_with_ping()
# Verify data received in "tx" message is for tx
with mininode_lock:
incoming_tx = testnode.last_tx.tx
incoming_tx.rehash()
assert_equal(tx.sha256, incoming_tx.sha256)
def run_test(self):
testnode0 = TestNode()
connections = []
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0],
testnode0, "regtest", OVERWINTER_PROTO_VERSION))
testnode0.add_connection(connections[0])
# Start up network handling in another thread
NetworkThread().start()
testnode0.wait_for_verack()
# Verify mininodes are connected to zcashd nodes
peerinfo = self.nodes[0].getpeerinfo()
versions = [x["version"] for x in peerinfo]
assert_equal(1, versions.count(OVERWINTER_PROTO_VERSION))
assert_equal(0, peerinfo[0]["banscore"])
# Mine some blocks so we can spend
coinbase_blocks = self.nodes[0].generate(200)
node_address = self.nodes[0].getnewaddress()
# Sync nodes 0 and 1
sync_blocks(self.nodes[:2])
sync_mempools(self.nodes[:2])
# Verify block count
assert_equal(self.nodes[0].getblockcount(), 200)
assert_equal(self.nodes[1].getblockcount(), 200)
assert_equal(self.nodes[2].getblockcount(), 0)
# Mininodes send expiring soon transaction in "tx" message to zcashd node
self.send_transaction(testnode0, coinbase_blocks[0], node_address, 203)
# Assert that the tx is not in the mempool (expiring soon)
assert_equal([], self.nodes[0].getrawmempool())
assert_equal([], self.nodes[1].getrawmempool())
assert_equal([], self.nodes[2].getrawmempool())
# Mininodes send transaction in "tx" message to zcashd node
tx2 = self.send_transaction(testnode0, coinbase_blocks[1], node_address, 204)
# tx2 is not expiring soon
assert_equal([tx2.hash], self.nodes[0].getrawmempool())
assert_equal([tx2.hash], self.nodes[1].getrawmempool())
# node 2 is isolated
assert_equal([], self.nodes[2].getrawmempool())
# Verify txid for tx2
self.verify_inv(testnode0, tx2)
self.send_data_message(testnode0, tx2)
self.verify_last_tx(testnode0, tx2)
# Sync and mine an empty block with node 2, leaving tx in the mempool of node0 and node1
for blkhash in coinbase_blocks:
blk = self.nodes[0].getblock(blkhash, 0)
self.nodes[2].submitblock(blk)
self.nodes[2].generate(1)
# Verify block count
assert_equal(self.nodes[0].getblockcount(), 200)
assert_equal(self.nodes[1].getblockcount(), 200)
assert_equal(self.nodes[2].getblockcount(), 201)
# Reconnect node 2 to the network
connect_nodes_bi(self.nodes, 0, 2)
# Set up test node for node 2
testnode2 = TestNode()
connections.append(NodeConn('127.0.0.1', p2p_port(2), self.nodes[2],
testnode2, "regtest", OVERWINTER_PROTO_VERSION))
testnode2.add_connection(connections[-1])
# Verify block count
sync_blocks(self.nodes[:3])
assert_equal(self.nodes[0].getblockcount(), 201)
assert_equal(self.nodes[1].getblockcount(), 201)
assert_equal(self.nodes[2].getblockcount(), 201)
# Verify contents of mempool
assert_equal([tx2.hash], self.nodes[0].getrawmempool())
assert_equal([tx2.hash], self.nodes[1].getrawmempool())
assert_equal([], self.nodes[2].getrawmempool())
# Confirm tx2 cannot be submitted to a mempool because it is expiring soon.
try:
rawtx2 = hexlify(tx2.serialize())
self.nodes[2].sendrawtransaction(rawtx2)
fail("Sending transaction should have failed")
except JSONRPCException as e:
assert_equal(
"tx-expiring-soon: expiryheight is 204 but should be at least 205 to avoid transaction expiring soon",
e.error['message']
)
self.send_data_message(testnode0, tx2)
# Sync up with node after p2p messages delivered
testnode0.sync_with_ping()
# Verify node 0 does not reply to "getdata" by sending "tx" message, as tx2 is expiring soon
with mininode_lock:
assert_equal(testnode0.last_tx, None)
# Verify mininode received a "notfound" message containing the txid of tx2
with mininode_lock:
msg = testnode0.last_notfound
assert_equal(len(msg.inv), 1)
assert_equal(tx2.sha256, msg.inv[0].hash)
# Create a transaction to verify that processing of "getdata" messages is functioning
tx3 = self.send_transaction(testnode0, coinbase_blocks[2], node_address, 999)
self.send_data_message(testnode0, tx3)
self.verify_last_tx(testnode0, tx3)
# Verify txid for tx3 is returned in "inv", but tx2 which is expiring soon is not returned
self.verify_inv(testnode0, tx3)
self.verify_inv(testnode2, tx3)
# Verify contents of mempool
assert_equal({tx2.hash, tx3.hash}, set(self.nodes[0].getrawmempool()))
assert_equal({tx2.hash, tx3.hash}, set(self.nodes[1].getrawmempool()))
assert_equal({tx3.hash}, set(self.nodes[2].getrawmempool()))
# Verify banscore for nodes are still zero
assert_equal(0, sum(peer["banscore"] for peer in self.nodes[0].getpeerinfo()))
assert_equal(0, sum(peer["banscore"] for peer in self.nodes[2].getpeerinfo()))
[c.disconnect_node() for c in connections]
if __name__ == '__main__':
TxExpiringSoonTest().main()