Skip to content

Commit 4291882

Browse files
committed
consensus: consolidate transaction limits in the mempool
Remove `Mempool.`getTxSize`; the snapshot interface contains sizes now. Transaction size, block capacity, and mempool capacity are multi-dimensional vectors (incl ExUnits, etc), instead of merely bytes: see `TxMeasure`. A transaction cannot be added if it would push any component of the size over that component's capacity. The capacity override is still only specified in byte size, but the value is interpreted as a block count (rounded up).
1 parent d86a8fc commit 4291882

File tree

42 files changed

+783
-579
lines changed

Some content is hidden

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

42 files changed

+783
-579
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ forgeRegularBlock cfg bno sno st txs isLeader =
141141
foldr
142142
extendBlockPayloads
143143
initBlockPayloads
144-
(takeLargestPrefixThatFits st txs)
144+
$ takeLargestPrefixThatFits (byronGenesisConfig cfg) st txs
145145

146146
txPayload :: CC.UTxO.TxPayload
147147
txPayload = CC.UTxO.mkTxPayload (bpTxs blockPayloads)

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,9 @@ import Ouroboros.Consensus.Byron.Ledger.Serialisation
7171
(byronBlockEncodingOverhead)
7272
import Ouroboros.Consensus.Ledger.Abstract
7373
import Ouroboros.Consensus.Ledger.SupportsMempool
74-
import Ouroboros.Consensus.Mempool
7574
import Ouroboros.Consensus.Util (ShowProxy (..))
7675
import Ouroboros.Consensus.Util.Condense
7776

78-
{-------------------------------------------------------------------------------
79-
TxLimits
80-
-------------------------------------------------------------------------------}
81-
82-
instance TxLimits ByronBlock where
83-
type TxMeasure ByronBlock = ByteSize
84-
txMeasure _st = ByteSize . txInBlockSize . txForgetValidated
85-
txsBlockCapacity = ByteSize . txsMaxBytes
86-
8777
{-------------------------------------------------------------------------------
8878
Transactions
8979
-------------------------------------------------------------------------------}
@@ -132,19 +122,24 @@ instance LedgerSupportsMempool ByronBlock where
132122
where
133123
validationMode = CC.ValidationMode CC.NoBlockValidation Utxo.TxValidationNoCrypto
134124

135-
txsMaxBytes st =
136-
CC.getMaxBlockSize (tickedByronLedgerState st) - byronBlockEncodingOverhead
125+
txForgetValidated = forgetValidatedByronTx
137126

138-
txInBlockSize =
139-
fromIntegral
127+
instance TxLimits ByronBlock where
128+
type TxMeasure ByronBlock = ByteSize
129+
130+
blockCapacityTxMeasure _cfg st =
131+
ByteSize
132+
$ CC.getMaxBlockSize cvs - byronBlockEncodingOverhead
133+
where
134+
cvs = tickedByronLedgerState st
135+
136+
txMeasure _cfg _st =
137+
ByteSize
138+
. fromIntegral
140139
. Strict.length
141140
. CC.mempoolPayloadRecoverBytes
142141
. toMempoolPayload
143142

144-
txForgetValidated = forgetValidatedByronTx
145-
146-
txRefScriptSize _ _ _ = 0
147-
148143
data instance TxId (GenTx ByronBlock)
149144
= ByronTxId !Utxo.TxId
150145
| ByronDlgId !Delegation.CertificateId

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE DeriveAnyClass #-}
44
{-# LANGUAGE DeriveGeneric #-}
5+
{-# LANGUAGE EmptyCase #-}
56
{-# LANGUAGE FlexibleContexts #-}
67
{-# LANGUAGE FlexibleInstances #-}
8+
{-# LANGUAGE LambdaCase #-}
79
{-# LANGUAGE MultiParamTypeClasses #-}
810
{-# LANGUAGE NamedFieldPuns #-}
911
{-# LANGUAGE OverloadedStrings #-}
@@ -56,7 +58,7 @@ import Data.Maybe (listToMaybe, mapMaybe)
5658
import Data.Proxy
5759
import Data.SOP.BasicFunctors
5860
import Data.SOP.InPairs (RequiringBoth (..), ignoringBoth)
59-
import Data.SOP.Strict (hpure)
61+
import qualified Data.SOP.Strict as SOP
6062
import Data.SOP.Tails (Tails (..))
6163
import qualified Data.SOP.Tails as Tails
6264
import Data.Void
@@ -77,6 +79,8 @@ import Ouroboros.Consensus.HardFork.History (Bound (boundSlot),
7779
addSlots)
7880
import Ouroboros.Consensus.HardFork.Simple
7981
import Ouroboros.Consensus.Ledger.Abstract
82+
import Ouroboros.Consensus.Ledger.SupportsMempool (ByteSize,
83+
TxMeasure)
8084
import Ouroboros.Consensus.Ledger.SupportsProtocol
8185
(LedgerSupportsProtocol)
8286
import Ouroboros.Consensus.Protocol.Abstract
@@ -282,6 +286,8 @@ type CardanoHardForkConstraints c =
282286
)
283287

284288
instance CardanoHardForkConstraints c => CanHardFork (CardanoEras c) where
289+
type HardForkTxMeasure (CardanoEras c) = ConwayMeasure
290+
285291
hardForkEraTranslation = EraTranslation {
286292
translateLedgerState =
287293
PCons translateLedgerStateByronToShelleyWrapper
@@ -310,7 +316,7 @@ instance CardanoHardForkConstraints c => CanHardFork (CardanoEras c) where
310316
}
311317
hardForkChainSel =
312318
-- Byron <-> Shelley, ...
313-
TCons (hpure CompareBlockNo)
319+
TCons (SOP.hpure CompareBlockNo)
314320
-- Inter-Shelley-based
315321
$ Tails.hcpure (Proxy @(HasPraosSelectView c)) CompareSameSelectView
316322
hardForkInjectTxs =
@@ -348,6 +354,34 @@ instance CardanoHardForkConstraints c => CanHardFork (CardanoEras c) where
348354
)
349355
$ PNil
350356

357+
hardForkInjTxMeasure =
358+
fromByteSize `o`
359+
fromByteSize `o`
360+
fromByteSize `o`
361+
fromByteSize `o`
362+
fromAlonzo `o`
363+
fromAlonzo `o`
364+
fromConway `o`
365+
nil
366+
where
367+
nil :: SOP.NS f '[] -> a
368+
nil = \case {}
369+
370+
infixr `o`
371+
o ::
372+
(TxMeasure x -> a)
373+
-> (SOP.NS WrapTxMeasure xs -> a)
374+
-> SOP.NS WrapTxMeasure (x : xs)
375+
-> a
376+
o f g = \case
377+
SOP.Z (WrapTxMeasure x) -> f x
378+
SOP.S y -> g y
379+
380+
fromByteSize :: ByteSize -> ConwayMeasure
381+
fromByteSize x = fromAlonzo $ AlonzoMeasure x mempty
382+
fromAlonzo x = fromConway $ ConwayMeasure x mempty
383+
fromConway x = x
384+
351385
class (SelectView (BlockProtocol blk) ~ PraosChainSelectView c) => HasPraosSelectView c blk
352386
instance (SelectView (BlockProtocol blk) ~ PraosChainSelectView c) => HasPraosSelectView c blk
353387

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

Lines changed: 13 additions & 6 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 Ouroboros.Consensus.Protocol.Abstract (CanBeLeader, IsLeader)
2524
import Ouroboros.Consensus.Protocol.Ledger.HotKey (HotKey)
2625
import Ouroboros.Consensus.Shelley.Eras (EraCrypto)
@@ -40,7 +39,7 @@ import Ouroboros.Consensus.Util.Assert
4039

4140
forgeShelleyBlock ::
4241
forall m era proto.
43-
(ShelleyCompatible proto era, TxLimits (ShelleyBlock proto era), Monad m)
42+
(ShelleyCompatible proto era, TxLimits (ShelleyBlock proto era), Monad m)
4443
=> HotKey (EraCrypto era) m
4544
-> CanBeLeader proto
4645
-> TopLevelConfig (ShelleyBlock proto era)
@@ -66,13 +65,15 @@ forgeShelleyBlock
6665
assert (verifyBlockIntegrity (configSlotsPerKESPeriod $ configConsensus cfg) blk) $
6766
assertWithMsg bodySizeEstimate blk
6867
where
68+
lcfg = configLedger cfg
69+
6970
protocolVersion = shelleyProtocolVersion $ configBlock cfg
7071

7172
body =
7273
SL.toTxSeq @era
7374
. Seq.fromList
7475
. fmap extractTx
75-
$ takeLargestPrefixThatFits tickedLedger txs
76+
$ takeLargestPrefixThatFits lcfg tickedLedger txs
7677

7778
extractTx :: Validated (GenTx (ShelleyBlock proto era)) -> Core.Tx era
7879
extractTx (ShelleyValidatedTx _txid vtx) = SL.extractTx vtx
@@ -97,6 +98,12 @@ forgeShelleyBlock
9798
| otherwise
9899
= return ()
99100

100-
estimatedBodySize, actualBodySize :: Int
101-
estimatedBodySize = fromIntegral $ foldl' (+) 0 $ map (txInBlockSize . txForgetValidated) txs
102-
actualBodySize = SL.bBodySize protocolVersion body
101+
actualBodySize, estimatedBodySize :: Int
102+
actualBodySize = SL.bBodySize protocolVersion body
103+
104+
estimatedBodySize =
105+
fromIntegral . unByteSize
106+
$ foldl' (<>) mempty
107+
$ map
108+
(txMeasureByteSize . txMeasure lcfg tickedLedger . txForgetValidated)
109+
txs

0 commit comments

Comments
 (0)