Skip to content

Commit ea83018

Browse files
Merge #1927
1927: Remove redundant arg from Ouroboros.Consensus.Node.run r=dcoutts a=dcoutts The IsProducer argument to run node is completely redundant and just makes more work for the node top level. The Node.run uses the IsProducer to decide if we should run the block production thread or not. Whether or not a node can be a producer is a function of the protocol's ConsensusConfig. So we extend ConsensusProtocol with checkIfCanBeLeader. This is much like the existing checkIsLeader but it only checks if we can ever be a slot leader, based on just the ConsensusConfig. This works out well since checkIsLeader already does much the same thing for each protocol. The only slightly dicy one is the LeaderSchedule overlay but there we can use a conservative approximation. Co-authored-by: Duncan Coutts <[email protected]>
2 parents 5837c63 + 3f0f18a commit ea83018

File tree

8 files changed

+41
-21
lines changed

8 files changed

+41
-21
lines changed

ouroboros-consensus-shelley/src/Ouroboros/Consensus/Shelley/Protocol.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ instance TPraosCrypto c => ConsensusProtocol (TPraos c) where
319319

320320
protocolSecurityParam = tpraosSecurityParam . tpraosParams
321321

322+
checkIfCanBeLeader TPraosConfig{tpraosIsCoreNodeOrNot} =
323+
case tpraosIsCoreNodeOrNot of
324+
TPraosIsACoreNode{} -> True
325+
TPraosIsNotACoreNode -> False
326+
322327
checkIsLeader cfg@TPraosConfig{..} slot lv cs =
323328
case tpraosIsCoreNodeOrNot of
324329
TPraosIsNotACoreNode -> return Nothing

ouroboros-consensus/ouroboros-consensus-mock/src/Ouroboros/Consensus/Mock/Protocol/Praos.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE FlexibleInstances #-}
77
{-# LANGUAGE KindSignatures #-}
88
{-# LANGUAGE LambdaCase #-}
9+
{-# LANGUAGE NamedFieldPuns #-}
910
{-# LANGUAGE RecordWildCards #-}
1011
{-# LANGUAGE ScopedTypeVariables #-}
1112
{-# LANGUAGE StandaloneDeriving #-}
@@ -259,6 +260,11 @@ instance PraosCrypto c => ConsensusProtocol (Praos c) where
259260
type ValidateView (Praos c) = PraosValidateView c
260261
type ConsensusState (Praos c) = [BlockInfo c]
261262

263+
checkIfCanBeLeader PraosConfig{praosNodeId} =
264+
case praosNodeId of
265+
CoreId{} -> True
266+
RelayId{} -> False -- Relays are never leaders
267+
262268
checkIsLeader cfg@PraosConfig{..} slot _u cs =
263269
case praosNodeId of
264270
RelayId _ -> return Nothing

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module Ouroboros.Consensus.Node
1111
( DiffusionTracers (..)
1212
, DiffusionArguments (..)
1313
, run
14-
, IsProducer (..)
1514
-- * Exposed by 'run'
1615
, RunNode (..)
1716
, Tracers
@@ -65,6 +64,7 @@ import Ouroboros.Consensus.Node.State
6564
import Ouroboros.Consensus.Node.Tracers
6665
import Ouroboros.Consensus.NodeKernel
6766
import Ouroboros.Consensus.NodeNetwork
67+
import Ouroboros.Consensus.Protocol.Abstract
6868
import Ouroboros.Consensus.Util.IOLike
6969
import Ouroboros.Consensus.Util.Orphans ()
7070
import Ouroboros.Consensus.Util.Random
@@ -83,12 +83,6 @@ import Ouroboros.Consensus.Storage.LedgerDB.InMemory
8383
import Ouroboros.Consensus.Storage.VolatileDB
8484
(BlockValidationPolicy (..), mkBlocksPerFile)
8585

86-
-- | Whether the node produces blocks or not.
87-
data IsProducer
88-
= IsProducer
89-
| IsNotProducer
90-
deriving (Eq, Show)
91-
9286
-- | Start a node.
9387
--
9488
-- This opens the 'ChainDB', sets up the 'NodeKernel' and initialises the
@@ -107,7 +101,6 @@ run
107101
-> NetworkMagic
108102
-> FilePath -- ^ Database path
109103
-> ProtocolInfo blk
110-
-> IsProducer
111104
-> (ChainDbArgs IO blk -> ChainDbArgs IO blk)
112105
-- ^ Customise the 'ChainDbArgs'
113106
-> (NodeArgs IO RemoteConnectionId blk -> NodeArgs IO RemoteConnectionId blk)
@@ -117,7 +110,7 @@ run
117110
-- layer is initialised.
118111
-> IO ()
119112
run tracers protocolTracers chainDbTracer diffusionTracers diffusionArguments
120-
networkMagic dbPath pInfo isProducer customiseChainDbArgs
113+
networkMagic dbPath pInfo customiseChainDbArgs
121114
customiseNodeArgs onNodeKernel = do
122115
either throwM return =<< checkDbMarker
123116
hasFS
@@ -172,7 +165,6 @@ run tracers protocolTracers chainDbTracer diffusionTracers diffusionArguments
172165
tracers
173166
btime
174167
chainDB
175-
isProducer
176168
nodeKernel <- initNodeKernel nodeArgs
177169
onNodeKernel registry nodeKernel
178170

@@ -328,9 +320,8 @@ mkNodeArgs
328320
-> Tracers IO RemoteConnectionId blk
329321
-> BlockchainTime IO
330322
-> ChainDB IO blk
331-
-> IsProducer
332323
-> NodeArgs IO RemoteConnectionId blk
333-
mkNodeArgs registry cfg initState tracers btime chainDB isProducer = NodeArgs
324+
mkNodeArgs registry cfg initState tracers btime chainDB = NodeArgs
334325
{ tracers
335326
, registry
336327
, maxClockSkew = ClockSkew 1
@@ -347,9 +338,12 @@ mkNodeArgs registry cfg initState tracers btime chainDB isProducer = NodeArgs
347338
, miniProtocolParameters = defaultMiniProtocolParameters
348339
}
349340
where
350-
blockProduction = case isProducer of
351-
IsNotProducer -> Nothing
352-
IsProducer -> Just BlockProduction
353-
{ produceBlock = \_lift' -> nodeForgeBlock cfg
354-
, runMonadRandomDict = runMonadRandomIO
355-
}
341+
blockProduction
342+
| checkIfCanBeLeader (configConsensus cfg)
343+
= Just BlockProduction
344+
{ produceBlock = \_lift' -> nodeForgeBlock cfg
345+
, runMonadRandomDict = runMonadRandomIO
346+
}
347+
| otherwise
348+
= Nothing
349+

ouroboros-consensus/src/Ouroboros/Consensus/Protocol/Abstract.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ class ( Show (ConsensusState p)
157157
-> ConsensusState p
158158
-> m (Maybe (IsLeader p))
159159

160+
-- | Check if a node is configured such that it can be a leader.
161+
checkIfCanBeLeader :: ConsensusConfig p -> Bool
162+
160163
-- | Apply a header
161164
updateConsensusState :: HasCallStack
162165
=> ConsensusConfig p

ouroboros-consensus/src/Ouroboros/Consensus/Protocol/BFT.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE FlexibleContexts #-}
55
{-# LANGUAGE FlexibleInstances #-}
66
{-# LANGUAGE MultiParamTypeClasses #-}
7+
{-# LANGUAGE NamedFieldPuns #-}
78
{-# LANGUAGE RecordWildCards #-}
89
{-# LANGUAGE ScopedTypeVariables #-}
910
{-# LANGUAGE StandaloneDeriving #-}
@@ -134,11 +135,14 @@ instance BftCrypto c => ConsensusProtocol (Bft c) where
134135

135136
protocolSecurityParam = bftSecurityParam . bftParams
136137

138+
checkIfCanBeLeader BftConfig{bftNodeId} =
139+
case bftNodeId of
140+
CoreId{} -> True
141+
RelayId{} -> False -- Relays are never leaders
142+
137143
checkIsLeader BftConfig{..} (SlotNo n) _l _cs = do
138144
return $ case bftNodeId of
139-
RelayId _ ->
140-
-- Relays are never leaders
141-
Nothing
145+
RelayId _ -> Nothing
142146
CoreId (CoreNodeId i) ->
143147
if n `mod` numCoreNodes == i
144148
then Just ()

ouroboros-consensus/src/Ouroboros/Consensus/Protocol/LeaderSchedule.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ instance ConsensusProtocol p => ConsensusProtocol (WithLeaderSchedule p) where
6363
compareCandidates WLSConfig{..} = compareCandidates wlsConfigP
6464
protocolSecurityParam WLSConfig{..} = protocolSecurityParam wlsConfigP
6565

66+
checkIfCanBeLeader _ = True -- Conservative approximation
67+
6668
checkIsLeader WLSConfig{..} slot _ _ = return $
6769
case Map.lookup slot $ getLeaderSchedule wlsConfigSchedule of
6870
Nothing -> Nothing

ouroboros-consensus/src/Ouroboros/Consensus/Protocol/ModChainSel.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ instance (Typeable p, Typeable s, ChainSelection p s)
5656
type ValidateView (ModChainSel p s) = ValidateView p
5757
type SelectView (ModChainSel p s) = SelectView' p
5858

59+
checkIfCanBeLeader (McsConsensusConfig cfg) = checkIfCanBeLeader cfg
5960
checkIsLeader (McsConsensusConfig cfg) = checkIsLeader cfg
6061
updateConsensusState (McsConsensusConfig cfg) = updateConsensusState cfg
6162
rewindConsensusState (McsConsensusConfig cfg) = rewindConsensusState cfg

ouroboros-consensus/src/Ouroboros/Consensus/Protocol/PBFT.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ instance PBftCrypto c => ConsensusProtocol (PBft c) where
259259

260260
protocolSecurityParam = pbftSecurityParam . pbftParams
261261

262+
checkIfCanBeLeader PBftConfig{pbftIsLeader} =
263+
case pbftIsLeader of
264+
PBftIsALeader{} -> True
265+
PBftIsNotALeader -> False
266+
262267
checkIsLeader PBftConfig{pbftIsLeader, pbftParams} (SlotNo n) _l _cs =
263268
case pbftIsLeader of
264269
PBftIsNotALeader -> return Nothing

0 commit comments

Comments
 (0)