lntemp: after mining, sync to latest block

Ensure active nodes are synced to the latest block mined.

There are two scenarios where they might not be synced to the correct
block even when SyncedToChain is true:
  1. The backend may have rejected a newly mined block (e.g., see
     #7241).
  2. The backend might not have fully processed the new blocks yet.

In either case SyncedToChain will (correctly) be true since the node is
indeed fully synced to the backend. This commit makes sure we detect
case 1 above, while making sure we continue to wait in case 2.
This commit is contained in:
Matt Morehouse 2022-12-07 15:23:29 -06:00
parent d9febbb9fc
commit b72993c9dd
No known key found for this signature in database
GPG Key ID: CC8ECA224831C982
2 changed files with 35 additions and 2 deletions

View File

@ -1291,7 +1291,8 @@ func (h *HarnessTest) MineBlocks(num uint32) []*wire.MsgBlock {
blocks := h.Miner.MineBlocksSlow(num)
// Make sure all the active nodes are synced.
h.AssertActiveNodesSynced()
bestBlock := blocks[len(blocks)-1]
h.AssertActiveNodesSyncedTo(bestBlock)
return blocks
}
@ -1318,7 +1319,8 @@ func (h *HarnessTest) MineBlocksAndAssertNumTxes(num uint32,
}
// Finally, make sure all the active nodes are synced.
h.AssertActiveNodesSynced()
bestBlock := blocks[len(blocks)-1]
h.AssertActiveNodesSyncedTo(bestBlock)
return blocks
}

View File

@ -39,6 +39,29 @@ func (h *HarnessTest) WaitForBlockchainSync(hn *node.HarnessNode) {
require.NoError(h, err, "timeout waiting for blockchain sync")
}
// WaitForBlockchainSyncTo waits until the node is synced to bestBlock.
func (h *HarnessTest) WaitForBlockchainSyncTo(hn *node.HarnessNode,
bestBlock *wire.MsgBlock) {
bestBlockHash := bestBlock.BlockHash().String()
err := wait.NoError(func() error {
resp := hn.RPC.GetInfo()
if resp.SyncedToChain {
if resp.BlockHash == bestBlockHash {
return nil
}
return fmt.Errorf("%s's backend is synced to the "+
"wrong block (expected=%s, actual=%s)",
hn.Name(), bestBlockHash, resp.BlockHash)
}
return fmt.Errorf("%s is not synced to chain", hn.Name())
}, DefaultTimeout)
require.NoError(h, err, "timeout waiting for blockchain sync")
}
// AssertPeerConnected asserts that the given node b is connected to a.
func (h *HarnessTest) AssertPeerConnected(a, b *node.HarnessNode) {
err := wait.NoError(func() error {
@ -1324,6 +1347,14 @@ func (h *HarnessTest) AssertActiveNodesSynced() {
}
}
// AssertActiveNodesSyncedTo asserts all active nodes have synced to the
// provided bestBlock.
func (h *HarnessTest) AssertActiveNodesSyncedTo(bestBlock *wire.MsgBlock) {
for _, node := range h.manager.activeNodes {
h.WaitForBlockchainSyncTo(node, bestBlock)
}
}
// AssertPeerNotConnected asserts that the given node b is not connected to a.
func (h *HarnessTest) AssertPeerNotConnected(a, b *node.HarnessNode) {
err := wait.NoError(func() error {