Skip to content

Commit

Permalink
lntemp: after mining, sync to latest block
Browse files Browse the repository at this point in the history
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
     lightningnetwork#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.
  • Loading branch information
morehouse committed Dec 12, 2022
1 parent d9febbb commit b72993c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lntemp/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions lntemp/harness_assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b72993c

Please sign in to comment.