{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
module Ouroboros.Consensus.Leios.TxCache.API
(
LeiosTxCache (..)
, evictOlderThan
, insertBody
, ReferencesTxsByHash (..)
, PackedEndorserBlock
, packEndorserBlock
, TxState (..)
, LandedTx (..)
, maxLeiosPointCount
, LeiosTxCacheInsertBodySummary (..)
, defaultLeiosTxCacheShift
, worstCaseCacheTxCount
, TxArrivalPrior (..)
, bucketTxArrival
) where
import Cardano.Crypto.Hash (Hash (UnsafeHash), hashToBytesShort)
import Cardano.Slotting.Slot (SlotNo)
import qualified Data.ByteString.Short as SBS
import Data.Maybe.Strict (StrictMaybe)
import Data.Set (Set)
import qualified Data.Vector.Unboxed as VU
import Ouroboros.Consensus.Leios.Types
( BytesSize
, EndorserBlock
, LeiosPoint
, TxHash
, TxLocation
, TxOffset
, endorserBlockReferences
, maxTxsPerEndorserBlock
)
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Trace
( FetchArrivalBytes
, LeiosTxCacheInsertBodySummary (..)
, fetchArrivalEvicted
, fetchArrivalExtra
, fetchArrivalGood
)
maxLeiosPointCount :: Int
maxLeiosPointCount :: Int
maxLeiosPointCount = Int
128
worstCaseCacheTxCount :: Int
worstCaseCacheTxCount :: Int
worstCaseCacheTxCount = Int
maxLeiosPointCount Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
maxTxsPerEndorserBlock
defaultLeiosTxCacheShift :: Int
defaultLeiosTxCacheShift :: Int
defaultLeiosTxCacheShift = Int
22
data LeiosTxCache m a v b = LeiosTxCache
{ forall (m :: * -> *) a v b.
LeiosTxCache m a v b
-> forall w.
SlotNo -> w -> (w -> TxHash -> w) -> m (Set LeiosPoint, w)
evictOlderThanFoldl' ::
forall w.
SlotNo ->
w ->
(w -> TxHash -> w) ->
m (Set LeiosPoint, w)
, forall (m :: * -> *) a v b.
LeiosTxCache m a v b
-> forall w.
LeiosPoint
-> b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> m (Maybe LeiosTxCacheInsertBodySummary, w)
insertBodyAndFoldl' ::
forall w.
LeiosPoint ->
b ->
w ->
(w -> TxOffset -> TxHash -> BytesSize -> Maybe TxLocation -> w) ->
m (Maybe LeiosTxCacheInsertBodySummary, w)
, forall (m :: * -> *) a v b.
LeiosTxCache m a v b
-> LeiosPoint -> [LandedTx a v] -> m FetchArrivalBytes
insertLandedTxs :: LeiosPoint -> [LandedTx a v] -> m FetchArrivalBytes
, forall (m :: * -> *) a v b.
LeiosTxCache m a v b
-> forall r.
(forall (n :: * -> *).
Monad n =>
(TxHash -> n (StrictMaybe (TxState a v))) -> n r)
-> m r
withLookupTx ::
forall r.
(forall n. Monad n => (TxHash -> n (StrictMaybe (TxState a v))) -> n r) ->
m r
}
evictOlderThan :: Functor m => LeiosTxCache m a v b -> SlotNo -> m ()
evictOlderThan :: forall (m :: * -> *) a v b.
Functor m =>
LeiosTxCache m a v b -> SlotNo -> m ()
evictOlderThan LeiosTxCache m a v b
cache SlotNo
boundary =
() () -> m (Set LeiosPoint, ()) -> m ()
forall a b. a -> m b -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ LeiosTxCache m a v b
-> forall w.
SlotNo -> w -> (w -> TxHash -> w) -> m (Set LeiosPoint, w)
forall (m :: * -> *) a v b.
LeiosTxCache m a v b
-> forall w.
SlotNo -> w -> (w -> TxHash -> w) -> m (Set LeiosPoint, w)
evictOlderThanFoldl' LeiosTxCache m a v b
cache SlotNo
boundary () (\() TxHash
_txh -> ())
insertBody ::
Functor m =>
LeiosTxCache m a v b ->
LeiosPoint ->
b ->
m (Maybe LeiosTxCacheInsertBodySummary)
insertBody :: forall (m :: * -> *) a v b.
Functor m =>
LeiosTxCache m a v b
-> LeiosPoint -> b -> m (Maybe LeiosTxCacheInsertBodySummary)
insertBody LeiosTxCache m a v b
cache LeiosPoint
point b
b =
(Maybe LeiosTxCacheInsertBodySummary, ())
-> Maybe LeiosTxCacheInsertBodySummary
forall a b. (a, b) -> a
fst ((Maybe LeiosTxCacheInsertBodySummary, ())
-> Maybe LeiosTxCacheInsertBodySummary)
-> m (Maybe LeiosTxCacheInsertBodySummary, ())
-> m (Maybe LeiosTxCacheInsertBodySummary)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LeiosTxCache m a v b
-> forall w.
LeiosPoint
-> b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> m (Maybe LeiosTxCacheInsertBodySummary, w)
forall (m :: * -> *) a v b.
LeiosTxCache m a v b
-> forall w.
LeiosPoint
-> b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> m (Maybe LeiosTxCacheInsertBodySummary, w)
insertBodyAndFoldl' LeiosTxCache m a v b
cache LeiosPoint
point b
b () (\() Int
_off TxHash
_txh BytesSize
_sz Maybe TxLocation
_loc -> ())
class ReferencesTxsByHash b where
foldTxReferences :: (r -> TxHash -> BytesSize -> r) -> r -> b -> r
data PackedEndorserBlock
= MkPackedEndorserBlock !SBS.ShortByteString !(VU.Vector BytesSize)
packEndorserBlock :: EndorserBlock -> PackedEndorserBlock
packEndorserBlock :: EndorserBlock -> PackedEndorserBlock
packEndorserBlock EndorserBlock
eb =
ShortByteString -> Vector BytesSize -> PackedEndorserBlock
MkPackedEndorserBlock
([ShortByteString] -> ShortByteString
forall a. Monoid a => [a] -> a
mconcat (((TxHash, BytesSize) -> ShortByteString)
-> [(TxHash, BytesSize)] -> [ShortByteString]
forall a b. (a -> b) -> [a] -> [b]
map (TxHash -> ShortByteString
forall h a. Hash h a -> ShortByteString
hashToBytesShort (TxHash -> ShortByteString)
-> ((TxHash, BytesSize) -> TxHash)
-> (TxHash, BytesSize)
-> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxHash, BytesSize) -> TxHash
forall a b. (a, b) -> a
fst) [(TxHash, BytesSize)]
refs))
([BytesSize] -> Vector BytesSize
forall a. Unbox a => [a] -> Vector a
VU.fromList (((TxHash, BytesSize) -> BytesSize)
-> [(TxHash, BytesSize)] -> [BytesSize]
forall a b. (a -> b) -> [a] -> [b]
map (TxHash, BytesSize) -> BytesSize
forall a b. (a, b) -> b
snd [(TxHash, BytesSize)]
refs))
where
refs :: [(TxHash, BytesSize)]
refs = EndorserBlock -> [(TxHash, BytesSize)]
endorserBlockReferences EndorserBlock
eb
instance ReferencesTxsByHash PackedEndorserBlock where
foldTxReferences :: forall r.
(r -> TxHash -> BytesSize -> r) -> r -> PackedEndorserBlock -> r
foldTxReferences r -> TxHash -> BytesSize -> r
f r
z (MkPackedEndorserBlock ShortByteString
hashes Vector BytesSize
sizes) =
(r -> Int -> BytesSize -> r) -> r -> Vector BytesSize -> r
forall b a. Unbox b => (a -> Int -> b -> a) -> a -> Vector b -> a
VU.ifoldl' (\r
acc Int
i BytesSize
sz -> r -> TxHash -> BytesSize -> r
f r
acc (Int -> TxHash
forall {h} {a}. HashAlgorithm h => Int -> Hash h a
hashAt Int
i) BytesSize
sz) r
z Vector BytesSize
sizes
where
hashAt :: Int -> Hash h a
hashAt Int
i = ShortByteString -> Hash h a
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> ShortByteString -> ShortByteString
SBS.take Int
32 (Int -> ShortByteString -> ShortByteString
SBS.drop (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i) ShortByteString
hashes))
data TxState a v
=
TxInserted !a
|
TxValidated !v
deriving (TxState a v -> TxState a v -> Bool
(TxState a v -> TxState a v -> Bool)
-> (TxState a v -> TxState a v -> Bool) -> Eq (TxState a v)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a v. (Eq a, Eq v) => TxState a v -> TxState a v -> Bool
$c== :: forall a v. (Eq a, Eq v) => TxState a v -> TxState a v -> Bool
== :: TxState a v -> TxState a v -> Bool
$c/= :: forall a v. (Eq a, Eq v) => TxState a v -> TxState a v -> Bool
/= :: TxState a v -> TxState a v -> Bool
Eq, Int -> TxState a v -> ShowS
[TxState a v] -> ShowS
TxState a v -> String
(Int -> TxState a v -> ShowS)
-> (TxState a v -> String)
-> ([TxState a v] -> ShowS)
-> Show (TxState a v)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a v. (Show a, Show v) => Int -> TxState a v -> ShowS
forall a v. (Show a, Show v) => [TxState a v] -> ShowS
forall a v. (Show a, Show v) => TxState a v -> String
$cshowsPrec :: forall a v. (Show a, Show v) => Int -> TxState a v -> ShowS
showsPrec :: Int -> TxState a v -> ShowS
$cshow :: forall a v. (Show a, Show v) => TxState a v -> String
show :: TxState a v -> String
$cshowList :: forall a v. (Show a, Show v) => [TxState a v] -> ShowS
showList :: [TxState a v] -> ShowS
Show)
data LandedTx a v = MkLandedTx
{ forall a v. LandedTx a v -> Int
ltOffset :: !TxOffset
, forall a v. LandedTx a v -> TxHash
ltHash :: !TxHash
, forall a v. LandedTx a v -> BytesSize
ltSize :: !BytesSize
, forall a v. LandedTx a v -> TxState a v
ltState :: !(TxState a v)
}
data TxArrivalPrior
=
TxEvicted
|
TxWasAbsent
|
TxWasHeld
deriving (TxArrivalPrior -> TxArrivalPrior -> Bool
(TxArrivalPrior -> TxArrivalPrior -> Bool)
-> (TxArrivalPrior -> TxArrivalPrior -> Bool) -> Eq TxArrivalPrior
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxArrivalPrior -> TxArrivalPrior -> Bool
== :: TxArrivalPrior -> TxArrivalPrior -> Bool
$c/= :: TxArrivalPrior -> TxArrivalPrior -> Bool
/= :: TxArrivalPrior -> TxArrivalPrior -> Bool
Eq, Int -> TxArrivalPrior -> ShowS
[TxArrivalPrior] -> ShowS
TxArrivalPrior -> String
(Int -> TxArrivalPrior -> ShowS)
-> (TxArrivalPrior -> String)
-> ([TxArrivalPrior] -> ShowS)
-> Show TxArrivalPrior
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxArrivalPrior -> ShowS
showsPrec :: Int -> TxArrivalPrior -> ShowS
$cshow :: TxArrivalPrior -> String
show :: TxArrivalPrior -> String
$cshowList :: [TxArrivalPrior] -> ShowS
showList :: [TxArrivalPrior] -> ShowS
Show)
bucketTxArrival :: TxArrivalPrior -> BytesSize -> FetchArrivalBytes
bucketTxArrival :: TxArrivalPrior -> BytesSize -> FetchArrivalBytes
bucketTxArrival = \case
TxArrivalPrior
TxEvicted -> BytesSize -> FetchArrivalBytes
fetchArrivalEvicted
TxArrivalPrior
TxWasAbsent -> BytesSize -> FetchArrivalBytes
fetchArrivalGood
TxArrivalPrior
TxWasHeld -> BytesSize -> FetchArrivalBytes
fetchArrivalExtra