lightninglabs / neutrino

Privacy-Preserving Bitcoin Light Client
MIT License
894 stars 182 forks source link

Regtest: "block target difficulty is higher than max" #255

Closed amanzag closed 1 year ago

amanzag commented 2 years ago

Whenever I try to use neutrino in regtest mode, I run into this error:

I/GoLog   (29209): 2022-08-02 14:49:05.236 [DBG] BTCN: Difficulty retarget at block height 4032
I/GoLog   (29209): 2022-08-02 14:49:05.236 [DBG] BTCN: Old target 207fffff (7fffff0000000000000000000000000000000000000000000000000000000000)
I/GoLog   (29209): 2022-08-02 14:49:05.236 [DBG] BTCN: New target 201fffff (1fffff0000000000000000000000000000000000000000000000000000000000)
I/GoLog   (29209): 2022-08-02 14:49:05.236 [DBG] BTCN: Actual timespan 33h35m0s, adjusted timespan 84h0m0s, target timespan 336h0m0s
I/GoLog   (29209): 2022-08-02 14:49:05.236 [WRN] BTCN: Header doesn't pass sanity check: block target difficulty of 7fffff0000000000000000000000000000000000000000000000000000000000 is higher than max of 1fffff0000000000000000000000000000000000000000000000000000000000 -- disconnecting peer

There must be some flaw in the difficulty calculation logic here: https://github.com/lightninglabs/neutrino/blob/master/blockmanager.go#L2795-L2834

Related issue (but I think it belongs here): https://github.com/btcsuite/btcd/issues/1819

Roasbeef commented 2 years ago

Are you able to reproduce the same thing w/ simnet? IIRC, I think btcd will do difficulty retargets for regtest, while bitcoind never does. This is what historically led to the creation of simnet (which does the retargets).

amanzag commented 2 years ago

I think you're right. I can't reproduce with simnet and that's probably because I'm running neutrino against a bitcoind which, as you say, doesn't do retargets, as can be seen here: https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L407

Shouldn't btcd/neutrino disable retargets for regtest as well?

amanzag commented 2 years ago

I created a pull request https://github.com/lightninglabs/neutrino/pull/256 that fixes the issue. It's probably not the right way of fixing it, but if you point me in the right direction I can change it.

guggero commented 1 year ago

bitcoind was refactored a bit, the fPowNoRetargeting field can now be found here: https://github.com/bitcoin/bitcoin/blob/e695d8536e534f1e59de4a62a0d87a93e7a73456/src/kernel/chainparams.cpp#L415