Skip to content

Commit

Permalink
diffusion: removed extraAPI
Browse files Browse the repository at this point in the history
It can be passed directly, no need to clutter types with it.
  • Loading branch information
coot committed Feb 7, 2025
1 parent ab11387 commit cf13ae7
Show file tree
Hide file tree
Showing 25 changed files with 183 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Ouroboros.Network.PeerSelection.LedgerPeers.Type
, AccPoolStake (..)
, IsBigLedgerPeer (..)
, LedgerPeersConsensusInterface (..)
, mapExtraAPI
, UseLedgerPeers (..)
, AfterSlot (..)
, LedgerPeersKind (..)
Expand Down Expand Up @@ -225,17 +224,11 @@ data IsBigLedgerPeer

-- | Return ledger state information and ledger peers.
--
data LedgerPeersConsensusInterface extraAPI m = LedgerPeersConsensusInterface {
data LedgerPeersConsensusInterface m = LedgerPeersConsensusInterface {
lpGetLatestSlot :: STM m (WithOrigin SlotNo)
, lpGetLedgerPeers :: STM m [(PoolStake, NonEmpty RelayAccessPoint)]
-- | Extension point so that third party users can add more actions
, lpExtraAPI :: extraAPI
}

mapExtraAPI :: (a -> b) -> LedgerPeersConsensusInterface a m -> LedgerPeersConsensusInterface b m
mapExtraAPI f lpci@LedgerPeersConsensusInterface{ lpExtraAPI = api } =
lpci { lpExtraAPI = f api }

instance ToJSON RelayAccessPointCoded where
toJSON (RelayAccessPointCoded (RelayAccessDomain domain port)) =
object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ data LimitsAndTimeouts header block = LimitsAndTimeouts

-- | Arguments for protocol handlers required by 'nodeApplications'.
--
data AppArgs extraAPI header block m = AppArgs
data AppArgs header block m = AppArgs
{ aaLedgerPeersConsensusInterface
:: LedgerPeersConsensusInterface extraAPI m
:: LedgerPeersConsensusInterface m
, aaKeepAliveStdGen
:: StdGen
, aaDiffusionMode
Expand All @@ -212,7 +212,7 @@ data AppArgs extraAPI header block m = AppArgs

-- | Protocol handlers.
--
applications :: forall extraAPI block header s m.
applications :: forall block header s m.
( Alternative (STM m)
, MonadAsync m
, MonadFork m
Expand All @@ -235,11 +235,11 @@ applications :: forall extraAPI block header s m.
-> NodeKernel header block s m
-> Codecs NtNAddr header block m
-> LimitsAndTimeouts header block
-> AppArgs extraAPI header block m
-> AppArgs header block m
-> (block -> header)
-> Common.Applications NtNAddr NtNVersion NtNVersionData
NtCAddr NtCVersion NtCVersionData
extraAPI m ()
m ()
applications debugTracer nodeKernel
Codecs { chainSyncCodec, blockFetchCodec
, keepAliveCodec, pingPongCodec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ diffusionSimulation

}

interfaces :: Node.Interfaces (Cardano.LedgerPeersConsensusInterface m) m
interfaces :: Node.Interfaces m
interfaces =
Node.Interfaces
{ Node.iNtnSnocket = ntnSnocket
Expand All @@ -1173,8 +1173,7 @@ diffusionSimulation
, Node.iNtcBearer = makeFDBearer
, Node.iRng = rng
, Node.iDomainMap = dMapVar
, Node.iLedgerPeersConsensusInterface
=
, Node.iLedgerPeersConsensusInterface =
LedgerPeersConsensusInterface
(pure maxBound)
(do
Expand All @@ -1183,14 +1182,15 @@ diffusionSimulation
$ accPoolStake
$ getLedgerPools
$ ledgerPools)
Cardano.LedgerPeersConsensusInterface {
Cardano.getLedgerStateJudgement = pure TooOld
, Cardano.updateOutboundConnectionsState =
\a -> do
a' <- readTVar onlyOutboundConnectionsStateVar
when (a /= a') $
writeTVar onlyOutboundConnectionsStateVar a
}
, Node.iLedgerPeersConsensusCardanoInterface =
Cardano.LedgerPeersConsensusInterface {
Cardano.getLedgerStateJudgement = pure TooOld
, Cardano.updateOutboundConnectionsState =
\a -> do
a' <- readTVar onlyOutboundConnectionsStateVar
when (a /= a') $
writeTVar onlyOutboundConnectionsStateVar a
}
, Node.iConnStateIdSupply = connStateIdSupply
}

Expand Down Expand Up @@ -1269,17 +1269,14 @@ diffusionSimulation
Cardano.cardanoPublicRootPeersAPI
(Cardano.cardanoPeerSelectionGovernorArgs
(Cardano.cardanoExtraArgsToPeerSelectionActions cardanoExtraArgs)
(Node.iLedgerPeersConsensusCardanoInterface interfaces)
readUseLedgerPeers
peerSharing
( Cardano.updateOutboundConnectionsState
$ lpExtraAPI
$ Node.iLedgerPeersConsensusInterface
$ interfaces)
)
Cardano.cardanoPeerSelectionStatetoCounters
(flip Cardano.ExtraPeers Set.empty)
requestPublicRootPeers'
peerChurnGovernor
(peerChurnGovernor $ Node.iLedgerPeersConsensusCardanoInterface interfaces)
tracersExtraAddr
( contramap (DiffusionFetchTrace . (\(TraceLabelPeer _ a) -> a))
. tracerWithName addr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ import Ouroboros.Network.Mock.ConcreteBlock (Block (..), BlockHeader (..),
convertSlotToTimeForTestsAssumingNoHardFork)
import Ouroboros.Network.Mock.ProducerState (ChainProducerState (..))

import Ouroboros.Cardano.Network.LedgerPeerConsensusInterface qualified as Cardano

import Ouroboros.Network.AnchoredFragment qualified as AF
import Ouroboros.Network.Block (MaxSlotNo (..), maxSlotNoFromWithOrigin,
pointSlot)
Expand Down Expand Up @@ -123,7 +125,7 @@ import Test.Ouroboros.Network.PeerSelection.RootPeersDNS (DNSLookupDelay,
DNSTimeout, mockDNSActions)


data Interfaces extraAPI m = Interfaces
data Interfaces m = Interfaces
{ iNtnSnocket :: Snocket m (NtNFD m) NtNAddr
, iNtnBearer :: MakeBearer m (NtNFD m)
, iAcceptVersion :: NtNVersionData -> NtNVersionData -> Accept NtNVersionData
Expand All @@ -133,7 +135,9 @@ data Interfaces extraAPI m = Interfaces
, iRng :: StdGen
, iDomainMap :: StrictTVar m (Map Domain [(IP, TTL)])
, iLedgerPeersConsensusInterface
:: LedgerPeersConsensusInterface extraAPI m
:: LedgerPeersConsensusInterface m
, iLedgerPeersConsensusCardanoInterface
:: Cardano.LedgerPeersConsensusInterface m
, iConnStateIdSupply :: ConnStateIdSupply m
}

Expand Down Expand Up @@ -169,7 +173,7 @@ data Arguments extraChurnArgs extraFlags m = Arguments
--
type ResolverException = SomeException

run :: forall extraState extraDebugState extraAPI
run :: forall extraState extraDebugState
extraPeers extraFlags extraChurnArgs extraCounters
exception resolver resolverError m.
( Alternative (STM m)
Expand Down Expand Up @@ -199,7 +203,7 @@ run :: forall extraState extraDebugState extraAPI
)
=> Node.BlockGeneratorArgs Block StdGen
-> Node.LimitsAndTimeouts BlockHeader Block
-> Interfaces extraAPI m
-> Interfaces m
-> Arguments extraChurnArgs extraFlags m
-> extraState
-> extraCounters
Expand All @@ -210,7 +214,6 @@ run :: forall extraState extraDebugState extraAPI
extraDebugState
extraFlags
extraPeers
extraAPI
extraCounters
NtNAddr
(PeerConnectionHandle
Expand Down Expand Up @@ -240,7 +243,6 @@ run :: forall extraState extraDebugState extraAPI
extraDebugState
extraFlags
extraPeers
extraAPI
extraCounters
NtNAddr
-> m Void)
Expand All @@ -264,7 +266,7 @@ run blockGeneratorArgs limits ni na
let -- diffusion interfaces
interfaces :: Common.Interfaces (NtNFD m) NtNAddr NtNVersion NtNVersionData
(NtCFD m) NtCAddr NtCVersion NtCVersionData
resolver ResolverException extraState extraFlags extraPeers extraAPI m
resolver ResolverException extraState extraFlags extraPeers m
interfaces = Common.Interfaces
{ Common.diNtnSnocket = iNtnSnocket ni
, Common.diNtnBearer = iNtnBearer ni
Expand Down Expand Up @@ -455,7 +457,7 @@ run blockGeneratorArgs limits ni na

argsExtra :: Common.ArgumentsExtra
extraState extraDebugState
extraFlags extraPeers extraAPI
extraFlags extraPeers
extraChurnArgs extraCounters exception
NtNAddr resolver resolverError m
argsExtra = Common.ArgumentsExtra
Expand All @@ -480,7 +482,7 @@ run blockGeneratorArgs limits ni na
, Common.daPeerSelectionStateToExtraCounters = psToExtraCounters
}

appArgs :: Node.AppArgs extraAPI BlockHeader Block m
appArgs :: Node.AppArgs BlockHeader Block m
appArgs = Node.AppArgs
{ Node.aaLedgerPeersConsensusInterface
= iLedgerPeersConsensusInterface ni
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ tests = testGroup "Ouroboros.Network.LedgerPeers"
, testProperty "LedgerPeerSnapshot JSON version 1" prop_ledgerPeerSnapshotJSONV1
]

type ExtraTestInterface = ()

newtype ArbitraryPortNumber = ArbitraryPortNumber { getArbitraryPortNumber :: PortNumber }

instance Arbitrary ArbitraryPortNumber where
Expand Down Expand Up @@ -297,7 +295,6 @@ prop_pick100 seed (NonNegative n) (ArbLedgerPeersKind ledgerPeersKind) (MockRoot
LedgerPeersConsensusInterface
(pure $ At slot)
(pure (Map.elems accumulatedStakeMap))
()

in counterexample (show accumulatedStakeMap) $ ioProperty $ do
tr' <- evaluateTrace (runSimTrace sim)
Expand Down Expand Up @@ -355,11 +352,10 @@ prop_pick (LedgerPools lps) (ArbLedgerPeersKind ledgerPeersKind) count seed (Moc
]
)
where
interface :: LedgerPeersConsensusInterface ExtraTestInterface (IOSim s)
interface :: LedgerPeersConsensusInterface (IOSim s)
interface = LedgerPeersConsensusInterface
(pure $ At slot)
(pure lps)
()

domainMap :: Map Domain (Set IP)
domainMap = Map.fromList [("relay.iohk.example", Set.singleton (read "2.2.2.2"))]
Expand Down Expand Up @@ -480,11 +476,10 @@ prop_getLedgerPeers (ArbitrarySlotNo curSlot)
then Origin
else At curSlot

interface :: LedgerPeersConsensusInterface ExtraTestInterface (IOSim s)
interface :: LedgerPeersConsensusInterface (IOSim s)
interface = LedgerPeersConsensusInterface
(pure $ curSlotWO)
(pure (Map.elems (accPoolStake lps)))
()

-- | Checks validity of LedgerPeerSnapshot CBOR encoding, and whether
-- round trip cycle is the identity function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4002,7 +4002,15 @@ _governorFindingPublicRoots targetNumberOfRootPeers readDomains readUseBootstrap
publicStateVar <- makePublicPeerSelectionStateVar
debugStateVar <- newTVarIO $ emptyPeerSelectionState (mkStdGen 42) (ExtraState.empty consensusMode (NumberOfBigLedgerPeers 0)) ExtraPeers.empty
dnsSemaphore <- newLedgerAndPublicRootDNSSemaphore
let interfaces = PeerSelectionInterfaces {
let ledgerPeersConsensusInterface = Cardano.LedgerPeersConsensusInterface {
getLedgerStateJudgement = readLedgerStateJudgement,
updateOutboundConnectionsState = \a -> do
a' <- readTVar olocVar
when (a /= a') $
writeTVar olocVar a
}

interfaces = PeerSelectionInterfaces {
countersVar,
publicStateVar,
debugStateVar,
Expand All @@ -4015,9 +4023,9 @@ _governorFindingPublicRoots targetNumberOfRootPeers readDomains readUseBootstrap
genesisPeerTargets = targets,
readUseBootstrapPeers = readUseBootstrapPeers
}
ledgerPeersConsensusInterface
(return DontUseLedgerPeers)
peerSharing
(Cardano.updateOutboundConnectionsState (lpExtraAPI (getLedgerStateCtx actions)))


publicRootPeersProvider
Expand Down Expand Up @@ -4051,7 +4059,6 @@ _governorFindingPublicRoots targetNumberOfRootPeers readDomains readUseBootstrap
Cardano.ExtraState
PeerTrustable
(Cardano.ExtraPeers SockAddr)
(Cardano.LedgerPeersConsensusInterface IO)
(Cardano.ExtraPeerSelectionSetsWithSizes SockAddr)
SockAddr
PeerSharing
Expand All @@ -4075,14 +4082,7 @@ _governorFindingPublicRoots targetNumberOfRootPeers readDomains readUseBootstrap
getLedgerStateCtx =
LedgerPeersConsensusInterface {
lpGetLatestSlot = pure Origin,
lpGetLedgerPeers = pure [],
lpExtraAPI = Cardano.LedgerPeersConsensusInterface {
getLedgerStateJudgement = readLedgerStateJudgement,
updateOutboundConnectionsState = \a -> do
a' <- readTVar olocVar
when (a /= a') $
writeTVar olocVar a
}
lpGetLedgerPeers = pure []
},
peerSelectionTargets = targets,
readLedgerPeerSnapshot = pure Nothing,
Expand Down
Loading

0 comments on commit cf13ae7

Please sign in to comment.