Skip to content

Commit dd352dc

Browse files
committed
TOSQUASH a much smaller change
1 parent a1e4821 commit dd352dc

File tree

2 files changed

+44
-50
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ChainSync

2 files changed

+44
-50
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ChainSync/Client.hs

+23-25
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ import Control.Monad.Class.MonadTimer (MonadTimer)
8080
import Control.Monad.Except (runExcept, throwError)
8181
import Control.Tracer
8282
import Data.Foldable (traverse_)
83-
import Data.Function (fix)
8483
import Data.Functor ((<&>))
8584
import Data.Kind (Type)
8685
import Data.Map.Strict (Map)
@@ -406,15 +405,15 @@ bracketChainSyncClient
406405
let handle = ChainSyncClientHandle {
407406
cschGDDKill = throwTo tid DensityTooLow
408407
, cschOnGsmStateChanged = \gsmTransition time -> do
409-
updateLopBucketConfig lopBucket gsmTransition time
410-
pure $ pure ()
408+
updateLopBucketConfig lopBucket gsmTransition time
409+
pure $ pure ()
411410
, cschState
412411
, cschJumping
413412
, cschJumpInfo
414413
}
415414
insertHandle = atomicallyWithMonotonicTime $ \time -> do
416-
initialGsmState <- getGsmState
417-
initializeLopBucketConfig lopBucket initialGsmState time
415+
gsmState <- getGsmState
416+
initializeLopBucketConfig lopBucket gsmState time
418417
cschcAddHandle varHandles peer handle
419418
deleteHandle = atomically $ cschcRemoveHandle varHandles peer
420419
bracket_ insertHandle deleteHandle $ f Jumping.noJumping
@@ -427,35 +426,34 @@ bracketChainSyncClient
427426
acquireContext lopBucket cschState (CSJEnabledConfig jumpSize) = do
428427
tid <- myThreadId
429428
atomicallyWithMonotonicTime $ \time -> do
430-
initialGsmState <- getGsmState
431-
initializeLopBucketConfig lopBucket initialGsmState time
429+
gsmState <- getGsmState
430+
initializeLopBucketConfig lopBucket gsmState time
432431
cschJumpInfo <- newTVar Nothing
433432
context <- Jumping.makeContext varHandles jumpSize tracerCsj
434-
Jumping.registerClient context peer cschState $ \cschJumping ->
435-
fix $ \handle -> ChainSyncClientHandle -- NB @handle@ only occurs under a lambda
433+
Jumping.registerClient gsmState context peer cschState $ \cschJumping ->
434+
ChainSyncClientHandle
436435
{ cschGDDKill = throwTo tid DensityTooLow
437436
, cschOnGsmStateChanged = \gsmTransition time' -> do
438437
updateLopBucketConfig lopBucket gsmTransition time'
439-
let peerContext =
440-
context {Jumping.peer = peer, Jumping.handle = handle}
441438
-- 'cschJumpInfo' does not need to be reset. The ChainSync
442-
-- client constantly updates it, even when its not
443-
-- registered with CSJ. That's necessary: when this peer
444-
-- /re-registers/, it might immediately be the Dynamo, and
445-
-- the Dynamo must not have 'Nothing' in its
446-
-- 'cschJumpInfo'.
439+
-- client constantly updates it, even when its not engaged
440+
-- with CSJ. That's necessary: when this peer /re-engages/,
441+
-- it might immediately be the Dynamo, and the Dynamo must
442+
-- not have 'Nothing' in its 'cschJumpInfo'.
447443
case gsmTransition of
448444
PreSyncingSyncing -> pure $ pure ()
449-
SyncingCaughtUp -> do
450-
writeTVar cschJumping $ Disengaged DisengagedDone
451-
-- The GSM only transitions to CaughtUp if all peers
452-
-- have send MsgAwaitReply, so DisengagedDone is
453-
-- correct here.
454-
mbEv <- Jumping.unregisterClient peerContext
455-
pure $ traverse_ (traceWith (Jumping.tracer peerContext)) mbEv
445+
SyncingCaughtUp ->
446+
-- The GSM only transitions to CaughtUp if all peers have
447+
-- sent MsgAwaitReply, so all peers are already
448+
-- disengaged.
449+
pure $ pure ()
456450
CaughtUpPreSyncing -> do
457-
mbEv <- Jumping.reregisterClient peerContext
458-
pure $ traverse_ (traceWith (Jumping.tracer peerContext)) mbEv
451+
-- TODO do we want to do something more sophisticated
452+
-- than just letting a race condition determine the
453+
-- Dynamo?
454+
(csjState, mbEv) <- Jumping.engageClient context cschState
455+
writeTVar cschJumping csjState
456+
pure $ traverse_ (traceWith (Jumping.tracer context)) mbEv
459457
SyncingPreSyncing -> pure $ pure ()
460458
, cschState
461459
, cschJumping

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ChainSync/Client/Jumping.hs

+21-25
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ module Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping (
186186
, TraceCsjReason (..)
187187
, TraceEventCsj (..)
188188
, TraceEventDbf (..)
189+
, engageClient
189190
, getDynamo
190191
, makeContext
191192
, mkJumping
192193
, noJumping
193194
, registerClient
194-
, reregisterClient
195195
, rotateDynamo
196196
, unregisterClient
197197
) where
@@ -212,6 +212,8 @@ import Data.Void (absurd)
212212
import GHC.Generics (Generic)
213213
import Ouroboros.Consensus.Block (HasHeader (getHeaderFields), Header,
214214
Point (..), castPoint, pointSlot, succWithOrigin)
215+
import Ouroboros.Consensus.Node.GsmState (GsmState)
216+
import qualified Ouroboros.Consensus.Node.GsmState as GSM
215217
import Ouroboros.Consensus.Ledger.SupportsProtocol
216218
(LedgerSupportsProtocol)
217219
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.State
@@ -781,48 +783,42 @@ registerClient ::
781783
( LedgerSupportsProtocol blk,
782784
IOLike m
783785
) =>
786+
GsmState ->
784787
Context m peer blk ->
785788
peer ->
786789
StrictTVar m (ChainSyncState blk) ->
787790
-- | A function to make a client handle from a jumping state.
788791
(StrictTVar m (ChainSyncJumpingState m blk) -> ChainSyncClientHandle m blk) ->
789792
STM m (PeerContext m peer blk, Maybe (TraceEventCsj peer blk))
790-
registerClient context peer csState mkHandle = do
791-
(csjState, mbEv) <- getDynamo (handlesCol context) >>= \case
792-
Nothing -> do
793-
fragment <- csCandidate <$> readTVar csState
794-
pure (Dynamo DynamoStarted $ pointSlot $ AF.anchorPoint fragment, Just InitializedAsDynamo)
795-
Just (_, handle) -> do
796-
mJustInfo <- readTVar (cschJumpInfo handle)
797-
(\x -> (x, Nothing)) <$> newJumper mJustInfo (Happy FreshJumper Nothing)
793+
registerClient gsmState context peer csState mkHandle = do
794+
(csjState, mbEv) <- case gsmState of
795+
GSM.CaughtUp -> pure (Disengaged DisengagedDone, Nothing)
796+
GSM.PreSyncing -> engageClient context csState
797+
GSM.Syncing -> engageClient context csState
798798
cschJumping <- newTVar csjState
799799
let handle = mkHandle cschJumping
800800
cschcAddHandle (handlesCol context) peer handle
801801
pure (context {peer, handle}, mbEv)
802802

803-
-- | Same as 'registerClient', but for a peer that had been previously disengaged.
803+
-- | Adds a client to the overall CSJ state
804804
--
805-
-- This is specifically used for when the GSM transitions from
806-
-- 'Ouroboros.Consensus.Node.GsmState.CaughtUp' back to
807-
-- 'Ouroboros.Consensus.Node.GsmState.PreSyncing'.
808-
reregisterClient ::
805+
-- This client can be either a fresh peer or a peer that had previously
806+
-- disengaged.
807+
engageClient ::
809808
( LedgerSupportsProtocol blk,
810809
IOLike m
811810
) =>
812-
PeerContext m peer blk ->
813-
STM m (Maybe (TraceEventCsj peer blk))
814-
reregisterClient peerContext = do
815-
let h = handle peerContext
816-
(csjState, mbEv) <- getDynamo (handlesCol peerContext) >>= \case
811+
Context m peer blk ->
812+
StrictTVar m (ChainSyncState blk) ->
813+
STM m (ChainSyncJumpingState m blk, Maybe (TraceEventCsj peer blk))
814+
engageClient context csState = do
815+
getDynamo (handlesCol context) >>= \case
817816
Nothing -> do
818-
fragment <- csCandidate <$> readTVar (cschState h)
817+
fragment <- csCandidate <$> readTVar csState
819818
pure (Dynamo DynamoStarted $ pointSlot $ AF.anchorPoint fragment, Just InitializedAsDynamo)
820-
Just (_, dynHandle) -> do
821-
mJustInfo <- readTVar (cschJumpInfo dynHandle)
819+
Just (_, handle) -> do
820+
mJustInfo <- readTVar (cschJumpInfo handle)
822821
(\x -> (x, Nothing)) <$> newJumper mJustInfo (Happy FreshJumper Nothing)
823-
writeTVar (cschJumping h) csjState
824-
cschcAddHandle (handlesCol peerContext) (peer peerContext) h
825-
pure mbEv
826822

827823
-- | Unregister a client from a 'PeerContext'; this might trigger the election
828824
-- of a new dynamo or objector if the peer was one of these two.

0 commit comments

Comments
 (0)