|
31 | 31 | disconnect_nodes,
|
32 | 32 | get_datadir_path,
|
33 | 33 | initialize_datadir,
|
34 |
| - sync_blocks, |
35 |
| - sync_mempools, |
36 | 34 | )
|
37 | 35 |
|
38 | 36 |
|
@@ -549,15 +547,54 @@ def join_network(self):
|
549 | 547 | connect_nodes(self.nodes[1], 2)
|
550 | 548 | self.sync_all()
|
551 | 549 |
|
552 |
| - def sync_blocks(self, nodes=None, **kwargs): |
553 |
| - sync_blocks(nodes or self.nodes, **kwargs) |
554 |
| - |
555 |
| - def sync_mempools(self, nodes=None, **kwargs): |
556 |
| - sync_mempools(nodes or self.nodes, **kwargs) |
557 |
| - |
558 |
| - def sync_all(self, nodes=None, **kwargs): |
559 |
| - self.sync_blocks(nodes, **kwargs) |
560 |
| - self.sync_mempools(nodes, **kwargs) |
| 550 | + def sync_blocks(self, nodes=None, wait=1, timeout=60): |
| 551 | + """ |
| 552 | + Wait until everybody has the same tip. |
| 553 | + sync_blocks needs to be called with an rpc_connections set that has least |
| 554 | + one node already synced to the latest, stable tip, otherwise there's a |
| 555 | + chance it might return before all nodes are stably synced. |
| 556 | + """ |
| 557 | + rpc_connections = nodes or self.nodes |
| 558 | + timeout = int(timeout * self.options.timeout_factor) |
| 559 | + stop_time = time.time() + timeout |
| 560 | + while time.time() <= stop_time: |
| 561 | + best_hash = [x.getbestblockhash() for x in rpc_connections] |
| 562 | + if best_hash.count(best_hash[0]) == len(rpc_connections): |
| 563 | + return |
| 564 | + # Check that each peer has at least one connection |
| 565 | + assert (all([len(x.getpeerinfo()) for x in rpc_connections])) |
| 566 | + time.sleep(wait) |
| 567 | + raise AssertionError("Block sync timed out after {}s:{}".format( |
| 568 | + timeout, |
| 569 | + "".join("\n {!r}".format(b) for b in best_hash), |
| 570 | + )) |
| 571 | + |
| 572 | + def sync_mempools(self, nodes=None, wait=1, timeout=60, flush_scheduler=True): |
| 573 | + """ |
| 574 | + Wait until everybody has the same transactions in their memory |
| 575 | + pools |
| 576 | + """ |
| 577 | + rpc_connections = nodes or self.nodes |
| 578 | + timeout = int(timeout * self.options.timeout_factor) |
| 579 | + stop_time = time.time() + timeout |
| 580 | + while time.time() <= stop_time: |
| 581 | + pool = [set(r.getrawmempool()) for r in rpc_connections] |
| 582 | + if pool.count(pool[0]) == len(rpc_connections): |
| 583 | + if flush_scheduler: |
| 584 | + for r in rpc_connections: |
| 585 | + r.syncwithvalidationinterfacequeue() |
| 586 | + return |
| 587 | + # Check that each peer has at least one connection |
| 588 | + assert (all([len(x.getpeerinfo()) for x in rpc_connections])) |
| 589 | + time.sleep(wait) |
| 590 | + raise AssertionError("Mempool sync timed out after {}s:{}".format( |
| 591 | + timeout, |
| 592 | + "".join("\n {!r}".format(m) for m in pool), |
| 593 | + )) |
| 594 | + |
| 595 | + def sync_all(self, nodes=None): |
| 596 | + self.sync_blocks(nodes) |
| 597 | + self.sync_mempools(nodes) |
561 | 598 |
|
562 | 599 | # Private helper methods. These should not be accessed by the subclass test scripts.
|
563 | 600 |
|
|
0 commit comments