From 5036697010397f408b3cc81600fd48dbf957116f Mon Sep 17 00:00:00 2001 From: Edmund Noble Date: Wed, 8 Jan 2025 12:41:18 -0500 Subject: [PATCH] Include difficulty as Double in object-encoded block header Change-Id: Id0000000010fc50d9363200a7d4ea8c251a2f25a --- src/Chainweb/BlockHeader/Internal.hs | 2 +- src/Chainweb/BlockWeight.hs | 5 ++++- src/Chainweb/Difficulty.hs | 14 +++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Chainweb/BlockHeader/Internal.hs b/src/Chainweb/BlockHeader/Internal.hs index f1583b6bf5..cc6bbedf1f 100644 --- a/src/Chainweb/BlockHeader/Internal.hs +++ b/src/Chainweb/BlockHeader/Internal.hs @@ -1009,6 +1009,7 @@ blockHeaderProperties (ObjectEncoded b) = , "parent" .= _blockParent b , "adjacents" .= _blockAdjacentHashes b , "target" .= _blockTarget b + , "difficultyDouble" .= difficultyToDouble (targetToDifficulty (_blockTarget b)) , "payloadHash" .= _blockPayloadHash b , "chainId" .= _chainId b , "weight" .= _blockWeight b @@ -1205,4 +1206,3 @@ _rankedBlockPayloadHash h = RankedBlockPayloadHash rankedBlockPayloadHash :: Getter BlockHeader RankedBlockPayloadHash rankedBlockPayloadHash = to _rankedBlockPayloadHash {-# INLINE rankedBlockPayloadHash #-} - diff --git a/src/Chainweb/BlockWeight.hs b/src/Chainweb/BlockWeight.hs index 28557e29aa..59604a3085 100644 --- a/src/Chainweb/BlockWeight.hs +++ b/src/Chainweb/BlockWeight.hs @@ -22,6 +22,7 @@ module Chainweb.BlockWeight ( -- * Block Weight BlockWeight(..) +, blockWeightToDouble , encodeBlockWeight , decodeBlockWeight , encodeBlockWeightBe @@ -66,6 +67,9 @@ instance MerkleHashAlgorithm a => IsMerkleLogEntry a ChainwebHashTag BlockWeight {-# INLINE toMerkleNode #-} {-# INLINE fromMerkleNode #-} +blockWeightToDouble :: BlockWeight -> Double +blockWeightToDouble (BlockWeight diff) = difficultyToDouble diff + encodeBlockWeight :: BlockWeight -> Put encodeBlockWeight (BlockWeight w) = encodeHashDifficulty w {-# INLINE encodeBlockWeight #-} @@ -81,4 +85,3 @@ encodeBlockWeightBe (BlockWeight w) = encodeHashDifficultyBe w decodeBlockWeightBe :: Get BlockWeight decodeBlockWeightBe = BlockWeight <$> decodeHashDifficultyBe {-# INLINE decodeBlockWeightBe #-} - diff --git a/src/Chainweb/Difficulty.hs b/src/Chainweb/Difficulty.hs index 72345ffa78..4513bd2d9d 100644 --- a/src/Chainweb/Difficulty.hs +++ b/src/Chainweb/Difficulty.hs @@ -14,6 +14,7 @@ {-# LANGUAGE TypeOperators #-} {-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE TypeApplications #-} -- | -- Module: Chainweb.Difficulty @@ -32,6 +33,7 @@ module Chainweb.Difficulty -- * PowHashNat , PowHashNat(..) , powHashNat +, powHashNatToDouble , encodePowHashNat , decodePowHashNat , encodePowHashNatBe @@ -50,6 +52,7 @@ module Chainweb.Difficulty -- * HashDifficulty , HashDifficulty(..) +, difficultyToDouble , encodeHashDifficulty , decodeHashDifficulty , encodeHashDifficultyBe @@ -89,6 +92,7 @@ import Chainweb.Utils import Chainweb.Utils.Serialization import Numeric.Additive +import Data.Ratio -- -------------------------------------------------------------------------- -- -- Large Word Orphans @@ -141,6 +145,12 @@ powHashToWord256 :: (32 <= PowHashBytesCount) => PowHash -> Word256 powHashToWord256 = either error id . runGetEitherS decodeWordLe . SB.fromShort . powHashBytes {-# INLINE powHashToWord256 #-} +-- Strictly for presenting difficulty approximately to interfaces that can't understand Word256. +powHashNatToDouble :: PowHashNat -> Double +powHashNatToDouble (PowHashNat w) = realToFrac $ + (fromIntegral w :: Integer) % (fromIntegral (maxBound @Word256) :: Integer) +{-# INLINE powHashNatToDouble #-} + encodePowHashNat :: PowHashNat -> Put encodePowHashNat (PowHashNat n) = encodeWordLe n {-# INLINE encodePowHashNat #-} @@ -257,6 +267,9 @@ newtype HashDifficulty = HashDifficulty PowHashNat deriving newtype (AdditiveSemigroup, AdditiveAbelianSemigroup) deriving newtype (Num, Integral, Real) +difficultyToDouble :: HashDifficulty -> Double +difficultyToDouble (HashDifficulty phn) = powHashNatToDouble phn + encodeHashDifficulty :: HashDifficulty -> Put encodeHashDifficulty (HashDifficulty x) = encodePowHashNat x {-# INLINE encodeHashDifficulty #-} @@ -347,4 +360,3 @@ legacyAdjust (BlockDelay bd) (WindowWidth ww) (TimeSpan delta) (HashTarget oldTa newTarget :: HashTarget newTarget = HashTarget . PowHashNat $ maxTargetWord `div` ceiling newDiff -