Repository: bitcoin/bitcoin (language: cpp)

Issue:
`invalidateblock` during background validation
If you call `invalidateblock` (on the tip) during background validation, the node will currently do the following:
#### bitcoin-cli invalidateblock 00000000000000000000f5752e9599f03a7b3a3e2356af91faaf74e41fdf58fe
```bash
2024-09-24T13:01:15Z [background validation] UpdateTip: new best=0000000000000000008dd2857255b2e5fc0c3955740c74dac6bafd0de09c344e height=482000 version=0x20000000 log2_work=86.991735 tx=249395394 date='2017-08-25T19:52:51Z' progress=0.230269 cache=238.7MiB(1748399txo)
2024-09-24T13:02:42Z Saw new header hash=00000000000000000000f5752e9599f03a7b3a3e2356af91faaf74e41fdf58fe height=862671
2024-09-24T13:02:45Z UpdateTip: new best=00000000000000000000f5752e9599f03a7b3a3e2356af91faaf74e41fdf58fe height=862671 version=0x2be3a000 log2_work=95.172229 tx=1085328468 date='2024-09-24T13:02:21Z' progress=1.000000 cache=11.6MiB(81087txo)
2024-09-24T13:02:52Z [background validation] UpdateTip: new best=000000000000000000b7738efb4c4acbc841c032294d2e6ab09295f4117c7b3a height=484000 version=0x20000002 log2_work=87.061771 tx=252628356 date='2017-09-07T11:50:44Z' progress=0.233253 cache=688.5MiB(5222117txo)
2024-09-24T13:02:58Z UpdateTip: new best=000000000000000000029f913a20c71e01321ec8b320418b6fe7c38e708a0eec height=862670 version=0x3fff0000 log2_work=95.172216 tx=1085323202 date='2024-09-24T13:00:39Z' progress=0.999999 cache=11.8MiB(77967txo)
2024-09-24T13:02:58Z InvalidChainFound: invalid block=00000000000000000000f5752e9599f03a7b3a3e2356af91faaf74e41fdf58fe  height=862671  log2_work=95.172229  date=2024-09-24T13:02:21Z
2024-09-24T13:02:58Z InvalidChainFound:  current best=000000000000000000029f913a20c71e01321ec8b320418b6fe7c38e708a0eec  height=862670  log2_work=95.172216  date=2024-09-24T13:00:39Z
2024-09-24T13:02:58Z CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.
Chain state database corruption likely.
2024-09-24T13:02:58Z CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.
Chain state database corruption likely.
2024-09-24T13:02:58Z CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.
Chain state database corruption likely.
2024-09-24T13:02:58Z CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.
<repeat for every new block processed in the background validation>
```
#### bitcoin-cli reconsiderblock 00000000000000000000f5752e9599f03a7b3a3e2356af91faaf74e41fdf58fe
```bash
2024-09-24T13:03:10Z CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.
Chain state database corruption likely.
2024-09-24T13:03:10Z CheckForkWarningConditions: Warning: Found invalid chain at least ~6 blocks longer than our best chain.
Chain state database corruption likely.
2024-09-24T13:03:10Z UpdateTip: new best=00000000000000000000f5752e9599f03a7b3a3e2356af91faaf74e41fdf58fe height=862671 version=0x2be3a000 log2_work=95.172229 tx=1085328468 date='2024-09-24T13:02:21Z' progress=1.000000 cache=11.6MiB(81332txo)
2024-09-24T13:03:16Z New outbound-full-relay v2 peer connected: version: 70016, blocks=862671, peer=69
2024-09-24T13:04:44Z [background validation] UpdateTip: new best=000000000000000000a2a8104d61651f76c666b70754d6e9346176385f7afa24 height=486000 version=0x20000000 log2_work=87.131847 tx=255540060 date='2017-09-19T09:09:00Z' progress=0.235942 cache=1014.2MiB(7475508txo)
2024-09-24T13:06:42Z [background validation] UpdateTip: new best=000000000000000000f980eebec0616501eead146975f1df5ca9a1db40309547 height=488000 version=0x20000000 log2_work=87.210415 tx=258791302 date='2017-10-02T21:02:34Z' progress=0.238943 cache=1284.3MiB(9681780txo)
2024-09-24T13:08:01Z [background validation] UpdateTip: new best=000000000000000000de069137b17b8d5a3dfbd5b145b2dcfb203f15d0c4de90 height=490000 version=0x20000000 log2_work=87.286453 tx=262368718 date='2017-10-15T17:57:17Z' progress=0.242246 cache=1521.0MiB(11621530txo)
```

Printing these warning messages does not seem useful, or even the correct thing to do during a background sync; as the chain being warned about, is still our best, active chain?


Discussion / hints:

Fix patch:
diff --git a/src/validation.cpp b/src/validation.cpp
index b47b1d09ae1e9..5da3387ad296f 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -2020,7 +2020,8 @@ void Chainstate::CheckForkWarningConditions()
 
     // Before we get past initial download, we cannot reliably alert about forks
     // (we assume we don't get stuck on a fork before finishing our initial sync)
-    if (m_chainman.IsInitialBlockDownload()) {
+    // Also not applicable to the background chainstate
+    if (m_chainman.IsInitialBlockDownload() || this->GetRole() == ChainstateRole::BACKGROUND) {
         return;
     }
 


Test patch: