Skip to content

Commit

Permalink
Allow to disable *all* Genesis components individually
Browse files Browse the repository at this point in the history
Previously, it wasn't possible to eg run *just* CSJ.
  • Loading branch information
amesgen authored and facundominguez committed May 30, 2024
1 parent 4b437c5 commit f69fd1f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ import Ouroboros.Consensus.Fragment.InFuture (CheckInFuture,
ClockSkew)
import qualified Ouroboros.Consensus.Fragment.InFuture as InFuture
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState (..))
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
(CSJConfig (..), ChainSyncLoPBucketConfig (..))
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.InFutureCheck as InFutureCheck
import qualified Ouroboros.Consensus.Network.NodeToClient as NTC
import qualified Ouroboros.Consensus.Network.NodeToNode as NTN
Expand All @@ -90,8 +88,7 @@ import Ouroboros.Consensus.Node.DbMarker
import Ouroboros.Consensus.Node.ErrorPolicy
import Ouroboros.Consensus.Node.ExitPolicy
import Ouroboros.Consensus.Node.Genesis (GenesisConfig (..),
GenesisNodeKernelArgs, GenesisSwitch (..),
mkGenesisNodeKernelArgs)
GenesisNodeKernelArgs, mkGenesisNodeKernelArgs)
import Ouroboros.Consensus.Node.GSM (GsmNodeKernelArgs (..))
import qualified Ouroboros.Consensus.Node.GSM as GSM
import Ouroboros.Consensus.Node.InitStorage
Expand Down Expand Up @@ -197,7 +194,7 @@ data RunNodeArgs m addrNTN addrNTC blk (p2p :: Diffusion.P2P) = RunNodeArgs {

, rnGetUseBootstrapPeers :: STM m UseBootstrapPeers

, rnGenesisConfig :: GenesisSwitch GenesisConfig
, rnGenesisConfig :: GenesisConfig
}


Expand Down Expand Up @@ -254,7 +251,7 @@ data LowLevelRunNodeArgs m addrNTN addrNTC versionDataNTN versionDataNTC blk
-- | See 'NTN.ChainSyncTimeout'
, llrnChainSyncTimeout :: m NTN.ChainSyncTimeout

, llrnGenesisConfig :: GenesisSwitch GenesisConfig
, llrnGenesisConfig :: GenesisConfig

-- | How to run the data diffusion applications
--
Expand Down Expand Up @@ -527,12 +524,8 @@ runWith RunNodeArgs{..} encAddrNtN decAddrNtN LowLevelRunNodeArgs{..} =
(NTN.defaultCodecs codecConfig version encAddrNTN decAddrNTN)
NTN.byteLimits
llrnChainSyncTimeout
(case llrnGenesisConfig of
GenesisEnabled gcfg -> gcsChainSyncLoPBucketConfig gcfg
GenesisDisabled -> ChainSyncLoPBucketDisabled)
(case llrnGenesisConfig of
GenesisEnabled gcfg -> gcsCSJConfig gcfg
GenesisDisabled -> CSJDisabled)
(gcChainSyncLoPBucketConfig llrnGenesisConfig)
(gcCSJConfig llrnGenesisConfig)
(reportMetric Diffusion.peerMetricsConfiguration peerMetrics)
(NTN.mkHandlers nodeKernelArgs nodeKernel)

Expand Down Expand Up @@ -721,7 +714,7 @@ mkNodeKernelArgs ::
-> GSM.MarkerFileView m
-> STM m UseBootstrapPeers
-> StrictSTM.StrictTVar m (Diffusion.PublicPeerSelectionState addrNTN)
-> GenesisSwitch (GenesisNodeKernelArgs m blk)
-> GenesisNodeKernelArgs m blk
-> m (NodeKernelArgs m addrNTN (ConnectionId addrNTC) blk)
mkNodeKernelArgs
registry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Ouroboros.Consensus.Node.Genesis (
-- * 'GenesisConfig'
GenesisConfig (..)
, LoEAndGDDConfig (..)
, disableGenesisConfig
, enableGenesisConfigDefault
-- * NodeKernel helpers
, GenesisNodeKernelArgs (..)
, GenesisSwitch (..)
, defaultGenesisConfig
, mkGenesisNodeKernelArgs
, setGetLoEFragment
) where

import Control.Monad (join)
import Data.Traversable (for)
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
(CSJConfig (..), CSJEnabledConfig (..),
Expand All @@ -28,29 +33,39 @@ import Ouroboros.Consensus.Util.IOLike
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
import qualified Ouroboros.Network.AnchoredFragment as AF

-- We have multiple other Genesis-related types of a similar shape ('LoE', LoP
-- and CSJ configs), maybe unify?
data GenesisSwitch a =
GenesisDisabled
| GenesisEnabled !a
-- | Whether to en-/disable the Limit on Eagerness and the Genesis Density
-- Disconnector.
data LoEAndGDDConfig a =
LoEAndGDDEnabled !a
| LoEAndGDDDisabled
deriving stock (Show, Functor, Foldable, Traversable)

-- | Aggregating the various configs for Genesis-related subcomponents.
data GenesisConfig = GenesisConfig {
gcsChainSyncLoPBucketConfig :: !ChainSyncLoPBucketConfig
, gcsCSJConfig :: !CSJConfig
gcChainSyncLoPBucketConfig :: !ChainSyncLoPBucketConfig
, gcCSJConfig :: !CSJConfig
, gcLoEAndGDDConfig :: !(LoEAndGDDConfig ())
}

-- TODO justification/derivation from other parameters
defaultGenesisConfig :: GenesisConfig
defaultGenesisConfig = GenesisConfig {
gcsChainSyncLoPBucketConfig = ChainSyncLoPBucketEnabled ChainSyncLoPBucketEnabledConfig {
enableGenesisConfigDefault :: GenesisConfig
enableGenesisConfigDefault = GenesisConfig {
gcChainSyncLoPBucketConfig = ChainSyncLoPBucketEnabled ChainSyncLoPBucketEnabledConfig {
csbcCapacity = 100_000 -- number of tokens
, csbcRate = 500 -- tokens per second leaking, 1/2ms
}
, gcsCSJConfig = CSJEnabled CSJEnabledConfig {
, gcCSJConfig = CSJEnabled CSJEnabledConfig {
csjcJumpSize = 3 * 2160 * 20 -- mainnet forecast range
}
, gcLoEAndGDDConfig = LoEAndGDDEnabled ()
}

-- | Disable all Genesis components, yielding Praos behavior.
disableGenesisConfig :: GenesisConfig
disableGenesisConfig = GenesisConfig {
gcChainSyncLoPBucketConfig = ChainSyncLoPBucketDisabled
, gcCSJConfig = CSJDisabled
, gcLoEAndGDDConfig = LoEAndGDDDisabled
}

-- | Genesis-related arguments needed by the NodeKernel initialization logic.
Expand All @@ -59,31 +74,33 @@ data GenesisNodeKernelArgs m blk = GenesisNodeKernelArgs {
-- action. We use this extra indirection to update this action after we
-- opened the ChainDB (which happens before we initialize the NodeKernel).
-- After that, this TVar will not be modified again.
gnkaGetLoEFragment :: !(StrictTVar m (ChainDB.GetLoEFragment m blk))
gnkaGetLoEFragment :: !(LoEAndGDDConfig (StrictTVar m (ChainDB.GetLoEFragment m blk)))
}

-- | Create the initial 'GenesisNodeKernelArgs" (with a temporary
-- 'ChainDB.GetLoEFragment' that will be replaced via 'setGetLoEFragment') and a
-- function to update the 'ChainDbArgs' accordingly.
mkGenesisNodeKernelArgs ::
forall m blk a. (IOLike m, GetHeader blk)
=> GenesisSwitch a
-> m ( GenesisSwitch (GenesisNodeKernelArgs m blk)
forall m blk. (IOLike m, GetHeader blk)
=> GenesisConfig
-> m ( GenesisNodeKernelArgs m blk
, Complete ChainDbArgs m blk -> Complete ChainDbArgs m blk
)
mkGenesisNodeKernelArgs = \case
GenesisDisabled -> pure (GenesisDisabled, id)
GenesisEnabled{} -> do
varGetLoEFragment <- newTVarIO $ pure $
-- Use the most conservative LoE fragment until 'setGetLoEFragment' is
-- called.
ChainDB.LoEEnabled $ AF.Empty AF.AnchorGenesis
let getLoEFragment = join $ readTVarIO varGetLoEFragment
updateChainDbArgs cfg = cfg { ChainDB.cdbsArgs =
(ChainDB.cdbsArgs cfg) { ChainDB.cdbsLoE = getLoEFragment }
}
gnka = GenesisEnabled $ GenesisNodeKernelArgs varGetLoEFragment
pure (gnka, updateChainDbArgs)
mkGenesisNodeKernelArgs gcfg = do
gnkaGetLoEFragment <- for (gcLoEAndGDDConfig gcfg) $ \() ->
newTVarIO $ pure $
-- Use the most conservative LoE fragment until 'setGetLoEFragment'
-- is called.
ChainDB.LoEEnabled $ AF.Empty AF.AnchorGenesis
let updateChainDbArgs = case gnkaGetLoEFragment of
LoEAndGDDDisabled -> id
LoEAndGDDEnabled varGetLoEFragment -> \cfg ->
cfg { ChainDB.cdbsArgs =
(ChainDB.cdbsArgs cfg) { ChainDB.cdbsLoE = getLoEFragment }
}
where
getLoEFragment = join $ readTVarIO varGetLoEFragment
pure (GenesisNodeKernelArgs {gnkaGetLoEFragment}, updateChainDbArgs)

-- | Set 'gnkaGetLoEFragment' to the actual logic for determining the current
-- LoE fragment.
Expand All @@ -92,10 +109,10 @@ setGetLoEFragment ::
=> STM m GSM.GsmState
-> STM m (AnchoredFragment (Header blk))
-- ^ The LoE fragment.
-> GenesisNodeKernelArgs m blk
-> StrictTVar m (ChainDB.GetLoEFragment m blk)
-> m ()
setGetLoEFragment readGsmState readLoEFragment ctx =
atomically $ writeTVar (gnkaGetLoEFragment ctx) getLoEFragment
setGetLoEFragment readGsmState readLoEFragment varGetLoEFragment =
atomically $ writeTVar varGetLoEFragment getLoEFragment
where
getLoEFragment :: ChainDB.GetLoEFragment m blk
getLoEFragment = atomically $ readGsmState >>= \case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
viewChainSyncState)
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.InFutureCheck
(SomeHeaderInFutureCheck)
import Ouroboros.Consensus.Node.Genesis (GenesisNodeKernelArgs,
GenesisSwitch (..), setGetLoEFragment)
import Ouroboros.Consensus.Node.Genesis (GenesisNodeKernelArgs (..),
LoEAndGDDConfig (..), setGetLoEFragment)
import Ouroboros.Consensus.Node.GSM (GsmNodeKernelArgs (..))
import qualified Ouroboros.Consensus.Node.GSM as GSM
import Ouroboros.Consensus.Node.Run
Expand Down Expand Up @@ -182,7 +182,7 @@ data NodeKernelArgs m addrNTN addrNTC blk = NodeKernelArgs {
, peerSharingRng :: StdGen
, publicPeerSelectionStateVar
:: StrictSTM.StrictTVar m (PublicPeerSelectionState addrNTN)
, genesisArgs :: GenesisSwitch (GenesisNodeKernelArgs m blk)
, genesisArgs :: GenesisNodeKernelArgs m blk
}

initNodeKernel ::
Expand Down Expand Up @@ -275,14 +275,14 @@ initNodeKernel args@NodeKernelArgs { registry, cfg, tracers
ps_POLICY_PEER_SHARE_STICKY_TIME
ps_POLICY_PEER_SHARE_MAX_PEERS

case genesisArgs of
GenesisDisabled -> pure ()
GenesisEnabled ctx -> do
case gnkaGetLoEFragment genesisArgs of
LoEAndGDDDisabled -> pure ()
LoEAndGDDEnabled varGetLoEFragment -> do
varLoEFragment <- newTVarIO $ AF.Empty AF.AnchorGenesis
setGetLoEFragment
(readTVar varGsmState)
(readTVar varLoEFragment)
ctx
varGetLoEFragment

void $ forkLinkedWatcher registry "NodeKernel.GDD" $
gddWatcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client as CSClient
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.InFutureCheck as InFutureCheck
import qualified Ouroboros.Consensus.Network.NodeToNode as NTN
import Ouroboros.Consensus.Node.ExitPolicy
import Ouroboros.Consensus.Node.Genesis (GenesisSwitch (..))
import Ouroboros.Consensus.Node.Genesis
import qualified Ouroboros.Consensus.Node.GSM as GSM
import Ouroboros.Consensus.Node.InitStorage
import Ouroboros.Consensus.Node.NetworkProtocolVersion
Expand Down Expand Up @@ -1034,7 +1034,9 @@ runThreadNetwork systemTime ThreadNetworkArgs
}
, getUseBootstrapPeers = pure DontUseBootstrapPeers
, publicPeerSelectionStateVar
, genesisArgs = GenesisDisabled
, genesisArgs = GenesisNodeKernelArgs {
gnkaGetLoEFragment = LoEAndGDDDisabled
}
}

nodeKernel <- initNodeKernel nodeKernelArgs
Expand Down

0 comments on commit f69fd1f

Please sign in to comment.