From 98d1482942ed769842d9aa31ed0dbdb4b4ec0036 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Thu, 16 Aug 2018 22:43:27 -0700 Subject: [PATCH] chainntnfs/interface_test: fix unreliable historical block ntfns test After joining the two forked chains, it is necessary to ensure they both agree on the same best hash before proceeding to UnsafeStart the notifier. This is because when the BitcoindClient starts, it retrieves its best known block then calls GetBlockHeaderVerbose on the hash of the retrieved block. This block could be a reorged block if JoinNodes has not completed sync. If it is the case that the best block retrieved has been reorged out of the chain, GetBlockHeaderVerbose errors because bitcoind sets the number of confirmations to -1 on reorged blocks, and the btcd rpc client panics when parsing a block whose number of confirmations is negative. This parsing error is expected to be fixed, and as a more permanent solution chain backends should ensure that the `best block` they retrieve during startup has not been reorged out of the chain. --- chainntnfs/interface_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/chainntnfs/interface_test.go b/chainntnfs/interface_test.go index 384cd7ee9..f51d912b3 100644 --- a/chainntnfs/interface_test.go +++ b/chainntnfs/interface_test.go @@ -1367,6 +1367,30 @@ func testCatchUpOnMissedBlocksWithReorg(miner1 *rpctest.Harness, t.Fatalf("unable to join node on blocks: %v", err) } + // The two should be on the same block hash. + timeout := time.After(10 * time.Second) + for { + nodeHash1, _, err := miner1.Node.GetBestBlock() + if err != nil { + t.Fatalf("unable to get current block hash: %v", err) + } + + nodeHash2, _, err := miner2.Node.GetBestBlock() + if err != nil { + t.Fatalf("unable to get current block hash: %v", err) + } + + if *nodeHash1 == *nodeHash2 { + break + } + select { + case <-timeout: + t.Fatalf("Unable to sync two chains") + case <-time.After(50 * time.Millisecond): + continue + } + } + // Next, start the notifier with outdated best block information. // We set the notifier's best block to be the last block mined on the // shorter chain, to test that the notifier correctly rewinds to