Skip to content

Commit

Permalink
Rename History to Rearview
Browse files Browse the repository at this point in the history
I like colorful names
  • Loading branch information
HeinrichApfelmus committed Oct 29, 2021
1 parent ba9233f commit 284c66d
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions lib/core/src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Cardano.Wallet.Network

-- * Logging (for testing)
, FollowStats (..)
, History (..)
, Rearview (..)
, emptyStats
, updateStats
) where
Expand Down Expand Up @@ -335,7 +335,7 @@ instance HasSeverityAnnotation (ChainSyncLog block point) where
-- Includes computed statistics about synchronization progress.
data ChainFollowLog
= MsgChainSync (ChainSyncLog BlockHeader ChainPoint)
| MsgFollowStats (FollowStats History)
| MsgFollowStats (FollowStats Rearview)
| MsgStartFollowing
deriving (Show, Eq, Generic)

Expand All @@ -357,7 +357,7 @@ instance HasSeverityAnnotation ChainFollowLog where
-------------------------------------------------------------------------------}
-- | Statistics of interest from the follow-function.
--
-- The @f@ allows us to use 'History' to keep track of both current and
-- The @f@ allows us to use 'Rearview' to keep track of both current and
-- previously logged stats, and perform operations over it in a nice way.
data FollowStats f = FollowStats
{ blocksApplied :: !(f Int)
Expand All @@ -372,11 +372,11 @@ data FollowStats f = FollowStats
-- It seems UTCTime contains thunks internally. This shouldn't matter as we
-- 1. Change it seldom - from @flush@, not from @updateStats@
-- 2. Set to a completely new value when we do change it.
deriving via (AllowThunksIn '["time"] (FollowStats History))
instance (NoThunks (FollowStats History))
deriving via (AllowThunksIn '["time"] (FollowStats Rearview))
instance (NoThunks (FollowStats Rearview))

deriving instance Show (FollowStats History)
deriving instance Eq (FollowStats History)
deriving instance Show (FollowStats Rearview)
deriving instance Eq (FollowStats Rearview)

-- | Change the @f@ wrapping each record field.
hoistStats
Expand All @@ -386,37 +386,37 @@ hoistStats
hoistStats f (FollowStats a b c d e) =
FollowStats (f a) (f b) (f c) (f d) (f e)

-- | A 'History' consists of a past value and a present value.
-- | A 'Rearview' consists of a past value and a present value.
-- Useful for keeping track of past logs.
--
-- The idea is to
-- 1. Reconstruct a model of the @current@ @state@ using a @Trace@
-- 2. Sometimes log the difference between the @current@ state and the most
-- recently logged one.
data History a = History
data Rearview a = Rearview
{ past :: !a -- ^ Most previously logged state
, current :: !a -- ^ Not-yet logged state
} deriving (Eq, Show, Functor, Generic)

instance NoThunks a => NoThunks (History a)
instance NoThunks a => NoThunks (Rearview a)

initHistory :: a -> History a
initHistory a = History a a
initRearview :: a -> Rearview a
initRearview a = Rearview a a

-- | Modify the present state of a @History state@
overCurrent :: (a -> a) -> History a -> History a
overCurrent f (History pas cur) = History pas (f cur)
-- | Modify the present state of a @Rearview state@
overCurrent :: (a -> a) -> Rearview a -> Rearview a
overCurrent f (Rearview pas cur) = Rearview pas (f cur)

emptyStats :: UTCTime -> FollowStats History
emptyStats :: UTCTime -> FollowStats Rearview
emptyStats t = FollowStats (f 0) (f 0) (f ChainPointAtGenesis) (f t) (f p)
where
f = initHistory
f = initRearview
p = NotResponding -- Hijacked as an initial value for simplicity.

-- | Update the current statistics based on a new log message.
updateStats
:: ChainSyncLog block ChainPoint
-> FollowStats History -> FollowStats History
-> FollowStats Rearview -> FollowStats Rearview
updateStats msg s = case msg of
MsgChainRollForward blocks _tip ->
s { blocksApplied = overCurrent (+ NE.length blocks) (blocksApplied s) }
Expand All @@ -427,24 +427,24 @@ updateStats msg s = case msg of
s { localTip = overCurrent (const point) (localTip s) }
_ -> s

instance ToText (FollowStats History) where
instance ToText (FollowStats Rearview) where
toText st@(FollowStats b r tip t progress) =
syncStatus <> " " <> stats <> sevExpl
where
syncStatus = case progress of
History NotResponding Ready ->
Rearview NotResponding Ready ->
"In sync."
History Ready Ready ->
Rearview Ready Ready ->
"Still in sync."
History NotResponding NotResponding ->
Rearview NotResponding NotResponding ->
"Still not syncing."
History (Syncing _p) Ready ->
Rearview (Syncing _p) Ready ->
"In sync!"
History Ready (Syncing p) ->
Rearview Ready (Syncing p) ->
"Fell out of sync (" <> (pretty p) <> ")"
History _ (Syncing p) ->
Rearview _ (Syncing p) ->
"Syncing (" <> (pretty p) <> ")"
History past_ NotResponding ->
Rearview past_ NotResponding ->
"Not responding. Previously " <> (pretty past_) <> "."
stats = mconcat
[ "Applied " <> pretty (using (-) b) <> " blocks, "
Expand All @@ -466,7 +466,7 @@ instance ToText (FollowStats History) where
-- But this check might be in the wrong place. Might be better to
-- produce new logs from inside the updateStats function and immeditely
-- warn there.
explainedSeverityAnnotation :: FollowStats History -> (Severity, Maybe Text)
explainedSeverityAnnotation :: FollowStats Rearview -> (Severity, Maybe Text)
explainedSeverityAnnotation s
| progressMovedBackwards = (Warning, Just "progress decreased")
| noBlocks && notRestored = (Warning, Just "not applying blocks")
Expand All @@ -478,17 +478,17 @@ explainedSeverityAnnotation s
notRestored = current (prog s) /= Ready
noBlocks = (current (blocksApplied s) - past (blocksApplied s)) <= 0

instance HasSeverityAnnotation (FollowStats History) where
instance HasSeverityAnnotation (FollowStats Rearview) where
getSeverityAnnotation = fst . explainedSeverityAnnotation

-- | Update the 'TMVar' holding the 'FollowStats'@ @'History'
-- | Update the 'TMVar' holding the 'FollowStats'@ @'Rearview'
-- to forget the 'past' values and replace them with the 'current' ones.
-- Also update the time and sync process.
flushStats
:: UTCTime
-> (SlotNo -> IO SyncProgress)
-> StrictTMVar IO (FollowStats History)
-> IO (FollowStats History)
-> StrictTMVar IO (FollowStats Rearview)
-> IO (FollowStats Rearview)
flushStats t calcSyncProgress var = do
s <- atomically $ takeTMVar var
p <- calcSyncProgress $ pseudoPointSlot $ current $ localTip s
Expand All @@ -497,7 +497,7 @@ flushStats t calcSyncProgress var = do
atomically $ putTMVar var $ hoistStats forgetPast s'
return s'
where
forgetPast (History _past curr) = initHistory curr
forgetPast (Rearview _past curr) = initRearview curr

-- See NOTE [PointSlotNo].
-- Fortunately, this is not important for the use here.
Expand Down

0 comments on commit 284c66d

Please sign in to comment.