| 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 |
from __future__ import print_function |
| 33 |
import time |
| 34 |
|
| 35 |
"""Test daemon P2P |
| 36 |
""" |
| 37 |
|
| 38 |
from framework.daemon import Daemon |
| 39 |
from framework.wallet import Wallet |
| 40 |
|
| 41 |
class P2PTest(): |
| 42 |
def run_test(self): |
| 43 |
self.reset() |
| 44 |
self.create() |
| 45 |
self.mine(80) |
| 46 |
self.test_p2p_reorg() |
| 47 |
txid = self.test_p2p_tx_propagation() |
| 48 |
self.test_p2p_block_propagation_shared(txid) |
| 49 |
txid = self.test_p2p_tx_propagation() |
| 50 |
self.test_p2p_block_propagation_new(txid) |
| 51 |
|
| 52 |
def reset(self): |
| 53 |
print('Resetting blockchain') |
| 54 |
daemon = Daemon() |
| 55 |
res = daemon.get_height() |
| 56 |
daemon.pop_blocks(res.height - 1) |
| 57 |
daemon.flush_txpool() |
| 58 |
|
| 59 |
def create(self): |
| 60 |
print('Creating wallet') |
| 61 |
seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted' |
| 62 |
self.wallet = Wallet(idx = 4) |
| 63 |
|
| 64 |
try: self.wallet.close_wallet() |
| 65 |
except: pass |
| 66 |
res = self.wallet.restore_deterministic_wallet(seed = seed) |
| 67 |
|
| 68 |
def mine(self, blocks): |
| 69 |
assert blocks >= 1 |
| 70 |
|
| 71 |
print("Generating", blocks, 'blocks') |
| 72 |
|
| 73 |
daemon = Daemon(idx = 2) |
| 74 |
|
| 75 |
|
| 76 |
res_generateblocks = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', blocks) |
| 77 |
|
| 78 |
def test_p2p_reorg(self): |
| 79 |
print('Testing P2P reorg') |
| 80 |
daemon2 = Daemon(idx = 2) |
| 81 |
daemon3 = Daemon(idx = 3) |
| 82 |
|
| 83 |
|
| 84 |
time.sleep(1) |
| 85 |
|
| 86 |
res = daemon2.get_info() |
| 87 |
height = res.height |
| 88 |
assert height > 0 |
| 89 |
top_block_hash = res.top_block_hash |
| 90 |
assert len(top_block_hash) == 64 |
| 91 |
|
| 92 |
res = daemon3.get_info() |
| 93 |
assert res.height == height |
| 94 |
assert res.top_block_hash == top_block_hash |
| 95 |
|
| 96 |
|
| 97 |
daemon2.out_peers(0) |
| 98 |
daemon3.out_peers(0) |
| 99 |
|
| 100 |
res = daemon2.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 2) |
| 101 |
res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3) |
| 102 |
|
| 103 |
res = daemon2.get_info() |
| 104 |
assert res.height == height + 2 |
| 105 |
daemon2_top_block_hash = res.top_block_hash |
| 106 |
assert daemon2_top_block_hash != top_block_hash |
| 107 |
res = daemon3.get_info() |
| 108 |
assert res.height == height + 3 |
| 109 |
daemon3_top_block_hash = res.top_block_hash |
| 110 |
assert daemon3_top_block_hash != top_block_hash |
| 111 |
assert daemon3_top_block_hash != daemon2_top_block_hash |
| 112 |
|
| 113 |
|
| 114 |
daemon2.out_peers(8) |
| 115 |
daemon3.out_peers(8) |
| 116 |
time.sleep(10) |
| 117 |
res = daemon2.get_info() |
| 118 |
assert res.height == height + 3 |
| 119 |
assert res.top_block_hash == daemon3_top_block_hash |
| 120 |
|
| 121 |
|
| 122 |
daemon2.out_peers(0) |
| 123 |
daemon3.out_peers(0) |
| 124 |
|
| 125 |
res = daemon2.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3) |
| 126 |
res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 2) |
| 127 |
|
| 128 |
res = daemon2.get_info() |
| 129 |
assert res.height == height + 6 |
| 130 |
daemon2_top_block_hash = res.top_block_hash |
| 131 |
assert daemon2_top_block_hash != top_block_hash |
| 132 |
res = daemon3.get_info() |
| 133 |
assert res.height == height + 5 |
| 134 |
daemon3_top_block_hash = res.top_block_hash |
| 135 |
assert daemon3_top_block_hash != top_block_hash |
| 136 |
assert daemon3_top_block_hash != daemon2_top_block_hash |
| 137 |
|
| 138 |
|
| 139 |
daemon2.out_peers(8) |
| 140 |
daemon3.out_peers(8) |
| 141 |
time.sleep(5) |
| 142 |
res = daemon3.get_info() |
| 143 |
assert res.height == height + 6 |
| 144 |
assert res.top_block_hash == daemon2_top_block_hash |
| 145 |
|
| 146 |
|
| 147 |
daemon2.out_peers(0) |
| 148 |
daemon3.out_peers(0) |
| 149 |
res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 500) |
| 150 |
|
| 151 |
|
| 152 |
daemon2.out_peers(8) |
| 153 |
daemon3.out_peers(8) |
| 154 |
loops = 100 |
| 155 |
while True: |
| 156 |
res2 = daemon2.get_info() |
| 157 |
res3 = daemon3.get_info() |
| 158 |
if res2.top_block_hash == res3.top_block_hash: |
| 159 |
break |
| 160 |
time.sleep(10) |
| 161 |
loops -= 1 |
| 162 |
assert loops >= 0 |
| 163 |
|
| 164 |
def test_p2p_tx_propagation(self): |
| 165 |
print('Testing P2P tx propagation') |
| 166 |
daemons = (Daemon(idx=2), Daemon(idx=3)) |
| 167 |
|
| 168 |
for daemon in daemons: |
| 169 |
res = daemon.get_transaction_pool_hashes() |
| 170 |
assert len(res.get('tx_hashes', [])) == 0 |
| 171 |
|
| 172 |
self.wallet.refresh() |
| 173 |
res = self.wallet.get_balance() |
| 174 |
|
| 175 |
dst = {'address': '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 'amount': 1000000000000} |
| 176 |
res = self.wallet.transfer([dst]) |
| 177 |
assert len(res.tx_hash) == 32*2 |
| 178 |
txid = res.tx_hash |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
timeout = 13.5 |
| 186 |
pending_daemons = set(daemons) |
| 187 |
expected_hashes = [txid] |
| 188 |
wait_cutoff = time.monotonic() + timeout |
| 189 |
while True: |
| 190 |
done = [] |
| 191 |
for daemon in pending_daemons: |
| 192 |
res = daemon.get_transaction_pool_hashes() |
| 193 |
hashes = res.get('tx_hashes') |
| 194 |
if hashes: |
| 195 |
assert hashes == expected_hashes |
| 196 |
done.append(daemon) |
| 197 |
pending_daemons.difference_update(done) |
| 198 |
if len(pending_daemons) == 0: |
| 199 |
break |
| 200 |
max_delay = wait_cutoff - time.monotonic() |
| 201 |
if max_delay <= 0: |
| 202 |
break |
| 203 |
time.sleep(min(.2, max_delay)) |
| 204 |
npending = len(pending_daemons) |
| 205 |
assert npending == 0, '%d daemons pending' % npending |
| 206 |
|
| 207 |
return txid |
| 208 |
|
| 209 |
def test_p2p_block_propagation_shared(self, mempool_txid): |
| 210 |
print('Testing P2P block propagation with shared TX') |
| 211 |
daemon2 = Daemon(idx = 2) |
| 212 |
daemon3 = Daemon(idx = 3) |
| 213 |
|
| 214 |
|
| 215 |
res = daemon2.get_transaction_pool_hashes() |
| 216 |
assert mempool_txid in res.get('tx_hashes', []) |
| 217 |
|
| 218 |
res = daemon3.get_transaction_pool_hashes() |
| 219 |
assert mempool_txid in res.get('tx_hashes', []) |
| 220 |
|
| 221 |
|
| 222 |
res = daemon2.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1) |
| 223 |
block_height = res.height |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
deadline = time.monotonic() + 5 |
| 234 |
result = None |
| 235 |
while result is None: |
| 236 |
res2 = daemon2.get_info() |
| 237 |
res3 = daemon3.get_info() |
| 238 |
if res2.top_block_hash == res3.top_block_hash: |
| 239 |
result = True |
| 240 |
elif time.monotonic() > deadline: |
| 241 |
result = False |
| 242 |
else: |
| 243 |
time.sleep(.25) |
| 244 |
assert result, "Shared tx block propagation timed out" |
| 245 |
|
| 246 |
|
| 247 |
for daemon in [daemon2, daemon3]: |
| 248 |
res = daemon.get_transaction_pool_hashes() |
| 249 |
assert not 'tx_hashes' in res or len(res.tx_hashes) == 0 |
| 250 |
|
| 251 |
res = daemon.get_transactions([mempool_txid]) |
| 252 |
assert len(res.get('txs', [])) == 1 |
| 253 |
tx_details = res.txs[0] |
| 254 |
assert ('in_pool' not in tx_details) or (not tx_details.in_pool) |
| 255 |
assert tx_details.block_height == block_height |
| 256 |
|
| 257 |
def test_p2p_block_propagation_new(self, mempool_txid): |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
print('Testing P2P block propagation with (possibly) new TX') |
| 266 |
daemon2 = Daemon(idx = 2) |
| 267 |
daemon3 = Daemon(idx = 3) |
| 268 |
|
| 269 |
|
| 270 |
res = daemon3.get_transaction_pool_hashes() |
| 271 |
assert mempool_txid in res.get('tx_hashes', []) |
| 272 |
|
| 273 |
|
| 274 |
daemon2.flush_txpool() |
| 275 |
|
| 276 |
|
| 277 |
res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1) |
| 278 |
block_height = res.height |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
deadline = time.monotonic() + 5 |
| 285 |
result = None |
| 286 |
while result is None: |
| 287 |
res2 = daemon2.get_info() |
| 288 |
res3 = daemon3.get_info() |
| 289 |
if res2.top_block_hash == res3.top_block_hash: |
| 290 |
result = True |
| 291 |
elif time.monotonic() > deadline: |
| 292 |
result = False |
| 293 |
else: |
| 294 |
time.sleep(.25) |
| 295 |
assert result, "New tx block propagation timed out" |
| 296 |
|
| 297 |
|
| 298 |
for daemon in [daemon2, daemon3]: |
| 299 |
res = daemon.get_transaction_pool_hashes() |
| 300 |
assert not 'tx_hashes' in res or len(res.tx_hashes) == 0 |
| 301 |
|
| 302 |
res = daemon.get_transactions([mempool_txid]) |
| 303 |
assert len(res.get('txs', [])) == 1 |
| 304 |
tx_details = res.txs[0] |
| 305 |
assert ('in_pool' not in tx_details) or (not tx_details.in_pool) |
| 306 |
assert tx_details.block_height == block_height |
| 307 |
|
| 308 |
|
| 309 |
if __name__ == '__main__': |
| 310 |
P2PTest().run_test() |