{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Ouroboros.Consensus.Mempool.TxLimits (
ByteSize (..)
, TxLimits (..)
, (Ouroboros.Consensus.Mempool.TxLimits.<=)
, Overrides
, applyOverrides
, getOverrides
, mkOverrides
, noOverridesMeasure
) where
import Data.Coerce (coerce)
import Data.Word (Word32)
import Data.Measure (BoundedMeasure, Measure)
import qualified Data.Measure as Measure
import Ouroboros.Consensus.Ledger.Abstract (Validated)
import Ouroboros.Consensus.Ledger.Basics (LedgerState)
import Ouroboros.Consensus.Ledger.SupportsMempool (GenTx)
import Ouroboros.Consensus.Ticked (Ticked (..))
class BoundedMeasure (TxMeasure blk) => TxLimits blk where
type TxMeasure blk
txMeasure :: Validated (GenTx blk) -> TxMeasure blk
txsBlockCapacity :: Ticked (LedgerState blk) -> TxMeasure blk
(<=) :: Measure a => a -> a -> Bool
<= :: a -> a -> Bool
(<=) = a -> a -> Bool
forall a. Measure a => a -> a -> Bool
(Measure.<=)
newtype ByteSize = ByteSize { ByteSize -> Word32
unByteSize :: Word32 }
deriving stock (Int -> ByteSize -> ShowS
[ByteSize] -> ShowS
ByteSize -> String
(Int -> ByteSize -> ShowS)
-> (ByteSize -> String) -> ([ByteSize] -> ShowS) -> Show ByteSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ByteSize] -> ShowS
$cshowList :: [ByteSize] -> ShowS
show :: ByteSize -> String
$cshow :: ByteSize -> String
showsPrec :: Int -> ByteSize -> ShowS
$cshowsPrec :: Int -> ByteSize -> ShowS
Show)
deriving newtype (ByteSize -> ByteSize -> Bool
(ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool) -> Eq ByteSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ByteSize -> ByteSize -> Bool
$c/= :: ByteSize -> ByteSize -> Bool
== :: ByteSize -> ByteSize -> Bool
$c== :: ByteSize -> ByteSize -> Bool
Eq, Eq ByteSize
Eq ByteSize
-> (ByteSize -> ByteSize -> Ordering)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> Ord ByteSize
ByteSize -> ByteSize -> Bool
ByteSize -> ByteSize -> Ordering
ByteSize -> ByteSize -> ByteSize
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ByteSize -> ByteSize -> ByteSize
$cmin :: ByteSize -> ByteSize -> ByteSize
max :: ByteSize -> ByteSize -> ByteSize
$cmax :: ByteSize -> ByteSize -> ByteSize
>= :: ByteSize -> ByteSize -> Bool
$c>= :: ByteSize -> ByteSize -> Bool
> :: ByteSize -> ByteSize -> Bool
$c> :: ByteSize -> ByteSize -> Bool
<= :: ByteSize -> ByteSize -> Bool
$c<= :: ByteSize -> ByteSize -> Bool
< :: ByteSize -> ByteSize -> Bool
$c< :: ByteSize -> ByteSize -> Bool
compare :: ByteSize -> ByteSize -> Ordering
$ccompare :: ByteSize -> ByteSize -> Ordering
$cp1Ord :: Eq ByteSize
Ord)
deriving newtype (Measure ByteSize
ByteSize
Measure ByteSize -> ByteSize -> BoundedMeasure ByteSize
forall a. Measure a -> a -> BoundedMeasure a
maxBound :: ByteSize
$cmaxBound :: ByteSize
$cp1BoundedMeasure :: Measure ByteSize
BoundedMeasure, Eq ByteSize
ByteSize
Eq ByteSize
-> ByteSize
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> Measure ByteSize
ByteSize -> ByteSize -> ByteSize
forall a.
Eq a
-> a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> Measure a
max :: ByteSize -> ByteSize -> ByteSize
$cmax :: ByteSize -> ByteSize -> ByteSize
min :: ByteSize -> ByteSize -> ByteSize
$cmin :: ByteSize -> ByteSize -> ByteSize
plus :: ByteSize -> ByteSize -> ByteSize
$cplus :: ByteSize -> ByteSize -> ByteSize
zero :: ByteSize
$czero :: ByteSize
$cp1Measure :: Eq ByteSize
Measure)
newtype Overrides blk =
Overrides {Overrides blk -> TxMeasure blk
getOverrides :: TxMeasure blk}
instance TxLimits blk => Monoid (Overrides blk) where
mempty :: Overrides blk
mempty = TxMeasure blk -> Overrides blk
forall blk. TxMeasure blk -> Overrides blk
Overrides TxMeasure blk
forall a. BoundedMeasure a => a
noOverridesMeasure
instance TxLimits blk => Semigroup (Overrides blk) where
<> :: Overrides blk -> Overrides blk -> Overrides blk
(<>) = (TxMeasure blk -> TxMeasure blk -> TxMeasure blk)
-> Overrides blk -> Overrides blk -> Overrides blk
coerce ((TxMeasure blk -> TxMeasure blk -> TxMeasure blk)
-> Overrides blk -> Overrides blk -> Overrides blk)
-> (TxMeasure blk -> TxMeasure blk -> TxMeasure blk)
-> Overrides blk
-> Overrides blk
-> Overrides blk
forall a b. (a -> b) -> a -> b
$ Measure (TxMeasure blk) =>
TxMeasure blk -> TxMeasure blk -> TxMeasure blk
forall a. Measure a => a -> a -> a
Measure.min @(TxMeasure blk)
noOverridesMeasure :: BoundedMeasure a => a
noOverridesMeasure :: a
noOverridesMeasure = a
forall a. BoundedMeasure a => a
Measure.maxBound
mkOverrides :: TxMeasure blk -> Overrides blk
mkOverrides :: TxMeasure blk -> Overrides blk
mkOverrides = TxMeasure blk -> Overrides blk
forall blk. TxMeasure blk -> Overrides blk
Overrides
applyOverrides ::
TxLimits blk
=> Overrides blk
-> TxMeasure blk
-> TxMeasure blk
applyOverrides :: Overrides blk -> TxMeasure blk -> TxMeasure blk
applyOverrides (Overrides TxMeasure blk
m') TxMeasure blk
m = TxMeasure blk -> TxMeasure blk -> TxMeasure blk
forall a. Measure a => a -> a -> a
Measure.min TxMeasure blk
m' TxMeasure blk
m