Skip to content

Commit 70d61fc

Browse files
committed
Generalize chainSyncState to peerState in the GSM
1 parent 4890191 commit 70d61fc

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Node/GSM.hs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ import qualified Ouroboros.Consensus.HardFork.History.Qry as Qry
5656
import qualified Ouroboros.Consensus.Ledger.Basics as L
5757
import Ouroboros.Consensus.Node.GsmState
5858
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
59-
import Ouroboros.Consensus.Util.NormalForm.StrictTVar (StrictTVar)
60-
import qualified Ouroboros.Consensus.Util.NormalForm.StrictTVar as StrictSTM
6159
import System.FS.API
6260
( HasFS
6361
, createDirectoryIfMissing
@@ -97,7 +95,7 @@ data CandidateVersusSelection
9795
WhetherCandidateIsBetter !Bool
9896
deriving (Eq, Show)
9997

100-
data GsmView m upstreamPeer selection chainSyncState = GsmView
98+
data GsmView m upstreamPeer selection peerState = GsmView
10199
{ antiThunderingHerd :: Maybe StdGen
102100
-- ^ An initial seed used to randomly increase 'minCaughtUpDuration' by up
103101
-- to 15% every transition from Syncing to CaughtUp, in order to avoid a
@@ -108,13 +106,13 @@ data GsmView m upstreamPeer selection chainSyncState = GsmView
108106
STM
109107
m
110108
( selection ->
111-
chainSyncState ->
109+
peerState ->
112110
CandidateVersusSelection
113111
)
114112
-- ^ Whether the candidate from the @chainSyncState@ is preferable to the
115113
-- selection. This can depend on external state (Peras certificates boosting
116114
-- blocks).
117-
, peerIsIdle :: chainSyncState -> Bool
115+
, peerIsIdle :: peerState -> Bool
118116
, durationUntilTooOld :: Maybe (selection -> m DurationFromNow)
119117
-- ^ How long from now until the selection will be so old that the node
120118
-- should exit the @CaughtUp@ state
@@ -123,10 +121,8 @@ data GsmView m upstreamPeer selection chainSyncState = GsmView
123121
, equivalent :: selection -> selection -> Bool
124122
-- ^ Whether the two selections are equivalent for the purpose of the
125123
-- Genesis State Machine
126-
, getChainSyncStates ::
127-
STM m (Map.Map upstreamPeer (StrictTVar m chainSyncState))
128-
-- ^ The current ChainSync state with the latest candidates from the
129-
-- upstream peers
124+
, getPeerStates :: STM m (Map.Map upstreamPeer peerState)
125+
-- ^ The current peer state with the latest candidates from the upstream peers
130126
, getCurrentSelection :: STM m selection
131127
-- ^ The node's current selection
132128
, minCaughtUpDuration :: NominalDiffTime
@@ -244,7 +240,7 @@ realGsmEntryPoints tracerArgs gsmView =
244240
, peerIsIdle
245241
, durationUntilTooOld
246242
, equivalent
247-
, getChainSyncStates
243+
, getPeerStates
248244
, getCurrentSelection
249245
, minCaughtUpDuration
250246
, setCaughtUpPersistentMark
@@ -370,12 +366,13 @@ realGsmEntryPoints tracerArgs gsmView =
370366

371367
blockUntilCaughtUp :: STM m (TraceGsmEvent tracedSelection)
372368
blockUntilCaughtUp = do
373-
-- STAGE 1: all ChainSync clients report no subsequent headers
374-
varsState <- getChainSyncStates
375-
states <- traverse StrictSTM.readTVar varsState
369+
-- STAGE 1: all peers are idle, which means that
370+
-- * all ChainSync clients report no subsequent headers, and
371+
-- * all PerasCertDiffusion clients report no subsequent certificates
372+
peerStates <- getPeerStates
376373
check $
377-
not (Map.null states)
378-
&& all peerIsIdle states
374+
not (Map.null peerStates)
375+
&& all peerIsIdle peerStates
379376

380377
-- STAGE 2: no candidate is better than the node's current
381378
-- selection
@@ -388,16 +385,15 @@ realGsmEntryPoints tracerArgs gsmView =
388385
-- block; general Praos reasoning ensures that won't take particularly
389386
-- long.
390387
selection <- getCurrentSelection
391-
candidates <- traverse StrictSTM.readTVar varsState
392388
candidateOverSelection <- getCandidateOverSelection
393389
let ok candidate =
394390
WhetherCandidateIsBetter False
395391
== candidateOverSelection selection candidate
396-
check $ all ok candidates
392+
check $ all ok peerStates
397393

398394
pure $
399395
GsmEventEnterCaughtUp
400-
(Map.size states)
396+
(Map.size peerStates)
401397
(cnvSelection selection)
402398

403399
-- STAGE 3: the previous stages weren't so slow that the idler

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ initNodeKernel
292292
<&> \wd (_headers, lst) ->
293293
GSM.getDurationUntilTooOld wd (getTipSlot lst)
294294
, GSM.equivalent = (==) `on` (AF.headPoint . fst)
295-
, GSM.getChainSyncStates = fmap cschState <$> cschcMap varChainSyncHandles
295+
, GSM.getPeerStates = traverse (readTVar . cschState) =<< cschcMap varChainSyncHandles
296296
, GSM.getCurrentSelection = do
297297
headers <- ChainDB.getCurrentChainWithTime chainDB
298298
extLedgerState <- ChainDB.getCurrentLedger chainDB

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/GSM.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ setupGsm isHaaSatisfied vars = do
142142
, GSM.peerIsIdle = isIdling
143143
, GSM.durationUntilTooOld = Just durationUntilTooOld
144144
, GSM.equivalent = (==) -- unsound, but harmless in this test
145-
, GSM.getChainSyncStates = readTVar varStates
145+
, GSM.getPeerStates = traverse readTVar =<< readTVar varStates
146146
, GSM.getCurrentSelection = readTVar varSelection
147147
, GSM.minCaughtUpDuration = thrashLimit
148148
, GSM.setCaughtUpPersistentMark = \b ->

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/Genesis/Tests/LoE/CaughtUp.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ mkGsmEntryPoints varChainSyncHandles chainDB writeGsmState =
283283
{ GSM.getCandidateOverSelection = pure candidateOverSelection
284284
, GSM.peerIsIdle = csIdling
285285
, GSM.equivalent = (==) `on` AF.headPoint
286-
, GSM.getChainSyncStates = fmap cschState <$> cschcMap varChainSyncHandles
286+
, GSM.getPeerStates = traverse readTVar =<< fmap cschState <$> cschcMap varChainSyncHandles
287287
, GSM.getCurrentSelection = ChainDB.getCurrentChain chainDB
288288
, -- Make sure that we stay in CaughtUp for the duration of the test once we
289289
-- have entered it.

0 commit comments

Comments
 (0)