{-# LANGUAGE LambdaCase #-}
module Test.Consensus.Leios.TxCache.Reference (tests) where
import Cardano.Crypto.Hash (Hash (UnsafeHash))
import Cardano.Slotting.Slot (SlotNo (..))
import qualified Data.ByteString.Short as SBS
import qualified Data.List as List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Word (Word64, Word8)
import Ouroboros.Consensus.Leios.TxCache.API
( LeiosTxCacheInsertBodySummary (..)
, ReferencesTxsByHash (..)
, TxArrivalPrior (..)
, TxState (..)
, defaultLeiosTxCacheShift
, maxLeiosPointCount
)
import Ouroboros.Consensus.Leios.TxCache.Reference
import Ouroboros.Consensus.Leios.Types
( EndorserBlockHash
, LeiosPoint (..)
, TxHash
, TxLocation (..)
, TxOffset
)
import Test.Tasty (TestTree, adjustOption, testGroup)
import Test.Tasty.HUnit (Assertion, testCase, (@?=))
import Test.Tasty.QuickCheck
( Gen
, Property
, QuickCheckTests (..)
, chooseInt
, conjoin
, counterexample
, elements
, forAllShrink
, listOf
, oneof
, shrinkList
, shuffle
, testProperty
, vectorOf
, (===)
)
tests :: TestTree
tests :: TestTree
tests =
[Char] -> [TestTree] -> TestTree
testGroup
[Char]
"TxCache.Reference"
[ [Char] -> [TestTree] -> TestTree
testGroup
[Char]
"points"
[ [Char] -> Assertion -> TestTree
testCase [Char]
"taking a body in retains its point" Assertion
test_pointOne
, [Char] -> Assertion -> TestTree
testCase [Char]
"taking a body in again at the same point is a no-op" Assertion
test_pointDup
, [Char] -> Assertion -> TestTree
testCase [Char]
"one EB's body taken in at two points -> two points, each summarised" Assertion
test_pointTwo
]
, [Char] -> [TestTree] -> TestTree
testGroup
[Char]
"bodies"
[ [Char] -> Assertion -> TestTree
testCase [Char]
"taking a body in records none of its txs" Assertion
test_bodyRecordsNoTxs
, [Char] -> Assertion -> TestTree
testCase
[Char]
"taking a body in makes its point the youngest reference of the txs the cache holds"
Assertion
test_bodyBumpsHeldTxs
, [Char] -> Assertion -> TestTree
testCase [Char]
"taking a body in summarises what the cache holds of its txs" Assertion
test_bodySummary
]
, [Char] -> [TestTree] -> TestTree
testGroup
[Char]
"txs"
[ [Char] -> Assertion -> TestTree
testCase [Char]
"landing a tx not yet applied -> lookupTx TxInserted" Assertion
test_unapplied
, [Char] -> Assertion -> TestTree
testCase [Char]
"landing an applied tx -> lookupTx TxValidated" Assertion
test_applied
, [Char] -> Assertion -> TestTree
testCase [Char]
"landing at a point the cache does not retain is a no-op" Assertion
test_landUnretained
, [Char] -> Assertion -> TestTree
testCase [Char]
"landing a tx again counts it as held" Assertion
test_landAgain
, [Char] -> Assertion -> TestTree
testCase [Char]
"landing an unapplied tx keeps an applied one applied" Assertion
test_unappliedAfterApplied
]
, [Char] -> [TestTree] -> TestTree
testGroup
[Char]
"eviction"
[ [Char] -> Assertion -> TestTree
testCase [Char]
"over-cap insert evicts the oldest point and its txs" Assertion
test_evict
, [Char] -> Assertion -> TestTree
testCase [Char]
"an EB's tx survives the eviction of one of the EB's two points" Assertion
test_evictSharedBody
, [Char] -> Assertion -> TestTree
testCase [Char]
"a shared tx survives one referrer's eviction" Assertion
test_evictShared
, [Char] -> Assertion -> TestTree
testCase
[Char]
"a full cache evicts the oldest point before taking a body in, so the body sees none of that point's txs"
Assertion
test_evictBeforeInsert
, [Char] -> Assertion -> TestTree
testCase [Char]
"evictOlderThan drops points below the boundary, cascading txs" Assertion
test_evictOlderThan
, [Char] -> Assertion -> TestTree
testCase [Char]
"evictOlderThan keeps points at the boundary slot (exclusive)" Assertion
test_evictOlderThanExclusive
, [Char] -> Assertion -> TestTree
testCase [Char]
"evictOlderThan below every slot evicts nothing" Assertion
test_evictOlderThanNone
, [Char] -> Assertion -> TestTree
testCase [Char]
"evictOlderThan clears every point in a stale slot" Assertion
test_evictOlderThanWholeSlot
, [Char] -> Assertion -> TestTree
testCase
[Char]
"evictOlderThan visits each forgotten tx once, in the body of its youngest reference, oldest point first"
Assertion
test_evictOlderThanOrder
, [Char] -> Assertion -> TestTree
testCase [Char]
"insert below the pruned slot is ignored" Assertion
test_prunedIgnoresOlderInsert
, [Char] -> Assertion -> TestTree
testCase [Char]
"insert at the pruned slot is admitted (exclusive)" Assertion
test_prunedAllowsBoundaryInsert
, [Char] -> Assertion -> TestTree
testCase [Char]
"a lower evictOlderThan boundary does not lower the pruned slot" Assertion
test_prunedSlotMonotone
, [Char] -> Assertion -> TestTree
testCase [Char]
"a full cache ignores a body older than every point it retains" Assertion
test_insertOldest
]
, [Char] -> [TestTree] -> TestTree
testGroup
[Char]
"locations"
[ [Char] -> Assertion -> TestTree
testCase [Char]
"landing records where a retained point holds its txs" Assertion
test_land
, [Char] -> Assertion -> TestTree
testCase [Char]
"taking a body in reports its txs' locations" Assertion
test_bodyReportsLocations
, [Char] -> Assertion -> TestTree
testCase [Char]
"a tx keeps the location of the younger of two points it landed at" Assertion
test_youngerWins
, [Char] -> Assertion -> TestTree
testCase [Char]
"evicting the point a location names drops the location" Assertion
test_evictDropsLocation
, [Char] -> Assertion -> TestTree
testCase
[Char]
"evicting an older point keeps a tx's location at a younger point it landed at"
Assertion
test_evictKeepsYoungerLocation
, [Char] -> Assertion -> TestTree
testCase
[Char]
"landing at a point the cache ignored keeps the retained points' locations"
Assertion
test_landIgnoredPointKeepsLocations
, [Char] -> Assertion -> TestTree
testCase
[Char]
"a tx keeps its location at a retained point while older points land theirs and are evicted"
Assertion
test_olderLandingsKeepLocation
]
, (QuickCheckTests -> QuickCheckTests) -> TestTree -> TestTree
forall v. IsOption v => (v -> v) -> TestTree -> TestTree
adjustOption (\(QuickCheckTests Int
n) -> Int -> QuickCheckTests
QuickCheckTests (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10)) (TestTree -> TestTree) -> TestTree -> TestTree
forall a b. (a -> b) -> a -> b
$
[Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"the index matches a recomputed model, through eviction" Property
prop_model
]
type Idx = LeiosTxCacheIndex Int Int TestBody
newtype TestBody = TestBody [TxHash]
deriving (TestBody -> TestBody -> Bool
(TestBody -> TestBody -> Bool)
-> (TestBody -> TestBody -> Bool) -> Eq TestBody
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TestBody -> TestBody -> Bool
== :: TestBody -> TestBody -> Bool
$c/= :: TestBody -> TestBody -> Bool
/= :: TestBody -> TestBody -> Bool
Eq, Int -> TestBody -> ShowS
[TestBody] -> ShowS
TestBody -> [Char]
(Int -> TestBody -> ShowS)
-> (TestBody -> [Char]) -> ([TestBody] -> ShowS) -> Show TestBody
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TestBody -> ShowS
showsPrec :: Int -> TestBody -> ShowS
$cshow :: TestBody -> [Char]
show :: TestBody -> [Char]
$cshowList :: [TestBody] -> ShowS
showList :: [TestBody] -> ShowS
Show)
instance ReferencesTxsByHash TestBody where
foldTxReferences :: forall r.
(r -> Hash HASH EraIndependentTx -> BytesSize -> r)
-> r -> TestBody -> r
foldTxReferences r -> Hash HASH EraIndependentTx -> BytesSize -> r
f r
z (TestBody [Hash HASH EraIndependentTx]
hs) =
(r -> Hash HASH EraIndependentTx -> r)
-> r -> [Hash HASH EraIndependentTx] -> r
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\r
acc Hash HASH EraIndependentTx
txh -> r -> Hash HASH EraIndependentTx -> BytesSize -> r
f r
acc Hash HASH EraIndependentTx
txh BytesSize
dummySize) r
z [Hash HASH EraIndependentTx]
hs
where
dummySize :: BytesSize
dummySize = BytesSize
0
empty :: Idx
empty :: Idx
empty = Idx
forall a v b. LeiosTxCacheIndex a v b
emptyLeiosTxCacheIndex
txhOf :: Word8 -> TxHash
txhOf :: Word8 -> Hash HASH EraIndependentTx
txhOf Word8
w = ShortByteString -> Hash HASH EraIndependentTx
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (ShortByteString -> Hash HASH EraIndependentTx)
-> ShortByteString -> Hash HASH EraIndependentTx
forall a b. (a -> b) -> a -> b
$ [Word8] -> ShortByteString
SBS.pack (Word8
w Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate Int
31 Word8
0)
ebhOf :: Word8 -> EndorserBlockHash
ebhOf :: Word8 -> EndorserBlockHash
ebhOf Word8
w = ShortByteString -> EndorserBlockHash
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (ShortByteString -> EndorserBlockHash)
-> ShortByteString -> EndorserBlockHash
forall a b. (a -> b) -> a -> b
$ [Word8] -> ShortByteString
SBS.pack (Word8
w Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate Int
31 Word8
0)
pointAt :: Word64 -> Word8 -> LeiosPoint
pointAt :: Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (Word64 -> SlotNo
SlotNo Word64
s) (Word8 -> EndorserBlockHash
ebhOf Word8
e)
insert ::
Word64 ->
Word8 ->
[Word8] ->
Idx ->
(Idx, Maybe LeiosTxCacheInsertBodySummary, [(TxOffset, Maybe TxLocation)])
insert :: Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
s Word8
e [Word8]
ts Idx
idx =
let (Idx
idx', (Maybe LeiosTxCacheInsertBodySummary
summary, [(Int, Maybe TxLocation)]
located)) =
Int
-> LeiosPoint
-> TestBody
-> [(Int, Maybe TxLocation)]
-> ([(Int, Maybe TxLocation)]
-> Int
-> Hash HASH EraIndependentTx
-> BytesSize
-> Maybe TxLocation
-> [(Int, Maybe TxLocation)])
-> Idx
-> (Idx,
(Maybe LeiosTxCacheInsertBodySummary, [(Int, Maybe TxLocation)]))
forall b w a v.
ReferencesTxsByHash b =>
Int
-> LeiosPoint
-> b
-> w
-> (w
-> Int
-> Hash HASH EraIndependentTx
-> BytesSize
-> Maybe TxLocation
-> w)
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b,
(Maybe LeiosTxCacheInsertBodySummary, w))
insertBodyAndFoldl'
(Int
2 Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
defaultLeiosTxCacheShift)
(Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e)
([Hash HASH EraIndependentTx] -> TestBody
TestBody ((Word8 -> Hash HASH EraIndependentTx)
-> [Word8] -> [Hash HASH EraIndependentTx]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> Hash HASH EraIndependentTx
txhOf [Word8]
ts))
[]
(\[(Int, Maybe TxLocation)]
acc Int
off Hash HASH EraIndependentTx
_txh BytesSize
_sz Maybe TxLocation
loc -> (Int
off, Maybe TxLocation
loc) (Int, Maybe TxLocation)
-> [(Int, Maybe TxLocation)] -> [(Int, Maybe TxLocation)]
forall a. a -> [a] -> [a]
: [(Int, Maybe TxLocation)]
acc)
Idx
idx
in (Idx
idx', Maybe LeiosTxCacheInsertBodySummary
summary, [(Int, Maybe TxLocation)] -> [(Int, Maybe TxLocation)]
forall a. [a] -> [a]
reverse [(Int, Maybe TxLocation)]
located)
body :: Word64 -> Word8 -> [Word8] -> Idx -> Idx
body :: Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
s Word8
e [Word8]
ts Idx
idx = let (Idx
idx', Maybe LeiosTxCacheInsertBodySummary
_summary, [(Int, Maybe TxLocation)]
_located) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
s Word8
e [Word8]
ts Idx
idx in Idx
idx'
bodies :: Int -> Int -> Idx -> Idx
bodies :: Int -> Int -> Idx -> Idx
bodies Int
lo Int
hi Idx
idx0 =
(Idx -> Int -> Idx) -> Idx -> [Int] -> Idx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\Idx
idx Int
i -> Word64 -> Word8 -> [Word8] -> Idx -> Idx
body (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i) [] Idx
idx) Idx
idx0 [Int
lo .. Int
hi]
land :: Word64 -> Word8 -> [(TxOffset, Word8)] -> Idx -> Idx
land :: Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
s Word8
e [(Int, Word8)]
offTxs Idx
idx0 =
(Idx -> (Int, Word8) -> Idx) -> Idx -> [(Int, Word8)] -> Idx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl'
(\Idx
idx (Int
off, Word8
t) -> (Idx, TxArrivalPrior) -> Idx
forall a b. (a, b) -> a
fst (LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e) Int
off (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
t) (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
0) Idx
idx))
Idx
idx0
[(Int, Word8)]
offTxs
bodyLanded :: Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded :: Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
s Word8
e [Word8]
ts = Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
s Word8
e ([Int] -> [Word8] -> [(Int, Word8)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Word8]
ts) (Idx -> Idx) -> (Idx -> Idx) -> Idx -> Idx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
s Word8
e [Word8]
ts
evictBelow :: SlotNo -> Idx -> (Idx, Set LeiosPoint, [TxHash])
evictBelow :: SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow SlotNo
boundary Idx
idx =
let (Idx
idx', Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
evTxs) = SlotNo
-> [Hash HASH EraIndependentTx]
-> ([Hash HASH EraIndependentTx]
-> Hash HASH EraIndependentTx -> [Hash HASH EraIndependentTx])
-> Idx
-> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
forall b w a v.
ReferencesTxsByHash b =>
SlotNo
-> w
-> (w -> Hash HASH EraIndependentTx -> w)
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, Set LeiosPoint, w)
evictOlderThanFoldl' SlotNo
boundary [] ((Hash HASH EraIndependentTx
-> [Hash HASH EraIndependentTx] -> [Hash HASH EraIndependentTx])
-> [Hash HASH EraIndependentTx]
-> Hash HASH EraIndependentTx
-> [Hash HASH EraIndependentTx]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) Idx
idx
in (Idx
idx', Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx] -> [Hash HASH EraIndependentTx]
forall a. [a] -> [a]
reverse [Hash HASH EraIndependentTx]
evTxs)
retains :: Word64 -> Word8 -> Idx -> Bool
retains :: Word64 -> Word8 -> Idx -> Bool
retains Word64
s Word8
e Idx
idx = LeiosPoint -> Map LeiosPoint TestBody -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member (Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e) (Idx -> Map LeiosPoint TestBody
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState Idx
idx)
pointCount :: Idx -> Int
pointCount :: Idx -> Int
pointCount = Map LeiosPoint TestBody -> Int
forall k a. Map k a -> Int
Map.size (Map LeiosPoint TestBody -> Int)
-> (Idx -> Map LeiosPoint TestBody) -> Idx -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Idx -> Map LeiosPoint TestBody
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState
youngestRef :: Word8 -> Idx -> Maybe LeiosPoint
youngestRef :: Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
t Idx
idx = TrackedTx Int Int -> LeiosPoint
forall a v. TrackedTx a v -> LeiosPoint
ttYoungestReference (TrackedTx Int Int -> LeiosPoint)
-> Maybe (TrackedTx Int Int) -> Maybe LeiosPoint
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Hash HASH EraIndependentTx
-> Map (Hash HASH EraIndependentTx) (TrackedTx Int Int)
-> Maybe (TrackedTx Int Int)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
t) (Idx -> Map (Hash HASH EraIndependentTx) (TrackedTx Int Int)
forall a v b.
LeiosTxCacheIndex a v b
-> Map (Hash HASH EraIndependentTx) (TrackedTx a v)
txState Idx
idx)
locationOf :: Word8 -> Idx -> Maybe TxLocation
locationOf :: Word8 -> Idx -> Maybe TxLocation
locationOf Word8
t = Hash HASH EraIndependentTx -> Idx -> Maybe TxLocation
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> Maybe TxLocation
lookupTxLocation (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
t)
test_pointOne :: Assertion
test_pointOne :: Assertion
test_pointOne = Word64 -> Word8 -> Idx -> Bool
retains Word64
1 Word8
1 (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [] Idx
empty) Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True
test_pointDup :: Assertion
test_pointDup :: Assertion
test_pointDup = do
let idx :: Idx
idx = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty)
(Idx -> Int
pointCount Idx
idx, Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
10 Idx
idx) (Int, Maybe LeiosPoint) -> (Int, Maybe LeiosPoint) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Int
1, LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1))
test_pointTwo :: Assertion
test_pointTwo :: Assertion
test_pointTwo = do
let (Idx
idx1, Maybe LeiosTxCacheInsertBodySummary
summary1, [(Int, Maybe TxLocation)]
_) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
1 Word8
1 [Word8
10] Idx
empty
(Idx
idx2, Maybe LeiosTxCacheInsertBodySummary
summary2, [(Int, Maybe TxLocation)]
_) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
2 Word8
1 [Word8
10] (Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
1 Word8
1 [(Int
0, Word8
10)] Idx
idx1)
(Idx -> Int
pointCount Idx
idx2, Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
10 Idx
idx2, LeiosTxCacheInsertBodySummary -> Int
ibsAcquired (LeiosTxCacheInsertBodySummary -> Int)
-> Maybe LeiosTxCacheInsertBodySummary -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LeiosTxCacheInsertBodySummary
summary1, LeiosTxCacheInsertBodySummary -> Int
ibsAcquired (LeiosTxCacheInsertBodySummary -> Int)
-> Maybe LeiosTxCacheInsertBodySummary -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LeiosTxCacheInsertBodySummary
summary2)
(Int, Maybe LeiosPoint, Maybe Int, Maybe Int)
-> (Int, Maybe LeiosPoint, Maybe Int, Maybe Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Int
2, LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
1), Int -> Maybe Int
forall a. a -> Maybe a
Just Int
0, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1)
test_bodyRecordsNoTxs :: Assertion
test_bodyRecordsNoTxs :: Assertion
test_bodyRecordsNoTxs = Map (Hash HASH EraIndependentTx) (TrackedTx Int Int) -> Bool
forall k a. Map k a -> Bool
Map.null (Idx -> Map (Hash HASH EraIndependentTx) (TrackedTx Int Int)
forall a v b.
LeiosTxCacheIndex a v b
-> Map (Hash HASH EraIndependentTx) (TrackedTx a v)
txState (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
10, Word8
11] Idx
empty)) Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True
test_bodyBumpsHeldTxs :: Assertion
test_bodyBumpsHeldTxs :: Assertion
test_bodyBumpsHeldTxs = do
let idx :: Idx
idx = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
2 Word8
2 [Word8
10, Word8
11] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty)
(Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
10 Idx
idx, Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
11 Idx
idx) (Maybe LeiosPoint, Maybe LeiosPoint)
-> (Maybe LeiosPoint, Maybe LeiosPoint) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2), Maybe LeiosPoint
forall a. Maybe a
Nothing)
test_bodySummary :: Assertion
test_bodySummary :: Assertion
test_bodySummary = do
let base :: Idx
base = (Idx, TxArrivalPrior) -> Idx
forall a b. (a, b) -> a
fst (LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
1 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
11) (Int -> TxState Int Int
forall a v. v -> TxState a v
TxValidated Int
0) (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10, Word8
11] Idx
empty))
(Idx
_, Maybe LeiosTxCacheInsertBodySummary
summary, [(Int, Maybe TxLocation)]
_) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
2 Word8
2 [Word8
10, Word8
11, Word8
12] Idx
base
((\LeiosTxCacheInsertBodySummary
s -> (LeiosTxCacheInsertBodySummary -> Int
ibsTxsInEb LeiosTxCacheInsertBodySummary
s, LeiosTxCacheInsertBodySummary -> Int
ibsAcquired LeiosTxCacheInsertBodySummary
s, LeiosTxCacheInsertBodySummary -> Int
ibsValidated LeiosTxCacheInsertBodySummary
s, LeiosTxCacheInsertBodySummary -> Int
ibsCacheTxCount LeiosTxCacheInsertBodySummary
s)) (LeiosTxCacheInsertBodySummary -> (Int, Int, Int, Int))
-> Maybe LeiosTxCacheInsertBodySummary
-> Maybe (Int, Int, Int, Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LeiosTxCacheInsertBodySummary
summary)
Maybe (Int, Int, Int, Int)
-> Maybe (Int, Int, Int, Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Int, Int, Int, Int) -> Maybe (Int, Int, Int, Int)
forall a. a -> Maybe a
Just (Int
3, Int
2, Int
1, Int
2)
test_unapplied :: Assertion
test_unapplied :: Assertion
test_unapplied =
Hash HASH EraIndependentTx -> Idx -> StrictMaybe (TxState Int Int)
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx
(Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10)
((Idx, TxArrivalPrior) -> Idx
forall a b. (a, b) -> a
fst (LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
7) (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
10] Idx
empty)))
StrictMaybe (TxState Int Int)
-> StrictMaybe (TxState Int Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= TxState Int Int -> StrictMaybe (TxState Int Int)
forall a. a -> StrictMaybe a
SJust (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
7)
test_applied :: Assertion
test_applied :: Assertion
test_applied =
Hash HASH EraIndependentTx -> Idx -> StrictMaybe (TxState Int Int)
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx
(Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10)
((Idx, TxArrivalPrior) -> Idx
forall a b. (a, b) -> a
fst (LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) (Int -> TxState Int Int
forall a v. v -> TxState a v
TxValidated Int
9) (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
10] Idx
empty)))
StrictMaybe (TxState Int Int)
-> StrictMaybe (TxState Int Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= TxState Int Int -> StrictMaybe (TxState Int Int)
forall a. a -> StrictMaybe a
SJust (Int -> TxState Int Int
forall a v. v -> TxState a v
TxValidated Int
9)
test_landUnretained :: Assertion
test_landUnretained :: Assertion
test_landUnretained = do
let (Idx
idx, TxArrivalPrior
prior) = LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
7) Idx
empty
(TxArrivalPrior
prior, Hash HASH EraIndependentTx -> Idx -> StrictMaybe (TxState Int Int)
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) Idx
idx) (TxArrivalPrior, StrictMaybe (TxState Int Int))
-> (TxArrivalPrior, StrictMaybe (TxState Int Int)) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (TxArrivalPrior
TxEvicted, StrictMaybe (TxState Int Int)
forall a. StrictMaybe a
SNothing)
test_landAgain :: Assertion
test_landAgain :: Assertion
test_landAgain = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
2 Word8
2 [Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty)
(Idx
idx, TxArrivalPrior
prior) = LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2) Int
0 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
7) Idx
base
(TxArrivalPrior
prior, Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
10 Idx
idx, Hash HASH EraIndependentTx -> Idx -> StrictMaybe (TxState Int Int)
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) Idx
idx)
(TxArrivalPrior, Maybe LeiosPoint, StrictMaybe (TxState Int Int))
-> (TxArrivalPrior, Maybe LeiosPoint,
StrictMaybe (TxState Int Int))
-> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (TxArrivalPrior
TxWasHeld, LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2), TxState Int Int -> StrictMaybe (TxState Int Int)
forall a. a -> StrictMaybe a
SJust (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
7))
test_unappliedAfterApplied :: Assertion
test_unappliedAfterApplied :: Assertion
test_unappliedAfterApplied = do
let applied :: Idx
applied = (Idx, TxArrivalPrior) -> Idx
forall a b. (a, b) -> a
fst (LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) (Int -> TxState Int Int
forall a v. v -> TxState a v
TxValidated Int
9) (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
10] Idx
empty))
(Idx
idx, TxArrivalPrior
prior) = LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0 (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) (Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted Int
7) Idx
applied
(TxArrivalPrior
prior, Hash HASH EraIndependentTx -> Idx -> StrictMaybe (TxState Int Int)
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10) Idx
idx) (TxArrivalPrior, StrictMaybe (TxState Int Int))
-> (TxArrivalPrior, StrictMaybe (TxState Int Int)) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (TxArrivalPrior
TxWasHeld, TxState Int Int -> StrictMaybe (TxState Int Int)
forall a. a -> StrictMaybe a
SJust (Int -> TxState Int Int
forall a v. v -> TxState a v
TxValidated Int
9))
test_evict :: Assertion
test_evict :: Assertion
test_evict = do
let base :: Idx
base = Int -> Int -> Idx -> Idx
bodies Int
2 Int
maxLeiosPointCount (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
200] Idx
empty)
idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
129 Word8
129 [] Idx
base
(Idx -> Int
pointCount Idx
idx', Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
200 Idx
idx')
(Int, Maybe LeiosPoint) -> (Int, Maybe LeiosPoint) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Int
maxLeiosPointCount, Maybe LeiosPoint
forall a. Maybe a
Nothing)
test_evictSharedBody :: Assertion
test_evictSharedBody :: Assertion
test_evictSharedBody = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
128 Word8
1 [Word8
200] (Int -> Int -> Idx -> Idx
bodies Int
2 Int
127 (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
200] Idx
empty))
idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
129 Word8
129 [] Idx
base
(Word64 -> Word8 -> Idx -> Bool
retains Word64
1 Word8
1 Idx
idx', Word64 -> Word8 -> Idx -> Bool
retains Word64
128 Word8
1 Idx
idx', Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
200 Idx
idx')
(Bool, Bool, Maybe LeiosPoint)
-> (Bool, Bool, Maybe LeiosPoint) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
False, Bool
True, LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
128 Word8
1))
test_evictShared :: Assertion
test_evictShared :: Assertion
test_evictShared = do
let base :: Idx
base = Int -> Int -> Idx -> Idx
bodies Int
3 Int
maxLeiosPointCount (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
2 Word8
2 [Word8
200] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
200] Idx
empty))
idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
129 Word8
129 [] Idx
base
(Word64 -> Word8 -> Idx -> Bool
retains Word64
1 Word8
1 Idx
idx', Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
200 Idx
idx') (Bool, Maybe LeiosPoint) -> (Bool, Maybe LeiosPoint) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
False, LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2))
test_evictBeforeInsert :: Assertion
test_evictBeforeInsert :: Assertion
test_evictBeforeInsert = do
let base :: Idx
base = Int -> Int -> Idx -> Idx
bodies Int
2 Int
maxLeiosPointCount (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
200] Idx
empty)
(Idx
idx', Maybe LeiosTxCacheInsertBodySummary
summary, [(Int, Maybe TxLocation)]
located) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
129 Word8
129 [Word8
200] Idx
base
(Word64 -> Word8 -> Idx -> Bool
retains Word64
1 Word8
1 Idx
idx', Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
200 Idx
idx', LeiosTxCacheInsertBodySummary -> Int
ibsAcquired (LeiosTxCacheInsertBodySummary -> Int)
-> Maybe LeiosTxCacheInsertBodySummary -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LeiosTxCacheInsertBodySummary
summary, [(Int, Maybe TxLocation)]
located)
(Bool, Maybe LeiosPoint, Maybe Int, [(Int, Maybe TxLocation)])
-> (Bool, Maybe LeiosPoint, Maybe Int, [(Int, Maybe TxLocation)])
-> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
False, Maybe LeiosPoint
forall a. Maybe a
Nothing, Int -> Maybe Int
forall a. a -> Maybe a
Just Int
0, [(Int
0, Maybe TxLocation
forall a. Maybe a
Nothing)])
test_evictOlderThan :: Assertion
test_evictOlderThan :: Assertion
test_evictOlderThan = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
3 Word8
3 [Word8
30] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
2 Word8
2 [Word8
20] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty))
(Idx
idx', Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
evTxs) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
3) Idx
base
( Set LeiosPoint
evPoints
, [Hash HASH EraIndependentTx]
evTxs
, Word64 -> Word8 -> Idx -> Bool
retains Word64
3 Word8
3 Idx
idx'
, Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
10 Idx
idx'
, Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
30 Idx
idx'
)
(Set LeiosPoint, [Hash HASH EraIndependentTx], Bool,
Maybe LeiosPoint, Maybe LeiosPoint)
-> (Set LeiosPoint, [Hash HASH EraIndependentTx], Bool,
Maybe LeiosPoint, Maybe LeiosPoint)
-> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= ( [LeiosPoint] -> Set LeiosPoint
forall a. Ord a => [a] -> Set a
Set.fromList [Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1, Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2]
, [Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10, Word8 -> Hash HASH EraIndependentTx
txhOf Word8
20]
, Bool
True
, Maybe LeiosPoint
forall a. Maybe a
Nothing
, LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
3 Word8
3)
)
test_evictOlderThanExclusive :: Assertion
test_evictOlderThanExclusive :: Assertion
test_evictOlderThanExclusive = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
3 Word8
3 [Word8
30] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
2 Word8
2 [Word8
20] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty))
(Idx
_, Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
evTxs) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
2) Idx
base
(Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
evTxs) (Set LeiosPoint, [Hash HASH EraIndependentTx])
-> (Set LeiosPoint, [Hash HASH EraIndependentTx]) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosPoint -> Set LeiosPoint
forall a. a -> Set a
Set.singleton (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1), [Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10])
test_evictOlderThanNone :: Assertion
test_evictOlderThanNone :: Assertion
test_evictOlderThanNone = do
let (Idx
idx', Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
evTxs) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
1) (Int -> Int -> Idx -> Idx
bodies Int
1 Int
3 Idx
empty)
(Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
evTxs, Idx -> Int
pointCount Idx
idx') (Set LeiosPoint, [Hash HASH EraIndependentTx], Int)
-> (Set LeiosPoint, [Hash HASH EraIndependentTx], Int) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Set LeiosPoint
forall a. Set a
Set.empty, [], Int
3)
test_evictOlderThanWholeSlot :: Assertion
test_evictOlderThanWholeSlot :: Assertion
test_evictOlderThanWholeSlot = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
2 Word8
3 [] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
2 [] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [] Idx
empty))
(Idx
idx', Set LeiosPoint
evPoints, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
2) Idx
base
(Set LeiosPoint
evPoints, Word64 -> Word8 -> Idx -> Bool
retains Word64
2 Word8
3 Idx
idx') (Set LeiosPoint, Bool) -> (Set LeiosPoint, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= ([LeiosPoint] -> Set LeiosPoint
forall a. Ord a => [a] -> Set a
Set.fromList [Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1, Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
2], Bool
True)
test_evictOlderThanOrder :: Assertion
test_evictOlderThanOrder :: Assertion
test_evictOlderThanOrder = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
2 Word8
2 [Word8
12, Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10, Word8
11] Idx
empty)
(Idx
_, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
evTxs) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
3) Idx
base
[Hash HASH EraIndependentTx]
evTxs [Hash HASH EraIndependentTx]
-> [Hash HASH EraIndependentTx] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Word8 -> Hash HASH EraIndependentTx
txhOf Word8
11, Word8 -> Hash HASH EraIndependentTx
txhOf Word8
12, Word8 -> Hash HASH EraIndependentTx
txhOf Word8
10]
test_prunedIgnoresOlderInsert :: Assertion
test_prunedIgnoresOlderInsert :: Assertion
test_prunedIgnoresOlderInsert = do
let (Idx
pruned, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
5) (Int -> Int -> Idx -> Idx
bodies Int
1 Int
10 Idx
empty)
idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
4 Word8
40 [] Idx
pruned
(Idx -> Int
pointCount Idx
idx', Word64 -> Word8 -> Idx -> Bool
retains Word64
4 Word8
40 Idx
idx') (Int, Bool) -> (Int, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Idx -> Int
pointCount Idx
pruned, Bool
False)
test_prunedAllowsBoundaryInsert :: Assertion
test_prunedAllowsBoundaryInsert :: Assertion
test_prunedAllowsBoundaryInsert = do
let (Idx
pruned, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
5) (Int -> Int -> Idx -> Idx
bodies Int
1 Int
10 Idx
empty)
Word64 -> Word8 -> Idx -> Bool
retains Word64
5 Word8
55 (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
5 Word8
55 [] Idx
pruned) Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True
test_prunedSlotMonotone :: Assertion
test_prunedSlotMonotone :: Assertion
test_prunedSlotMonotone = do
let (Idx
p1, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
5) (Int -> Int -> Idx -> Idx
bodies Int
1 Int
10 Idx
empty)
(Idx
p2, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
3) Idx
p1
idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
4 Word8
40 [] Idx
p2
(Idx -> Int
pointCount Idx
idx', Word64 -> Word8 -> Idx -> Bool
retains Word64
4 Word8
40 Idx
idx') (Int, Bool) -> (Int, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Idx -> Int
pointCount Idx
p2, Bool
False)
test_insertOldest :: Assertion
test_insertOldest :: Assertion
test_insertOldest = do
let full :: Idx
full = Int -> Int -> Idx -> Idx
bodies Int
2 (Int
maxLeiosPointCount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Idx
empty
(Idx
idx', Maybe LeiosTxCacheInsertBodySummary
summary, [(Int, Maybe TxLocation)]
_) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
1 Word8
250 [Word8
250] Idx
full
(Word64 -> Word8 -> Idx -> Bool
retains Word64
1 Word8
250 Idx
idx', Word64 -> Word8 -> Idx -> Bool
retains Word64
2 Word8
2 Idx
idx', Maybe LeiosTxCacheInsertBodySummary
summary) (Bool, Bool, Maybe LeiosTxCacheInsertBodySummary)
-> (Bool, Bool, Maybe LeiosTxCacheInsertBodySummary) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
False, Bool
True, Maybe LeiosTxCacheInsertBodySummary
forall a. Maybe a
Nothing)
test_land :: Assertion
test_land :: Assertion
test_land = do
let idx :: Idx
idx = Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10, Word8
11] Idx
empty
(Word8 -> Idx -> Maybe TxLocation
locationOf Word8
10 Idx
idx, Word8 -> Idx -> Maybe TxLocation
locationOf Word8
11 Idx
idx)
(Maybe TxLocation, Maybe TxLocation)
-> (Maybe TxLocation, Maybe TxLocation) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0), TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
1))
test_bodyReportsLocations :: Assertion
test_bodyReportsLocations :: Assertion
test_bodyReportsLocations = do
let (Idx
_, Maybe LeiosTxCacheInsertBodySummary
_, [(Int, Maybe TxLocation)]
located) = Word64
-> Word8
-> [Word8]
-> Idx
-> (Idx, Maybe LeiosTxCacheInsertBodySummary,
[(Int, Maybe TxLocation)])
insert Word64
2 Word8
2 [Word8
11, Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty)
[(Int, Maybe TxLocation)]
located [(Int, Maybe TxLocation)] -> [(Int, Maybe TxLocation)] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [(Int
0, Maybe TxLocation
forall a. Maybe a
Nothing), (Int
1, TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
1 Word8
1) Int
0))]
test_youngerWins :: Assertion
test_youngerWins :: Assertion
test_youngerWins = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
5 Word8
5 [Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
11, Word8
10] Idx
empty)
youngerFirst :: Idx
youngerFirst = Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
1 Word8
1 [(Int
1, Word8
10)] (Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
5 Word8
5 [(Int
0, Word8
10)] Idx
base)
olderFirst :: Idx
olderFirst = Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
5 Word8
5 [(Int
0, Word8
10)] (Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
1 Word8
1 [(Int
1, Word8
10)] Idx
base)
(Word8 -> Idx -> Maybe TxLocation
locationOf Word8
10 Idx
youngerFirst, Word8 -> Idx -> Maybe TxLocation
locationOf Word8
10 Idx
olderFirst)
(Maybe TxLocation, Maybe TxLocation)
-> (Maybe TxLocation, Maybe TxLocation) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
5 Word8
5) Int
0), TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
5 Word8
5) Int
0))
test_evictDropsLocation :: Assertion
test_evictDropsLocation :: Assertion
test_evictDropsLocation = do
let base :: Idx
base = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
2 Word8
2 [Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
1 [Word8
10] Idx
empty)
(Idx
idx, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
2) Idx
base
(Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
10 Idx
idx, Word8 -> Idx -> Maybe TxLocation
locationOf Word8
10 Idx
idx) (Maybe LeiosPoint, Maybe TxLocation)
-> (Maybe LeiosPoint, Maybe TxLocation) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2), Maybe TxLocation
forall a. Maybe a
Nothing)
test_evictKeepsYoungerLocation :: Assertion
test_evictKeepsYoungerLocation :: Assertion
test_evictKeepsYoungerLocation = do
let base :: Idx
base = Word64 -> Word8 -> [(Int, Word8)] -> Idx -> Idx
land Word64
1 Word8
1 [(Int
0, Word8
10)] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
5 Word8
5 [Word8
10] (Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
1 Word8
1 [Word8
10] Idx
empty))
(Idx
idx, Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
2) Idx
base
Word8 -> Idx -> Maybe TxLocation
locationOf Word8
10 Idx
idx Maybe TxLocation -> Maybe TxLocation -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
5 Word8
5) Int
0)
test_landIgnoredPointKeepsLocations :: Assertion
test_landIgnoredPointKeepsLocations :: Assertion
test_landIgnoredPointKeepsLocations = do
let points :: [Word8]
points = [Word8
2 .. Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxLeiosPointCount Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
1] :: [Word8]
full :: Idx
full = (Idx -> Word8 -> Idx) -> Idx -> [Word8] -> Idx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\Idx
idx Word8
e -> Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
e) Word8
e [Word8
e] Idx
idx) Idx
empty [Word8]
points
idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded Word64
1 Word8
250 [Word8
250] Idx
full
(Word8 -> Idx -> Maybe TxLocation
locationOf Word8
2 Idx
idx', Word8 -> Idx -> Maybe TxLocation
locationOf Word8
250 Idx
idx')
(Maybe TxLocation, Maybe TxLocation)
-> (Maybe TxLocation, Maybe TxLocation) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
2 Word8
2) Int
0), Maybe TxLocation
forall a. Maybe a
Nothing)
test_olderLandingsKeepLocation :: Assertion
test_olderLandingsKeepLocation :: Assertion
test_olderLandingsKeepLocation = do
let step :: Idx -> Word8 -> Idx
step Idx
idx Word8
e = Word64 -> Word8 -> [Word8] -> Idx -> Idx
bodyLanded (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
e) Word8
e [Word8
e] Idx
idx
youngest :: Word8
youngest = Word8
200
idx' :: Idx
idx' = (Idx -> Word8 -> Idx) -> Idx -> [Word8] -> Idx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' Idx -> Word8 -> Idx
step Idx
empty (Word8
youngest Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: [Word8
1 .. Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
maxLeiosPointCount])
(Word64 -> Word8 -> Idx -> Bool
retains Word64
200 Word8
youngest Idx
idx', Word8 -> Idx -> Maybe TxLocation
locationOf Word8
youngest Idx
idx')
(Bool, Maybe TxLocation) -> (Bool, Maybe TxLocation) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
True, TxLocation -> Maybe TxLocation
forall a. a -> Maybe a
Just (LeiosPoint -> Int -> TxLocation
MkTxLocation (Word64 -> Word8 -> LeiosPoint
pointAt Word64
200 Word8
youngest) Int
0))
data Op
=
OpBody Word64 Word8
|
OpLand Word64 Word8 [(TxOffset, TxState Int Int)]
| OpEvict Word64
deriving Int -> Op -> ShowS
[Op] -> ShowS
Op -> [Char]
(Int -> Op -> ShowS)
-> (Op -> [Char]) -> ([Op] -> ShowS) -> Show Op
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Op -> ShowS
showsPrec :: Int -> Op -> ShowS
$cshow :: Op -> [Char]
show :: Op -> [Char]
$cshowList :: [Op] -> ShowS
showList :: [Op] -> ShowS
Show
genW :: Num a => Int -> Int -> Gen a
genW :: forall a. Num a => Int -> Int -> Gen a
genW Int
lo Int
hi = Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> a) -> Gen Int -> Gen a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
chooseInt (Int
lo, Int
hi)
genOps :: Gen [Op]
genOps :: Gen [Op]
genOps = do
bs <- Int -> Gen (Word64, Word8) -> Gen [(Word64, Word8)]
forall a. Int -> Gen a -> Gen [a]
vectorOf Int
200 ((,) (Word64 -> Word8 -> (Word64, Word8))
-> Gen Word64 -> Gen (Word8 -> (Word64, Word8))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> Gen Word64
forall a. Num a => Int -> Int -> Gen a
genW Int
1 Int
300 Gen (Word8 -> (Word64, Word8)) -> Gen Word8 -> Gen (Word64, Word8)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Int -> Gen Word8
forall a. Num a => Int -> Int -> Gen a
genW Int
1 Int
20)
lands <- listOf $ do
(s, e) <- oneof [elements bs, (,) <$> genW 1 300 <*> genW 1 20]
offs <- elements [[0], [1], [0, 1]]
OpLand s e
<$> traverse (\Int
off -> (,) Int
off (TxState Int Int -> (Int, TxState Int Int))
-> Gen (TxState Int Int) -> Gen (Int, TxState Int Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Gen (TxState Int Int)] -> Gen (TxState Int Int)
forall a. HasCallStack => [Gen a] -> Gen a
oneof [Int -> TxState Int Int
forall a v. a -> TxState a v
TxInserted (Int -> TxState Int Int) -> Gen Int -> Gen (TxState Int Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> Gen Int
forall a. Num a => Int -> Int -> Gen a
genW Int
0 Int
3, Int -> TxState Int Int
forall a v. v -> TxState a v
TxValidated (Int -> TxState Int Int) -> Gen Int -> Gen (TxState Int Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> Gen Int
forall a. Num a => Int -> Int -> Gen a
genW Int
0 Int
3]) offs
nEvicts <- chooseInt (0, 3)
evicts <- vectorOf nEvicts (OpEvict <$> genW 1 300)
shuffle (map (uncurry OpBody) bs ++ lands ++ evicts)
data ModelTx = MkModelTx
{ ModelTx -> Set LeiosPoint
mRefs :: Set LeiosPoint
, ModelTx -> TxState Int Int
mPayload :: TxState Int Int
, ModelTx -> Set (LeiosPoint, Int)
mLandings :: Set (LeiosPoint, TxOffset)
}
type Model = Map Word8 ModelTx
prop_model :: Property
prop_model :: Property
prop_model = Gen [Op] -> ([Op] -> [[Op]]) -> ([Op] -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> [a]) -> (a -> prop) -> Property
forAllShrink Gen [Op]
genOps ((Op -> [Op]) -> [Op] -> [[Op]]
forall a. (a -> [a]) -> [a] -> [[a]]
shrinkList ([Op] -> Op -> [Op]
forall a b. a -> b -> a
const [])) (([Op] -> Property) -> Property) -> ([Op] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[Op]
ops ->
[Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
[ [Char] -> Property -> Property
forall prop. Testable prop => [Char] -> prop -> Property
counterexample ([Char]
"after " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> Op -> [Char]
forall a. Show a => a -> [Char]
show Op
op) (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
[Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
[ [Char] -> Bool -> Property
forall prop. Testable prop => [Char] -> prop -> Property
counterexample [Char]
"cap exceeded" (Bool -> Property) -> Bool -> Property
forall a b. (a -> b) -> a -> b
$ Idx -> Int
pointCount Idx
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxLeiosPointCount
, Map (Hash HASH EraIndependentTx) (TrackedTx Int Int)
-> Set (Hash HASH EraIndependentTx)
forall k a. Map k a -> Set k
Map.keysSet (Idx -> Map (Hash HASH EraIndependentTx) (TrackedTx Int Int)
forall a v b.
LeiosTxCacheIndex a v b
-> Map (Hash HASH EraIndependentTx) (TrackedTx a v)
txState Idx
idx) Set (Hash HASH EraIndependentTx)
-> Set (Hash HASH EraIndependentTx) -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== (Word8 -> Hash HASH EraIndependentTx)
-> Set Word8 -> Set (Hash HASH EraIndependentTx)
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map Word8 -> Hash HASH EraIndependentTx
txhOf (Map Word8 ModelTx -> Set Word8
forall k a. Map k a -> Set k
Map.keysSet Map Word8 ModelTx
model)
, [Idx
-> Word8
-> (Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)
observe Idx
idx Word8
t | Word8
t <- [Word8]
txs] [(Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)]
-> [(Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)]
-> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [Map Word8 ModelTx
-> Word8
-> (Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)
forall {k}.
Ord k =>
Map k ModelTx
-> k
-> (Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)
expected Map Word8 ModelTx
model Word8
t | Word8
t <- [Word8]
txs]
]
| (Op
op, (Idx
idx, Map Word8 ModelTx
model)) <- [Op]
-> [(Idx, Map Word8 ModelTx)] -> [(Op, (Idx, Map Word8 ModelTx))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Op]
ops (Int -> [(Idx, Map Word8 ModelTx)] -> [(Idx, Map Word8 ModelTx)]
forall a. Int -> [a] -> [a]
drop Int
1 (((Idx, Map Word8 ModelTx) -> Op -> (Idx, Map Word8 ModelTx))
-> (Idx, Map Word8 ModelTx) -> [Op] -> [(Idx, Map Word8 ModelTx)]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl (Idx, Map Word8 ModelTx) -> Op -> (Idx, Map Word8 ModelTx)
step (Idx
empty, Map Word8 ModelTx
forall k a. Map k a
Map.empty) [Op]
ops))
]
where
txs :: [Word8]
txs = [Word8
1 .. Word8
21]
observe :: Idx
-> Word8
-> (Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)
observe Idx
idx Word8
t = (Word8 -> Idx -> Maybe LeiosPoint
youngestRef Word8
t Idx
idx, Hash HASH EraIndependentTx -> Idx -> StrictMaybe (TxState Int Int)
forall a v b.
Hash HASH EraIndependentTx
-> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx (Word8 -> Hash HASH EraIndependentTx
txhOf Word8
t) Idx
idx, Word8 -> Idx -> Maybe TxLocation
locationOf Word8
t Idx
idx)
expected :: Map k ModelTx
-> k
-> (Maybe LeiosPoint, StrictMaybe (TxState Int Int),
Maybe TxLocation)
expected Map k ModelTx
model k
t = case k -> Map k ModelTx -> Maybe ModelTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
t Map k ModelTx
model of
Maybe ModelTx
Nothing -> (Maybe LeiosPoint
forall a. Maybe a
Nothing, StrictMaybe (TxState Int Int)
forall a. StrictMaybe a
SNothing, Maybe TxLocation
forall a. Maybe a
Nothing)
Just ModelTx
mtx ->
( Set LeiosPoint -> Maybe LeiosPoint
forall a. Set a -> Maybe a
Set.lookupMax (ModelTx -> Set LeiosPoint
mRefs ModelTx
mtx)
, TxState Int Int -> StrictMaybe (TxState Int Int)
forall a. a -> StrictMaybe a
SJust (ModelTx -> TxState Int Int
mPayload ModelTx
mtx)
, (LeiosPoint -> Int -> TxLocation)
-> (LeiosPoint, Int) -> TxLocation
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry LeiosPoint -> Int -> TxLocation
MkTxLocation ((LeiosPoint, Int) -> TxLocation)
-> Maybe (LeiosPoint, Int) -> Maybe TxLocation
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set (LeiosPoint, Int) -> Maybe (LeiosPoint, Int)
forall a. Set a -> Maybe a
Set.lookupMax (ModelTx -> Set (LeiosPoint, Int)
mLandings ModelTx
mtx)
)
step :: (Idx, Model) -> Op -> (Idx, Model)
step :: (Idx, Map Word8 ModelTx) -> Op -> (Idx, Map Word8 ModelTx)
step (Idx
idx, Map Word8 ModelTx
model) = \case
OpBody Word64
s Word8
e ->
let idx' :: Idx
idx' = Word64 -> Word8 -> [Word8] -> Idx -> Idx
body Word64
s Word8
e [Word8
e, Word8
e Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
1] Idx
idx
pruned :: Map Word8 ModelTx
pruned = Idx -> Idx -> Map Word8 ModelTx -> Map Word8 ModelTx
forall {a} {v} {a} {a} {v} {a} {k}.
LeiosTxCacheIndex a v a
-> LeiosTxCacheIndex a v a -> Map k ModelTx -> Map k ModelTx
prune Idx
idx Idx
idx' Map Word8 ModelTx
model
inserted :: Bool
inserted = LeiosPoint -> Map LeiosPoint TestBody -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.notMember (Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e) (Idx -> Map LeiosPoint TestBody
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState Idx
idx) Bool -> Bool -> Bool
&& Word64 -> Word8 -> Idx -> Bool
retains Word64
s Word8
e Idx
idx'
refer :: ModelTx -> ModelTx
refer ModelTx
mtx = ModelTx
mtx{mRefs = Set.insert (pointAt s e) (mRefs mtx)}
in ( Idx
idx'
, if Bool
inserted then (Map Word8 ModelTx -> Word8 -> Map Word8 ModelTx)
-> Map Word8 ModelTx -> [Word8] -> Map Word8 ModelTx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' ((Word8 -> Map Word8 ModelTx -> Map Word8 ModelTx)
-> Map Word8 ModelTx -> Word8 -> Map Word8 ModelTx
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((ModelTx -> ModelTx)
-> Word8 -> Map Word8 ModelTx -> Map Word8 ModelTx
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust ModelTx -> ModelTx
refer)) Map Word8 ModelTx
pruned [Word8
e, Word8
e Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
1] else Map Word8 ModelTx
pruned
)
OpLand Word64
s Word8
e [(Int, TxState Int Int)]
offPayloads
| Word64 -> Word8 -> Idx -> Bool
retains Word64
s Word8
e Idx
idx ->
( (Idx -> (Int, TxState Int Int) -> Idx)
-> Idx -> [(Int, TxState Int Int)] -> Idx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl'
(\Idx
i (Int
off, TxState Int Int
payload) -> (Idx, TxArrivalPrior) -> Idx
forall a b. (a, b) -> a
fst (LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState Int Int
-> Idx
-> (Idx, TxArrivalPrior)
forall a v b.
LeiosPoint
-> Int
-> Hash HASH EraIndependentTx
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx (Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e) Int
off (Word8 -> Hash HASH EraIndependentTx
txhOf (Word8
e Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
off)) TxState Int Int
payload Idx
i))
Idx
idx
[(Int, TxState Int Int)]
offPayloads
, (Map Word8 ModelTx -> (Int, TxState Int Int) -> Map Word8 ModelTx)
-> Map Word8 ModelTx
-> [(Int, TxState Int Int)]
-> Map Word8 ModelTx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl'
(\Map Word8 ModelTx
m (Int
off, TxState Int Int
payload) -> (Maybe ModelTx -> Maybe ModelTx)
-> Word8 -> Map Word8 ModelTx -> Map Word8 ModelTx
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter (ModelTx -> Maybe ModelTx
forall a. a -> Maybe a
Just (ModelTx -> Maybe ModelTx)
-> (Maybe ModelTx -> ModelTx) -> Maybe ModelTx -> Maybe ModelTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> TxState Int Int -> Maybe ModelTx -> ModelTx
landed Int
off TxState Int Int
payload) (Word8
e Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
off) Map Word8 ModelTx
m)
Map Word8 ModelTx
model
[(Int, TxState Int Int)]
offPayloads
)
| Bool
otherwise -> (Idx
idx, Map Word8 ModelTx
model)
where
p :: LeiosPoint
p = Word64 -> Word8 -> LeiosPoint
pointAt Word64
s Word8
e
landed :: Int -> TxState Int Int -> Maybe ModelTx -> ModelTx
landed Int
off TxState Int Int
payload = \case
Maybe ModelTx
Nothing -> Set LeiosPoint
-> TxState Int Int -> Set (LeiosPoint, Int) -> ModelTx
MkModelTx (LeiosPoint -> Set LeiosPoint
forall a. a -> Set a
Set.singleton LeiosPoint
p) TxState Int Int
payload ((LeiosPoint, Int) -> Set (LeiosPoint, Int)
forall a. a -> Set a
Set.singleton (LeiosPoint
p, Int
off))
Just ModelTx
mtx ->
MkModelTx
{ mRefs :: Set LeiosPoint
mRefs = LeiosPoint -> Set LeiosPoint -> Set LeiosPoint
forall a. Ord a => a -> Set a -> Set a
Set.insert LeiosPoint
p (ModelTx -> Set LeiosPoint
mRefs ModelTx
mtx)
, mPayload :: TxState Int Int
mPayload = case (TxState Int Int
payload, ModelTx -> TxState Int Int
mPayload ModelTx
mtx) of
(TxInserted Int
_, held :: TxState Int Int
held@TxValidated{}) -> TxState Int Int
held
(TxState Int Int, TxState Int Int)
_ -> TxState Int Int
payload
, mLandings :: Set (LeiosPoint, Int)
mLandings = (LeiosPoint, Int) -> Set (LeiosPoint, Int) -> Set (LeiosPoint, Int)
forall a. Ord a => a -> Set a -> Set a
Set.insert (LeiosPoint
p, Int
off) (ModelTx -> Set (LeiosPoint, Int)
mLandings ModelTx
mtx)
}
OpEvict Word64
boundary ->
let (Idx
idx', Set LeiosPoint
_, [Hash HASH EraIndependentTx]
_) = SlotNo
-> Idx -> (Idx, Set LeiosPoint, [Hash HASH EraIndependentTx])
evictBelow (Word64 -> SlotNo
SlotNo Word64
boundary) Idx
idx in (Idx
idx', Idx -> Idx -> Map Word8 ModelTx -> Map Word8 ModelTx
forall {a} {v} {a} {a} {v} {a} {k}.
LeiosTxCacheIndex a v a
-> LeiosTxCacheIndex a v a -> Map k ModelTx -> Map k ModelTx
prune Idx
idx Idx
idx' Map Word8 ModelTx
model)
prune :: LeiosTxCacheIndex a v a
-> LeiosTxCacheIndex a v a -> Map k ModelTx -> Map k ModelTx
prune LeiosTxCacheIndex a v a
idx LeiosTxCacheIndex a v a
idx' =
(ModelTx -> Maybe ModelTx) -> Map k ModelTx -> Map k ModelTx
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybe ((ModelTx -> Maybe ModelTx) -> Map k ModelTx -> Map k ModelTx)
-> (ModelTx -> Maybe ModelTx) -> Map k ModelTx -> Map k ModelTx
forall a b. (a -> b) -> a -> b
$ \ModelTx
mtx ->
let gone :: Set LeiosPoint
gone = Map LeiosPoint a -> Set LeiosPoint
forall k a. Map k a -> Set k
Map.keysSet (LeiosTxCacheIndex a v a -> Map LeiosPoint a
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v a
idx) Set LeiosPoint -> Set LeiosPoint -> Set LeiosPoint
forall a. Ord a => Set a -> Set a -> Set a
`Set.difference` Map LeiosPoint a -> Set LeiosPoint
forall k a. Map k a -> Set k
Map.keysSet (LeiosTxCacheIndex a v a -> Map LeiosPoint a
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v a
idx')
refs :: Set LeiosPoint
refs = ModelTx -> Set LeiosPoint
mRefs ModelTx
mtx Set LeiosPoint -> Set LeiosPoint -> Set LeiosPoint
forall a. Ord a => Set a -> Set a -> Set a
`Set.difference` Set LeiosPoint
gone
in if Set LeiosPoint -> Bool
forall a. Set a -> Bool
Set.null Set LeiosPoint
refs
then Maybe ModelTx
forall a. Maybe a
Nothing
else
ModelTx -> Maybe ModelTx
forall a. a -> Maybe a
Just
ModelTx
mtx
{ mRefs = refs
, mLandings = Set.filter (\(LeiosPoint
q, Int
_off) -> LeiosPoint -> Set LeiosPoint -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.notMember LeiosPoint
q Set LeiosPoint
gone) (mLandings mtx)
}