Check difficulty transition against `Target` instead of `Work`
authorWilmer Paulino <wilmer@wilmerpaulino.com>
Fri, 28 Apr 2023 09:06:23 +0000 (02:06 -0700)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Wed, 10 May 2023 18:39:46 +0000 (11:39 -0700)
`rust-bitcoin v0.30.0` made some changes in this area that no longer
allow us to work with the previously exposed `U256` type. While `Work`
and `Target` (they're inverses of each other) essentially represent the
same concept, it makes more sense from their API's perspective to only
expose difficulty transitions and adjustments on `Target`s.

lightning-block-sync/src/poll.rs

index 9f7e8becf5060c2f62471825a2aa0cfeca573f5e..e7171cf3656138036d50385691c96e419db29bcd 100644 (file)
@@ -136,8 +136,11 @@ impl ValidatedBlockHeader {
 
                if let Network::Bitcoin = network {
                        if self.height % 2016 == 0 {
-                               let previous_work = previous_header.header.work();
-                               if work > (previous_work << 2) || work < (previous_work >> 2) {
+                               let target = self.header.target();
+                               let previous_target = previous_header.header.target();
+                               let min_target = previous_target >> 2;
+                               let max_target = previous_target << 2;
+                               if target > max_target || target < min_target {
                                        return Err(BlockSourceError::persistent("invalid difficulty transition"))
                                }
                        } else if self.header.bits != previous_header.header.bits {