Skip to content

Commit df2bd10

Browse files
committed
WIP holy moly mempool txlimits consolidation
1 parent 3be53f2 commit df2bd10

File tree

50 files changed

+912
-665
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+912
-665
lines changed

ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ library unstable-byronspec
198198
cardano-ledger-byron-test,
199199
cborg >=0.2.2 && <0.3,
200200
containers >=0.5 && <0.7,
201+
measures,
201202
mtl,
202203
nothunks,
203204
ouroboros-consensus ^>=0.19,
205+
ouroboros-network-api,
204206
serialise ^>=0.2,
205207
small-steps,
206208
transformers,
@@ -355,6 +357,7 @@ test-suite shelley-test
355357
ouroboros-consensus-cardano,
356358
ouroboros-consensus-diffusion:unstable-diffusion-testlib,
357359
ouroboros-consensus-protocol,
360+
ouroboros-network-api,
358361
sop-core,
359362
strict-sop-core,
360363
tasty,
@@ -395,6 +398,7 @@ library unstable-cardano-testlib
395398
cardano-slotting,
396399
cardano-strict-containers,
397400
containers,
401+
measures,
398402
microlens,
399403
mtl,
400404
ouroboros-consensus:{ouroboros-consensus, unstable-consensus-testlib},
@@ -446,6 +450,7 @@ test-suite cardano-test
446450
containers,
447451
contra-tracer,
448452
filepath,
453+
measures,
449454
microlens,
450455
ouroboros-consensus:{ouroboros-consensus, unstable-consensus-testlib, unstable-mempool-test-utils},
451456
ouroboros-consensus-cardano:{ouroboros-consensus-cardano, unstable-cardano-testlib},
@@ -542,6 +547,7 @@ library unstable-cardano-tools
542547
filepath,
543548
fs-api ^>=0.2.0.1,
544549
githash,
550+
measures,
545551
microlens,
546552
mtl,
547553
network,

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Forge.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ forgeRegularBlock cfg maxTxCapacityOverrides bno sno st txs isLeader =
138138
isLeader
139139
(reAnnotate byronProtVer $ Annotated toSign ())
140140
where
141+
lcfg = byronGenesisConfig cfg
142+
141143
epochSlots :: CC.Slot.EpochSlots
142144
epochSlots = byronEpochSlots cfg
143145

@@ -146,7 +148,7 @@ forgeRegularBlock cfg maxTxCapacityOverrides bno sno st txs isLeader =
146148
foldr
147149
extendBlockPayloads
148150
initBlockPayloads
149-
(takeLargestPrefixThatFits maxTxCapacityOverrides st txs)
151+
(takeLargestPrefixThatFits maxTxCapacityOverrides lcfg st txs)
150152

151153
txPayload :: CC.UTxO.TxPayload
152154
txPayload = CC.UTxO.mkTxPayload (bpTxs blockPayloads)

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE DeriveAnyClass #-}
3-
{-# LANGUAGE DeriveGeneric #-}
4-
{-# LANGUAGE DerivingVia #-}
5-
{-# LANGUAGE FlexibleContexts #-}
6-
{-# LANGUAGE FlexibleInstances #-}
7-
{-# LANGUAGE LambdaCase #-}
8-
{-# LANGUAGE OverloadedStrings #-}
9-
{-# LANGUAGE TypeFamilies #-}
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveAnyClass #-}
3+
{-# LANGUAGE DeriveGeneric #-}
4+
{-# LANGUAGE DerivingStrategies #-}
5+
{-# LANGUAGE DerivingVia #-}
6+
{-# LANGUAGE FlexibleContexts #-}
7+
{-# LANGUAGE FlexibleInstances #-}
8+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9+
{-# LANGUAGE LambdaCase #-}
10+
{-# LANGUAGE OverloadedStrings #-}
11+
{-# LANGUAGE StandaloneDeriving #-}
12+
{-# LANGUAGE TypeFamilies #-}
1013

1114
{-# OPTIONS_GHC -Wno-orphans #-}
1215

@@ -71,18 +74,29 @@ import Ouroboros.Consensus.Byron.Ledger.Serialisation
7174
(byronBlockEncodingOverhead)
7275
import Ouroboros.Consensus.Ledger.Abstract
7376
import Ouroboros.Consensus.Ledger.SupportsMempool
74-
import Ouroboros.Consensus.Mempool
7577
import Ouroboros.Consensus.Util (ShowProxy (..))
7678
import Ouroboros.Consensus.Util.Condense
79+
import Ouroboros.Consensus.Util.Orphans ()
80+
import Ouroboros.Network.SizeInBytes (SizeInBytes (..))
7781

7882
{-------------------------------------------------------------------------------
7983
TxLimits
8084
-------------------------------------------------------------------------------}
8185

8286
instance TxLimits ByronBlock where
83-
type TxMeasure ByronBlock = ByteSize
84-
txMeasure _st = ByteSize . txInBlockSize . txForgetValidated
85-
txsBlockCapacity = ByteSize . txsMaxBytes
87+
type TxMeasure ByronBlock = SizeInBytes
88+
89+
blockTxCapacity _cfg st = SizeInBytes $
90+
CC.getMaxBlockSize (tickedByronLedgerState st) - byronBlockEncodingOverhead
91+
92+
txInBlockSize _cfg _st =
93+
SizeInBytes
94+
. toEnum
95+
. Strict.length
96+
. CC.mempoolPayloadRecoverBytes
97+
. toMempoolPayload
98+
99+
txMeasureBytes _ = id
86100

87101
{-------------------------------------------------------------------------------
88102
Transactions
@@ -132,19 +146,8 @@ instance LedgerSupportsMempool ByronBlock where
132146
where
133147
validationMode = CC.ValidationMode CC.NoBlockValidation Utxo.TxValidationNoCrypto
134148

135-
txsMaxBytes st =
136-
CC.getMaxBlockSize (tickedByronLedgerState st) - byronBlockEncodingOverhead
137-
138-
txInBlockSize =
139-
fromIntegral
140-
. Strict.length
141-
. CC.mempoolPayloadRecoverBytes
142-
. toMempoolPayload
143-
144149
txForgetValidated = forgetValidatedByronTx
145150

146-
txRefScriptSize _ _ _ = 0
147-
148151
data instance TxId (GenTx ByronBlock)
149152
= ByronTxId !Utxo.TxId
150153
| ByronDlgId !Delegation.CertificateId

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/CanHardFork.hs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ import Data.Function ((&))
5353
import Data.Functor.Identity
5454
import qualified Data.Map.Strict as Map
5555
import Data.Maybe (listToMaybe, mapMaybe)
56+
import qualified Data.Measure as Measure
5657
import Data.Proxy
5758
import Data.SOP.BasicFunctors
5859
import Data.SOP.InPairs (RequiringBoth (..), ignoringBoth)
59-
import Data.SOP.Strict (hpure)
60+
import Data.SOP.Strict (hcmap, hcollapse, hpure)
6061
import Data.SOP.Tails (Tails (..))
6162
import qualified Data.SOP.Tails as Tails
6263
import Data.Void
@@ -77,6 +78,7 @@ import Ouroboros.Consensus.HardFork.History (Bound (boundSlot),
7778
addSlots)
7879
import Ouroboros.Consensus.HardFork.Simple
7980
import Ouroboros.Consensus.Ledger.Abstract
81+
import Ouroboros.Consensus.Ledger.SupportsMempool (TxMeasure)
8082
import Ouroboros.Consensus.Ledger.SupportsProtocol
8183
(LedgerSupportsProtocol)
8284
import Ouroboros.Consensus.Protocol.Abstract
@@ -88,12 +90,14 @@ import qualified Ouroboros.Consensus.Protocol.Praos as Praos
8890
import Ouroboros.Consensus.Protocol.TPraos
8991
import qualified Ouroboros.Consensus.Protocol.TPraos as TPraos
9092
import Ouroboros.Consensus.Shelley.Ledger
93+
import qualified Ouroboros.Consensus.Shelley.Ledger.Mempool as ConwayMeasure
9194
import Ouroboros.Consensus.Shelley.Node ()
9295
import Ouroboros.Consensus.Shelley.Protocol.Praos ()
9396
import Ouroboros.Consensus.Shelley.ShelleyHFC
9497
import Ouroboros.Consensus.TypeFamilyWrappers
9598
import Ouroboros.Consensus.Util (eitherToMaybe)
9699
import Ouroboros.Consensus.Util.RedundantConstraints
100+
import Ouroboros.Network.SizeInBytes (SizeInBytes (..))
97101

98102
{-------------------------------------------------------------------------------
99103
Figure out the transition point for Byron
@@ -281,7 +285,40 @@ type CardanoHardForkConstraints c =
281285
, DSIGN c ~ Ed25519DSIGN
282286
)
283287

288+
-----
289+
290+
class InjMeasure a where
291+
injMeasure :: a -> ConwayMeasure.ConwayMeasure
292+
293+
instance InjMeasure SizeInBytes where
294+
injMeasure x = injMeasure $ ConwayMeasure.AlonzoMeasure x Measure.zero
295+
296+
instance InjMeasure AlonzoMeasure where
297+
injMeasure x = injMeasure $ ConwayMeasure.ConwayMeasure x Measure.zero
298+
299+
instance InjMeasure ConwayMeasure where injMeasure = id
300+
301+
class InjMeasure (TxMeasure blk) => WrapInjMeasure blk
302+
instance InjMeasure (TxMeasure blk) => WrapInjMeasure blk
303+
304+
-----
305+
284306
instance CardanoHardForkConstraints c => CanHardFork (CardanoEras c) where
307+
type HardForkTxMeasure (CardanoEras c) = ConwayMeasure.ConwayMeasure
308+
309+
hardForkMeasureTx =
310+
hcollapse
311+
. hcmap (Proxy @WrapInjMeasure) aux
312+
where
313+
aux ::
314+
WrapInjMeasure blk
315+
=> WrapTxMeasure blk
316+
-> K ConwayMeasure.ConwayMeasure blk
317+
aux (WrapTxMeasure x) = K $ injMeasure x
318+
319+
hardForkTxMeasureBytes _prx =
320+
ConwayMeasure.byteSize . ConwayMeasure.alonzoMeasure
321+
285322
hardForkEraTranslation = EraTranslation {
286323
translateLedgerState =
287324
PCons translateLedgerStateByronToShelleyWrapper

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Eras.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,12 @@ instance (Praos.PraosCrypto c) => ShelleyBasedEra (ConwayEra c) where
299299

300300
getConwayEraGovDict _ = Just ConwayEraGovDict
301301

302-
getBabbageTxDict _ = Nothing
302+
getBabbageTxDict _ = Just $ BabbageTxDict $ \a b ->
303+
SL.ApplyTxError
304+
$ pure
305+
$ Conway.ConwayUtxowFailure
306+
$ Conway.UtxoFailure
307+
$ Conway.MaxTxSizeUTxO a b
303308

304309
applyAlonzoBasedTx :: forall era.
305310
( ShelleyBasedEra era,

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Forge.hs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import Ouroboros.Consensus.Block
2020
import Ouroboros.Consensus.Config
2121
import Ouroboros.Consensus.Ledger.Abstract
2222
import Ouroboros.Consensus.Ledger.SupportsMempool
23-
import Ouroboros.Consensus.Mempool (TxLimits)
2423
import qualified Ouroboros.Consensus.Mempool as Mempool
2524
import Ouroboros.Consensus.Protocol.Abstract (CanBeLeader, IsLeader)
2625
import Ouroboros.Consensus.Protocol.Ledger.HotKey (HotKey)
@@ -41,7 +40,10 @@ import Ouroboros.Consensus.Util.Assert
4140

4241
forgeShelleyBlock ::
4342
forall m era proto.
44-
(ShelleyCompatible proto era, TxLimits (ShelleyBlock proto era), Monad m)
43+
( ShelleyCompatible proto era
44+
, TxLimits (ShelleyBlock proto era)
45+
, Monad m
46+
)
4547
=> HotKey (EraCrypto era) m
4648
-> CanBeLeader proto
4749
-> TopLevelConfig (ShelleyBlock proto era)
@@ -76,7 +78,11 @@ forgeShelleyBlock
7678
SL.toTxSeq @era
7779
. Seq.fromList
7880
. fmap extractTx
79-
$ takeLargestPrefixThatFits maxTxCapacityOverrides tickedLedger txs
81+
$ takeLargestPrefixThatFits
82+
maxTxCapacityOverrides
83+
(topLevelConfigLedger cfg)
84+
tickedLedger
85+
txs
8086

8187
extractTx :: Validated (GenTx (ShelleyBlock proto era)) -> Core.Tx era
8288
extractTx (ShelleyValidatedTx _txid vtx) = SL.extractTx vtx
@@ -102,5 +108,11 @@ forgeShelleyBlock
102108
= return ()
103109

104110
estimatedBodySize, actualBodySize :: Int
105-
estimatedBodySize = fromIntegral $ foldl' (+) 0 $ map (txInBlockSize . txForgetValidated) txs
106111
actualBodySize = SL.bBodySize protocolVersion body
112+
estimatedBodySize =
113+
fromIntegral
114+
$ foldl' (+) 0
115+
$ map ( txMeasureBytes (Proxy @(ShelleyBlock proto era))
116+
. txInBlockSize (topLevelConfigLedger cfg) tickedLedger
117+
. txForgetValidated
118+
) txs

0 commit comments

Comments
 (0)