@@ -80,7 +80,6 @@ import Control.Monad.Class.MonadTimer (MonadTimer)
80
80
import Control.Monad.Except (runExcept , throwError )
81
81
import Control.Tracer
82
82
import Data.Foldable (traverse_ )
83
- import Data.Function (fix )
84
83
import Data.Functor ((<&>) )
85
84
import Data.Kind (Type )
86
85
import Data.Map.Strict (Map )
@@ -406,15 +405,15 @@ bracketChainSyncClient
406
405
let handle = ChainSyncClientHandle {
407
406
cschGDDKill = throwTo tid DensityTooLow
408
407
, cschOnGsmStateChanged = \ gsmTransition time -> do
409
- updateLopBucketConfig lopBucket gsmTransition time
410
- pure $ pure ()
408
+ updateLopBucketConfig lopBucket gsmTransition time
409
+ pure $ pure ()
411
410
, cschState
412
411
, cschJumping
413
412
, cschJumpInfo
414
413
}
415
414
insertHandle = atomicallyWithMonotonicTime $ \ time -> do
416
- initialGsmState <- getGsmState
417
- initializeLopBucketConfig lopBucket initialGsmState time
415
+ gsmState <- getGsmState
416
+ initializeLopBucketConfig lopBucket gsmState time
418
417
cschcAddHandle varHandles peer handle
419
418
deleteHandle = atomically $ cschcRemoveHandle varHandles peer
420
419
bracket_ insertHandle deleteHandle $ f Jumping. noJumping
@@ -427,35 +426,34 @@ bracketChainSyncClient
427
426
acquireContext lopBucket cschState (CSJEnabledConfig jumpSize) = do
428
427
tid <- myThreadId
429
428
atomicallyWithMonotonicTime $ \ time -> do
430
- initialGsmState <- getGsmState
431
- initializeLopBucketConfig lopBucket initialGsmState time
429
+ gsmState <- getGsmState
430
+ initializeLopBucketConfig lopBucket gsmState time
432
431
cschJumpInfo <- newTVar Nothing
433
432
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
436
435
{ cschGDDKill = throwTo tid DensityTooLow
437
436
, cschOnGsmStateChanged = \ gsmTransition time' -> do
438
437
updateLopBucketConfig lopBucket gsmTransition time'
439
- let peerContext =
440
- context {Jumping. peer = peer, Jumping. handle = handle}
441
438
-- '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'.
447
443
case gsmTransition of
448
444
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 ()
456
450
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
459
457
SyncingPreSyncing -> pure $ pure ()
460
458
, cschState
461
459
, cschJumping
0 commit comments