Wait until everybody has the same tip. sync_blocks needs to be called with an rpc_connections set that has least one node already synced to the latest, stable tip, otherwise there's a chance it might return before all nodes are stably synced.
(rpc_connections, *, wait=1, timeout=60)
| 356 | connect_nodes(nodes[b], a) |
| 357 | |
| 358 | def sync_blocks(rpc_connections, *, wait=1, timeout=60): |
| 359 | """ |
| 360 | Wait until everybody has the same tip. |
| 361 | |
| 362 | sync_blocks needs to be called with an rpc_connections set that has least |
| 363 | one node already synced to the latest, stable tip, otherwise there's a |
| 364 | chance it might return before all nodes are stably synced. |
| 365 | """ |
| 366 | # Use getblockcount() instead of waitforblockheight() to determine the |
| 367 | # initial max height because the two RPCs look at different internal global |
| 368 | # variables (chainActive vs latestBlock) and the former gets updated |
| 369 | # earlier. |
| 370 | maxheight = max(x.getblockcount() for x in rpc_connections) |
| 371 | start_time = cur_time = time.time() |
| 372 | while cur_time <= start_time + timeout: |
| 373 | tips = [r.waitforblockheight(maxheight, int(wait * 1000)) for r in rpc_connections] |
| 374 | if all(t["height"] == maxheight for t in tips): |
| 375 | if all(t["hash"] == tips[0]["hash"] for t in tips): |
| 376 | return |
| 377 | raise AssertionError("Block sync failed, mismatched block hashes:{}".format( |
| 378 | "".join("\n {!r}".format(tip) for tip in tips))) |
| 379 | cur_time = time.time() |
| 380 | raise AssertionError("Block sync to height {} timed out:{}".format( |
| 381 | maxheight, "".join("\n {!r}".format(tip) for tip in tips))) |
| 382 | |
| 383 | def sync_chain(rpc_connections, *, wait=1, timeout=60): |
| 384 | """ |