Skip to content

Commit

Permalink
Include difficulty as Double in BlockUpdate telemetry
Browse files Browse the repository at this point in the history
This allows it to be more easily ingested by ElasticSearch.

Change-Id: Id0000000010fc50d9363200a7d4ea8c251a2f25a
  • Loading branch information
edmundnoble committed Jan 14, 2025
1 parent 65991a0 commit 668cb81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions node/src/ChainwebNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import Chainweb.Chainweb.CutResources
import Chainweb.Counter
import Chainweb.Cut.CutHashes
import Chainweb.CutDB
import Chainweb.Difficulty
import Chainweb.Logger
import Chainweb.Logging.Config
import Chainweb.Logging.Miner
Expand Down Expand Up @@ -223,6 +224,7 @@ data BlockUpdate = BlockUpdate
{ _blockUpdateBlockHeader :: !(ObjectEncoded BlockHeader)
, _blockUpdateOrphaned :: !Bool
, _blockUpdateTxCount :: !Int
, _blockUpdateDifficultyDouble :: !Double
}
deriving (Show, Eq, Ord, Generic, NFData)

Expand All @@ -231,10 +233,12 @@ instance ToJSON BlockUpdate where
$ "header" .= _blockUpdateBlockHeader o
<> "orphaned" .= _blockUpdateOrphaned o
<> "txCount" .= _blockUpdateTxCount o
<> "difficultyDouble" .= _blockUpdateDifficultyDouble o
toJSON o = object
[ "header" .= _blockUpdateBlockHeader o
, "orphaned" .= _blockUpdateOrphaned o
, "txCount" .= _blockUpdateTxCount o
, "difficultyDouble" .= _blockUpdateDifficultyDouble o
]

{-# INLINE toEncoding #-}
Expand All @@ -261,10 +265,12 @@ runBlockUpdateMonitor logger db = L.withLoggerLabel ("component", "block-update-
<$> pure (ObjectEncoded bh) -- _blockUpdateBlockHeader
<*> pure False -- _blockUpdateOrphaned
<*> txCount bh -- _blockUpdateTxCount
<*> pure (difficultyToDouble (targetToDifficulty (view blockTarget bh))) -- _blockUpdateDifficultyDouble
toUpdate (Left bh) = BlockUpdate
<$> pure (ObjectEncoded bh) -- _blockUpdateBlockHeader
<*> pure True -- _blockUpdateOrphaned
<*> ((0 -) <$> txCount bh) -- _blockUpdateTxCount
<*> pure (difficultyToDouble (targetToDifficulty (view blockTarget bh))) -- _blockUpdateDifficultyDouble

-- type CutLog = HM.HashMap ChainId (ObjectEncoded BlockHeader)

Expand Down
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 #-}

15 changes: 14 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,10 @@ newtype HashDifficulty = HashDifficulty PowHashNat
deriving newtype (AdditiveSemigroup, AdditiveAbelianSemigroup)
deriving newtype (Num, Integral, Real)

-- Strictly for presenting difficulty approximately to interfaces that can't understand Word256.
difficultyToDouble :: HashDifficulty -> Double
difficultyToDouble (HashDifficulty phn) = powHashNatToDouble phn

encodeHashDifficulty :: HashDifficulty -> Put
encodeHashDifficulty (HashDifficulty x) = encodePowHashNat x
{-# INLINE encodeHashDifficulty #-}
Expand Down Expand Up @@ -347,4 +361,3 @@ legacyAdjust (BlockDelay bd) (WindowWidth ww) (TimeSpan delta) (HashTarget oldTa

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

0 comments on commit 668cb81

Please sign in to comment.