Skip to content

consensus: integrate HeaderWithTime #6211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ if impl (ghc >= 9.12)
-- https://github.com/haskell-servant/servant/pull/1810
, servant:base
, servant-server:base


source-repository-package
type: git
location: https://github.com/IntersectMBO/ouroboros-consensus.git
tag: abe2ef2b9
subdir:
ouroboros-consensus
ouroboros-consensus-diffusion
--sha256: sha256-D1oxLyKk+HfpBjaakTmzzSYRJrPT9U0LEtFPsPXCAb4=
2 changes: 1 addition & 1 deletion cardano-node/cardano-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ library
, network-mux >= 0.5
, nothunks
, optparse-applicative-fork >= 0.18.1
, ouroboros-consensus ^>= 0.26.0.1
, ouroboros-consensus ^>= 0.26
, ouroboros-consensus-cardano ^>= 0.25
, ouroboros-consensus-diffusion ^>= 0.22
, ouroboros-consensus-protocol
Expand Down
25 changes: 21 additions & 4 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/Peer.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.Node.Tracing.Tracers.Peer
( PeerT (..)
Expand All @@ -14,6 +16,7 @@ import Cardano.Logging hiding (traceWith)
import Cardano.Node.Orphans ()
import Cardano.Node.Queries
import Ouroboros.Consensus.Block (Header)
import Ouroboros.Consensus.HeaderValidation (HeaderWithTime (..))
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client (ChainSyncClientHandle,
csCandidate, cschcMap, viewChainSyncState)
import Ouroboros.Consensus.Util.Orphans ()
Expand Down Expand Up @@ -49,7 +52,8 @@ import Text.Printf (printf)
-- The thread is linked to the parent thread for proper error propagation
-- and labeled for easier debugging and identification.
startPeerTracer
:: Tracer IO [PeerT blk] -- ^ Tracer for the peer list
:: forall blk. Net.HasHeader (Header blk)
=> Tracer IO [PeerT blk] -- ^ Tracer for the peer list
-> NodeKernelData blk -- ^ Node kernel containing peer data
-> Int -- ^ Delay in milliseconds between traces
-> IO ()
Expand Down Expand Up @@ -104,18 +108,31 @@ ppStatus = \case
PeerFetchStatusReady {} -> "ready"

getCurrentPeers
:: NodeKernelData blk
:: forall blk. Net.HasHeader (Header blk)
=> NodeKernelData blk
-> IO [PeerT blk]
getCurrentPeers nkd = mapNodeKernelDataIO extractPeers nkd
<&> fromSMaybe mempty
where
tuple3pop :: (a, b, c) -> (a, b)
tuple3pop (a, b, _) = (a, b)

peerFetchStatusForgetTime :: PeerFetchStatus (HeaderWithTime blk) -> PeerFetchStatus (Header blk)
peerFetchStatusForgetTime = \case
PeerFetchStatusShutdown -> PeerFetchStatusShutdown
PeerFetchStatusStarting -> PeerFetchStatusStarting
PeerFetchStatusAberrant -> PeerFetchStatusAberrant
PeerFetchStatusBusy -> PeerFetchStatusBusy
PeerFetchStatusReady points idle -> PeerFetchStatusReady (Set.mapMonotonic Net.castPoint points) idle

peerFetchInFlightForgetTime :: PeerFetchInFlight (HeaderWithTime blk) -> PeerFetchInFlight (Header blk)
peerFetchInFlightForgetTime inflight =
inflight {peerFetchBlocksInFlight = Set.mapMonotonic Net.castPoint (peerFetchBlocksInFlight inflight)}

getCandidates
:: STM.STM IO (Map peer (ChainSyncClientHandle IO blk))
-> STM.STM IO (Map peer (Net.AnchoredFragment (Header blk)))
getCandidates handle = viewChainSyncState handle csCandidate
getCandidates handle = viewChainSyncState handle (Net.mapAnchoredFragment hwtHeader . csCandidate)

extractPeers :: NodeKernel IO RemoteAddress LocalConnectionId blk
-> IO [PeerT blk]
Expand All @@ -129,7 +146,7 @@ getCurrentPeers nkd = mapNodeKernelDataIO extractPeers nkd

let peers = flip Map.mapMaybeWithKey candidates $ \cid af ->
maybe Nothing
(\(status, inflight) -> Just $ PeerT cid af status inflight)
(\(status, inflight) -> Just $ PeerT cid af (peerFetchStatusForgetTime status) (peerFetchInFlightForgetTime inflight))
$ Map.lookup cid peerStates
pure . Map.elems $ peers

Expand Down
18 changes: 12 additions & 6 deletions cardano-node/src/Cardano/Tracing/OrphanInstances/Consensus.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Cardano.Tracing.OrphanInstances.Consensus () where

import Cardano.Node.Tracing.Tracers.ConsensusStartupException
(ConsensusStartupException (..))
import Cardano.Prelude (maximumDef)
import Cardano.Prelude (Typeable, maximumDef)
import Cardano.Slotting.Slot (fromWithOrigin)
import Cardano.Tracing.OrphanInstances.Common
import Cardano.Tracing.OrphanInstances.Network ()
Expand Down Expand Up @@ -74,8 +74,8 @@ import Ouroboros.Consensus.Util.Condense
import Ouroboros.Consensus.Util.Enclose
import Ouroboros.Consensus.Util.Orphans ()
import qualified Ouroboros.Network.AnchoredFragment as AF
import Ouroboros.Network.Block (BlockNo (..), ChainUpdate (..), MaxSlotNo(..), SlotNo (..), StandardHash,
Tip (..), blockHash, pointSlot, tipFromHeader)
import Ouroboros.Network.Block (BlockNo (..), ChainUpdate (..), MaxSlotNo (..),
SlotNo (..), StandardHash, Tip (..), blockHash, pointSlot, tipFromHeader)
import Ouroboros.Network.BlockFetch.ClientState (TraceLabelPeer (..))
import Ouroboros.Network.Point (withOrigin)
import Ouroboros.Network.SizeInBytes (SizeInBytes (..))
Expand Down Expand Up @@ -133,6 +133,12 @@ instance ConvertRawHash blk => ConvertRawHash (Header blk) where
hashSize :: proxy (Header blk) -> Word32
hashSize _ = hashSize (Proxy @blk)

instance ConvertRawHash blk => ConvertRawHash (HeaderWithTime blk) where
toShortRawHash _ = toShortRawHash (Proxy @blk)
fromShortRawHash _ = fromShortRawHash (Proxy @blk)
hashSize :: proxy (HeaderWithTime blk) -> Word32
hashSize _ = hashSize (Proxy @blk)

--
-- * instances of @HasPrivacyAnnotation@ and @HasSeverityAnnotation@
--
Expand Down Expand Up @@ -1779,10 +1785,10 @@ instance ToObject selection => ToObject (TraceGsmEvent selection) where
instance HasPrivacyAnnotation (TraceGDDEvent peer blk) where
instance HasSeverityAnnotation (TraceGDDEvent peer blk) where
getSeverityAnnotation _ = Debug
instance (ToObject peer, ConvertRawHash blk, GetHeader blk) => Transformable Text IO (TraceGDDEvent peer blk) where
instance (Typeable blk, ToObject peer, ConvertRawHash blk, GetHeader blk) => Transformable Text IO (TraceGDDEvent peer blk) where
trTransformer = trStructured

instance (ToObject peer, ConvertRawHash blk, GetHeader blk) => ToObject (TraceGDDEvent peer blk) where
instance (Typeable blk, ToObject peer, ConvertRawHash blk, GetHeader blk) => ToObject (TraceGDDEvent peer blk) where
toObject verb (TraceGDDDebug (GDDDebugInfo {..})) = mconcat $
[ "kind" .= String "TraceGDDEvent"
, "losingPeers".= toJSON (map (toObject verb) losingPeers)
Expand Down Expand Up @@ -1828,7 +1834,7 @@ instance (ToObject peer, ConvertRawHash blk, GetHeader blk) => ToObject (TraceGD
, "peer" .= toJSON (map (toObject verb) $ toList peer)
]

instance (ConvertRawHash blk, GetHeader blk) => ToObject (DensityBounds blk) where
instance (Typeable blk, ConvertRawHash blk, GetHeader blk) => ToObject (DensityBounds blk) where
toObject verb DensityBounds {..} = mconcat
[ "kind" .= String "DensityBounds"
, "clippedFragment" .= toObject verb clippedFragment
Expand Down
22 changes: 19 additions & 3 deletions cardano-node/src/Cardano/Tracing/Peer.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.Tracing.Peer
( Peer (..)
Expand All @@ -17,6 +19,7 @@ import Cardano.BM.Tracing
import Cardano.Node.Orphans ()
import Cardano.Node.Queries
import Ouroboros.Consensus.Block (Header)
import Ouroboros.Consensus.HeaderValidation (HeaderWithTime (..))
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client (ChainSyncClientHandle,
csCandidate, cschcMap, viewChainSyncState)
import Ouroboros.Consensus.Util.Orphans ()
Expand Down Expand Up @@ -87,18 +90,31 @@ ppStatus = \case
PeerFetchStatusStarting -> "starting"

getCurrentPeers
:: NodeKernelData blk
:: forall blk. Net.HasHeader (Header blk)
=> NodeKernelData blk
-> IO [Peer blk]
getCurrentPeers nkd = mapNodeKernelDataIO extractPeers nkd
<&> fromSMaybe mempty
where
tuple3pop :: (a, b, c) -> (a, b)
tuple3pop (a, b, _) = (a, b)

peerFetchStatusForgetTime :: PeerFetchStatus (HeaderWithTime blk) -> PeerFetchStatus (Header blk)
peerFetchStatusForgetTime = \case
PeerFetchStatusShutdown -> PeerFetchStatusShutdown
PeerFetchStatusStarting -> PeerFetchStatusStarting
PeerFetchStatusAberrant -> PeerFetchStatusAberrant
PeerFetchStatusBusy -> PeerFetchStatusBusy
PeerFetchStatusReady points idle -> PeerFetchStatusReady (Set.mapMonotonic Net.castPoint points) idle

peerFetchInFlightForgetTime :: PeerFetchInFlight (HeaderWithTime blk) -> PeerFetchInFlight (Header blk)
peerFetchInFlightForgetTime inflight =
inflight {peerFetchBlocksInFlight = Set.mapMonotonic Net.castPoint (peerFetchBlocksInFlight inflight)}

getCandidates
:: STM.STM IO (Map peer (ChainSyncClientHandle IO blk))
-> STM.STM IO (Map peer (Net.AnchoredFragment (Header blk)))
getCandidates handle = viewChainSyncState handle csCandidate
getCandidates handle = viewChainSyncState handle (Net.mapAnchoredFragment hwtHeader . csCandidate)

extractPeers :: NodeKernel IO RemoteAddress LocalConnectionId blk
-> IO [Peer blk]
Expand All @@ -112,7 +128,7 @@ getCurrentPeers nkd = mapNodeKernelDataIO extractPeers nkd

let peers = flip Map.mapMaybeWithKey candidates $ \cid af ->
maybe Nothing
(\(status, inflight) -> Just $ Peer cid af status inflight)
(\(status, inflight) -> Just $ Peer cid af (peerFetchStatusForgetTime status) (peerFetchInFlightForgetTime inflight))
$ Map.lookup cid peerStates
pure . Map.elems $ peers

Expand Down
Loading