From 6c37cae63905ccf9e4b5b3e978be21d5a2c2a560 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 7 May 2021 19:11:23 +0200 Subject: [PATCH] lntest+routing: update best height after graph pruning It seems #5246 introduced a subtle bug that lead to the error "out of order block: expecting height=1, got height=XXX" some times during startup. Apparently it can happen that during pruning of the graph tip some blocks can come in before we start our chain view and the new block subscription. By querying the chain backend for the best height before syncing with the graph we ensure that we never miss a block. --- lntest/harness.go | 18 +++++++++++++++--- routing/router.go | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lntest/harness.go b/lntest/harness.go index 0d8bf520..f83e7ae6 100644 --- a/lntest/harness.go +++ b/lntest/harness.go @@ -177,9 +177,16 @@ func (n *NetworkHarness) SetUp(testCase string, lndArgs []string) error { default: } + // First, make a connection between the two nodes. This will wait until + // both nodes are fully started since the Connect RPC is guarded behind + // the server.Started() flag that waits for all subsystems to be ready. + ctxb := context.Background() + if err := n.ConnectNodes(ctxb, n.Alice, n.Bob); err != nil { + return err + } + // Load up the wallets of the seeder nodes with 10 outputs of 1 BTC // each. - ctxb := context.Background() addrReq := &lnrpc.NewAddressRequest{ Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH, } @@ -216,8 +223,13 @@ func (n *NetworkHarness) SetUp(testCase string, lndArgs []string) error { return err } - // Finally, make a connection between both of the nodes. - if err := n.ConnectNodes(ctxb, n.Alice, n.Bob); err != nil { + // Now we want to wait for the nodes to catch up. + ctxt, cancel := context.WithTimeout(ctxb, DefaultTimeout) + defer cancel() + if err := n.Alice.WaitForBlockchainSync(ctxt); err != nil { + return err + } + if err := n.Bob.WaitForBlockchainSync(ctxt); err != nil { return err } diff --git a/routing/router.go b/routing/router.go index aab8e7d6..48ba3062 100644 --- a/routing/router.go +++ b/routing/router.go @@ -563,6 +563,14 @@ func (r *ChannelRouter) Start() error { } } + // The graph pruning might have taken a while and there could be + // new blocks available. + bestHash, bestHeight, err = r.cfg.Chain.GetBestBlock() + if err != nil { + return err + } + r.bestHeight = uint32(bestHeight) + // Before we begin normal operation of the router, we first need // to synchronize the channel graph to the latest state of the // UTXO set.