{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}

-- | Tests for the "Ouroboros.Consensus.Storage.LeiosDB" interface.
--
-- These tests verify the semantics of the LeiosDbHandle operations for both
-- InMemory and SQLite implementations. Each test case uses a fresh database
-- to ensure isolation and serve as an (too optimistic) performance baseline.
module Test.Ouroboros.Storage.LeiosDB (tests) where

import Cardano.Crypto.Hash (Hash (UnsafeHash), hashToStringAsHex)
import Cardano.Slotting.Slot (SlotNo (..))
import Control.Concurrent.Class.MonadSTM.Strict (atomically, newTVarIO, writeTVar)
import Control.DeepSeq (force)
import Control.Exception (bracket, try)
import Control.Monad (forM, forM_, replicateM, void)
import Control.Monad.Class.MonadTime.SI (diffTime, getMonotonicTime)
import Control.Tracer (nullTracer)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as SBS
import Data.Function ((&))
import Data.List (nub)
import qualified Data.Map.Strict as Map
import Data.Maybe (isJust)
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Time.Clock (DiffTime)
import qualified Database.SQLite3 as SQL
import Ouroboros.Consensus.Leios.Types
  ( BytesSize
  , EndorserBlock
  , EndorserBlockHash
  , LeiosPoint (..)
  , LeiosTx (..)
  , Serialised
  , TxHash
  , TxLocation (..)
  , TxOffset
  , endorserBlockBytes
  , endorserBlockReferences
  , hashLeiosTx
  , mkEndorserBlock
  , serialiseEndorserBlock
  )
import Ouroboros.Consensus.Storage.LeiosDB
  ( LeiosDbHandle (..)
  , LeiosDbReader (..)
  , LeiosDbWriter (..)
  , LocalFills
  , Promise (..)
  , newLeiosDBInMemory
  , withLeiosDBSQLite
  , withReader
  , withWriter
  )
import Ouroboros.Consensus.Storage.LeiosDB.Exception (LeiosDbException)
import Ouroboros.Consensus.Storage.LeiosDB.Impl.SQLite.Connection
  ( closeChecked
  , mkConn
  , openRawConnection
  )
import Ouroboros.Consensus.Storage.LeiosDB.Impl.SQLite.Maintenance
  ( deleteEbsFrom
  , gcBatchSize
  , initialStats
  )
import Ouroboros.Consensus.Storage.LeiosDB.Impl.SQLite.Read (lookupBlob, readPartition)
import Ouroboros.Consensus.Storage.LeiosDB.Impl.SQLite.Tooling (withExistingLeiosDbFile)
import System.Directory (doesFileExist, removeDirectoryRecursive)
import System.IO.Temp (createTempDirectory, getCanonicalTemporaryDirectory)
import Test.QuickCheck
  ( Gen
  , Property
  , chooseInt
  , counterexample
  , elements
  , forAll
  , forAllBlind
  , forAllShrinkShow
  , frequency
  , ioProperty
  , listOf
  , shrink
  , shrinkList
  , sublistOf
  , tabulate
  , vector
  , (===)
  )
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, assertBool, assertFailure, testCase, (@?=))
import Test.Tasty.QuickCheck (testProperty)

tests :: TestTree
tests :: TestTree
tests =
  [Char] -> [TestTree] -> TestTree
testGroup [Char]
"LeiosDB" ([TestTree] -> TestTree) -> [TestTree] -> TestTree
forall a b. (a -> b) -> a -> b
$
    (DbImpl -> [TestTree]) -> [TestTree]
forEachImplementation DbImpl -> [TestTree]
mkTestGroups
      [TestTree] -> [TestTree] -> [TestTree]
forall a. Semigroup a => a -> a -> a
<> [ [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"the two implementations agree on what a reader sees" Property
prop_implementationsAgree
         , [Char] -> [TestTree] -> TestTree
testGroup
             [Char]
"deleteEbsFrom on a partition file"
             [ [Char] -> Assertion -> TestTree
testCase [Char]
"drops the EBs announced after the slot, with their bodies" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> LeiosDbHandle IO -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_truncateDropsEbsAfterSlot
             , [Char] -> Assertion -> TestTree
testCase [Char]
"runs on the immutable partition" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> Assertion
test_truncateImmutablePartition
             ]
         , [Char] -> [TestTree] -> TestTree
testGroup
             [Char]
"garbage collection (SQLite)"
             [ [Char] -> Assertion -> TestTree
testCase [Char]
"collects more points than one job deletes" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> LeiosDbHandle IO -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_gcCollectsManyPoints
             , [Char] -> Assertion -> TestTree
testCase [Char]
"keeps only the newest point of an EB announced again in each round" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> LeiosDbHandle IO -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_gcBoundsPointsOfReannouncedEb
             , [Char] -> Assertion -> TestTree
testCase [Char]
"drops the writes of a point older than the promoted slot" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> LeiosDbHandle IO -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_gcDropsWritesOfCollectedPoint
             ]
         , [Char] -> [TestTree] -> TestTree
testGroup
             [Char]
"reads (SQLite)"
             [ [Char] -> Assertion -> TestTree
testCase [Char]
"a read a promotion overtook reads the immutable partition" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> LeiosDbHandle IO -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_readOvertakenByCollection
             ]
         , [Char] -> [TestTree] -> TestTree
testGroup
             [Char]
"promotion (SQLite)"
             [ [Char] -> Assertion -> TestTree
testCase [Char]
"promotes an EB again after a crash lost the volatile txs that completed it" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> Assertion
test_promoteAgainAfterLosingVolatileTxs
             , [Char] -> Assertion -> TestTree
testCase [Char]
"promotes an EB at the certified point, not at another point of it" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> LeiosDbHandle IO -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_promoteAtCertifiedSlot
             , [Char] -> Assertion -> TestTree
testCase
                 [Char]
"a truncated and reopened database keeps reading a promoted EB from the immutable partition"
                 (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ ([Char] -> [Char] -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> Assertion
test_truncateKeepsPromotedEb
             ]
         , [Char] -> [TestTree] -> TestTree
testGroup
             [Char]
"file initialisation (SQLite)"
             [ [Char] -> Assertion -> TestTree
testCase [Char]
"handle creation applies both schemas" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> Assertion
test_schemaAtHandleCreation
             , [Char] -> Assertion -> TestTree
testCase [Char]
"zero-byte files get a schema" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> Assertion
test_zeroByteFilesGetSchema
             , [Char] -> Assertion -> TestTree
testCase [Char]
"reopening keeps the data" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
                 ([Char] -> [Char] -> Assertion) -> Assertion
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> Assertion
test_reopenKeepsData
             ]
         ]

-- | Database creation strategy for different implementations.
data DbImpl
  = InMemory
  | SQLite

-- | A reader and a writer onto the same database -- what every caller outside
-- these tests now holds, since reads and writes no longer share a connection.
data RW = RW
  { RW -> LeiosDbReader IO
rwReader :: LeiosDbReader IO
  , RW -> LeiosDbWriter IO
rwWriter :: LeiosDbWriter IO
  }

withRW :: LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW :: forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db RW -> IO a
k = LeiosDbHandle IO -> (LeiosDbReader IO -> IO a) -> IO a
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbReader m -> m a) -> m a
withReader LeiosDbHandle IO
db ((LeiosDbReader IO -> IO a) -> IO a)
-> (LeiosDbReader IO -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \LeiosDbReader IO
r -> LeiosDbHandle IO -> (LeiosDbWriter IO -> IO a) -> IO a
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbWriter m -> m a) -> m a
withWriter LeiosDbHandle IO
db ((LeiosDbWriter IO -> IO a) -> IO a)
-> (LeiosDbWriter IO -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \LeiosDbWriter IO
w -> RW -> IO a
k (LeiosDbReader IO -> LeiosDbWriter IO -> RW
RW LeiosDbReader IO
r LeiosDbWriter IO
w)

-- Writes are awaited, so a read that follows one sees it.
rwInsertEbBody :: RW -> LeiosPoint -> EndorserBlock -> IO ()
rwInsertEbBody :: RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
rw LeiosPoint
point EndorserBlock
eb = IO [Int] -> Assertion
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO [Int] -> Assertion) -> IO [Int] -> Assertion
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
rw LeiosPoint
point EndorserBlock
eb Map Int TxLocation
forall a. Monoid a => a
mempty

-- | Also fill from local bytes, and return the offsets the point holds.
rwInsertEbBodyFilling :: RW -> LeiosPoint -> EndorserBlock -> LocalFills -> IO [TxOffset]
rwInsertEbBodyFilling :: RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
rw LeiosPoint
point EndorserBlock
eb Map Int TxLocation
fills = Set Int -> [Int]
forall a. Set a -> [a]
Set.toAscList (Set Int -> [Int]) -> IO (Set Int) -> IO [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Promise IO (Set Int) -> IO (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise IO (Set Int) -> IO (Set Int))
-> IO (Promise IO (Set Int)) -> IO (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter IO
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> IO (Promise IO (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody (RW -> LeiosDbWriter IO
rwWriter RW
rw) LeiosPoint
point EndorserBlock
eb Map Int TxLocation
fills)

rwInsertTxs :: RW -> LeiosPoint -> [(TxOffset, BS.ByteString)] -> IO ()
rwInsertTxs :: RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
rw LeiosPoint
point [(Int, ByteString)]
txs = Promise IO () -> Assertion
forall (m :: * -> *) a. Promise m a -> m a
await (Promise IO () -> Assertion) -> IO (Promise IO ()) -> Assertion
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter IO
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> IO (Promise IO ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs (RW -> LeiosDbWriter IO
rwWriter RW
rw) LeiosPoint
point ([(Int, ByteString)] -> Map Int ByteString
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Int, ByteString)]
txs)

rwScanComplete :: RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete :: RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete = LeiosDbReader IO -> HasCallStack => SlotNo -> IO [LeiosPoint]
LeiosDbReader IO -> SlotNo -> IO [LeiosPoint]
forall (m :: * -> *).
LeiosDbReader m -> HasCallStack => SlotNo -> m [LeiosPoint]
scanCompleteEbClosuresNotOlderThanSlot (LeiosDbReader IO -> SlotNo -> IO [LeiosPoint])
-> (RW -> LeiosDbReader IO) -> RW -> SlotNo -> IO [LeiosPoint]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RW -> LeiosDbReader IO
rwReader

rwLookupEbBody :: RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody :: RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody = LeiosDbReader IO
-> HasCallStack =>
   LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
LeiosDbReader IO
-> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack =>
   LeiosPoint -> m (Maybe (Serialised EndorserBlock))
lookupEbBody (LeiosDbReader IO
 -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock)))
-> (RW -> LeiosDbReader IO)
-> RW
-> LeiosPoint
-> IO (Maybe (Serialised EndorserBlock))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RW -> LeiosDbReader IO
rwReader

rwLookupEbClosure :: RW -> LeiosPoint -> IO (Maybe [BS.ByteString])
rwLookupEbClosure :: RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point = ([LeiosTx] -> [ByteString])
-> Maybe [LeiosTx] -> Maybe [ByteString]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((LeiosTx -> ByteString) -> [LeiosTx] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map LeiosTx -> ByteString
leiosTxBytes) (Maybe [LeiosTx] -> Maybe [ByteString])
-> IO (Maybe [LeiosTx]) -> IO (Maybe [ByteString])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LeiosDbReader IO
-> HasCallStack => LeiosPoint -> IO (Maybe [LeiosTx])
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack => LeiosPoint -> m (Maybe [LeiosTx])
lookupEbClosure (RW -> LeiosDbReader IO
rwReader RW
con) LeiosPoint
point

rwBatchRetrieveTxs :: RW -> LeiosPoint -> [TxOffset] -> IO (Map.Map TxOffset BS.ByteString)
rwBatchRetrieveTxs :: RW -> LeiosPoint -> [Int] -> IO (Map Int ByteString)
rwBatchRetrieveTxs RW
rw LeiosPoint
point = LeiosDbReader IO
-> HasCallStack => LeiosPoint -> Set Int -> IO (Map Int ByteString)
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack => LeiosPoint -> Set Int -> m (Map Int ByteString)
batchRetrieveTxs (RW -> LeiosDbReader IO
rwReader RW
rw) LeiosPoint
point (Set Int -> IO (Map Int ByteString))
-> ([Int] -> Set Int) -> [Int] -> IO (Map Int ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> Set Int
forall a. Ord a => [a] -> Set a
Set.fromList

-- | Create a fresh database and run an action with it.
-- Ensures proper cleanup for SQLite databases.
withFreshDb :: DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb :: forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
InMemory LeiosDbHandle IO -> IO a
action =
  IO (LeiosDbHandle IO)
forall (m :: * -> *). IOLike m => m (LeiosDbHandle m)
newLeiosDBInMemory IO (LeiosDbHandle IO) -> (LeiosDbHandle IO -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LeiosDbHandle IO -> IO a
action
withFreshDb DbImpl
SQLite LeiosDbHandle IO -> IO a
action = (LeiosDbHandle IO -> IO a) -> IO a
forall a. (LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteDb LeiosDbHandle IO -> IO a
action

withFreshSQLiteDb :: (LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteDb :: forall a. (LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteDb LeiosDbHandle IO -> IO a
action = ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile (\[Char]
_vol [Char]
_imm -> LeiosDbHandle IO -> IO a
action)

-- | Create a fresh SQLite database and hand its paths to the action, which
-- the tests that read the files directly need. The database is torn down -- writes
-- flushed, threads stopped, connections closed -- before the directory is
-- removed; a still-open connection makes the removal flaky (a hard failure
-- on Windows, where deleting an open WAL is a sharing violation).
withFreshSQLiteFile :: (FilePath -> FilePath -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile :: forall a. ([Char] -> [Char] -> LeiosDbHandle IO -> IO a) -> IO a
withFreshSQLiteFile [Char] -> [Char] -> LeiosDbHandle IO -> IO a
action =
  ([Char] -> [Char] -> IO a) -> IO a
forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths (([Char] -> [Char] -> IO a) -> IO a)
-> ([Char] -> [Char] -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \[Char]
volDbPath [Char]
immDbPath ->
    Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> IO a) -> IO a)
-> (LeiosDbHandle IO -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$
      [Char] -> [Char] -> LeiosDbHandle IO -> IO a
action [Char]
volDbPath [Char]
immDbPath

-- | Paths for both partition files in a fresh temporary directory, which is
-- removed afterwards. Nothing is created at the paths.
withTempLeiosDbPaths :: (FilePath -> FilePath -> IO a) -> IO a
withTempLeiosDbPaths :: forall a. ([Char] -> [Char] -> IO a) -> IO a
withTempLeiosDbPaths [Char] -> [Char] -> IO a
action = do
  sysTmp <- IO [Char]
getCanonicalTemporaryDirectory
  bracket
    (createTempDirectory sysTmp "leios-test")
    removeDirectoryRecursive
    (\[Char]
tmpDir -> [Char] -> [Char] -> IO a
action ([Char]
tmpDir [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"/test.vol.db") ([Char]
tmpDir [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"/test.imm.db"))

-- | Run tests for each database implementation.
forEachImplementation :: (DbImpl -> [TestTree]) -> [TestTree]
forEachImplementation :: (DbImpl -> [TestTree]) -> [TestTree]
forEachImplementation DbImpl -> [TestTree]
mkTests =
  [ [Char] -> [TestTree] -> TestTree
testGroup [Char]
"InMemory" (DbImpl -> [TestTree]
mkTests DbImpl
InMemory)
  , [Char] -> [TestTree] -> TestTree
testGroup [Char]
"SQLite" (DbImpl -> [TestTree]
mkTests DbImpl
SQLite)
  ]

-- | Create the test groups for a given database implementation.
mkTestGroups :: DbImpl -> [TestTree]
mkTestGroups :: DbImpl -> [TestTree]
mkTestGroups DbImpl
impl =
  [ [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"points"
      [ [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"bodies written at several points are each read back" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> Property
prop_pointsAccumulate DbImpl
impl
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a body write records its point" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_ebBodyWritesPoint
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a body write reports the txs the point holds" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_ebBodyReportsHeldTxs
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a point holds only what is written at it" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_readsAtPoint
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"ebs"
      [ [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"insert then lookup" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_ebsInsertThenLookup DbImpl
impl
      , [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"lookup missing returns empty" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_ebsLookupMissing DbImpl
impl
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"transactions"
      [ [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"insert then retrieve" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_txsInsertThenRetrieve DbImpl
impl
      , [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"retrieve missing returns empty" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_txsRetrieveMissing DbImpl
impl
      , [Char] -> Assertion -> TestTree
testCase [Char]
"out of bounds and wrong-sized writes are dropped" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_malformedTxWritesDropped
      , [Char] -> Assertion -> TestTree
testCase [Char]
"reads a point older than the promoted slot from the immutable partition only" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> Assertion
test_readsDispatchBySlot DbImpl
impl
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"local fills"
      [ [Char] -> Assertion -> TestTree
testCase [Char]
"a body fills a tx from another endorser block's bytes" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_fillFromAnotherEb
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a fill from a source without the bytes fills nothing" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_fillWithoutSourceBytes
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a body whose fills complete its closure completes it" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_fillCompletesClosure
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a body fills its txs from another point of the same endorser block" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_fillFromAnotherPoint
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a body that misstates a tx's size fills nothing for it" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_fillOfMisstatedSize
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"completion"
      [ [Char] -> Assertion -> TestTree
testCase [Char]
"a scan finds an EB once each of its txs is written" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_completion
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a tx whose bytes are zeroes counts once written" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_completionZeroTx
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a scan finds each point of an EB whose closure is complete" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_multipleSlotsSameHash
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a scan finds a point written after another completed, once complete itself" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_scanFindsLaterPoint
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"garbage collection"
      [ [Char] -> Assertion -> TestTree
testCase [Char]
"evicts an EB all of whose points are older, with its txs" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_gcEvictsOldEb
      , [Char] -> Assertion -> TestTree
testCase [Char]
"keeps an EB with a point at the slot" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_gcKeepsEbWithPointAtSlot
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"garbage collection of an EB a newer point keeps"
      [ [Char] -> Assertion -> TestTree
testCase [Char]
"drops its older points and still finds it complete" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_gcDropsOlderPoints
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"promotion"
      [ [Char] -> Assertion -> TestTree
testCase [Char]
"keeps the closure of a promoted EB its garbage collection evicts" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_gcKeepsPromotedEb
      , [Char] -> Assertion -> TestTree
testCase [Char]
"fails for an EB whose closure is incomplete, copying nothing" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_promoteIncompleteFails
      , [Char] -> Assertion -> TestTree
testCase [Char]
"fails for an EB none of whose txs is written" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_promoteNothingWrittenFails
      , [Char] -> Assertion -> TestTree
testCase [Char]
"promotes an EB promoted at another point" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_promoteAtEachPoint
      , [Char] -> Assertion -> TestTree
testCase [Char]
"a truncation drops the promoted EBs at or after its slot, which can be written again" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
          DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl LeiosDbHandle IO -> Assertion
test_truncateImmutable
      ]
  , [Char] -> [TestTree] -> TestTree
testGroup
      [Char]
"lookupEbClosure"
      [ [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"complete EB returns its txs" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_completedEbComplete DbImpl
impl
      , [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"an EB with txs missing returns Nothing" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_completedEbPartialTxs DbImpl
impl
      , [Char] -> Property -> TestTree
forall a. Testable a => [Char] -> a -> TestTree
testProperty [Char]
"no body returns Nothing" (Property -> TestTree) -> Property -> TestTree
forall a b. (a -> b) -> a -> b
$ DbImpl -> Property
prop_completedEbNoBody DbImpl
impl
      ]
  ]

-- * QuickCheck generators

-- | Generate a random EndorserBlockHash (32 random bytes).
-- With 256 bits of randomness, collisions are practically impossible.
genEbHash :: Gen EndorserBlockHash
genEbHash :: Gen EndorserBlockHash
genEbHash = ShortByteString -> EndorserBlockHash
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (ShortByteString -> EndorserBlockHash)
-> ([Word8] -> ShortByteString) -> [Word8] -> EndorserBlockHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ShortByteString
SBS.pack ([Word8] -> EndorserBlockHash)
-> Gen [Word8] -> Gen EndorserBlockHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Gen [Word8]
forall a. Arbitrary a => Int -> Gen [a]
vector Int
32

-- | Generate a random SlotNo.
genSlotNo :: Gen SlotNo
genSlotNo :: Gen SlotNo
genSlotNo = Word64 -> SlotNo
SlotNo (Word64 -> SlotNo) -> (Int -> Word64) -> Int -> SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> SlotNo) -> Gen Int -> Gen SlotNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
chooseInt (Int
0, Int
forall a. Bounded a => a
maxBound)

-- | Generate a random LeiosPoint.
genPoint :: Gen LeiosPoint
genPoint :: Gen LeiosPoint
genPoint = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (SlotNo -> EndorserBlockHash -> LeiosPoint)
-> Gen SlotNo -> Gen (EndorserBlockHash -> LeiosPoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen SlotNo
genSlotNo Gen (EndorserBlockHash -> LeiosPoint)
-> Gen EndorserBlockHash -> Gen LeiosPoint
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen EndorserBlockHash
genEbHash

-- | Generate a EndorserBlock with the given number of transactions.
genEb :: Int -> Gen EndorserBlock
genEb :: Int -> Gen EndorserBlock
genEb Int
numTxs = [BytesSize] -> EndorserBlock
ebOfSizes ([BytesSize] -> EndorserBlock)
-> Gen [BytesSize] -> Gen EndorserBlock
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Gen BytesSize -> Gen [BytesSize]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
numTxs (Int -> BytesSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> BytesSize) -> Gen Int -> Gen BytesSize
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
chooseInt (Int
50, Int
500))

-- | Generate a LeiosPoint and EndorserBlock together with the given number of transactions.
genPointAndEb :: Int -> Gen (LeiosPoint, EndorserBlock)
genPointAndEb :: Int -> Gen (LeiosPoint, EndorserBlock)
genPointAndEb Int
numTxs = (,) (LeiosPoint -> EndorserBlock -> (LeiosPoint, EndorserBlock))
-> Gen LeiosPoint
-> Gen (EndorserBlock -> (LeiosPoint, EndorserBlock))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen LeiosPoint
genPoint Gen (EndorserBlock -> (LeiosPoint, EndorserBlock))
-> Gen EndorserBlock -> Gen (LeiosPoint, EndorserBlock)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Gen EndorserBlock
genEb Int
numTxs

-- | The bytes of the test tx at this offset: of this size, and distinct per
-- offset.
testTxBytes :: TxOffset -> BytesSize -> BS.ByteString
testTxBytes :: Int -> BytesSize -> ByteString
testTxBytes Int
off BytesSize
size = Int -> Word8 -> ByteString
BS.replicate (BytesSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral BytesSize
size) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
off Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
255) Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
1)

-- | The hash of 'testTxBytes'.
testTxHash :: TxOffset -> BytesSize -> TxHash
testTxHash :: Int -> BytesSize -> TxHash
testTxHash Int
off BytesSize
size = LeiosTx -> TxHash
hashLeiosTx (ByteString -> LeiosTx
MkLeiosTx (Int -> BytesSize -> ByteString
testTxBytes Int
off BytesSize
size))

-- | An EB naming txs of these sizes, each 'testTxBytes' at its offset.
ebOfSizes :: [BytesSize] -> EndorserBlock
ebOfSizes :: [BytesSize] -> EndorserBlock
ebOfSizes [BytesSize]
sizes = [(TxHash, BytesSize)] -> EndorserBlock
mkEndorserBlock [(Int -> BytesSize -> TxHash
testTxHash Int
off BytesSize
size, BytesSize
size) | (Int
off, BytesSize
size) <- [Int] -> [BytesSize] -> [(Int, BytesSize)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [BytesSize]
sizes]

-- | Bytes for the tx at the given offset of the EB: of its declared size, as
-- a write must be to be accepted, and hashing to its declared hash if the EB is
-- 'ebOfSizes'.
txBytesFor :: EndorserBlock -> TxOffset -> BS.ByteString
txBytesFor :: EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
off = Int -> BytesSize -> ByteString
testTxBytes Int
off BytesSize
size
 where
  (TxHash
_, BytesSize
size) = EndorserBlock -> [(TxHash, BytesSize)]
endorserBlockReferences EndorserBlock
eb [(TxHash, BytesSize)] -> Int -> (TxHash, BytesSize)
forall a. HasCallStack => [a] -> Int -> a
!! Int
off

-- | Every tx of the EB, with 'txBytesFor'.
allTxsFor :: EndorserBlock -> [(TxOffset, BS.ByteString)]
allTxsFor :: EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb = [(Int
off, EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
off) | Int
off <- [Int
0 .. [(TxHash, BytesSize)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (EndorserBlock -> [(TxHash, BytesSize)]
endorserBlockReferences EndorserBlock
eb) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]

-- * Test fixtures for unit tests

-- | Create a simple test EndorserBlockHash from a seed byte.
mkTestEbHash :: Word -> EndorserBlockHash
mkTestEbHash :: Word -> EndorserBlockHash
mkTestEbHash Word
seed = 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] -> ShortByteString) -> [Word8] -> ShortByteString
forall a b. (a -> b) -> a -> b
$ Int -> Word8 -> [Word8]
forall a. Int -> a -> [a]
replicate Int
32 (Word -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
seed)

-- | Create a test LeiosPoint.
mkTestPoint :: SlotNo -> Word -> LeiosPoint
mkTestPoint :: SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
slot Word
seed = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
slot (Word -> EndorserBlockHash
mkTestEbHash Word
seed)

-- | Create a test EndorserBlock with the given number of transactions.
mkTestEb :: Int -> EndorserBlock
mkTestEb :: Int -> EndorserBlock
mkTestEb Int
numTxs = [BytesSize] -> EndorserBlock
ebOfSizes [BytesSize
100 BytesSize -> BytesSize -> BytesSize
forall a. Num a => a -> a -> a
+ Int -> BytesSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i | Int
i <- [Int
0 .. Int
numTxs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]

-- * Timing helpers

-- | Measure the time of an IO action and return the result with 'DiffTime'.
timed :: IO a -> IO (a, DiffTime)
timed :: forall a. IO a -> IO (a, DiffTime)
timed IO a
action = do
  start <- IO Time
forall (m :: * -> *). MonadMonotonicTime m => m Time
getMonotonicTime
  result <- action
  end <- getMonotonicTime
  pure (result, diffTime end start)

-- | Convert 'DiffTime' to a bucket label.
timeBucket :: DiffTime -> String
timeBucket :: DiffTime -> [Char]
timeBucket DiffTime
d
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
micro Integer
1 = [Char]
"<1μs"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
micro Integer
10 = [Char]
"1μs-10μs"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
micro Integer
100 = [Char]
"10μs-100μs"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
1 = [Char]
"100μs-1ms"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
2 = [Char]
"1-2ms"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
3 = [Char]
"2-3ms"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
4 = [Char]
"3-4ms"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
5 = [Char]
"4-5ms"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
10 = [Char]
"5-10ms"
  | DiffTime
d DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< Integer -> DiffTime
milli Integer
50 = [Char]
"10-50ms"
  | Bool
otherwise = [Char]
">50ms"
 where

milli :: Integer -> DiffTime
milli :: Integer -> DiffTime
milli Integer
x = Integer -> DiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x DiffTime -> DiffTime -> DiffTime
forall a. Fractional a => a -> a -> a
/ DiffTime
1000

micro :: Integer -> DiffTime
micro :: Integer -> DiffTime
micro Integer
x = Integer -> DiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x DiffTime -> DiffTime -> DiffTime
forall a. Fractional a => a -> a -> a
/ DiffTime
1_000_000

magnitudeBucket :: (Num a, Ord a) => a -> String
magnitudeBucket :: forall a. (Num a, Ord a) => a -> [Char]
magnitudeBucket a
size
  | a
size a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1 = [Char]
"0-1"
  | a
size a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
10 = [Char]
"1-10"
  | a
size a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
100 = [Char]
"10-100"
  | a
size a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1_000 = [Char]
"100-1_000"
  | a
size a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
10_000 = [Char]
"1_000-10_000"
  | a
size a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
100_000 = [Char]
"10_000-100_000"
  | Bool
otherwise = [Char]
">100_000"

-- * Property tests for points

-- | Property: bodies written at several points are each read back at theirs.
prop_pointsAccumulate :: DbImpl -> Property
prop_pointsAccumulate :: DbImpl -> Property
prop_pointsAccumulate DbImpl
impl =
  Gen Int
-> (Int -> [Int])
-> (Int -> [Char])
-> (Int -> Property)
-> Property
forall prop a.
Testable prop =>
Gen a -> (a -> [a]) -> (a -> [Char]) -> (a -> prop) -> Property
forAllShrinkShow ((Int, Int) -> Gen Int
chooseInt (Int
1, Int
10)) Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink Int -> [Char]
forall a. Show a => a -> [Char]
show ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
count ->
    Gen [LeiosPoint] -> ([LeiosPoint] -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen LeiosPoint -> Gen [LeiosPoint]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Gen LeiosPoint
genPoint) (([LeiosPoint] -> Property) -> Property)
-> ([LeiosPoint] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[LeiosPoint]
points ->
      IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
        insertTimes <- [LeiosPoint] -> (LeiosPoint -> IO DiffTime) -> IO [DiffTime]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [LeiosPoint]
points ((LeiosPoint -> IO DiffTime) -> IO [DiffTime])
-> (LeiosPoint -> IO DiffTime) -> IO [DiffTime]
forall a b. (a -> b) -> a -> b
$ \LeiosPoint
p ->
          ((), DiffTime) -> DiffTime
forall a b. (a, b) -> b
snd (((), DiffTime) -> DiffTime) -> IO ((), DiffTime) -> IO DiffTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Assertion -> IO ((), DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
p (Int -> EndorserBlock
mkTestEb Int
1))
        (bodies, lookupTime) <- timed $ mapM (rwLookupEbBody con) points
        pure $
          bodies === map (const (Just (serialiseEndorserBlock (mkTestEb 1)))) points
            & tabulate "insertEbBody" [timeBucket $ maximum insertTimes]
            & tabulate "lookupEbBody" [timeBucket lookupTime]

-- * Property tests for EBs

-- | Property: inserting an EB body and looking it up returns the correct txs.
prop_ebsInsertThenLookup :: DbImpl -> Property
prop_ebsInsertThenLookup :: DbImpl -> Property
prop_ebsInsertThenLookup DbImpl
impl =
  Gen Int
-> (Int -> [Int])
-> (Int -> [Char])
-> (Int -> Property)
-> Property
forall prop a.
Testable prop =>
Gen a -> (a -> [a]) -> (a -> [Char]) -> (a -> prop) -> Property
forAllShrinkShow ((Int, Int) -> Gen Int
chooseInt (Int
1, Int
50)) ((Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1) ([Int] -> [Int]) -> (Int -> [Int]) -> Int -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink) Int -> [Char]
forall a. Show a => a -> [Char]
show ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
numTxs ->
    Gen (LeiosPoint, EndorserBlock)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen (LeiosPoint, EndorserBlock)
genPointAndEb Int
numTxs) (((LeiosPoint, EndorserBlock) -> Property) -> Property)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \(LeiosPoint
point, EndorserBlock
eb) ->
      IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
        let expected :: Maybe (Serialised EndorserBlock)
expected = Serialised EndorserBlock -> Maybe (Serialised EndorserBlock)
forall a. a -> Maybe a
Just (EndorserBlock -> Serialised EndorserBlock
serialiseEndorserBlock EndorserBlock
eb)
        (_, insertTime) <- Assertion -> IO ((), DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (Assertion -> IO ((), DiffTime)) -> Assertion -> IO ((), DiffTime)
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
        (result, lookupTime) <- timed $ rwLookupEbBody con point
        pure $
          result == expected
            & counterexample ("Expected: " ++ show expected ++ "\nGot: " ++ show result)
            & tabulate "insertEbBody" [timeBucket insertTime]
            & tabulate "lookupEbBody" [timeBucket lookupTime]

-- | Property: looking up a non-existent EB returns 'Nothing'.
prop_ebsLookupMissing :: DbImpl -> Property
prop_ebsLookupMissing :: DbImpl -> Property
prop_ebsLookupMissing DbImpl
impl =
  Gen LeiosPoint -> (LeiosPoint -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen LeiosPoint
genPoint ((LeiosPoint -> Property) -> Property)
-> (LeiosPoint -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \LeiosPoint
missingPoint ->
    IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
      (result, lookupTime) <- IO (Maybe (Serialised EndorserBlock))
-> IO (Maybe (Serialised EndorserBlock), DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (IO (Maybe (Serialised EndorserBlock))
 -> IO (Maybe (Serialised EndorserBlock), DiffTime))
-> IO (Maybe (Serialised EndorserBlock))
-> IO (Maybe (Serialised EndorserBlock), DiffTime)
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con LeiosPoint
missingPoint
      pure $
        result === Nothing
          & tabulate "lookupEbBody (missing)" [timeBucket lookupTime]

-- * Property tests for transactions

-- | Property: inserting tx bytes and retrieving them returns the correct data.
prop_txsInsertThenRetrieve :: DbImpl -> Property
prop_txsInsertThenRetrieve :: DbImpl -> Property
prop_txsInsertThenRetrieve DbImpl
impl =
  Gen Int
-> (Int -> [Int])
-> (Int -> [Char])
-> (Int -> Property)
-> Property
forall prop a.
Testable prop =>
Gen a -> (a -> [a]) -> (a -> [Char]) -> (a -> prop) -> Property
forAllShrinkShow ((Int, Int) -> Gen Int
chooseInt (Int
1, Int
50)) ((Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1) ([Int] -> [Int]) -> (Int -> [Int]) -> Int -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink) Int -> [Char]
forall a. Show a => a -> [Char]
show ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
numTxs ->
    Gen (LeiosPoint, EndorserBlock)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind (Int -> Gen (LeiosPoint, EndorserBlock)
genPointAndEb Int
numTxs) (((LeiosPoint, EndorserBlock) -> Property) -> Property)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \(LeiosPoint
point, EndorserBlock
eb) ->
      Gen [Int] -> ([Int] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind ([Int] -> Gen [Int]
forall a. [a] -> Gen [a]
sublistOf [Int
0 .. Int
numTxs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]) (([Int] -> Property) -> Property)
-> ([Int] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[Int]
offsetsToInsert ->
        IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
          -- Insert the EB first (point then body)
          RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
          -- Forced, so that the tabulated insert time excludes building the
          -- bytes.
          let !txsToInsert :: [(Int, ByteString)]
txsToInsert =
                [(Int, ByteString)] -> [(Int, ByteString)]
forall a. NFData a => a -> a
force [(Int
off, EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
off) | Int
off <- [Int]
offsetsToInsert]
          insertTime <- ((), DiffTime) -> DiffTime
forall a b. (a, b) -> b
snd (((), DiffTime) -> DiffTime) -> IO ((), DiffTime) -> IO DiffTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Assertion -> IO ((), DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point [(Int, ByteString)]
txsToInsert)
          -- Retrieve all offsets
          let allOffsets = [Int
0 .. Int
numTxs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
          (results, retrieveTime) <- timed $ rwBatchRetrieveTxs con point allOffsets
          -- The inserted txs have their bytes, the others are absent.
          pure $
            results === Map.fromList [(off, txBytesFor eb off) | off <- offsetsToInsert]
              & counterexample ("Inserted offsets: " ++ show offsetsToInsert)
              & counterexample ("Total txs: " <> show numTxs)
              & counterexample ("Inserted txs: " <> show (length txsToInsert))
              & tabulate "insertTxs" [timeBucket insertTime]
              & tabulate "batchRetrieveTxs" [timeBucket retrieveTime]
              & tabulate "txs inserted" [magnitudeBucket $ length txsToInsert]

-- | Property: retrieving from non-existent EB returns empty list.
prop_txsRetrieveMissing :: DbImpl -> Property
prop_txsRetrieveMissing :: DbImpl -> Property
prop_txsRetrieveMissing DbImpl
impl =
  Gen LeiosPoint -> (LeiosPoint -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen LeiosPoint
genPoint ((LeiosPoint -> Property) -> Property)
-> (LeiosPoint -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \LeiosPoint
missingPoint ->
    IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
      (result, retrieveTime) <- IO (Map Int ByteString) -> IO (Map Int ByteString, DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (IO (Map Int ByteString) -> IO (Map Int ByteString, DiffTime))
-> IO (Map Int ByteString) -> IO (Map Int ByteString, DiffTime)
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> [Int] -> IO (Map Int ByteString)
rwBatchRetrieveTxs RW
con LeiosPoint
missingPoint [Int
0, Int
1, Int
2]
      pure $
        result === Map.empty
          & tabulate "batchRetrieveTxs (missing)" [timeBucket retrieveTime]

-- * Completion tests

-- | The scan finds an EB only once each of its txs is written, and only that
-- EB.
test_completion :: LeiosDbHandle IO -> IO ()
test_completion :: LeiosDbHandle IO -> Assertion
test_completion LeiosDbHandle IO
db = do
  let point :: LeiosPoint
point = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
1) Word
1
      eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
3
      otherPoint :: LeiosPoint
otherPoint = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
2) Word
2
      otherEb :: EndorserBlock
otherEb = Int -> EndorserBlock
mkTestEb Int
2
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
otherPoint EndorserBlock
otherEb
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point (Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
take Int
2 (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb))
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [])
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point (Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
drop Int
2 (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb))
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
point])

-- | A tx whose bytes are zeroes, as the closure holds before any write, counts
-- as written once it is: its marker, not its bytes, tells.
test_completionZeroTx :: LeiosDbHandle IO -> IO ()
test_completionZeroTx :: LeiosDbHandle IO -> Assertion
test_completionZeroTx LeiosDbHandle IO
db = do
  let point :: LeiosPoint
point = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
1) Word
1
      zero :: ByteString
zero = Word8 -> ByteString
BS.singleton Word8
0
      eb :: EndorserBlock
eb = [(TxHash, BytesSize)] -> EndorserBlock
mkEndorserBlock [(LeiosTx -> TxHash
hashLeiosTx (ByteString -> LeiosTx
MkLeiosTx ByteString
zero), BytesSize
1)]
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [])
    RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe [ByteString]
forall a. Maybe a
Nothing)
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point [(Int
0, ByteString
zero)]
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
point])
    RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just [ByteString
zero])

-- | The same EB content can be announced at several slots; the DB keeps each
-- 'LeiosPoint' on its own, and the scan finds each whose closure is complete.
test_multipleSlotsSameHash :: LeiosDbHandle IO -> IO ()
test_multipleSlotsSameHash :: LeiosDbHandle IO -> Assertion
test_multipleSlotsSameHash LeiosDbHandle IO
db = do
  let hashSeed :: Word
hashSeed = Word
1
      point1 :: LeiosPoint
point1 = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
1) Word
hashSeed
      point2 :: LeiosPoint
point2 = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
2) Word
hashSeed
      eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2 -- same content at both points (deterministic from numTxs)
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point1 EndorserBlock
eb
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point2 EndorserBlock
eb
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point1 (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
point1])
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point2 (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
point1, LeiosPoint
point2])

-- | An EB announced again after its closure completed at an older point: the
-- new point is complete only once its own txs are written.
test_scanFindsLaterPoint :: LeiosDbHandle IO -> IO ()
test_scanFindsLaterPoint :: LeiosDbHandle IO -> Assertion
test_scanFindsLaterPoint LeiosDbHandle IO
db = do
  let older :: LeiosPoint
older = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
10) Word
1
      younger :: LeiosPoint
younger = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
30) Word
1
      eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
older EndorserBlock
eb
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
older (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
younger EndorserBlock
eb
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
20 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [])
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
younger (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
20 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
younger])

-- | A body write records its point.
test_ebBodyWritesPoint :: LeiosDbHandle IO -> IO ()
test_ebBodyWritesPoint :: LeiosDbHandle IO -> Assertion
test_ebBodyWritesPoint LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
1 Word
1) EndorserBlock
eb
  RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
1 Word
1) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Serialised EndorserBlock -> Maybe (Serialised EndorserBlock)
forall a. a -> Maybe a
Just (EndorserBlock -> Serialised EndorserBlock
serialiseEndorserBlock EndorserBlock
eb))

-- | A body written again holds the txs written since, and one written at a new
-- point of the EB holds none, whatever another point holds.
test_ebBodyReportsHeldTxs :: LeiosDbHandle IO -> IO ()
test_ebBodyReportsHeldTxs :: LeiosDbHandle IO -> Assertion
test_ebBodyReportsHeldTxs LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
3
      point :: LeiosPoint
point = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
1 Word
1
  RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
con LeiosPoint
point EndorserBlock
eb Map Int TxLocation
forall a. Monoid a => a
mempty IO [Int] -> ([Int] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [])
  RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point (Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
take Int
2 (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb))
  RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
con LeiosPoint
point EndorserBlock
eb Map Int TxLocation
forall a. Monoid a => a
mempty IO [Int] -> ([Int] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Int
0, Int
1])
  RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) EndorserBlock
eb Map Int TxLocation
forall a. Monoid a => a
mempty IO [Int] -> ([Int] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([Int] -> [Int] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [])

-- | Each point of an EB is read on its own: a point holds only the body and
-- txs written at it, whatever another point of the EB holds.
test_readsAtPoint :: LeiosDbHandle IO -> IO ()
test_readsAtPoint :: LeiosDbHandle IO -> Assertion
test_readsAtPoint LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
1 Word
1] EndorserBlock
eb
  RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe (Serialised EndorserBlock)
forall a. Maybe a
Nothing)
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe [ByteString]
forall a. Maybe a
Nothing)
  RW -> LeiosPoint -> [Int] -> IO (Map Int ByteString)
rwBatchRetrieveTxs RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) [Int
0, Int
1] IO (Map Int ByteString)
-> (Map Int ByteString -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Map Int ByteString -> Map Int ByteString -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Map Int ByteString
forall k a. Map k a
Map.empty)
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) EndorserBlock
eb
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe [ByteString]
forall a. Maybe a
Nothing)
  RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1) IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))

-- | A tx write only fills a tx the body declares, at its declared size, the
-- first of a write winning. Anything else changes nothing.
--
-- Neither the LeiosDB nor this test requires the bytes to hash to the body's
-- tx hash: the LeiosDB assumes that its caller checked that.
test_malformedTxWritesDropped :: LeiosDbHandle IO -> IO ()
test_malformedTxWritesDropped :: LeiosDbHandle IO -> Assertion
test_malformedTxWritesDropped LeiosDbHandle IO
db = do
  let point :: LeiosPoint
point = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
1) Word
1
      eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      offsets :: [Int]
offsets = [Int
0, Int
1]
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
    -- An offset the body does not declare, and wrong-sized bytes.
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs
      RW
con
      LeiosPoint
point
      [ (Int
2, EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
0)
      , (Int
0, ByteString -> Word8 -> ByteString
BS.snoc (EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
0) Word8
0)
      , (Int
1, Int -> ByteString -> ByteString
BS.take Int
1 (EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
1))
      ]
    retrieved <- RW -> LeiosPoint -> [Int] -> IO (Map Int ByteString)
rwBatchRetrieveTxs RW
con LeiosPoint
point [Int]
offsets
    retrieved @?= Map.empty
    rwInsertTxs con point [(0, txBytesFor eb 0)]
    retrieved' <- rwBatchRetrieveTxs con point [0]
    retrieved' @?= Map.singleton 0 (txBytesFor eb 0)
    rwInsertTxs con point [(1, txBytesFor eb 1)]
    closure <- rwLookupEbClosure con point
    closure @?= Just (map snd (allTxsFor eb))

-- * Local fills

-- | Two endorser blocks that both reference the tx at offset 1 of 'mkTestEb'
-- 2: that one, and one that references it at offset 0, then a tx of its own.
fillFixtures :: (LeiosPoint, EndorserBlock, LeiosPoint, EndorserBlock)
fillFixtures :: (LeiosPoint, EndorserBlock, LeiosPoint, EndorserBlock)
fillFixtures =
  ( SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
1) Word
1
  , Int -> EndorserBlock
mkTestEb Int
2
  , SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
2) Word
2
  , [(TxHash, BytesSize)] -> EndorserBlock
mkEndorserBlock [(Int -> BytesSize -> TxHash
testTxHash Int
1 BytesSize
101, BytesSize
101), (Int -> BytesSize -> TxHash
testTxHash Int
1 BytesSize
107, BytesSize
107)]
  )

test_fillFromAnotherEb :: LeiosDbHandle IO -> IO ()
test_fillFromAnotherEb :: LeiosDbHandle IO -> Assertion
test_fillFromAnotherEb LeiosDbHandle IO
db = do
  let (LeiosPoint
pointA, EndorserBlock
ebA, LeiosPoint
pointB, EndorserBlock
ebB) = (LeiosPoint, EndorserBlock, LeiosPoint, EndorserBlock)
fillFixtures
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
pointA] EndorserBlock
ebA
    -- The second fill's source lists another tx at its offset.
    result <-
      RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling
        RW
con
        LeiosPoint
pointB
        EndorserBlock
ebB
        ( [(Int, TxLocation)] -> Map Int TxLocation
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
            [ (Int
0, LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
pointA Int
1)
            , (Int
1, LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
pointA Int
0)
            ]
        )
    result @?= [0]
    retrieved <- rwBatchRetrieveTxs con pointB [0, 1]
    retrieved @?= Map.singleton 0 (txBytesFor ebA 1)
    rwInsertTxs con pointB [(1, txBytesFor ebB 1)]
    rwScanComplete con 0 >>= (@?= [pointA, pointB])

test_fillWithoutSourceBytes :: LeiosDbHandle IO -> IO ()
test_fillWithoutSourceBytes :: LeiosDbHandle IO -> Assertion
test_fillWithoutSourceBytes LeiosDbHandle IO
db = do
  let (LeiosPoint
pointA, EndorserBlock
ebA, LeiosPoint
pointB, EndorserBlock
ebB) = (LeiosPoint, EndorserBlock, LeiosPoint, EndorserBlock)
fillFixtures
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
pointA EndorserBlock
ebA
    -- The first source holds the body but not its txs; the second is unknown.
    -- So neither tx is written.
    result <-
      RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling
        RW
con
        LeiosPoint
pointB
        EndorserBlock
ebB
        ( [(Int, TxLocation)] -> Map Int TxLocation
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
            [ (Int
0, LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
pointA Int
1)
            , (Int
1, LeiosPoint -> Int -> TxLocation
MkTxLocation (SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
1) Word
9) Int
0)
            ]
        )
    result @?= []

test_fillCompletesClosure :: LeiosDbHandle IO -> IO ()
test_fillCompletesClosure :: LeiosDbHandle IO -> Assertion
test_fillCompletesClosure LeiosDbHandle IO
db = do
  let (LeiosPoint
pointA, EndorserBlock
ebA, LeiosPoint
_, EndorserBlock
_) = (LeiosPoint, EndorserBlock, LeiosPoint, EndorserBlock)
fillFixtures
      pointB :: LeiosPoint
pointB = SlotNo -> Word -> LeiosPoint
mkTestPoint (Word64 -> SlotNo
SlotNo Word64
2) Word
2
      ebB :: EndorserBlock
ebB = [(TxHash, BytesSize)] -> EndorserBlock
mkEndorserBlock [(Int -> BytesSize -> TxHash
testTxHash Int
1 BytesSize
101, BytesSize
101)]
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
pointA] EndorserBlock
ebA
    result <- RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
con LeiosPoint
pointB EndorserBlock
ebB (Int -> TxLocation -> Map Int TxLocation
forall k a. k -> a -> Map k a
Map.singleton Int
0 (LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
pointA Int
1))
    result @?= [0]
    rwScanComplete con 0 >>= (@?= [pointA, pointB])

-- | An EB announced again: the new point fills its txs from the older one's,
-- which a point does not share otherwise.
test_fillFromAnotherPoint :: LeiosDbHandle IO -> IO ()
test_fillFromAnotherPoint :: LeiosDbHandle IO -> Assertion
test_fillFromAnotherPoint LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      older :: LeiosPoint
older = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
1 Word
1
      younger :: LeiosPoint
younger = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
2 Word
1
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
older] EndorserBlock
eb
  result <-
    RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling
      RW
con
      LeiosPoint
younger
      EndorserBlock
eb
      ([(Int, TxLocation)] -> Map Int TxLocation
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Int
off, LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
older Int
off) | Int
off <- [Int
0, Int
1]])
  result @?= [0, 1]
  rwLookupEbClosure con younger >>= (@?= Just (map snd (allTxsFor eb)))

-- | A body listing the tx of the source by its hash, but with another size.
test_fillOfMisstatedSize :: LeiosDbHandle IO -> IO ()
test_fillOfMisstatedSize :: LeiosDbHandle IO -> Assertion
test_fillOfMisstatedSize LeiosDbHandle IO
db = do
  let (LeiosPoint
pointA, EndorserBlock
ebA, LeiosPoint
pointB, EndorserBlock
_) = (LeiosPoint, EndorserBlock, LeiosPoint, EndorserBlock)
fillFixtures
      ebB :: EndorserBlock
ebB = [(TxHash, BytesSize)] -> EndorserBlock
mkEndorserBlock [(Int -> BytesSize -> TxHash
testTxHash Int
1 BytesSize
101, BytesSize
102)]
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
pointA] EndorserBlock
ebA
    result <- RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
con LeiosPoint
pointB EndorserBlock
ebB (Int -> TxLocation -> Map Int TxLocation
forall k a. k -> a -> Map k a
Map.singleton Int
0 (LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
pointA Int
1))
    result @?= []
    rwBatchRetrieveTxs con pointB [0] >>= (@?= Map.empty)

-- * Garbage collection

-- | Write the EB at these points, with its body and all its transactions.
writeWholeEb :: RW -> [LeiosPoint] -> EndorserBlock -> IO ()
writeWholeEb :: RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint]
points EndorserBlock
eb = do
  [LeiosPoint] -> (LeiosPoint -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [LeiosPoint]
points ((LeiosPoint -> Assertion) -> Assertion)
-> (LeiosPoint -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosPoint
point -> RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
  [LeiosPoint] -> (LeiosPoint -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [LeiosPoint]
points ((LeiosPoint -> Assertion) -> Assertion)
-> (LeiosPoint -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosPoint
point -> RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)

-- | An EB promoted at slot 5 up to slot 6, collected, and fetched again at slot
-- 10, so far one of its txs. The reads of slot 5 go to the immutable
-- partition, those of slot 10 to the volatile one, and neither falls back to
-- the other.
test_readsDispatchBySlot :: DbImpl -> IO ()
test_readsDispatchBySlot :: DbImpl -> Assertion
test_readsDispatchBySlot DbImpl
impl = DbImpl -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      promoted :: LeiosPoint
promoted = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1
      again :: LeiosPoint
again = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
promoted] EndorserBlock
eb
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [LeiosPoint
promoted]
  LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
again EndorserBlock
eb
  RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
again (Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
take Int
1 (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb))
  RW -> LeiosPoint -> [Int] -> IO (Map Int ByteString)
rwBatchRetrieveTxs RW
con LeiosPoint
again [Int
0, Int
1] IO (Map Int ByteString)
-> (Map Int ByteString -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Map Int ByteString -> Map Int ByteString -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int -> ByteString -> Map Int ByteString
forall k a. k -> a -> Map k a
Map.singleton Int
0 (EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
0))
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
again IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe [ByteString]
forall a. Maybe a
Nothing)
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
promoted IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))
  RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con LeiosPoint
promoted IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Serialised EndorserBlock -> Maybe (Serialised EndorserBlock)
forall a. a -> Maybe a
Just (EndorserBlock -> Serialised EndorserBlock
serialiseEndorserBlock EndorserBlock
eb))
  RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
promoted])

-- | A crash can lose the volatile writes that completed a promoted EB, but not
-- its immutable copy. Promoting it again, as when the crash lost the block
-- certifying it too, succeeds, and the closure is read from the immutable copy.
test_promoteAgainAfterLosingVolatileTxs :: FilePath -> FilePath -> IO ()
test_promoteAgainAfterLosingVolatileTxs :: [Char] -> [Char] -> Assertion
test_promoteAgainAfterLosingVolatileTxs [Char]
volDbPath [Char]
immDbPath = do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      point :: LeiosPoint
point = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
point] EndorserBlock
eb
    LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [LeiosPoint
point]
  -- What the crash leaves of the volatile partition: the body, but no txs.
  [Char] -> (Database -> Assertion) -> Assertion
forall a. [Char] -> (Database -> IO a) -> IO a
withSqlFile [Char]
volDbPath ((Database -> Assertion) -> Assertion)
-> (Database -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \Database
sql ->
    Database -> Text -> Assertion
SQL.exec
      Database
sql
      ([Char] -> Text
T.pack [Char]
"UPDATE ebs SET txWritten = zeroblob(length(txWritten)), txs = zeroblob(length(txs))")
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [LeiosPoint
point]
    RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))

-- | EBs promoted at slots 90 and 100 up to slot 110, and collected, before the
-- immutable partition is truncated to slot 100, as when the ChainDB opens on
-- an ImmutableDB whose tip is there. Reopened, the EB at slot 90 is still read
-- from the immutable partition, and the one at slot 100 can be written again.
test_truncateKeepsPromotedEb :: FilePath -> FilePath -> IO ()
test_truncateKeepsPromotedEb :: [Char] -> [Char] -> Assertion
test_truncateKeepsPromotedEb [Char]
volDbPath [Char]
immDbPath = do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      kept :: LeiosPoint
kept = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
90 Word
1
      dropped :: LeiosPoint
dropped = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
100 Word
2
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
kept, LeiosPoint
dropped] EndorserBlock
eb
    LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
110 [LeiosPoint
kept, LeiosPoint
dropped]
    LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db
    LeiosDbHandle IO -> HasCallStack => SlotNo -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> m ()
leiosDbTruncateImmutable LeiosDbHandle IO
db SlotNo
100
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
kept IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
kept])
    RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
dropped] EndorserBlock
eb
    RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
dropped IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))

-- | An EB announced again after the announcement that gets certified: the
-- immutable copy is at the certified slot.
test_promoteAtCertifiedSlot :: FilePath -> FilePath -> LeiosDbHandle IO -> IO ()
test_promoteAtCertifiedSlot :: [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_promoteAtCertifiedSlot [Char]
_volDbPath [Char]
immDbPath LeiosDbHandle IO
db = do
  let certified :: LeiosPoint
certified = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
certified, SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
20 Word
1] (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
11 [LeiosPoint
certified]
  [Char] -> EndorserBlockHash -> IO Int
ebSlotIn [Char]
immDbPath (Word -> EndorserBlockHash
mkTestEbHash Word
1) IO Int -> (Int -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
10)

-- | An EB promoted at slot 5 up to slot 6 and collected, read by a reader that
-- chose the volatile partition before that promotion: as of its choice, the
-- promoted slot was 0. It finds no row there, and reads the immutable partition
-- only if the promotion has overtaken it.
test_readOvertakenByCollection :: FilePath -> FilePath -> LeiosDbHandle IO -> IO ()
test_readOvertakenByCollection :: [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_readOvertakenByCollection [Char]
volDbPath [Char]
immDbPath LeiosDbHandle IO
db = do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      point :: LeiosPoint
point = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
point] EndorserBlock
eb
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [LeiosPoint
point]
  LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db
  statsVar <- LeiosDbStats -> IO (StrictTVar IO LeiosDbStats)
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
newTVarIO (LeiosDbStats -> IO (StrictTVar IO LeiosDbStats))
-> IO LeiosDbStats -> IO (StrictTVar IO LeiosDbStats)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< HasCallStack => [Char] -> [Char] -> IO LeiosDbStats
[Char] -> [Char] -> IO LeiosDbStats
initialStats [Char]
volDbPath [Char]
immDbPath
  promotedSlotVar <- newTVarIO 0
  bracket (openRawConnection volDbPath) closeChecked $ \Database
volDb ->
    IO Database
-> (Database -> Assertion) -> (Database -> Assertion) -> Assertion
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (HasCallStack => [Char] -> IO Database
[Char] -> IO Database
openRawConnection [Char]
immDbPath) HasCallStack => Database -> Assertion
Database -> Assertion
closeChecked ((Database -> Assertion) -> Assertion)
-> (Database -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \Database
immDb -> do
      let conn :: Conn
conn = Tracer IO TraceLeiosDb
-> StrictTVar IO LeiosDbStats
-> StrictTVar IO SlotNo
-> Database
-> Database
-> Conn
mkConn Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer StrictTVar IO LeiosDbStats
statsVar StrictTVar IO SlotNo
promotedSlotVar Database
volDb Database
immDb
      Conn
-> LeiosPoint
-> (Database -> IO (Maybe ByteString))
-> IO (Maybe ByteString)
forall a.
Conn -> LeiosPoint -> (Database -> IO (Maybe a)) -> IO (Maybe a)
readPartition Conn
conn LeiosPoint
point (Database -> LeiosPoint -> IO (Maybe ByteString)
`lookupBlob` LeiosPoint
point) IO (Maybe ByteString)
-> (Maybe ByteString -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe ByteString -> Maybe ByteString -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe ByteString
forall a. Maybe a
Nothing)
      -- The promotion completes while the read runs.
      let overtaken :: Database -> IO (Maybe ByteString)
overtaken Database
readDb = do
            STM IO () -> Assertion
forall a. HasCallStack => STM IO a -> IO a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM IO () -> Assertion) -> STM IO () -> Assertion
forall a b. (a -> b) -> a -> b
$ StrictTVar IO SlotNo -> SlotNo -> STM IO ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar IO SlotNo
promotedSlotVar SlotNo
6
            Database -> LeiosPoint -> IO (Maybe ByteString)
lookupBlob Database
readDb LeiosPoint
point
      Conn
-> LeiosPoint
-> (Database -> IO (Maybe ByteString))
-> IO (Maybe ByteString)
forall a.
Conn -> LeiosPoint -> (Database -> IO (Maybe a)) -> IO (Maybe a)
readPartition Conn
conn LeiosPoint
point Database -> IO (Maybe ByteString)
overtaken IO (Maybe ByteString)
-> (Maybe ByteString -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe ByteString -> Maybe ByteString -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (EndorserBlock -> ByteString
endorserBlockBytes EndorserBlock
eb))

-- | Whether the reader sees the EB's whole closure.
holdsClosure :: RW -> LeiosPoint -> IO Bool
holdsClosure :: RW -> LeiosPoint -> IO Bool
holdsClosure RW
con LeiosPoint
point = Maybe [ByteString] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [ByteString] -> Bool) -> IO (Maybe [ByteString]) -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point

-- | Advance the promoted slot without promoting anything, as for blocks that
-- certify nothing, then collect.
collectBefore :: LeiosDbHandle IO -> SlotNo -> IO ()
collectBefore :: LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
slot = do
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
slot []
  LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db

test_gcEvictsOldEb :: LeiosDbHandle IO -> IO ()
test_gcEvictsOldEb :: LeiosDbHandle IO -> Assertion
test_gcEvictsOldEb LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  -- The old EB references transactions 0, 1 and 2, and the other only 0 and 1.
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1] (Int -> EndorserBlock
mkTestEb Int
3)
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
2] (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
6
  RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe (Serialised EndorserBlock)
forall a. Maybe a
Nothing)
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
False)
  RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
2])
  -- The old EB's transactions went with it, so a new EB referencing them
  -- lacks them.
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
20 Word
3) (Int -> EndorserBlock
mkTestEb Int
3)
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
20 Word
3) IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
False)

-- | An EB written whole at slot 5 and again at slot 10, collected at slot 6.
test_gcDropsOlderPoints :: LeiosDbHandle IO -> IO ()
test_gcDropsOlderPoints :: LeiosDbHandle IO -> Assertion
test_gcDropsOlderPoints LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      again :: LeiosPoint
again = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1, LeiosPoint
again] EndorserBlock
eb
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
6
  RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint
again])

-- | After a promotion up to slot 6, a point at slot 5 and a body for another
-- point at slot 5 are written, and one at slot 4 that a later promotion up to
-- slot 3 does not bring back.
test_gcDropsWritesOfCollectedPoint :: FilePath -> FilePath -> LeiosDbHandle IO -> IO ()
test_gcDropsWritesOfCollectedPoint :: [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_gcDropsWritesOfCollectedPoint [Char]
volDbPath [Char]
_immDbPath LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1] (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
6
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
3
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
2) (Int -> EndorserBlock
mkTestEb Int
2)
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
3) (Int -> EndorserBlock
mkTestEb Int
3)
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
4 Word
1) (Int -> EndorserBlock
mkTestEb Int
2)
  [Char] -> IO Int
ebRows [Char]
volDbPath IO Int -> (Int -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
1)
  RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1])

-- | An EB written whole at slot 10 is announced again in each of nine more
-- rounds, each followed by a collection at its slot.
test_gcBoundsPointsOfReannouncedEb :: FilePath -> FilePath -> LeiosDbHandle IO -> IO ()
test_gcBoundsPointsOfReannouncedEb :: [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_gcBoundsPointsOfReannouncedEb [Char]
volDbPath [Char]
_immDbPath LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1] EndorserBlock
eb
  [SlotNo] -> (SlotNo -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SlotNo
2 .. SlotNo
10] ((SlotNo -> Assertion) -> Assertion)
-> (SlotNo -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \SlotNo
n -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint (SlotNo
10 SlotNo -> SlotNo -> SlotNo
forall a. Num a => a -> a -> a
* SlotNo
n) Word
1) EndorserBlock
eb
    LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db (SlotNo
10 SlotNo -> SlotNo -> SlotNo
forall a. Num a => a -> a -> a
* SlotNo
n)
  [Char] -> IO Int
ebRows [Char]
volDbPath IO Int -> (Int -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
1)
  RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
100 Word
1) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Serialised EndorserBlock -> Maybe (Serialised EndorserBlock)
forall a. a -> Maybe a
Just (EndorserBlock -> Serialised EndorserBlock
serialiseEndorserBlock EndorserBlock
eb))

test_gcKeepsEbWithPointAtSlot :: LeiosDbHandle IO -> IO ()
test_gcKeepsEbWithPointAtSlot :: LeiosDbHandle IO -> Assertion
test_gcKeepsEbWithPointAtSlot LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1, SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
6 Word
1] (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
6
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
6 Word
1) IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True)

-- | More points before the slot than the SQLite implementation deletes in one
-- job, and one at the slot.
test_gcCollectsManyPoints :: FilePath -> FilePath -> LeiosDbHandle IO -> IO ()
test_gcCollectsManyPoints :: [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_gcCollectsManyPoints [Char]
volDbPath [Char]
_immDbPath LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      n :: Int
n = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
gcBatchSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
  [Int] -> (Int -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1 .. Int
n] ((Int -> Assertion) -> Assertion)
-> (Int -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \Int
slot ->
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint (Int -> SlotNo
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
slot) Word
1) EndorserBlock
eb
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db (Int -> SlotNo
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
  [Char] -> IO Int
ebRows [Char]
volDbPath IO Int -> (Int -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
1)
  RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint (Int -> SlotNo
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) Word
1) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Serialised EndorserBlock -> Maybe (Serialised EndorserBlock)
forall a. a -> Maybe a
Just (EndorserBlock -> Serialised EndorserBlock
serialiseEndorserBlock EndorserBlock
eb))

test_gcKeepsPromotedEb :: LeiosDbHandle IO -> IO ()
test_gcKeepsPromotedEb :: LeiosDbHandle IO -> Assertion
test_gcKeepsPromotedEb LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1] (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1]
  LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True)

-- | An EB promoted at slot 10, and later at slot 20, both collected.
test_promoteAtEachPoint :: LeiosDbHandle IO -> IO ()
test_promoteAtEachPoint :: LeiosDbHandle IO -> Assertion
test_promoteAtEachPoint LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let earlier :: LeiosPoint
earlier = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
10 Word
1
      later :: LeiosPoint
later = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
20 Word
1
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
earlier, LeiosPoint
later] (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
11 [LeiosPoint
earlier]
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
21 [LeiosPoint
later]
  LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con LeiosPoint
earlier IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True)
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con LeiosPoint
later IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
True)

-- | EBs promoted at slots 4 and 5 up to slot 6 and collected, then the
-- immutable partition truncated to slot 5.
test_truncateImmutable :: LeiosDbHandle IO -> IO ()
test_truncateImmutable :: LeiosDbHandle IO -> Assertion
test_truncateImmutable LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      kept :: LeiosPoint
kept = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
4 Word
1
      dropped :: LeiosPoint
dropped = SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
2
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
kept, LeiosPoint
dropped] EndorserBlock
eb
  LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [LeiosPoint
kept, LeiosPoint
dropped]
  LeiosDbHandle IO -> HasCallStack => Assertion
forall (m :: * -> *). LeiosDbHandle m -> HasCallStack => m ()
garbageCollect LeiosDbHandle IO
db
  LeiosDbHandle IO -> HasCallStack => SlotNo -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> m ()
leiosDbTruncateImmutable LeiosDbHandle IO
db SlotNo
5
  (LeiosPoint -> IO Bool) -> [LeiosPoint] -> IO [Bool]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LeiosDbReader IO -> HasCallStack => LeiosPoint -> IO Bool
forall (m :: * -> *).
LeiosDbReader m -> HasCallStack => LeiosPoint -> m Bool
isImmutable (RW -> LeiosDbReader IO
rwReader RW
con)) [LeiosPoint
kept, LeiosPoint
dropped] IO [Bool] -> ([Bool] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([Bool] -> [Bool] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Bool
True, Bool
False])
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
kept IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
dropped IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe [ByteString]
forall a. Maybe a
Nothing)
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [LeiosPoint
dropped] EndorserBlock
eb
  RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
dropped IO (Maybe [ByteString])
-> (Maybe [ByteString] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe [ByteString] -> Maybe [ByteString] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)))

-- | A complete EB promoted along with one whose closure lacks a tx, up to slot 6.
test_promoteIncompleteFails :: LeiosDbHandle IO -> IO ()
test_promoteIncompleteFails :: LeiosDbHandle IO -> Assertion
test_promoteIncompleteFails LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  RW -> [LeiosPoint] -> EndorserBlock -> Assertion
writeWholeEb RW
con [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
4 Word
2] (Int -> EndorserBlock
mkTestEb Int
3)
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) (Int -> EndorserBlock
mkTestEb Int
2)
  RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) (Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
take Int
1 (EndorserBlock -> [(Int, ByteString)]
allTxsFor (Int -> EndorserBlock
mkTestEb Int
2)))
  LeiosDbHandle IO -> [LeiosPoint] -> Assertion
assertPromoteFails LeiosDbHandle IO
db [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
4 Word
2, SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1]
  LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
6
  RW -> LeiosPoint -> IO Bool
holdsClosure RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
4 Word
2) IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
False)

-- | An EB with its body but none of its txs.
test_promoteNothingWrittenFails :: LeiosDbHandle IO -> IO ()
test_promoteNothingWrittenFails :: LeiosDbHandle IO -> Assertion
test_promoteNothingWrittenFails LeiosDbHandle IO
db = LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
  RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) (Int -> EndorserBlock
mkTestEb Int
2)
  LeiosDbHandle IO -> [LeiosPoint] -> Assertion
assertPromoteFails LeiosDbHandle IO
db [SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1]
  LeiosDbReader IO -> HasCallStack => LeiosPoint -> IO Bool
forall (m :: * -> *).
LeiosDbReader m -> HasCallStack => LeiosPoint -> m Bool
isImmutable (RW -> LeiosDbReader IO
rwReader RW
con) (SlotNo -> Word -> LeiosPoint
mkTestPoint SlotNo
5 Word
1) IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
False)

-- | Promoting the points up to slot 6 fails.
assertPromoteFails :: LeiosDbHandle IO -> [LeiosPoint] -> Assertion
assertPromoteFails :: LeiosDbHandle IO -> [LeiosPoint] -> Assertion
assertPromoteFails LeiosDbHandle IO
db [LeiosPoint]
points =
  Assertion -> IO (Either LeiosDbException ())
forall a. IO a -> IO (Either LeiosDbException a)
tryDb (LeiosDbHandle IO
-> HasCallStack => SlotNo -> [LeiosPoint] -> Assertion
forall (m :: * -> *).
LeiosDbHandle m -> HasCallStack => SlotNo -> [LeiosPoint] -> m ()
leiosDbPromoteToImmutable LeiosDbHandle IO
db SlotNo
6 [LeiosPoint]
points) IO (Either LeiosDbException ())
-> (Either LeiosDbException () -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Left LeiosDbException
_ -> () -> Assertion
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    Right () -> [Char] -> Assertion
forall a. HasCallStack => [Char] -> IO a
assertFailure [Char]
"promoting an EB whose closure is incomplete succeeded"
 where
  tryDb :: IO a -> IO (Either LeiosDbException a)
  tryDb :: forall a. IO a -> IO (Either LeiosDbException a)
tryDb = IO a -> IO (Either LeiosDbException a)
forall e a. Exception e => IO a -> IO (Either e a)
try

-- * Property tests for lookupEbClosure

-- | Property: a complete EB returns its txs' bytes in body order.
prop_completedEbComplete :: DbImpl -> Property
prop_completedEbComplete :: DbImpl -> Property
prop_completedEbComplete DbImpl
impl =
  Gen Int
-> (Int -> [Int])
-> (Int -> [Char])
-> (Int -> Property)
-> Property
forall prop a.
Testable prop =>
Gen a -> (a -> [a]) -> (a -> [Char]) -> (a -> prop) -> Property
forAllShrinkShow ((Int, Int) -> Gen Int
chooseInt (Int
1, Int
20)) Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink Int -> [Char]
forall a. Show a => a -> [Char]
show ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
numTxs ->
    Gen (LeiosPoint, EndorserBlock)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind (Int -> Gen (LeiosPoint, EndorserBlock)
genPointAndEb Int
numTxs) (((LeiosPoint, EndorserBlock) -> Property) -> Property)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \(LeiosPoint
point, EndorserBlock
eb) ->
      IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
        RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
        RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
        (result, queryTime) <- IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime))
-> IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime)
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point
        pure $
          result === Just (map snd (allTxsFor eb))
            & tabulate "lookupEbClosure (complete)" [timeBucket queryTime]
            & tabulate "numTxs" [magnitudeBucket numTxs]

-- | Property: an EB with some txs missing has no closure, until they are
-- written.
prop_completedEbPartialTxs :: DbImpl -> Property
prop_completedEbPartialTxs :: DbImpl -> Property
prop_completedEbPartialTxs DbImpl
impl =
  Gen Int
-> (Int -> [Int])
-> (Int -> [Char])
-> (Int -> Property)
-> Property
forall prop a.
Testable prop =>
Gen a -> (a -> [a]) -> (a -> [Char]) -> (a -> prop) -> Property
forAllShrinkShow ((Int, Int) -> Gen Int
chooseInt (Int
1, Int
20)) Int -> [Int]
forall a. Arbitrary a => a -> [a]
shrink Int -> [Char]
forall a. Show a => a -> [Char]
show ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
numTxs ->
    Gen (LeiosPoint, EndorserBlock)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind (Int -> Gen (LeiosPoint, EndorserBlock)
genPointAndEb Int
numTxs) (((LeiosPoint, EndorserBlock) -> Property) -> Property)
-> ((LeiosPoint, EndorserBlock) -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \(LeiosPoint
point, EndorserBlock
eb) ->
      Gen [Int] -> ([Int] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind ([Int] -> Gen [Int]
forall a. [a] -> Gen [a]
sublistOf [Int
0 .. Int
numTxs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]) (([Int] -> Property) -> Property)
-> ([Int] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[Int]
offsetsToInsert ->
        IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
          RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con LeiosPoint
point EndorserBlock
eb
          RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con LeiosPoint
point [(Int
off, EndorserBlock -> Int -> ByteString
txBytesFor EndorserBlock
eb Int
off) | Int
off <- [Int]
offsetsToInsert]
          (result, queryTime) <- IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime))
-> IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime)
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point
          let expected
                | [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
offsetsToInsert Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
numTxs = [ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just (((Int, ByteString) -> ByteString)
-> [(Int, ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb))
                | Bool
otherwise = Maybe [ByteString]
forall a. Maybe a
Nothing
          pure $
            result === expected
              & counterexample ("Inserted offsets: " ++ show offsetsToInsert)
              & tabulate "lookupEbClosure (partial txs)" [timeBucket queryTime]
              & tabulate "numTxs" [magnitudeBucket numTxs]

-- | Property: a point with no body written returns Nothing.
prop_completedEbNoBody :: DbImpl -> Property
prop_completedEbNoBody :: DbImpl -> Property
prop_completedEbNoBody DbImpl
impl =
  Gen LeiosPoint -> (LeiosPoint -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen LeiosPoint
genPoint ((LeiosPoint -> Property) -> Property)
-> (LeiosPoint -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \LeiosPoint
point ->
    IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ DbImpl -> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
impl ((LeiosDbHandle IO -> IO Property) -> IO Property)
-> (LeiosDbHandle IO -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO -> (RW -> IO Property) -> IO Property
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> IO Property) -> IO Property)
-> (RW -> IO Property) -> IO Property
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
      (result, queryTime) <- IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime)
forall a. IO a -> IO (a, DiffTime)
timed (IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime))
-> IO (Maybe [ByteString]) -> IO (Maybe [ByteString], DiffTime)
forall a b. (a -> b) -> a -> b
$ RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
con LeiosPoint
point
      pure $
        result === Nothing
          & counterexample "Expected Nothing for EB with no body inserted"
          & tabulate "lookupEbClosure (no body)" [timeBucket queryTime]

-- * deleteEbsFrom on a partition file

test_truncateDropsEbsAfterSlot :: FilePath -> FilePath -> LeiosDbHandle IO -> IO ()
test_truncateDropsEbsAfterSlot :: [Char] -> [Char] -> LeiosDbHandle IO -> Assertion
test_truncateDropsEbsAfterSlot [Char]
volDbPath [Char]
_immDbPath LeiosDbHandle IO
db = do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      keptHash :: EndorserBlockHash
keptHash = Word -> EndorserBlockHash
mkTestEbHash Word
1
      droppedHash :: EndorserBlockHash
droppedHash = Word -> EndorserBlockHash
mkTestEbHash Word
2
  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
5 EndorserBlockHash
keptHash) EndorserBlock
eb
    -- The kept EB is announced again at a slot the truncation drops.
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
15 EndorserBlockHash
keptHash) EndorserBlock
eb
    RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
15 EndorserBlockHash
droppedHash) EndorserBlock
eb
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
5 EndorserBlockHash
keptHash) (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)
    RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
15 EndorserBlockHash
droppedHash) (EndorserBlock -> [(Int, ByteString)]
allTxsFor EndorserBlock
eb)

  IO Int -> Assertion
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Int -> Assertion) -> IO Int -> Assertion
forall a b. (a -> b) -> a -> b
$ [Char] -> (Database -> IO Int) -> IO Int
forall a. [Char] -> (Database -> IO a) -> IO a
withExistingLeiosDbFile [Char]
volDbPath (HasCallStack => Database -> SlotNo -> IO Int
Database -> SlotNo -> IO Int
`deleteEbsFrom` SlotNo
11)
  [Char] -> EndorserBlockHash -> IO Int
ebBodyRows [Char]
volDbPath EndorserBlockHash
droppedHash IO Int -> (Int -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Int -> Int -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Int
0)

  LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> do
    RW -> SlotNo -> IO [LeiosPoint]
rwScanComplete RW
con SlotNo
0 IO [LeiosPoint] -> ([LeiosPoint] -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ([LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
5 EndorserBlockHash
keptHash])
    RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
15 EndorserBlockHash
keptHash) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe (Serialised EndorserBlock)
forall a. Maybe a
Nothing)
    keptBody <- RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
5 EndorserBlockHash
keptHash)
    keptBody @?= Just (serialiseEndorserBlock eb)
    droppedBody <- rwLookupEbBody con (MkLeiosPoint 15 droppedHash)
    droppedBody @?= Nothing
    keptClosure <- rwLookupEbClosure con (MkLeiosPoint 5 keptHash)
    keptClosure @?= Just (map snd (allTxsFor eb))

-- | The immutable schema lacks the volatile-only columns and indexes; the
-- tooling that DBTruncater runs on both files must still work on it.
test_truncateImmutablePartition :: FilePath -> FilePath -> IO ()
test_truncateImmutablePartition :: [Char] -> [Char] -> Assertion
test_truncateImmutablePartition [Char]
volDbPath [Char]
immDbPath = do
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
_db -> () -> Assertion
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  IO Int -> Assertion
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Int -> Assertion) -> IO Int -> Assertion
forall a b. (a -> b) -> a -> b
$ [Char] -> (Database -> IO Int) -> IO Int
forall a. [Char] -> (Database -> IO a) -> IO a
withExistingLeiosDbFile [Char]
immDbPath (HasCallStack => Database -> SlotNo -> IO Int
Database -> SlotNo -> IO Int
`deleteEbsFrom` SlotNo
11)

-- * File initialisation

-- | Names of the tables and indexes in a database file.
schemaObjects :: FilePath -> IO [String]
schemaObjects :: [Char] -> IO [[Char]]
schemaObjects [Char]
path =
  IO Database
-> (Database -> Assertion)
-> (Database -> IO [[Char]])
-> IO [[Char]]
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (Text -> IO Database
SQL.open ([Char] -> Text
T.pack [Char]
path)) Database -> Assertion
SQL.close ((Database -> IO [[Char]]) -> IO [[Char]])
-> (Database -> IO [[Char]]) -> IO [[Char]]
forall a b. (a -> b) -> a -> b
$ \Database
db -> do
    Database -> Text -> Assertion
SQL.exec Database
db ([Char] -> Text
T.pack [Char]
"pragma busy_timeout = 1000;")
    IO Statement
-> (Statement -> Assertion)
-> (Statement -> IO [[Char]])
-> IO [[Char]]
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket
      (Database -> Text -> IO Statement
SQL.prepare Database
db ([Char] -> Text
T.pack [Char]
"SELECT name FROM sqlite_master WHERE type IN ('table', 'index')"))
      Statement -> Assertion
SQL.finalize
      ((Statement -> IO [[Char]]) -> IO [[Char]])
-> (Statement -> IO [[Char]]) -> IO [[Char]]
forall a b. (a -> b) -> a -> b
$ \Statement
stmt ->
        let loop :: [[Char]] -> IO [[Char]]
loop [[Char]]
acc =
              Statement -> IO StepResult
SQL.step Statement
stmt IO StepResult -> (StepResult -> IO [[Char]]) -> IO [[Char]]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                StepResult
SQL.Row -> Statement -> ColumnIndex -> IO Text
SQL.columnText Statement
stmt ColumnIndex
0 IO Text -> (Text -> IO [[Char]]) -> IO [[Char]]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
name -> [[Char]] -> IO [[Char]]
loop (Text -> [Char]
T.unpack Text
name [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: [[Char]]
acc)
                StepResult
SQL.Done -> [[Char]] -> IO [[Char]]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [[Char]]
acc
         in [[Char]] -> IO [[Char]]
loop []

test_schemaAtHandleCreation :: FilePath -> FilePath -> IO ()
test_schemaAtHandleCreation :: [Char] -> [Char] -> Assertion
test_schemaAtHandleCreation [Char]
volDbPath [Char]
immDbPath =
  -- No reader or writer is opened: the schema must not depend on one.
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
_db -> do
    [[Char]] -> ([Char] -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[Char]
volDbPath, [Char]
immDbPath] (([Char] -> Assertion) -> Assertion)
-> ([Char] -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \[Char]
path ->
      [Char] -> IO Bool
doesFileExist [Char]
path IO Bool -> (Bool -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool ([Char]
path [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" exists")
    vol <- [Char] -> IO [[Char]]
schemaObjects [Char]
volDbPath
    imm <- schemaObjects immDbPath
    forM_ [(vol, "vol"), (imm, "imm")] $ \([[Char]]
objs, [Char]
file) ->
      [[Char]] -> ([Char] -> Assertion) -> Assertion
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[Char]
"ebs"] (([Char] -> Assertion) -> Assertion)
-> ([Char] -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \[Char]
name ->
        HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool ([Char]
file [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" has " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
name) ([Char]
name [Char] -> [[Char]] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
objs)

-- | A file that exists without a schema, as a crash between creating the file
-- and applying the schema leaves it.
test_zeroByteFilesGetSchema :: FilePath -> FilePath -> IO ()
test_zeroByteFilesGetSchema :: [Char] -> [Char] -> Assertion
test_zeroByteFilesGetSchema [Char]
volDbPath [Char]
immDbPath = do
  [Char] -> [Char] -> Assertion
writeFile [Char]
volDbPath [Char]
""
  [Char] -> [Char] -> Assertion
writeFile [Char]
immDbPath [Char]
""
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db ->
    LeiosDbHandle IO -> (LeiosDbReader IO -> Assertion) -> Assertion
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbReader m -> m a) -> m a
withReader LeiosDbHandle IO
db ((LeiosDbReader IO -> Assertion) -> Assertion)
-> (LeiosDbReader IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbReader IO
reader -> do
      points <- LeiosDbReader IO -> HasCallStack => SlotNo -> IO [LeiosPoint]
forall (m :: * -> *).
LeiosDbReader m -> HasCallStack => SlotNo -> m [LeiosPoint]
scanCompleteEbClosuresNotOlderThanSlot LeiosDbReader IO
reader SlotNo
0
      points @?= []

test_reopenKeepsData :: FilePath -> FilePath -> IO ()
test_reopenKeepsData :: [Char] -> [Char] -> Assertion
test_reopenKeepsData [Char]
volDbPath [Char]
immDbPath = do
  let eb :: EndorserBlock
eb = Int -> EndorserBlock
mkTestEb Int
2
      ebHash :: EndorserBlockHash
ebHash = Word -> EndorserBlockHash
mkTestEbHash Word
1
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db ->
    LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con -> RW -> LeiosPoint -> EndorserBlock -> Assertion
rwInsertEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
5 EndorserBlockHash
ebHash) EndorserBlock
eb
  Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a.
Tracer IO TraceLeiosDb
-> [Char] -> [Char] -> (LeiosDbHandle IO -> IO a) -> IO a
withLeiosDBSQLite Tracer IO TraceLeiosDb
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer [Char]
volDbPath [Char]
immDbPath ((LeiosDbHandle IO -> Assertion) -> Assertion)
-> (LeiosDbHandle IO -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db ->
    LeiosDbHandle IO -> (RW -> Assertion) -> Assertion
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW -> Assertion) -> Assertion) -> (RW -> Assertion) -> Assertion
forall a b. (a -> b) -> a -> b
$ \RW
con ->
      RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
con (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
5 EndorserBlockHash
ebHash) IO (Maybe (Serialised EndorserBlock))
-> (Maybe (Serialised EndorserBlock) -> Assertion) -> Assertion
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Maybe (Serialised EndorserBlock)
-> Maybe (Serialised EndorserBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Serialised EndorserBlock -> Maybe (Serialised EndorserBlock)
forall a. a -> Maybe a
Just (EndorserBlock -> Serialised EndorserBlock
serialiseEndorserBlock EndorserBlock
eb))

-- | Open a database file on a connection of its own.
withSqlFile :: FilePath -> (SQL.Database -> IO a) -> IO a
withSqlFile :: forall a. [Char] -> (Database -> IO a) -> IO a
withSqlFile [Char]
path Database -> IO a
k =
  IO Database
-> (Database -> Assertion) -> (Database -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (Text -> IO Database
SQL.open ([Char] -> Text
T.pack [Char]
path)) Database -> Assertion
SQL.close ((Database -> IO a) -> IO a) -> (Database -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Database
sql -> do
    Database -> Text -> Assertion
SQL.exec Database
sql ([Char] -> Text
T.pack [Char]
"pragma busy_timeout = 1000;")
    Database -> IO a
k Database
sql

-- | An EB hash as an SQL blob literal.
sqlBlob :: EndorserBlockHash -> String
sqlBlob :: EndorserBlockHash -> [Char]
sqlBlob EndorserBlockHash
ebHash = [Char]
"x'" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> EndorserBlockHash -> [Char]
forall h a. Hash h a -> [Char]
hashToStringAsHex EndorserBlockHash
ebHash [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"'"

-- | The file's rows, of any EB.
ebRows :: FilePath -> IO Int
ebRows :: [Char] -> IO Int
ebRows [Char]
path = [Char] -> [Char] -> IO Int
queryCount [Char]
path [Char]
"SELECT COUNT(*) FROM ebs"

-- | How many rows of the file hold the EB.
ebBodyRows :: FilePath -> EndorserBlockHash -> IO Int
ebBodyRows :: [Char] -> EndorserBlockHash -> IO Int
ebBodyRows [Char]
path EndorserBlockHash
ebHash =
  [Char] -> [Char] -> IO Int
queryCount
    [Char]
path
    ([Char]
"SELECT COUNT(*) FROM ebs WHERE ebHashBytes = " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> EndorserBlockHash -> [Char]
sqlBlob EndorserBlockHash
ebHash)

-- | The slot of the file's row of the EB.
ebSlotIn :: FilePath -> EndorserBlockHash -> IO Int
ebSlotIn :: [Char] -> EndorserBlockHash -> IO Int
ebSlotIn [Char]
path EndorserBlockHash
ebHash =
  [Char] -> [Char] -> IO Int
queryCount [Char]
path ([Char]
"SELECT ebSlot FROM ebs WHERE ebHashBytes = " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> EndorserBlockHash -> [Char]
sqlBlob EndorserBlockHash
ebHash)

queryCount :: FilePath -> String -> IO Int
queryCount :: [Char] -> [Char] -> IO Int
queryCount [Char]
path [Char]
query =
  [Char] -> (Database -> IO Int) -> IO Int
forall a. [Char] -> (Database -> IO a) -> IO a
withSqlFile [Char]
path ((Database -> IO Int) -> IO Int) -> (Database -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Database
sql ->
    Database -> Text -> (Statement -> IO Int) -> IO Int
forall a. Database -> Text -> (Statement -> IO a) -> IO a
SQL.withStatement Database
sql ([Char] -> Text
T.pack [Char]
query) ((Statement -> IO Int) -> IO Int)
-> (Statement -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Statement
stmt ->
      Statement -> IO StepResult
SQL.step Statement
stmt IO StepResult -> IO Int -> IO Int
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> IO Int64 -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Statement -> ColumnIndex -> IO Int64
SQL.columnInt64 Statement
stmt ColumnIndex
0

-- * The two implementations agree

-- | An operation on the database: a write, or what the ChainDB has it do as
-- blocks become immutable.
data Op
  = OpBody LeiosPoint LocalFills
  | OpTxs LeiosPoint [TxOffset]
  | OpPromote EndorserBlockHash
  | OpGc SlotNo
  deriving Int -> Op -> [Char] -> [Char]
[Op] -> [Char] -> [Char]
Op -> [Char]
(Int -> Op -> [Char] -> [Char])
-> (Op -> [Char]) -> ([Op] -> [Char] -> [Char]) -> Show Op
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> Op -> [Char] -> [Char]
showsPrec :: Int -> Op -> [Char] -> [Char]
$cshow :: Op -> [Char]
show :: Op -> [Char]
$cshowList :: [Op] -> [Char] -> [Char]
showList :: [Op] -> [Char] -> [Char]
Show

-- | What each endorser block's body is, by its hash, with the index in the
-- pool of each transaction it references: the same hash always names the same
-- body, as it would on the wire.
type Bodies = Map.Map EndorserBlockHash (EndorserBlock, [Int])

-- | Property: running the same operations against both implementations, a
-- reader sees the same in each, each body write holds the same offsets, and
-- the scan finds the same endorser blocks complete.
--
-- The node runs on SQLite and its tests run on the in-memory implementation, so
-- those tests say something about the node only as far as the two agree.
prop_implementationsAgree :: Property
prop_implementationsAgree :: Property
prop_implementationsAgree =
  Gen Bodies -> (Bodies -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen Bodies
genBodies ((Bodies -> Property) -> Property)
-> (Bodies -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Bodies
bodies ->
    Gen [Op]
-> ([Op] -> [[Op]])
-> ([Op] -> [Char])
-> ([Op] -> Property)
-> Property
forall prop a.
Testable prop =>
Gen a -> (a -> [a]) -> (a -> [Char]) -> (a -> prop) -> Property
forAllShrinkShow (Bodies -> Gen [Op]
genOps Bodies
bodies) ((Op -> [Op]) -> [Op] -> [[Op]]
forall a. (a -> [a]) -> [a] -> [[a]]
shrinkList ([Op] -> Op -> [Op]
forall a b. a -> b -> a
const [])) [Op] -> [Char]
forall a. Show a => a -> [Char]
show (([Op] -> Property) -> Property) -> ([Op] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[Op]
ops ->
      IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ do
        ((nPromoted, nEvicted), inMemory) <-
          DbImpl
-> (LeiosDbHandle IO
    -> IO
         ((Int, Int),
          ([[Int]],
           ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
              Map Int ByteString)],
            [LeiosPoint]))))
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
forall a. DbImpl -> (LeiosDbHandle IO -> IO a) -> IO a
withFreshDb DbImpl
InMemory ((LeiosDbHandle IO
  -> IO
       ((Int, Int),
        ([[Int]],
         ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
            Map Int ByteString)],
          [LeiosPoint]))))
 -> IO
      ((Int, Int),
       ([[Int]],
        ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
           Map Int ByteString)],
         [LeiosPoint]))))
-> (LeiosDbHandle IO
    -> IO
         ((Int, Int),
          ([[Int]],
           ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
              Map Int ByteString)],
            [LeiosPoint]))))
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
forall a b. (a -> b) -> a -> b
$ \LeiosDbHandle IO
db -> LeiosDbHandle IO
-> Bodies
-> [Op]
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
observe LeiosDbHandle IO
db Bodies
bodies [Op]
ops
        (_, sqlite) <- withFreshDb SQLite $ \LeiosDbHandle IO
db -> LeiosDbHandle IO
-> Bodies
-> [Op]
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
observe LeiosDbHandle IO
db Bodies
bodies [Op]
ops
        pure $
          tabulate "endorser blocks promoted" [bucket nPromoted] $
            tabulate "endorser blocks evicted" [bucket nEvicted] $
              tabulate "local fills landed" [bucket (sum (map length (fst inMemory)))] $
                inMemory === sqlite
 where
  bucket :: a -> [Char]
bucket a
n = if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
3 then [Char]
"3+" else a -> [Char]
forall a. Show a => a -> [Char]
show a
n

-- | Three endorser blocks over a pool of eight transactions. Sometimes a
-- reference misstates its transaction's size.
genBodies :: Gen Bodies
genBodies :: Gen Bodies
genBodies =
  ([(EndorserBlockHash, (EndorserBlock, [Int]))] -> Bodies)
-> Gen [(EndorserBlockHash, (EndorserBlock, [Int]))] -> Gen Bodies
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(EndorserBlockHash, (EndorserBlock, [Int]))] -> Bodies
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList (Gen [(EndorserBlockHash, (EndorserBlock, [Int]))] -> Gen Bodies)
-> Gen [(EndorserBlockHash, (EndorserBlock, [Int]))] -> Gen Bodies
forall a b. (a -> b) -> a -> b
$
    [Word]
-> (Word -> Gen (EndorserBlockHash, (EndorserBlock, [Int])))
-> Gen [(EndorserBlockHash, (EndorserBlock, [Int]))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Word
1 .. Word
3] ((Word -> Gen (EndorserBlockHash, (EndorserBlock, [Int])))
 -> Gen [(EndorserBlockHash, (EndorserBlock, [Int]))])
-> (Word -> Gen (EndorserBlockHash, (EndorserBlock, [Int])))
-> Gen [(EndorserBlockHash, (EndorserBlock, [Int]))]
forall a b. (a -> b) -> a -> b
$ \Word
seed -> do
      indices <- (Int, Int) -> Gen Int
chooseInt (Int
1, Int
4) Gen Int -> (Int -> Gen [Int]) -> Gen [Int]
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
n -> Int -> Gen Int -> Gen [Int]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n ((Int, Int) -> Gen Int
chooseInt (Int
1, Int
8))
      refs <- forM indices $ \Int
i -> do
        let size :: BytesSize
size = Int -> BytesSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length (Int -> ByteString
poolTxBytes Int
i))
        misstate <- [(Int, Gen Bool)] -> Gen Bool
forall a. HasCallStack => [(Int, Gen a)] -> Gen a
frequency [(Int
9, Bool -> Gen Bool
forall a. a -> Gen a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False), (Int
1, Bool -> Gen Bool
forall a. a -> Gen a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True)]
        pure (hashLeiosTx (MkLeiosTx (poolTxBytes i)), if misstate then size + 1 else size)
      pure (mkTestEbHash seed, (mkEndorserBlock refs, indices))

-- | Writes, with tx offsets up to one past the largest body, so that some are
-- not in the body they are written for.
genOps :: Bodies -> Gen [Op]
genOps :: Bodies -> Gen [Op]
genOps Bodies
bodies = Gen Op -> Gen [Op]
forall a. Gen a -> Gen [a]
listOf Gen Op
genOp
 where
  genSlot :: Gen SlotNo
genSlot = Word64 -> SlotNo
SlotNo (Word64 -> SlotNo) -> (Int -> Word64) -> Int -> SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> SlotNo) -> Gen Int -> Gen SlotNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
chooseInt (Int
1, Int
4)
  genPointOf :: Gen LeiosPoint
genPointOf = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (SlotNo -> EndorserBlockHash -> LeiosPoint)
-> Gen SlotNo -> Gen (EndorserBlockHash -> LeiosPoint)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen SlotNo
genSlot Gen (EndorserBlockHash -> LeiosPoint)
-> Gen EndorserBlockHash -> Gen LeiosPoint
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [EndorserBlockHash] -> Gen EndorserBlockHash
forall a. HasCallStack => [a] -> Gen a
elements (Bodies -> [EndorserBlockHash]
forall k a. Map k a -> [k]
Map.keys Bodies
bodies)
  -- Mostly from a source that lists the same tx, which is the only kind that
  -- can land, another point of the same endorser block included; otherwise
  -- from anywhere.
  genFill :: LeiosPoint -> Gen (Int, TxLocation)
genFill LeiosPoint
point =
    [(Int, Gen (Int, TxLocation))] -> Gen (Int, TxLocation)
forall a. HasCallStack => [(Int, Gen a)] -> Gen a
frequency ([(Int, Gen (Int, TxLocation))] -> Gen (Int, TxLocation))
-> [(Int, Gen (Int, TxLocation))] -> Gen (Int, TxLocation)
forall a b. (a -> b) -> a -> b
$
      (Int
1, (,) (Int -> TxLocation -> (Int, TxLocation))
-> Gen Int -> Gen (TxLocation -> (Int, TxLocation))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int, Int) -> Gen Int
chooseInt (Int
0, Int
4) Gen (TxLocation -> (Int, TxLocation))
-> Gen TxLocation -> Gen (Int, TxLocation)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (LeiosPoint -> Int -> TxLocation
MkTxLocation (LeiosPoint -> Int -> TxLocation)
-> Gen LeiosPoint -> Gen (Int -> TxLocation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen LeiosPoint
genPointOf Gen (Int -> TxLocation) -> Gen Int -> Gen TxLocation
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 Int
chooseInt (Int
0, Int
4)))
        (Int, Gen (Int, TxLocation))
-> [(Int, Gen (Int, TxLocation))] -> [(Int, Gen (Int, TxLocation))]
forall a. a -> [a] -> [a]
: [ ( Int
3
            , do
                (dstOff, src, srcOff) <- [(Int, EndorserBlockHash, Int)]
-> Gen (Int, EndorserBlockHash, Int)
forall a. HasCallStack => [a] -> Gen a
elements [(Int, EndorserBlockHash, Int)]
matching
                slot <- genSlot
                pure (dstOff, MkTxLocation (MkLeiosPoint slot src) srcOff)
            )
          | let indices :: [Int]
indices = (EndorserBlock, [Int]) -> [Int]
forall a b. (a, b) -> b
snd (Bodies
bodies Bodies -> EndorserBlockHash -> (EndorserBlock, [Int])
forall k a. Ord k => Map k a -> k -> a
Map.! LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
point)
                matching :: [(Int, EndorserBlockHash, Int)]
matching =
                  [ (Int
dstOff, EndorserBlockHash
src, Int
srcOff)
                  | (Int
dstOff, Int
i) <- [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Int]
indices
                  , (EndorserBlockHash
src, (EndorserBlock
_, [Int]
srcIndices)) <- Bodies -> [(EndorserBlockHash, (EndorserBlock, [Int]))]
forall k a. Map k a -> [(k, a)]
Map.toList Bodies
bodies
                  , (Int
srcOff, Int
j) <- [Int] -> [Int] -> [(Int, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Int]
srcIndices
                  , Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
j
                  ]
          , Bool -> Bool
not ([(Int, EndorserBlockHash, Int)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Int, EndorserBlockHash, Int)]
matching)
          ]
  genOp :: Gen Op
genOp =
    [(Int, Gen Op)] -> Gen Op
forall a. HasCallStack => [(Int, Gen a)] -> Gen a
frequency
      [ (Int
3, Gen LeiosPoint
genPointOf Gen LeiosPoint -> (LeiosPoint -> Gen Op) -> Gen Op
forall a b. Gen a -> (a -> Gen b) -> Gen b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \LeiosPoint
point -> LeiosPoint -> Map Int TxLocation -> Op
OpBody LeiosPoint
point (Map Int TxLocation -> Op)
-> ([(Int, TxLocation)] -> Map Int TxLocation)
-> [(Int, TxLocation)]
-> Op
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, TxLocation)] -> Map Int TxLocation
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Int, TxLocation)] -> Op) -> Gen [(Int, TxLocation)] -> Gen Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Int, TxLocation) -> Gen [(Int, TxLocation)]
forall a. Gen a -> Gen [a]
listOf (LeiosPoint -> Gen (Int, TxLocation)
genFill LeiosPoint
point))
      , (Int
3, LeiosPoint -> [Int] -> Op
OpTxs (LeiosPoint -> [Int] -> Op) -> Gen LeiosPoint -> Gen ([Int] -> Op)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen LeiosPoint
genPointOf Gen ([Int] -> Op) -> Gen [Int] -> Gen Op
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Int] -> Gen [Int]
forall a. [a] -> Gen [a]
sublistOf [Int
0 .. Int
4])
      , (Int
3, EndorserBlockHash -> Op
OpPromote (EndorserBlockHash -> Op) -> Gen EndorserBlockHash -> Gen Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [EndorserBlockHash] -> Gen EndorserBlockHash
forall a. HasCallStack => [a] -> Gen a
elements (Bodies -> [EndorserBlockHash]
forall k a. Map k a -> [k]
Map.keys Bodies
bodies))
      , (Int
1, SlotNo -> Op
OpGc (SlotNo -> Op) -> (SlotNo -> SlotNo) -> SlotNo -> Op
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SlotNo -> SlotNo -> SlotNo
forall a. Num a => a -> a -> a
+ SlotNo
1) (SlotNo -> Op) -> Gen SlotNo -> Gen Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen SlotNo
genSlot)
      ]

-- | A transaction's bytes, by its index in the pool.
poolTxBytes :: Int -> BS.ByteString
poolTxBytes :: Int -> ByteString
poolTxBytes Int
i = Int -> Word8 -> ByteString
BS.replicate (Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)

-- | Run the operations, then read back everything a reader can see at each
-- point they name. Also how many endorser blocks the operations promoted and
-- evicted.
--
-- The operations run only as far as they meet the node's own preconditions: a
-- body is written once per point; and an
-- endorser block is promoted only with a complete closure, as the node promotes
-- only what an immutable block certifies, and its ChainSel adopts a block
-- certifying an endorser block only once it holds that endorser block's
-- closure. So this tracks which points and
-- bodies the garbage collection evicts: those of every endorser block all of
-- whose points are older than its slot, promoted or not, since a promoted one
-- has been copied; and every other point older than its slot. A point older
-- than the promoted slot is not written at all.
--
-- An endorser block is also promoted at most once: the node promotes only what
-- the immutable chain certifies, and a chain cannot certify the same endorser
-- block twice, since it cannot be valid twice.
observe ::
  LeiosDbHandle IO ->
  Bodies ->
  [Op] ->
  IO
    ( (Int, Int)
    , ( [[TxOffset]]
      , ( [ ( Maybe (Serialised EndorserBlock)
            , Maybe [BS.ByteString]
            , Map.Map TxOffset BS.ByteString
            )
          ]
        , [LeiosPoint]
        )
      )
    )
observe :: LeiosDbHandle IO
-> Bodies
-> [Op]
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
observe LeiosDbHandle IO
db Bodies
bodies [Op]
ops = LeiosDbHandle IO
-> (RW
    -> IO
         ((Int, Int),
          ([[Int]],
           ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
              Map Int ByteString)],
            [LeiosPoint]))))
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
forall a. LeiosDbHandle IO -> (RW -> IO a) -> IO a
withRW LeiosDbHandle IO
db ((RW
  -> IO
       ((Int, Int),
        ([[Int]],
         ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
            Map Int ByteString)],
          [LeiosPoint]))))
 -> IO
      ((Int, Int),
       ([[Int]],
        ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
           Map Int ByteString)],
         [LeiosPoint]))))
-> (RW
    -> IO
         ((Int, Int),
          ([[Int]],
           ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
              Map Int ByteString)],
            [LeiosPoint]))))
-> IO
     ((Int, Int),
      ([[Int]],
       ([(Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString)],
        [LeiosPoint])))
forall a b. (a -> b) -> a -> b
$ \RW
rw -> do
  (counts, held) <- RW
-> SlotNo
-> [LeiosPoint]
-> [LeiosPoint]
-> [EndorserBlockHash]
-> Int
-> [Op]
-> IO ((Int, Int), [[Int]])
go RW
rw SlotNo
0 [] [] [] Int
0 [Op]
ops
  seen <- forM (nub [p | op <- ops, Just p <- [opPoint op]]) $ \LeiosPoint
point ->
    (,,)
      (Maybe (Serialised EndorserBlock)
 -> Maybe [ByteString]
 -> Map Int ByteString
 -> (Maybe (Serialised EndorserBlock), Maybe [ByteString],
     Map Int ByteString))
-> IO (Maybe (Serialised EndorserBlock))
-> IO
     (Maybe [ByteString]
      -> Map Int ByteString
      -> (Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RW -> LeiosPoint -> IO (Maybe (Serialised EndorserBlock))
rwLookupEbBody RW
rw LeiosPoint
point
      IO
  (Maybe [ByteString]
   -> Map Int ByteString
   -> (Maybe (Serialised EndorserBlock), Maybe [ByteString],
       Map Int ByteString))
-> IO (Maybe [ByteString])
-> IO
     (Map Int ByteString
      -> (Maybe (Serialised EndorserBlock), Maybe [ByteString],
          Map Int ByteString))
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RW -> LeiosPoint -> IO (Maybe [ByteString])
rwLookupEbClosure RW
rw LeiosPoint
point
      IO
  (Map Int ByteString
   -> (Maybe (Serialised EndorserBlock), Maybe [ByteString],
       Map Int ByteString))
-> IO (Map Int ByteString)
-> IO
     (Maybe (Serialised EndorserBlock), Maybe [ByteString],
      Map Int ByteString)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RW -> LeiosPoint -> [Int] -> IO (Map Int ByteString)
rwBatchRetrieveTxs
        RW
rw
        LeiosPoint
point
        [Int
0 .. [(TxHash, BytesSize)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (EndorserBlock -> [(TxHash, BytesSize)]
endorserBlockReferences ((EndorserBlock, [Int]) -> EndorserBlock
forall a b. (a, b) -> a
fst (Bodies
bodies Bodies -> EndorserBlockHash -> (EndorserBlock, [Int])
forall k a. Ord k => Map k a -> k -> a
Map.! LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
point)))]
  complete <- rwScanComplete rw 0
  pure (counts, (held, (seen, complete)))
 where
  go :: RW
-> SlotNo
-> [LeiosPoint]
-> [LeiosPoint]
-> [EndorserBlockHash]
-> Int
-> [Op]
-> IO ((Int, Int), [[Int]])
go RW
_rw SlotNo
_promotedSlot [LeiosPoint]
_points [LeiosPoint]
_bodied [EndorserBlockHash]
promoted Int
nEvicted [] = ((Int, Int), [[Int]]) -> IO ((Int, Int), [[Int]])
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([EndorserBlockHash] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [EndorserBlockHash]
promoted, Int
nEvicted), [])
  go RW
rw SlotNo
promotedSlot [LeiosPoint]
points [LeiosPoint]
bodied [EndorserBlockHash]
promoted Int
nEvicted (Op
op : [Op]
rest) = case Op
op of
    OpBody LeiosPoint
point Map Int TxLocation
fills
      | LeiosPoint
point LeiosPoint -> [LeiosPoint] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [LeiosPoint]
bodied -> do
          held <- RW -> LeiosPoint -> EndorserBlock -> Map Int TxLocation -> IO [Int]
rwInsertEbBodyFilling RW
rw LeiosPoint
point ((EndorserBlock, [Int]) -> EndorserBlock
forall a b. (a, b) -> a
fst (Bodies
bodies Bodies -> EndorserBlockHash -> (EndorserBlock, [Int])
forall k a. Ord k => Map k a -> k -> a
Map.! LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
point)) Map Int TxLocation
fills
          let bodied' = if LeiosPoint -> Bool
collected LeiosPoint
point then [LeiosPoint]
bodied else LeiosPoint
point LeiosPoint -> [LeiosPoint] -> [LeiosPoint]
forall a. a -> [a] -> [a]
: [LeiosPoint]
bodied
          fmap (held :) <$> go rw promotedSlot (written point points) bodied' promoted nEvicted rest
      | Bool
otherwise -> RW
-> SlotNo
-> [LeiosPoint]
-> [LeiosPoint]
-> [EndorserBlockHash]
-> Int
-> [Op]
-> IO ((Int, Int), [[Int]])
go RW
rw SlotNo
promotedSlot [LeiosPoint]
points [LeiosPoint]
bodied [EndorserBlockHash]
promoted Int
nEvicted [Op]
rest
    OpTxs LeiosPoint
point [Int]
offsets -> do
      let indices :: [Int]
indices = (EndorserBlock, [Int]) -> [Int]
forall a b. (a, b) -> b
snd (Bodies
bodies Bodies -> EndorserBlockHash -> (EndorserBlock, [Int])
forall k a. Ord k => Map k a -> k -> a
Map.! LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
point)
      RW -> LeiosPoint -> [(Int, ByteString)] -> Assertion
rwInsertTxs
        RW
rw
        LeiosPoint
point
        [ (Int
offset, Int -> ByteString
poolTxBytes (if Int
offset Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
indices then [Int]
indices [Int] -> Int -> Int
forall a. HasCallStack => [a] -> Int -> a
!! Int
offset else Int
1))
        | Int
offset <- [Int]
offsets
        ]
      RW
-> SlotNo
-> [LeiosPoint]
-> [LeiosPoint]
-> [EndorserBlockHash]
-> Int
-> [Op]
-> IO ((Int, Int), [[Int]])
go RW
rw SlotNo
promotedSlot [LeiosPoint]
points [LeiosPoint]
bodied [EndorserBlockHash]
promoted Int
nEvicted [Op]
rest
    OpPromote EndorserBlockHash
ebHash -> case [LeiosPoint
p | LeiosPoint
p <- [LeiosPoint]
points, LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
p EndorserBlockHash -> EndorserBlockHash -> Bool
forall a. Eq a => a -> a -> Bool
== EndorserBlockHash
ebHash, EndorserBlockHash
ebHash EndorserBlockHash -> [EndorserBlockHash] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [EndorserBlockHash]
promoted] of
      LeiosPoint
point : [LeiosPoint]
_ -> do
        complete <- RW -> LeiosPoint -> IO Bool
holdsClosure RW
rw LeiosPoint
point
        if complete
          then do
            leiosDbPromoteToImmutable db promotedSlot [point]
            go rw promotedSlot points bodied (nub (ebHash : promoted)) nEvicted rest
          else go rw promotedSlot points bodied promoted nEvicted rest
      [] -> RW
-> SlotNo
-> [LeiosPoint]
-> [LeiosPoint]
-> [EndorserBlockHash]
-> Int
-> [Op]
-> IO ((Int, Int), [[Int]])
go RW
rw SlotNo
promotedSlot [LeiosPoint]
points [LeiosPoint]
bodied [EndorserBlockHash]
promoted Int
nEvicted [Op]
rest
    OpGc SlotNo
slot -> do
      LeiosDbHandle IO -> SlotNo -> Assertion
collectBefore LeiosDbHandle IO
db SlotNo
slot
      let slot' :: SlotNo
slot' = SlotNo -> SlotNo -> SlotNo
forall a. Ord a => a -> a -> a
max SlotNo
slot SlotNo
promotedSlot
          newest :: EndorserBlockHash -> SlotNo
newest EndorserBlockHash
ebHash = [SlotNo] -> SlotNo
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [LeiosPoint -> SlotNo
pointSlotNo LeiosPoint
p | LeiosPoint
p <- [LeiosPoint]
points, LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
p EndorserBlockHash -> EndorserBlockHash -> Bool
forall a. Eq a => a -> a -> Bool
== EndorserBlockHash
ebHash]
          evicted :: [EndorserBlockHash]
evicted =
            [EndorserBlockHash] -> [EndorserBlockHash]
forall a. Eq a => [a] -> [a]
nub [EndorserBlockHash
ebHash | LeiosPoint
p <- [LeiosPoint]
points, let ebHash :: EndorserBlockHash
ebHash = LeiosPoint -> EndorserBlockHash
pointEbHash LeiosPoint
p, EndorserBlockHash -> SlotNo
newest EndorserBlockHash
ebHash SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< SlotNo
slot']
          kept :: LeiosPoint -> Bool
kept LeiosPoint
p = LeiosPoint -> SlotNo
pointSlotNo LeiosPoint
p SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
>= SlotNo
slot'
      RW
-> SlotNo
-> [LeiosPoint]
-> [LeiosPoint]
-> [EndorserBlockHash]
-> Int
-> [Op]
-> IO ((Int, Int), [[Int]])
go
        RW
rw
        SlotNo
slot'
        ((LeiosPoint -> Bool) -> [LeiosPoint] -> [LeiosPoint]
forall a. (a -> Bool) -> [a] -> [a]
filter LeiosPoint -> Bool
kept [LeiosPoint]
points)
        ((LeiosPoint -> Bool) -> [LeiosPoint] -> [LeiosPoint]
forall a. (a -> Bool) -> [a] -> [a]
filter LeiosPoint -> Bool
kept [LeiosPoint]
bodied)
        [EndorserBlockHash]
promoted
        (Int
nEvicted Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [EndorserBlockHash] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [EndorserBlockHash]
evicted)
        [Op]
rest
   where
    collected :: LeiosPoint -> Bool
collected LeiosPoint
point = LeiosPoint -> SlotNo
pointSlotNo LeiosPoint
point SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< SlotNo
promotedSlot
    written :: LeiosPoint -> [LeiosPoint] -> [LeiosPoint]
written LeiosPoint
point [LeiosPoint]
ps
      | LeiosPoint -> Bool
collected LeiosPoint
point Bool -> Bool -> Bool
|| LeiosPoint
point LeiosPoint -> [LeiosPoint] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [LeiosPoint]
ps = [LeiosPoint]
ps
      | Bool
otherwise = LeiosPoint
point LeiosPoint -> [LeiosPoint] -> [LeiosPoint]
forall a. a -> [a] -> [a]
: [LeiosPoint]
ps
  opPoint :: Op -> Maybe LeiosPoint
opPoint = \case
    OpBody LeiosPoint
point Map Int TxLocation
_ -> LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just LeiosPoint
point
    OpTxs LeiosPoint
point [Int]
_ -> LeiosPoint -> Maybe LeiosPoint
forall a. a -> Maybe a
Just LeiosPoint
point
    Op
_ -> Maybe LeiosPoint
forall a. Maybe a
Nothing