Skip to content

Commit a47a954

Browse files
iohk-bors[bot]Denis Shevchenko
and
Denis Shevchenko
authored
Merge #4350
4350: cardano-node: sync percent fixed. r=denisshevchenko a=denisshevchenko Closes #4345 Co-authored-by: Denis Shevchenko <[email protected]>
2 parents 2812a57 + 3a2612f commit a47a954

File tree

1 file changed

+18
-41
lines changed

1 file changed

+18
-41
lines changed

Diff for: cardano-node/src/Cardano/Node/Tracing/StateRep.hs

+18-41
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,11 @@ import Cardano.Logging
2626
import Cardano.Prelude
2727
import Data.Aeson
2828
import Data.Time.Clock
29+
import Data.Time.Clock.POSIX
2930

30-
import Cardano.Api.Protocol.Types (BlockType (..), protocolInfo)
31-
import qualified Cardano.Ledger.Shelley.API as SL
3231
import Cardano.Node.Protocol.Types (SomeConsensusProtocol (..))
3332
import qualified Ouroboros.Consensus.Block.RealPoint as RP
34-
import Ouroboros.Consensus.Cardano.Block
35-
import Ouroboros.Consensus.Cardano.CanHardFork (shelleyLedgerConfig)
36-
import qualified Ouroboros.Consensus.Config as Consensus
37-
import Ouroboros.Consensus.HardFork.Combinator.Degenerate
3833
import qualified Ouroboros.Consensus.Node.NetworkProtocolVersion as NPV
39-
import Ouroboros.Consensus.Node.ProtocolInfo (ProtocolInfo (..))
40-
import Ouroboros.Consensus.Shelley.Ledger.Ledger
4134
import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
4235
import Ouroboros.Consensus.Storage.ImmutableDB.Chunks.Internal
4336
import qualified Ouroboros.Consensus.Storage.LedgerDB.OnDisk as LgrDb
@@ -165,7 +158,7 @@ traceNodeStateChainDB
165158
-> Trace IO NodeState
166159
-> ChainDB.TraceEvent blk
167160
-> IO ()
168-
traceNodeStateChainDB scp tr ev =
161+
traceNodeStateChainDB _scp tr ev =
169162
case ev of
170163
ChainDB.TraceOpenEvent ev' ->
171164
case ev' of
@@ -201,12 +194,13 @@ traceNodeStateChainDB scp tr ev =
201194
case ev' of
202195
ChainDB.AddedToCurrentChain _ (ChainDB.NewTipInfo currentTip ntEpoch sInEpoch _) _ _ -> do
203196
-- The slot of the latest block consumed (our progress).
204-
let RP.RealPoint slotSinceSystemStart _ = currentTip
197+
let RP.RealPoint ourSlotSinceSystemStart _ = currentTip
205198
-- The slot corresponding to the latest wall-clock time (our target).
206-
slotNow <- getSlotForNow scp slotSinceSystemStart
199+
slotSinceSystemStart <- getSlotForNow
207200
let syncProgressPct :: SyncPercentage
208-
syncProgressPct =
209-
(fromIntegral (unSlotNo slotSinceSystemStart) / fromIntegral (unSlotNo slotNow)) * 100.0
201+
syncProgressPct = ( fromIntegral (unSlotNo ourSlotSinceSystemStart)
202+
/ fromIntegral (unSlotNo slotSinceSystemStart)
203+
) * 100.0
210204
traceWith tr $ NodeAddBlock $
211205
AddedToCurrentChain ntEpoch (SlotNo sInEpoch) syncProgressPct
212206
_ -> return ()
@@ -242,32 +236,15 @@ traceNodeStateShutdown tr = traceWith tr . NodeShutdown
242236

243237
-- Misc.
244238

245-
getSlotForNow
246-
:: SomeConsensusProtocol
247-
-> SlotNo
248-
-> IO SlotNo
249-
getSlotForNow (SomeConsensusProtocol whichP pInfo) s = do
250-
now <- getCurrentTime
251-
let cfg = pInfoConfig $ protocolInfo pInfo
252-
case whichP of
253-
ShelleyBlockType -> do
254-
let DegenLedgerConfig cfgShelley = Consensus.configLedger cfg
255-
nowSinceSystemStart = now `diffUTCTime` getSystemStartTime cfgShelley
256-
return . SlotNo $ floor nowSinceSystemStart
257-
CardanoBlockType -> do
258-
let CardanoLedgerConfig _ cfgShelley cfgAllegra cfgMary
259-
cfgAlonzo cfgBabbage =
260-
Consensus.configLedger cfg
261-
latestNowSinceSystemStart = minimum
262-
[ now `diffUTCTime` getSystemStartTime cfgShelley
263-
, now `diffUTCTime` getSystemStartTime cfgAllegra
264-
, now `diffUTCTime` getSystemStartTime cfgMary
265-
, now `diffUTCTime` getSystemStartTime cfgAlonzo
266-
, now `diffUTCTime` getSystemStartTime cfgBabbage
267-
]
268-
return . SlotNo $ floor latestNowSinceSystemStart
269-
_ ->
270-
-- It is assumed that Byron isn't used already.
271-
return s
239+
getSlotForNow :: IO SlotNo
240+
getSlotForNow = do
241+
posixNow <- utc2s <$> getCurrentTime
242+
-- Since Shelley era the slot length is 1 second, so the number of seconds is the number of slots.
243+
let numberOfSlotsFromShelleyTillNow = posixNow - posixStartOfShelleyEra
244+
totalNumberOfSlotsTillNow = numberOfSlotsInByronEra + numberOfSlotsFromShelleyTillNow
245+
return $ SlotNo totalNumberOfSlotsTillNow
272246
where
273-
getSystemStartTime = SL.sgSystemStart . shelleyLedgerGenesis . shelleyLedgerConfig
247+
-- These numbers are taken from 'First-Block-of-Each-Era' wiki page.
248+
posixStartOfShelleyEra = 1596073491
249+
numberOfSlotsInByronEra = 4492799
250+
utc2s = fromInteger . round . utcTimeToPOSIXSeconds

0 commit comments

Comments
 (0)