{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE OverloadedStrings #-}

-- | One stop shop for configuring diffusion layer for upstream clients
-- This module contains Cardano specific configuration parameters

module Cardano.Network.Diffusion.Configuration
  ( DefaultNumBootstrapPeers (..)
  , NumberOfBigLedgerPeers (..)
  , defaultNumBootstrapPeers
  , defaultSyncTargets
  , defaultNumberOfBigLedgerPeers
  , defaultChainSyncIdleTimeout
  , mkDefaultBlockFetchConfiguration
  , mkDefaultMiniProtocolParameters
  , TxSubmissionLogicVersion (..)
  , defaultTxSubmissionLogicVersion
  , defaultNodeTxSubmissionConfig
  , srvPrefix
    -- * Re-exports
  , ChainSyncIdleTimeout (..)
  , BlockFetchConfiguration (..)
  , MiniProtocolParameters (..)
  , NodeTxSubmissionConfig (..)
  , Salt
  , mkSalt
  , mkSaltIO
  ) where

import Cardano.Network.NodeToNode (MiniProtocolParameters (..),
           NodeTxSubmissionConfig (..), mkDefaultMiniProtocolParameters)
import Cardano.Network.PeerSelection.Governor.PeerSelectionState
           (NumberOfBigLedgerPeers (..))

import Ouroboros.Network.BlockFetch (BlockFetchConfiguration (..),
           GenesisBlockFetchConfiguration (..))
import Ouroboros.Network.Hashable (Salt, mkSalt, mkSaltIO)
import Ouroboros.Network.PeerSelection.Governor.Types
           (PeerSelectionTargets (..))
import Ouroboros.Network.PeerSelection.RelayAccessPoint (SRVPrefix)
import Ouroboros.Network.Protocol.ChainSync.Codec (ChainSyncIdleTimeout (..))
import Ouroboros.Network.TxSubmission.Inbound.V2.Policy qualified as V2
import Ouroboros.Network.TxSubmission.Inbound.V2.Types
           (TxSubmissionLogicVersion (..))

-- | Default number of bootstrap peers
--
newtype DefaultNumBootstrapPeers =
  DefaultNumBootstrapPeers { DefaultNumBootstrapPeers -> Int
getDefaultNumBootstrapPeers :: Int }
  deriving (DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool
(DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool)
-> (DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool)
-> Eq DefaultNumBootstrapPeers
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool
== :: DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool
$c/= :: DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool
/= :: DefaultNumBootstrapPeers -> DefaultNumBootstrapPeers -> Bool
Eq, Int -> DefaultNumBootstrapPeers -> ShowS
[DefaultNumBootstrapPeers] -> ShowS
DefaultNumBootstrapPeers -> String
(Int -> DefaultNumBootstrapPeers -> ShowS)
-> (DefaultNumBootstrapPeers -> String)
-> ([DefaultNumBootstrapPeers] -> ShowS)
-> Show DefaultNumBootstrapPeers
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DefaultNumBootstrapPeers -> ShowS
showsPrec :: Int -> DefaultNumBootstrapPeers -> ShowS
$cshow :: DefaultNumBootstrapPeers -> String
show :: DefaultNumBootstrapPeers -> String
$cshowList :: [DefaultNumBootstrapPeers] -> ShowS
showList :: [DefaultNumBootstrapPeers] -> ShowS
Show)

defaultNumBootstrapPeers :: DefaultNumBootstrapPeers
defaultNumBootstrapPeers :: DefaultNumBootstrapPeers
defaultNumBootstrapPeers = Int -> DefaultNumBootstrapPeers
DefaultNumBootstrapPeers Int
30

-- | These targets are established when Genesis mode is enabled
-- in node configuration and when the node is syncing up
--
defaultSyncTargets :: PeerSelectionTargets
defaultSyncTargets :: PeerSelectionTargets
defaultSyncTargets =
  PeerSelectionTargets {
    targetNumberOfRootPeers :: Int
targetNumberOfRootPeers                 = Int
0,
    targetNumberOfKnownPeers :: Int
targetNumberOfKnownPeers                = Int
150,
    targetNumberOfEstablishedPeers :: Int
targetNumberOfEstablishedPeers          = Int
10,
    targetNumberOfActivePeers :: Int
targetNumberOfActivePeers               = Int
5,
    targetNumberOfKnownBigLedgerPeers :: Int
targetNumberOfKnownBigLedgerPeers       = Int
100,
    targetNumberOfEstablishedBigLedgerPeers :: Int
targetNumberOfEstablishedBigLedgerPeers = Int
40,
    targetNumberOfActiveBigLedgerPeers :: Int
targetNumberOfActiveBigLedgerPeers      = Int
30 }

-- | This parameter controls the minimum number of active connections
--   with big ledger peers that must be maintained when syncing in
--   Genesis mode such that trusted state can be signalled to Consensus.
--   Exiting syncing / entering deadline mode is predicated on this
--   condition. This should be below `targetNumberOfActiveBigLedgerPeers`
--   in `syncTargets` otherwise untrusted state will never be departed.
--   This value is lower than the target, because in Genesis we may
--   demote a big ledger peer for underperformance and not immediately
--   promote one from the warm set in case there are adversaries
--   whom are intentionally trying to slow us down.
--
defaultNumberOfBigLedgerPeers :: NumberOfBigLedgerPeers
defaultNumberOfBigLedgerPeers :: NumberOfBigLedgerPeers
defaultNumberOfBigLedgerPeers = Int -> NumberOfBigLedgerPeers
NumberOfBigLedgerPeers Int
5

defaultChainSyncIdleTimeout :: ChainSyncIdleTimeout
defaultChainSyncIdleTimeout :: ChainSyncIdleTimeout
defaultChainSyncIdleTimeout = DiffTime -> ChainSyncIdleTimeout
ChainSyncIdleTimeout DiffTime
3373

srvPrefix :: SRVPrefix
srvPrefix :: SRVPrefix
srvPrefix = SRVPrefix
"_cardano._tcp"


-- | Configuration for FetchDecisionPolicy.  The maximal number of requests in
-- flight is the `blockFetchPipeliningMax` of the given
-- `MiniProtocolParameters`.
--
mkDefaultBlockFetchConfiguration :: MiniProtocolParameters
                                 -> Salt
                                 -> BlockFetchConfiguration
mkDefaultBlockFetchConfiguration :: MiniProtocolParameters -> Salt -> BlockFetchConfiguration
mkDefaultBlockFetchConfiguration MiniProtocolParameters { Word16
blockFetchPipeliningMax :: MiniProtocolParameters -> Word16
blockFetchPipeliningMax :: Word16
blockFetchPipeliningMax }
                                 Salt
bfcSalt =
  BlockFetchConfiguration {
    bfcMaxConcurrencyBulkSync :: Word
bfcMaxConcurrencyBulkSync = Word
1,
    bfcMaxConcurrencyDeadline :: Word
bfcMaxConcurrencyDeadline = Word
1,
    bfcMaxRequestsInflight :: Word
bfcMaxRequestsInflight    = Word16 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
blockFetchPipeliningMax,
    bfcDecisionLoopIntervalGenesis :: DiffTime
bfcDecisionLoopIntervalGenesis = DiffTime
0.04,  -- 40ms
    bfcDecisionLoopIntervalPraos :: DiffTime
bfcDecisionLoopIntervalPraos = DiffTime
0.01,  -- 10ms
    bfcGenesisBFConfig :: GenesisBlockFetchConfiguration
bfcGenesisBFConfig        = GenesisBlockFetchConfiguration
      { gbfcGracePeriod :: DiffTime
gbfcGracePeriod = DiffTime
10 },  -- seconds
    Salt
bfcSalt :: Salt
bfcSalt :: Salt
bfcSalt }

-- | The default logic version is the legacy one, the new one is still
-- experimental.
defaultTxSubmissionLogicVersion :: TxSubmissionLogicVersion
defaultTxSubmissionLogicVersion :: TxSubmissionLogicVersion
defaultTxSubmissionLogicVersion = TxSubmissionLogicVersion
TxSubmissionLogicV1


-- | The default `NodeTxSubmissionConfig`: the settings of
-- `defaultTxSubmissionConfigV2`.
--
-- It will be overwritten if `TxSubmissionLogicV1` is used or negotiated version
-- is before `NodeToNodeV_16`, see `txSubmissionConfigForVersion`.
--
defaultNodeTxSubmissionConfig :: NodeTxSubmissionConfig
defaultNodeTxSubmissionConfig :: NodeTxSubmissionConfig
defaultNodeTxSubmissionConfig =
    NodeTxSubmissionConfig {
      maxNumUnacknowledgedTxIds :: NumTxIdsToAck
maxNumUnacknowledgedTxIds =
        TxSubmissionConfig -> NumTxIdsToAck
V2.maxNumUnacknowledgedTxIds TxSubmissionConfig
V2.defaultTxSubmissionConfigV2,
      maxNumTxIdsToRequest :: NumTxIdsToReq
maxNumTxIdsToRequest =
        TxSubmissionConfig -> NumTxIdsToReq
V2.maxNumTxIdsToRequest TxSubmissionConfig
V2.defaultTxSubmissionConfigV2
    }