Skip to content

Commit 33e98b5

Browse files
committed
Plug rotateDynamo into BlockFetchConsensusInterface
1 parent 73da667 commit 33e98b5

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import Data.Function (on)
4343
import Data.Functor ((<&>))
4444
import Data.Hashable (Hashable)
4545
import Data.List.NonEmpty (NonEmpty)
46-
import Data.Map.Strict (Map)
4746
import Data.Maybe (isJust, mapMaybe)
4847
import Data.Proxy
4948
import qualified Data.Text as Text
@@ -65,7 +64,7 @@ import qualified Ouroboros.Consensus.MiniProtocol.BlockFetch.ClientInterface as
6564
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
6665
(ChainSyncClientHandle (..),
6766
ChainSyncClientHandleCollection (..), ChainSyncState (..),
68-
newChainSyncClientHandleCollection, viewChainSyncState)
67+
newChainSyncClientHandleCollection)
6968
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.InFutureCheck
7069
(SomeHeaderInFutureCheck)
7170
import Ouroboros.Consensus.Node.Genesis (GenesisNodeKernelArgs (..),
@@ -386,9 +385,6 @@ initInternalState NodeKernelArgs { tracers, chainDB, registry, cfg
386385

387386
fetchClientRegistry <- newFetchClientRegistry
388387

389-
let getCandidates :: STM m (Map (ConnectionId addrNTN) (AnchoredFragment (Header blk)))
390-
getCandidates = viewChainSyncState (cschcMap varChainSyncHandles) csCandidate
391-
392388
slotForgeTimeOracle <- BlockFetchClientInterface.initSlotForgeTimeOracle cfg chainDB
393389
let readFetchMode = BlockFetchClientInterface.readFetchModeDefault
394390
btime
@@ -399,7 +395,7 @@ initInternalState NodeKernelArgs { tracers, chainDB, registry, cfg
399395
blockFetchInterface = BlockFetchClientInterface.mkBlockFetchConsensusInterface
400396
(configBlock cfg)
401397
(BlockFetchClientInterface.defaultChainDbView chainDB)
402-
getCandidates
398+
varChainSyncHandles
403399
blockFetchSize
404400
slotForgeTimeOracle
405401
readFetchMode

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@ import Control.Monad.Class.MonadTime
2323
import Control.Monad.Class.MonadTimer.SI (MonadTimer)
2424
import Control.Tracer (Tracer, nullTracer, traceWith)
2525
import Data.Functor.Contravariant ((>$<))
26-
import Data.Map.Strict (Map)
2726
import Network.TypedProtocol.Codec (AnyMessage, PeerHasAgency (..),
2827
PeerRole)
2928
import Ouroboros.Consensus.Block (HasHeader)
3029
import Ouroboros.Consensus.Block.Abstract (Header, Point (..))
3130
import qualified Ouroboros.Consensus.MiniProtocol.BlockFetch.ClientInterface as BlockFetchClientInterface
31+
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
32+
(ChainSyncClientHandleCollection)
3233
import Ouroboros.Consensus.Node.ProtocolInfo
3334
(NumCoreNodes (NumCoreNodes))
3435
import Ouroboros.Consensus.Storage.ChainDB.API
3536
import Ouroboros.Consensus.Util (ShowProxy)
3637
import Ouroboros.Consensus.Util.IOLike (DiffTime,
37-
Exception (fromException), IOLike, STM, atomically, retry,
38-
try)
38+
Exception (fromException), IOLike, atomically, retry, try)
3939
import Ouroboros.Consensus.Util.ResourceRegistry
40-
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
4140
import Ouroboros.Network.BlockFetch (BlockFetchConfiguration (..),
4241
FetchClientRegistry, FetchMode (..), blockFetchLogic,
4342
bracketFetchClient, bracketKeepAliveClient)
@@ -78,17 +77,17 @@ startBlockFetchLogic ::
7877
-> Tracer m (TraceEvent TestBlock)
7978
-> ChainDB m TestBlock
8079
-> FetchClientRegistry PeerId (Header TestBlock) TestBlock m
81-
-> STM m (Map PeerId (AnchoredFragment (Header TestBlock)))
80+
-> ChainSyncClientHandleCollection PeerId m TestBlock
8281
-> m ()
83-
startBlockFetchLogic registry tracer chainDb fetchClientRegistry getCandidates = do
82+
startBlockFetchLogic registry tracer chainDb fetchClientRegistry csHandlesCol = do
8483
let slotForgeTime :: BlockFetchClientInterface.SlotForgeTimeOracle m blk
8584
slotForgeTime _ = pure dawnOfTime
8685

8786
blockFetchConsensusInterface =
8887
BlockFetchClientInterface.mkBlockFetchConsensusInterface
8988
(TestBlockConfig $ NumCoreNodes 0) -- Only needed when minting blocks
9089
(BlockFetchClientInterface.defaultChainDbView chainDb)
91-
getCandidates
90+
csHandlesCol
9291
-- The size of headers in bytes is irrelevant because our tests
9392
-- do not serialize the blocks.
9493
(\_hdr -> 1000)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,7 @@ startNode ::
336336
LiveInterval TestBlock m ->
337337
m ()
338338
startNode schedulerConfig genesisTest interval = do
339-
let
340-
handles = psrHandles lrPeerSim
341-
getCandidates = viewChainSyncState (cschcMap handles) CSClient.csCandidate
339+
let handles = psrHandles lrPeerSim
342340
fetchClientRegistry <- newFetchClientRegistry
343341
let chainDbView = CSClient.defaultChainDbView lnChainDb
344342
activePeers = Map.toList $ Map.restrictKeys (psrPeers lrPeerSim) (lirActive liveResult)
@@ -385,7 +383,7 @@ startNode schedulerConfig genesisTest interval = do
385383
-- The block fetch logic needs to be started after the block fetch clients
386384
-- otherwise, an internal assertion fails because getCandidates yields more
387385
-- peer fragments than registered clients.
388-
BlockFetch.startBlockFetchLogic lrRegistry lrTracer lnChainDb fetchClientRegistry getCandidates
386+
BlockFetch.startBlockFetchLogic lrRegistry lrTracer lnChainDb fetchClientRegistry handles
389387

390388
for_ lrLoEVar $ \ var -> do
391389
forkLinkedWatcher lrRegistry "LoE updater background" $

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import qualified Ouroboros.Consensus.HardFork.Abstract as History
2626
import qualified Ouroboros.Consensus.HardFork.History as History
2727
import Ouroboros.Consensus.Ledger.Abstract
2828
import Ouroboros.Consensus.Ledger.Extended
29+
import Ouroboros.Consensus.Ledger.SupportsProtocol
30+
(LedgerSupportsProtocol)
31+
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client as CSClient
32+
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as Jumping
2933
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
3034
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
3135
import Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunishment
@@ -171,21 +175,25 @@ mkBlockFetchConsensusInterface ::
171175
forall m peer blk.
172176
( IOLike m
173177
, BlockSupportsDiffusionPipelining blk
174-
, BlockSupportsProtocol blk
178+
, Ord peer
179+
, LedgerSupportsProtocol blk
175180
)
176181
=> BlockConfig blk
177182
-> ChainDbView m blk
178-
-> STM m (Map peer (AnchoredFragment (Header blk)))
183+
-> CSClient.ChainSyncClientHandleCollection peer m blk
179184
-> (Header blk -> SizeInBytes)
180185
-> SlotForgeTimeOracle m blk
181186
-- ^ Slot forge time, see 'headerForgeUTCTime' and 'blockForgeUTCTime'.
182187
-> STM m FetchMode
183188
-- ^ See 'readFetchMode'.
184189
-> BlockFetchConsensusInterface peer (Header blk) blk m
185190
mkBlockFetchConsensusInterface
186-
bcfg chainDB getCandidates blockFetchSize slotForgeTime readFetchMode =
191+
bcfg chainDB csHandlesCol blockFetchSize slotForgeTime readFetchMode =
187192
BlockFetchConsensusInterface {..}
188193
where
194+
getCandidates :: STM m (Map peer (AnchoredFragment (Header blk)))
195+
getCandidates = CSClient.viewChainSyncState (CSClient.cschcMap csHandlesCol) CSClient.csCandidate
196+
189197
blockMatchesHeader :: Header blk -> blk -> Bool
190198
blockMatchesHeader = Block.blockMatchesHeader
191199

@@ -344,3 +352,6 @@ mkBlockFetchConsensusInterface
344352
blockForgeUTCTime = slotForgeTime . blockRealPoint . unFromConsensus
345353

346354
lastChainSelStarvation = getLastTimeStarved chainDB
355+
356+
demoteCSJDynamo :: peer -> m ()
357+
demoteCSJDynamo = void . atomically . Jumping.rotateDynamo csHandlesCol

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,20 +775,20 @@ rotateDynamo ::
775775
LedgerSupportsProtocol blk,
776776
MonadSTM m
777777
) =>
778-
Context m peer blk ->
778+
ChainSyncClientHandleCollection peer m blk ->
779779
peer ->
780780
STM m (Maybe (peer, ChainSyncClientHandle m blk))
781-
rotateDynamo context peer = do
782-
handles <- cschcMap (handlesCol context)
781+
rotateDynamo handlesCol peer = do
782+
handles <- cschcMap handlesCol
783783
case handles Map.!? peer of
784784
Nothing ->
785785
-- Do not re-elect a dynamo if the peer has been disconnected.
786-
getDynamo (handlesCol context)
786+
getDynamo handlesCol
787787
Just oldDynHandle ->
788788
readTVar (cschJumping oldDynHandle) >>= \case
789789
Dynamo{} -> do
790-
cschcRotateHandle (handlesCol context) peer
791-
peerStates <- cschcSeq (handlesCol context)
790+
cschcRotateHandle handlesCol peer
791+
peerStates <- cschcSeq handlesCol
792792
mEngaged <- findNonDisengaged peerStates
793793
case mEngaged of
794794
Nothing ->
@@ -806,7 +806,7 @@ rotateDynamo context peer = do
806806
pure $ Just (newDynamoId, newDynHandle)
807807
_ ->
808808
-- Do not re-elect a dynamo if the peer is not the dynamo.
809-
getDynamo (handlesCol context)
809+
getDynamo handlesCol
810810

811811
-- | Choose an unspecified new non-idling dynamo and demote all other peers to
812812
-- jumpers.

0 commit comments

Comments
 (0)