Skip to content
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

cardano-node: sync percent fixed. #4350

Merged
merged 1 commit into from
Aug 17, 2022
Merged
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
59 changes: 18 additions & 41 deletions cardano-node/src/Cardano/Node/Tracing/StateRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,11 @@ import Cardano.Logging
import Cardano.Prelude
import Data.Aeson
import Data.Time.Clock
import Data.Time.Clock.POSIX

import Cardano.Api.Protocol.Types (BlockType (..), protocolInfo)
import qualified Cardano.Ledger.Shelley.API as SL
import Cardano.Node.Protocol.Types (SomeConsensusProtocol (..))
import qualified Ouroboros.Consensus.Block.RealPoint as RP
import Ouroboros.Consensus.Cardano.Block
import Ouroboros.Consensus.Cardano.CanHardFork (shelleyLedgerConfig)
import qualified Ouroboros.Consensus.Config as Consensus
import Ouroboros.Consensus.HardFork.Combinator.Degenerate
import qualified Ouroboros.Consensus.Node.NetworkProtocolVersion as NPV
import Ouroboros.Consensus.Node.ProtocolInfo (ProtocolInfo (..))
import Ouroboros.Consensus.Shelley.Ledger.Ledger
import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
import Ouroboros.Consensus.Storage.ImmutableDB.Chunks.Internal
import qualified Ouroboros.Consensus.Storage.LedgerDB.OnDisk as LgrDb
Expand Down Expand Up @@ -165,7 +158,7 @@ traceNodeStateChainDB
-> Trace IO NodeState
-> ChainDB.TraceEvent blk
-> IO ()
traceNodeStateChainDB scp tr ev =
traceNodeStateChainDB _scp tr ev =
case ev of
ChainDB.TraceOpenEvent ev' ->
case ev' of
Expand Down Expand Up @@ -201,12 +194,13 @@ traceNodeStateChainDB scp tr ev =
case ev' of
ChainDB.AddedToCurrentChain _ (ChainDB.NewTipInfo currentTip ntEpoch sInEpoch _) _ _ -> do
-- The slot of the latest block consumed (our progress).
let RP.RealPoint slotSinceSystemStart _ = currentTip
let RP.RealPoint ourSlotSinceSystemStart _ = currentTip
-- The slot corresponding to the latest wall-clock time (our target).
slotNow <- getSlotForNow scp slotSinceSystemStart
slotSinceSystemStart <- getSlotForNow
let syncProgressPct :: SyncPercentage
syncProgressPct =
(fromIntegral (unSlotNo slotSinceSystemStart) / fromIntegral (unSlotNo slotNow)) * 100.0
syncProgressPct = ( fromIntegral (unSlotNo ourSlotSinceSystemStart)
/ fromIntegral (unSlotNo slotSinceSystemStart)
) * 100.0
traceWith tr $ NodeAddBlock $
AddedToCurrentChain ntEpoch (SlotNo sInEpoch) syncProgressPct
_ -> return ()
Expand Down Expand Up @@ -242,32 +236,15 @@ traceNodeStateShutdown tr = traceWith tr . NodeShutdown

-- Misc.

getSlotForNow
:: SomeConsensusProtocol
-> SlotNo
-> IO SlotNo
getSlotForNow (SomeConsensusProtocol whichP pInfo) s = do
now <- getCurrentTime
let cfg = pInfoConfig $ protocolInfo pInfo
case whichP of
ShelleyBlockType -> do
let DegenLedgerConfig cfgShelley = Consensus.configLedger cfg
nowSinceSystemStart = now `diffUTCTime` getSystemStartTime cfgShelley
return . SlotNo $ floor nowSinceSystemStart
CardanoBlockType -> do
let CardanoLedgerConfig _ cfgShelley cfgAllegra cfgMary
cfgAlonzo cfgBabbage =
Consensus.configLedger cfg
latestNowSinceSystemStart = minimum
[ now `diffUTCTime` getSystemStartTime cfgShelley
, now `diffUTCTime` getSystemStartTime cfgAllegra
, now `diffUTCTime` getSystemStartTime cfgMary
, now `diffUTCTime` getSystemStartTime cfgAlonzo
, now `diffUTCTime` getSystemStartTime cfgBabbage
]
return . SlotNo $ floor latestNowSinceSystemStart
_ ->
-- It is assumed that Byron isn't used already.
return s
getSlotForNow :: IO SlotNo
getSlotForNow = do
posixNow <- utc2s <$> getCurrentTime
-- Since Shelley era the slot length is 1 second, so the number of seconds is the number of slots.
let numberOfSlotsFromShelleyTillNow = posixNow - posixStartOfShelleyEra
totalNumberOfSlotsTillNow = numberOfSlotsInByronEra + numberOfSlotsFromShelleyTillNow
return $ SlotNo totalNumberOfSlotsTillNow
where
getSystemStartTime = SL.sgSystemStart . shelleyLedgerGenesis . shelleyLedgerConfig
-- These numbers are taken from 'First-Block-of-Each-Era' wiki page.
posixStartOfShelleyEra = 1596073491
numberOfSlotsInByronEra = 4492799
utc2s = fromInteger . round . utcTimeToPOSIXSeconds