Skip to content

Commit

Permalink
ouroboros-consensus: real byte size limits
Browse files Browse the repository at this point in the history
Fixes #1727.
  • Loading branch information
coot committed Oct 5, 2021
1 parent be8d490 commit 0271404
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions ouroboros-consensus-test/src/Test/ThreadNet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ runThreadNetwork systemTime ThreadNetworkArgs
-- node
nullDebugProtocolTracers
(customNodeToNodeCodecs pInfoConfig)
NTN.noByteLimits
-- see #1882, tests that can't cope with timeouts.
(pure $ NTN.ChainSyncTimeout
{ canAwaitTimeout = waitForever
Expand Down
85 changes: 75 additions & 10 deletions ouroboros-consensus/src/Ouroboros/Consensus/Network/NodeToNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -15,6 +16,10 @@ module Ouroboros.Consensus.Network.NodeToNode (
, Codecs (..)
, defaultCodecs
, identityCodecs
-- * Byte Limits
, ByteLimits
, byteLimits
, noByteLimits
-- * Tracers
, Tracers
, Tracers' (..)
Expand All @@ -39,6 +44,8 @@ import Control.Monad.Class.MonadTime (MonadTime)
import Control.Monad.Class.MonadTimer (MonadTimer)
import Control.Tracer
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BSL
import Data.Int (Int64)
import Data.Map.Strict (Map)
import Data.Void (Void)

Expand All @@ -52,6 +59,7 @@ import Ouroboros.Network.Channel
import Ouroboros.Network.Codec
import Ouroboros.Network.DeltaQ
import Ouroboros.Network.Driver
import Ouroboros.Network.Driver.Limits
import Ouroboros.Network.KeepAlive
import Ouroboros.Network.Mux
import Ouroboros.Network.NodeToNode
Expand Down Expand Up @@ -434,6 +442,62 @@ data Apps m peer bCS bBF bTX bTX2 bKA a = Apps {
, aKeepAliveServer :: ServerApp m peer bKA a
}


-- | Per mini-protocol byte limits; For each mini-protocol they provide
-- per-state byte size limits, i.e. how much data can arrive from the network.
--
-- They don't depend on the instantiation of the protocol parameters (which
-- block type is used, etc.), hence the use of 'RankNTypes'.
--
data ByteLimits bCS bBF bTX bTX2 bKA = ByteLimits {
blChainSync :: forall header point tip.
ProtocolSizeLimits
(ChainSync header point tip)
bCS

, blBlockFetch :: forall block point.
ProtocolSizeLimits
(BlockFetch block point)
bBF

, blTxSubmission :: forall txid tx.
ProtocolSizeLimits
(TxSubmission txid tx)
bTX

, blTxSubmission2 :: forall txid tx.
ProtocolSizeLimits
(TxSubmission2 txid tx)
bTX2

, blKeepAlive :: ProtocolSizeLimits
KeepAlive
bKA

}

noByteLimits :: ByteLimits bCS bBF bTX bTX2 bKA
noByteLimits = ByteLimits {
blChainSync = byteLimitsChainSync (const 0)
, blBlockFetch = byteLimitsBlockFetch (const 0)
, blTxSubmission = byteLimitsTxSubmission (const 0)
, blTxSubmission2 = byteLimitsTxSubmission2 (const 0)
, blKeepAlive = byteLimitsKeepAlive (const 0)
}

byteLimits :: ByteLimits ByteString ByteString ByteString ByteString ByteString
byteLimits = ByteLimits {
blChainSync = byteLimitsChainSync size
, blBlockFetch = byteLimitsBlockFetch size
, blTxSubmission = byteLimitsTxSubmission size
, blTxSubmission2 = byteLimitsTxSubmission2 size
, blKeepAlive = byteLimitsKeepAlive size
}
where
size :: ByteString -> Word
size = (fromIntegral :: Int64 -> Word)
. BSL.length

-- | Construct the 'NetworkApplication' for the node-to-node protocols
mkApps
:: forall m remotePeer localPeer blk e bCS bBF bTX bTX2 bKA.
Expand All @@ -450,10 +514,11 @@ mkApps
=> NodeKernel m remotePeer localPeer blk -- ^ Needed for bracketing only
-> Tracers m remotePeer blk e
-> (NodeToNodeVersion -> Codecs blk e m bCS bCS bBF bBF bTX bTX2 bKA)
-> ByteLimits bCS bBF bTX bTX2 bKA
-> m ChainSyncTimeout
-> Handlers m remotePeer blk
-> Apps m remotePeer bCS bBF bTX bTX2 bKA ()
mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
mkApps kernel Tracers {..} mkCodecs ByteLimits {..} genChainSyncTimeout Handlers {..} =
Apps {..}
where
aChainSyncClient
Expand Down Expand Up @@ -481,7 +546,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tChainSyncTracer)
(cChainSyncCodec (mkCodecs version))
(byteLimitsChainSync (const 0)) -- TODO: Real Bytelimits, see #1727
blChainSync
(timeLimitsChainSync chainSyncTimeout)
channel
$ chainSyncClientPeerPipelined
Expand All @@ -503,7 +568,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPeerWithLimits
(contramap (TraceLabelPeer them) tChainSyncSerialisedTracer)
(cChainSyncCodecSerialised (mkCodecs version))
(byteLimitsChainSync (const 0)) -- TODO: Real Bytelimits, see #1727
blChainSync
(timeLimitsChainSync chainSyncTimeout)
channel
$ chainSyncServerPeer
Expand All @@ -521,7 +586,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tBlockFetchTracer)
(cBlockFetchCodec (mkCodecs version))
(byteLimitsBlockFetch (const 0)) -- TODO: Real Bytelimits, see #1727
blBlockFetch
timeLimitsBlockFetch
channel
$ hBlockFetchClient version controlMessageSTM clientCtx
Expand All @@ -537,7 +602,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPeerWithLimits
(contramap (TraceLabelPeer them) tBlockFetchSerialisedTracer)
(cBlockFetchCodecSerialised (mkCodecs version))
(byteLimitsBlockFetch (const 0)) -- TODO: Real Bytelimits, see #1727
blBlockFetch
timeLimitsBlockFetch
channel
$ blockFetchServerPeer
Expand All @@ -554,7 +619,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmissionTracer)
(cTxSubmissionCodec (mkCodecs version))
(byteLimitsTxSubmission (const 0)) -- TODO: Real Bytelimits, see #1727
blTxSubmission
timeLimitsTxSubmission
channel
(txSubmissionClientPeer (hTxSubmissionClient version controlMessageSTM them))
Expand All @@ -569,7 +634,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmissionTracer)
(cTxSubmissionCodec (mkCodecs version))
(byteLimitsTxSubmission (const 0)) -- TODO: Real Bytelimits, see #1727
blTxSubmission
timeLimitsTxSubmission
channel
(txSubmissionServerPeerPipelined (hTxSubmissionServer version them))
Expand All @@ -585,7 +650,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmission2Tracer)
(cTxSubmission2Codec (mkCodecs version))
(byteLimitsTxSubmission2 (const 0)) -- TODO: Real Bytelimits, see #1727
blTxSubmission2
timeLimitsTxSubmission2
channel
(Hello.wrapClientPeer (txSubmissionClientPeer (hTxSubmissionClient version controlMessageSTM them)))
Expand All @@ -600,7 +665,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPipelinedPeerWithLimits
(contramap (TraceLabelPeer them) tTxSubmission2Tracer)
(cTxSubmission2Codec (mkCodecs version))
(byteLimitsTxSubmission2 (const 0)) -- TODO: Real Bytelimits, see #1727
blTxSubmission2
timeLimitsTxSubmission2
channel
(Hello.wrapServerPeerPipelined (txSubmissionServerPeerPipelined (hTxSubmissionServer version them)))
Expand All @@ -622,7 +687,7 @@ mkApps kernel Tracers {..} mkCodecs genChainSyncTimeout Handlers {..} =
runPeerWithLimits
nullTracer
(cKeepAliveCodec (mkCodecs version))
(byteLimitsKeepAlive (const 0)) -- TODO: Real Bytelimits, see #1727
blKeepAlive
timeLimitsKeepAlive
channel
$ keepAliveClientPeer
Expand Down
1 change: 1 addition & 0 deletions ouroboros-consensus/src/Ouroboros/Consensus/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ runWith RunNodeArgs{..} LowLevelRunNodeArgs{..} =
nodeKernel
rnTraceNTN
(NTN.defaultCodecs codecConfig version)
NTN.byteLimits
llrnChainSyncTimeout
(NTN.mkHandlers nodeKernelArgs nodeKernel)

Expand Down

0 comments on commit 0271404

Please sign in to comment.