Skip to content

Commit b078458

Browse files
authored
CSJ: prefer the Objector when replacing a Dynamo (#1417)
See the various commits. The `CSJ: prefer the Objector when replacing a Dynamo` commit in particular explains the key change within this PR.
2 parents 20fd93b + c914969 commit b078458

File tree

13 files changed

+300
-116
lines changed

13 files changed

+300
-116
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Patch
9+
10+
- A bullet item for the Patch category.
11+
12+
-->
13+
<!--
14+
### Non-Breaking
15+
16+
- A bullet item for the Non-Breaking category.
17+
18+
-->
19+
20+
### Breaking
21+
22+
- Added a new CSJ tracer to ChainSync client interface.
23+
- Renamed the existing tracer in `Ouroboros.Consensus.Node.Tracers.Tracers`.

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ mkApps kernel Tracers {..} mkCodecs ByteLimits {..} genChainSyncTimeout lopBucke
581581
(getFetchClientRegistry kernel) them $
582582
CsClient.bracketChainSyncClient
583583
(contramap (TraceLabelPeer them) (Node.chainSyncClientTracer (getTracers kernel)))
584+
(contramap (TraceLabelPeer them) (Node.csjTracer (getTracers kernel)))
584585
(CsClient.defaultChainDbView (getChainDB kernel))
585586
(getChainSyncHandles kernel)
586587
(getGsmState kernel)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ data Tracers' remotePeer localPeer blk f = Tracers
7272
, consensusErrorTracer :: f SomeException
7373
, gsmTracer :: f (TraceGsmEvent (Tip blk))
7474
, gddTracer :: f (TraceGDDEvent remotePeer blk)
75-
, csjTracer :: f (CSJumping.TraceEvent remotePeer)
75+
, csjTracer :: f (TraceLabelPeer remotePeer (CSJumping.TraceEventCsj remotePeer blk))
76+
, dbfTracer :: f (CSJumping.TraceEventDbf remotePeer)
7677
}
7778

7879
instance (forall a. Semigroup (f a))
@@ -97,6 +98,7 @@ instance (forall a. Semigroup (f a))
9798
, gsmTracer = f gsmTracer
9899
, gddTracer = f gddTracer
99100
, csjTracer = f csjTracer
101+
, dbfTracer = f dbfTracer
100102
}
101103
where
102104
f :: forall a. Semigroup a
@@ -129,6 +131,7 @@ nullTracers = Tracers
129131
, gsmTracer = nullTracer
130132
, gddTracer = nullTracer
131133
, csjTracer = nullTracer
134+
, dbfTracer = nullTracer
132135
}
133136

134137
showTracers :: ( Show blk
@@ -164,6 +167,7 @@ showTracers tr = Tracers
164167
, gsmTracer = showTracing tr
165168
, gddTracer = showTracing tr
166169
, csjTracer = showTracing tr
170+
, dbfTracer = showTracing tr
167171
}
168172

169173
{-------------------------------------------------------------------------------

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
@@ -410,7 +410,7 @@ initInternalState NodeKernelArgs { tracers, chainDB, registry, cfg
410410
(GSM.gsmStateToLedgerJudgement <$> readTVar varGsmState)
411411
blockFetchInterface :: BlockFetchConsensusInterface (ConnectionId addrNTN) (Header blk) blk m
412412
blockFetchInterface = BlockFetchClientInterface.mkBlockFetchConsensusInterface
413-
(csjTracer tracers)
413+
(dbfTracer tracers)
414414
(configBlock cfg)
415415
(BlockFetchClientInterface.defaultChainDbView chainDB)
416416
varChainSyncHandles

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import Cardano.Slotting.Slot (SlotNo (unSlotNo))
2727
import Data.Foldable (asum)
2828
import Data.Function ((&))
2929
import Data.Functor ((<&>))
30+
import Data.List (sortOn)
3031
import Data.Maybe (fromJust, fromMaybe)
32+
import Data.Ord (Down (Down))
3133
import qualified Data.Vector as Vector
3234
import Ouroboros.Consensus.Block.Abstract (blockNo, blockSlot,
33-
fromWithOrigin, unBlockNo)
35+
fromWithOrigin, pointSlot, unBlockNo)
3436
import qualified Ouroboros.Network.AnchoredFragment as AF
3537
import Text.Printf (printf)
3638

@@ -164,26 +166,33 @@ prettyBlockTree :: AF.HasHeader blk => BlockTree blk -> [String]
164166
prettyBlockTree blockTree =
165167
["slots: " ++ unwords (map (printf "%2d" . unSlotNo) [veryFirstSlot .. veryLastSlot])]
166168
++ [printTrunk honestFragment]
167-
++ (map printBranch adversarialFragments)
169+
++ (map (uncurry printBranch) adversarialFragments)
168170

169171
where
170172
honestFragment = btTrunk blockTree
171-
adversarialFragments = map btbSuffix (btBranches blockTree)
173+
adversarialFragments =
174+
sortOn (Down . pointSlot . AF.anchorPoint . snd)
175+
$ zip [1..]
176+
$ map btbSuffix (btBranches blockTree)
172177

173178
veryFirstSlot = firstNo $ honestFragment
174179

175180
veryLastSlot =
176181
foldl max 0 $
177182
map
178183
lastNo
179-
(honestFragment : adversarialFragments)
184+
(honestFragment : map snd adversarialFragments)
180185

181186
printTrunk :: AF.HasHeader blk => AF.AnchoredFragment blk -> String
182187
printTrunk = printLine (\_ -> "trunk: ─")
183188

184-
printBranch :: AF.HasHeader blk => AF.AnchoredFragment blk -> String
185-
printBranch = printLine $ \firstSlot ->
186-
" " ++ replicate (3 * fromIntegral (unSlotNo (firstSlot - veryFirstSlot))) ' ' ++ " ╰─"
189+
printBranch :: AF.HasHeader blk => Int -> AF.AnchoredFragment blk -> String
190+
printBranch idx = printLine $ \firstSlot ->
191+
let sidx = " (" ++ show idx ++ ")"
192+
pad = replicate 6 ' '
193+
prefix = sidx ++ drop (length sidx) pad
194+
in
195+
prefix ++ replicate (3 * fromIntegral (unSlotNo (firstSlot - veryFirstSlot))) ' ' ++ " ╰─"
187196

188197
printLine :: AF.HasHeader blk => (SlotNo -> String) -> AF.AnchoredFragment blk -> String
189198
printLine printHeader fragment =

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/ChainSync.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module Test.Consensus.PeerSimulator.ChainSync (
1212

1313
import Control.Exception (SomeException)
1414
import Control.Monad.Class.MonadTimer.SI (MonadTimer)
15-
import Control.Tracer (Tracer (Tracer), nullTracer, traceWith)
15+
import Control.Tracer (Tracer (Tracer), contramap, nullTracer,
16+
traceWith)
1617
import Data.Proxy (Proxy (..))
1718
import Network.TypedProtocol.Codec (AnyMessage)
1819
import Ouroboros.Consensus.Block (Header, Point)
@@ -153,6 +154,7 @@ runChainSyncClient
153154
channel =
154155
bracketChainSyncClient
155156
nullTracer
157+
(contramap (TraceCsjEvent peerId) tracer)
156158
chainDbView
157159
varHandles
158160
(pure Syncing)

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/Trace.hs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import Ouroboros.Consensus.Genesis.Governor (DensityBounds (..),
3333
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
3434
(TraceChainSyncClientEvent (..))
3535
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping
36-
(Instruction (..), JumpInstruction (..), JumpResult (..))
36+
(Instruction (..), JumpInstruction (..), JumpResult (..),
37+
TraceCsjReason (..), TraceEventCsj (..),
38+
TraceEventDbf (..))
3739
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.State
3840
(ChainSyncJumpingJumperState (..),
3941
ChainSyncJumpingState (..), DynamoInitState (..),
@@ -136,6 +138,8 @@ data TraceEvent blk
136138
| TraceBlockFetchClientTerminationEvent PeerId TraceBlockFetchClientTerminationEvent
137139
| TraceGenesisDDEvent (TraceGDDEvent PeerId blk)
138140
| TraceChainSyncSendRecvEvent PeerId String (TraceSendRecv (ChainSync (Header blk) (Point blk) (Tip blk)))
141+
| TraceDbfEvent (TraceEventDbf PeerId)
142+
| TraceCsjEvent PeerId (TraceEventCsj PeerId blk)
139143
| TraceOther String
140144

141145
-- * 'TestBlock'-specific tracers for the peer simulator
@@ -189,6 +193,8 @@ traceEventTestBlockWith setTickTime tracer0 tracer = \case
189193
TraceBlockFetchClientTerminationEvent peerId traceEvent -> traceBlockFetchClientTerminationEventTestBlockWith peerId tracer traceEvent
190194
TraceGenesisDDEvent gddEvent -> traceWith tracer (terseGDDEvent gddEvent)
191195
TraceChainSyncSendRecvEvent peerId peerType traceEvent -> traceChainSyncSendRecvEventTestBlockWith peerId peerType tracer traceEvent
196+
TraceDbfEvent traceEvent -> traceDbjEventWith tracer traceEvent
197+
TraceCsjEvent peerId traceEvent -> traceCsjEventWith peerId tracer traceEvent
192198
TraceOther msg -> traceWith tracer msg
193199

194200
traceSchedulerEventTestBlockWith ::
@@ -498,6 +504,40 @@ traceChainSyncSendRecvEventTestBlockWith pid ptp tracer = \case
498504
MsgIntersectNotFound tip -> "MsgIntersectNotFound " ++ terseTip tip
499505
MsgDone -> "MsgDone"
500506

507+
traceDbjEventWith ::
508+
Tracer m String ->
509+
TraceEventDbf PeerId ->
510+
m ()
511+
traceDbjEventWith tracer = traceWith tracer . \case
512+
RotatedDynamo old new -> "Rotated dynamo from " ++ condense old ++ " to " ++ condense new
513+
514+
traceCsjEventWith ::
515+
PeerId ->
516+
Tracer m String ->
517+
TraceEventCsj PeerId TestBlock ->
518+
m ()
519+
traceCsjEventWith peer tracer = f . \case
520+
BecomingObjector mbOld -> "is now the Objector" ++ replacing mbOld
521+
BlockedOnJump -> "is a happy Jumper blocked on the next CSJ instruction"
522+
InitializedAsDynamo -> "initialized as the Dynamo"
523+
NoLongerDynamo mbNew reason -> g reason ++ " and so is no longer the Dynamo" ++ replacedBy mbNew
524+
NoLongerObjector mbNew reason -> g reason ++ " and so is no longer the Objector" ++ replacedBy mbNew
525+
SentJumpInstruction p -> "instructed Jumpers to " ++ tersePoint p
526+
where
527+
f = traceUnitWith tracer ("CSJ " ++ condense peer)
528+
529+
g = \case
530+
BecauseCsjDisconnect -> "disconnected"
531+
BecauseCsjDisengage -> "disengaged"
532+
533+
replacedBy = \case
534+
Nothing -> ""
535+
Just new -> ", replaced by: " ++ condense new
536+
537+
replacing = \case
538+
Nothing -> ""
539+
Just old -> ", replacing: " ++ condense old
540+
501541
prettyDensityBounds :: [(PeerId, DensityBounds TestBlock)] -> [String]
502542
prettyDensityBounds bounds =
503543
showPeers (second showBounds <$> bounds)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Patch
9+
10+
- A bullet item for the Patch category.
11+
12+
-->
13+
<!--
14+
### Non-Breaking
15+
16+
- A bullet item for the Non-Breaking category.
17+
18+
-->
19+
20+
### Breaking
21+
22+
- Added a new CSJ tracer to ChainSync client interface.

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ library
308308
sop-core ^>=0.5,
309309
sop-extras ^>=0.2,
310310
streaming,
311+
strict >=0.1 && <0.6,
311312
strict-checked-vars ^>=0.2,
312313
strict-mvar ^>=1.5,
313314
strict-sop-core ^>=0.1,

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/BlockFetch/ClientInterface.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mkBlockFetchConsensusInterface ::
186186
, Ord peer
187187
, LedgerSupportsProtocol blk
188188
)
189-
=> Tracer m (CSJumping.TraceEvent peer)
189+
=> Tracer m (CSJumping.TraceEventDbf peer)
190190
-> BlockConfig blk
191191
-> ChainDbView m blk
192192
-> CSClient.ChainSyncClientHandleCollection peer m blk

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import Control.Monad (join, void)
7878
import Control.Monad.Class.MonadTimer (MonadTimer)
7979
import Control.Monad.Except (runExcept, throwError)
8080
import Control.Tracer
81+
import Data.Foldable (traverse_)
8182
import Data.Functor ((<&>))
8283
import Data.Kind (Type)
8384
import Data.Map.Strict (Map)
@@ -333,6 +334,7 @@ bracketChainSyncClient ::
333334
, MonadTimer m
334335
)
335336
=> Tracer m (TraceChainSyncClientEvent blk)
337+
-> Tracer m (Jumping.TraceEventCsj peer blk)
336338
-> ChainDbView m blk
337339
-> ChainSyncClientHandleCollection peer m blk
338340
-- ^ The kill handle and states for each peer, we need the whole map because we
@@ -348,6 +350,7 @@ bracketChainSyncClient ::
348350
-> m a
349351
bracketChainSyncClient
350352
tracer
353+
tracerCsj
351354
ChainDbView { getIsInvalidBlock }
352355
varHandles
353356
getGsmState
@@ -414,7 +417,8 @@ bracketChainSyncClient
414417
bracket_ insertHandle deleteHandle $ f Jumping.noJumping
415418

416419
withCSJCallbacks lopBucket csHandleState (CSJEnabled csjEnabledConfig) f =
417-
bracket (acquireContext lopBucket csHandleState csjEnabledConfig) releaseContext $ \peerContext ->
420+
bracket (acquireContext lopBucket csHandleState csjEnabledConfig) releaseContext $ \(peerContext, mbEv) -> do
421+
traverse_ (traceWith (Jumping.tracer peerContext)) mbEv
418422
f $ Jumping.mkJumping peerContext
419423

420424
acquireContext lopBucket cschState (CSJEnabledConfig jumpSize) = do
@@ -423,7 +427,7 @@ bracketChainSyncClient
423427
initialGsmState <- getGsmState
424428
updateLopBucketConfig lopBucket initialGsmState time
425429
cschJumpInfo <- newTVar Nothing
426-
context <- Jumping.makeContext varHandles jumpSize
430+
context <- Jumping.makeContext varHandles jumpSize tracerCsj
427431
Jumping.registerClient context peer cschState $ \cschJumping -> ChainSyncClientHandle
428432
{ cschGDDKill = throwTo tid DensityTooLow
429433
, cschOnGsmStateChanged = updateLopBucketConfig lopBucket
@@ -432,7 +436,9 @@ bracketChainSyncClient
432436
, cschJumpInfo
433437
}
434438

435-
releaseContext = atomically . Jumping.unregisterClient
439+
releaseContext (peerContext, _mbEv) = do
440+
mbEv <- atomically $ Jumping.unregisterClient peerContext
441+
traverse_ (traceWith (Jumping.tracer peerContext)) mbEv
436442

437443
invalidBlockWatcher varState =
438444
invalidBlockRejector

0 commit comments

Comments
 (0)