Skip to content

Commit

Permalink
Include difficulty as Double in object-encoded block header
Browse files Browse the repository at this point in the history
Change-Id: Id0000000010fc50d9363200a7d4ea8c251a2f25a
  • Loading branch information
edmundnoble committed Jan 8, 2025
1 parent f50341e commit 5036697
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Chainweb/BlockHeader/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1205,4 +1206,3 @@ _rankedBlockPayloadHash h = RankedBlockPayloadHash
rankedBlockPayloadHash :: Getter BlockHeader RankedBlockPayloadHash
rankedBlockPayloadHash = to _rankedBlockPayloadHash
{-# INLINE rankedBlockPayloadHash #-}

5 changes: 4 additions & 1 deletion src/Chainweb/BlockWeight.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Chainweb.BlockWeight
(
-- * Block Weight
BlockWeight(..)
, blockWeightToDouble
, encodeBlockWeight
, decodeBlockWeight
, encodeBlockWeightBe
Expand Down Expand Up @@ -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 #-}
Expand All @@ -81,4 +85,3 @@ encodeBlockWeightBe (BlockWeight w) = encodeHashDifficultyBe w
decodeBlockWeightBe :: Get BlockWeight
decodeBlockWeightBe = BlockWeight <$> decodeHashDifficultyBe
{-# INLINE decodeBlockWeightBe #-}

14 changes: 13 additions & 1 deletion src/Chainweb/Difficulty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
{-# LANGUAGE TypeOperators #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE TypeApplications #-}

-- |
-- Module: Chainweb.Difficulty
Expand All @@ -32,6 +33,7 @@ module Chainweb.Difficulty
-- * PowHashNat
, PowHashNat(..)
, powHashNat
, powHashNatToDouble
, encodePowHashNat
, decodePowHashNat
, encodePowHashNatBe
Expand All @@ -50,6 +52,7 @@ module Chainweb.Difficulty

-- * HashDifficulty
, HashDifficulty(..)
, difficultyToDouble
, encodeHashDifficulty
, decodeHashDifficulty
, encodeHashDifficultyBe
Expand Down Expand Up @@ -89,6 +92,7 @@ import Chainweb.Utils
import Chainweb.Utils.Serialization

import Numeric.Additive
import Data.Ratio

-- -------------------------------------------------------------------------- --
-- Large Word Orphans
Expand Down Expand Up @@ -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 #-}
Expand Down Expand Up @@ -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 #-}
Expand Down Expand Up @@ -347,4 +360,3 @@ legacyAdjust (BlockDelay bd) (WindowWidth ww) (TimeSpan delta) (HashTarget oldTa

newTarget :: HashTarget
newTarget = HashTarget . PowHashNat $ maxTargetWord `div` ceiling newDiff

0 comments on commit 5036697

Please sign in to comment.