{-# LANGUAGE TypeApplications #-}

-- | Tests of how the ChainDB and the LeiosDB interact: which endorser blocks the
-- ChainDB has the LeiosDB keep, once the blocks that announce and certify them
-- are immutable, and when ChainSel may select a block certifying one.
module Test.Consensus.Leios.ChainDB (tests) where

import Cardano.Crypto.Hash (Hash (UnsafeHash))
import Cardano.Crypto.Leios (VerificationError (..))
import Control.Monad (forM_, void)
import Control.Monad.Class.MonadSTM (atomically)
import Control.Monad.Class.MonadTimer.SI (threadDelay)
import Control.Monad.IOSim (IOSim, runSimOrThrow)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as SBS
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.Map.Strict as Map
import Data.Maybe (isJust)
import Ouroboros.Consensus.Block
  ( Point
  , Proxy (..)
  , SlotNo
  , WithOrigin (..)
  , blockHash
  , blockPoint
  , blockSlot
  , castPoint
  )
import Ouroboros.Consensus.Ledger.Extended (ExtValidationError (..))
import Ouroboros.Consensus.Leios.Acquisitions
  ( LeiosAcquisition (..)
  , LeiosAcquisitions (..)
  )
import Ouroboros.Consensus.Leios.Cert (LeiosCertRejection (..), LeiosClaim (..))
import Ouroboros.Consensus.Leios.Protocol (LeiosAnnouncement (..))
import Ouroboros.Consensus.Leios.Types
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Client.Replies (LeiosFetchClientContext (..))
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Decision (isFocusedPoint)
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Kernel (LeiosFetchKernel (..))
import Ouroboros.Consensus.NodeKernel (NodeKernel (..))
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
import Ouroboros.Consensus.Storage.LeiosDB (newLeiosDBInMemory)
import Ouroboros.Consensus.Storage.LeiosDB.API
  ( LeiosDbHandle (..)
  , LeiosDbReader (..)
  , LeiosDbWriter (..)
  , Promise (..)
  , withReader
  , withWriter
  )
import Ouroboros.Consensus.Util.IOLike (readMVar)
import Ouroboros.Consensus.Util.STM (forgetFingerprint)
import qualified Ouroboros.Network.AnchoredFragment as AF
import Test.Consensus.Leios.Committee (certOf)
import Test.Consensus.Leios.NodeUnderTest
import Test.Consensus.Leios.TestBlock
import Test.Tasty
import Test.Tasty.HUnit
import Test.Util.ChainDB (emptyNodeDBs)

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup
    TestName
"ChainDB and LeiosDB"
    [ TestName -> Assertion -> TestTree
testCase TestName
"ChainSel rejects a block whose certificate a minority made" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s. IOSim s (Maybe (ExtValidationError LeiosTestBlock)))
-> Maybe (ExtValidationError LeiosTestBlock)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow
          ((NodeUnderTest (IOSim s) -> IOSim s ())
-> LeiosTestBlock
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
forall s.
(NodeUnderTest (IOSim s) -> IOSim s ())
-> LeiosTestBlock
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
whyInvalid (\NodeUnderTest (IOSim s)
node -> NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock
announcingA, LeiosTestBlock
minorityCertifyingA]) LeiosTestBlock
minorityCertifyingA)
          Maybe (ExtValidationError LeiosTestBlock)
-> Maybe (ExtValidationError LeiosTestBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= ExtValidationError LeiosTestBlock
-> Maybe (ExtValidationError LeiosTestBlock)
forall a. a -> Maybe a
Just (LeiosCertRejection -> ExtValidationError LeiosTestBlock
forall blk. LeiosCertRejection -> ExtValidationError blk
ExtValidationErrorLeiosCert (VerificationError -> LeiosCertRejection
LeiosCertInvalid (Ratio Integer -> VerificationError
InsufficientWeight (Ratio Integer
1 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Ratio Integer
4))))
    , TestName -> Assertion -> TestTree
testCase TestName
"ChainSel does not verify a certificate again for a claim it established" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s. IOSim s (Maybe (ExtValidationError LeiosTestBlock)))
-> Maybe (ExtValidationError LeiosTestBlock)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ((NodeUnderTest (IOSim s) -> IOSim s ())
-> LeiosTestBlock
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
forall s.
(NodeUnderTest (IOSim s) -> IOSim s ())
-> LeiosTestBlock
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
whyInvalid NodeUnderTest (IOSim s) -> IOSim s ()
forall s. NodeUnderTest (IOSim s) -> IOSim s ()
addSibling LeiosTestBlock
minoritySibling) Maybe (ExtValidationError LeiosTestBlock)
-> Maybe (ExtValidationError LeiosTestBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Maybe (ExtValidationError LeiosTestBlock)
forall a. Maybe a
Nothing
    , TestName -> Assertion -> TestTree
testCase TestName
"a restart forgets a certifying block off the selection, until it arrives again" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s. IOSim s (Bool, Bool, Bool)) -> (Bool, Bool, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow (WhatOfA -> IOSim s (Bool, Bool, Bool)
forall s. WhatOfA -> IOSim s (Bool, Bool, Bool)
heldAcrossRestart WhatOfA
BodyOfA) (Bool, Bool, Bool) -> (Bool, Bool, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
True, Bool
False, Bool
True)
    , TestName -> Assertion -> TestTree
testCase TestName
"a restart keeps a certifying block on the selection" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s. IOSim s (Bool, Bool, Bool)) -> (Bool, Bool, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow (WhatOfA -> IOSim s (Bool, Bool, Bool)
forall s. WhatOfA -> IOSim s (Bool, Bool, Bool)
heldAcrossRestart WhatOfA
ClosureOfA) (Bool, Bool, Bool) -> (Bool, Bool, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (Bool
True, Bool
True, Bool
True)
    , TestName -> Assertion -> TestTree
testCase TestName
"the GC keeps an endorser block an immutable block certifies" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (Maybe (Serialised EndorserBlock),
 Maybe (Serialised EndorserBlock))
-> Maybe (Serialised EndorserBlock)
forall a b. (a, b) -> a
fst ((forall s.
 IOSim
   s
   (Maybe (Serialised EndorserBlock),
    Maybe (Serialised EndorserBlock)))
-> (Maybe (Serialised EndorserBlock),
    Maybe (Serialised EndorserBlock))
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim
  s
  (Maybe (Serialised EndorserBlock),
   Maybe (Serialised EndorserBlock))
forall s.
IOSim
  s
  (Maybe (Serialised EndorserBlock),
   Maybe (Serialised EndorserBlock))
bodiesAfterGc)
          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
ebA)
    , TestName -> Assertion -> TestTree
testCase TestName
"the GC evicts an endorser block no block certifies" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (Maybe (Serialised EndorserBlock),
 Maybe (Serialised EndorserBlock))
-> Maybe (Serialised EndorserBlock)
forall a b. (a, b) -> b
snd ((forall s.
 IOSim
   s
   (Maybe (Serialised EndorserBlock),
    Maybe (Serialised EndorserBlock)))
-> (Maybe (Serialised EndorserBlock),
    Maybe (Serialised EndorserBlock))
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim
  s
  (Maybe (Serialised EndorserBlock),
   Maybe (Serialised EndorserBlock))
forall s.
IOSim
  s
  (Maybe (Serialised EndorserBlock),
   Maybe (Serialised EndorserBlock))
bodiesAfterGc) 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
    , TestName -> Assertion -> TestTree
testCase TestName
"ChainSel parks a certifying block until the closure arrives" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s. IOSim s (Point LeiosTestBlock, Point LeiosTestBlock))
-> (Point LeiosTestBlock, Point LeiosTestBlock)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim s (Point LeiosTestBlock, Point LeiosTestBlock)
forall s. IOSim s (Point LeiosTestBlock, Point LeiosTestBlock)
tipsAroundClosure
          (Point LeiosTestBlock, Point LeiosTestBlock)
-> (Point LeiosTestBlock, Point LeiosTestBlock) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
announcingA, LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
certifyingA)
    , TestName -> Assertion -> TestTree
testCase TestName
"ChainSel selects a certifying block whose closure the LeiosDB held at start" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s. IOSim s (Point LeiosTestBlock)) -> Point LeiosTestBlock
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim s (Point LeiosTestBlock)
forall s. IOSim s (Point LeiosTestBlock)
tipWithClosureBeforeStart Point LeiosTestBlock -> Point LeiosTestBlock -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
certifyingA
    , TestName -> Assertion -> TestTree
testCase
        TestName
"after a crash, the ImmutableDB drops a block certifying an endorser block the LeiosDB lost"
        (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ (forall s. IOSim s (Point LeiosTestBlock, Bool))
-> (Point LeiosTestBlock, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ([LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
forall s.
[LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
afterCrash [LeiosTestBlock]
immutableChain LeiosPoint
pointA AOnRestart
LostA) (Point LeiosTestBlock, Bool)
-> (Point LeiosTestBlock, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
announcingA, Bool
False)
    , TestName -> Assertion -> TestTree
testCase
        TestName
"after a crash, the ImmutableDB keeps a block certifying an endorser block the LeiosDB holds"
        (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ (forall s. IOSim s (Point LeiosTestBlock, Bool))
-> (Point LeiosTestBlock, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ([LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
forall s.
[LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
afterCrash [LeiosTestBlock]
immutableChain LeiosPoint
pointA AOnRestart
KeptA)
          (Point LeiosTestBlock, Bool)
-> (Point LeiosTestBlock, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint ([LeiosTestBlock]
immutableChain [LeiosTestBlock] -> Int -> LeiosTestBlock
forall a. HasCallStack => [a] -> Int -> a
!! Int
5), Bool
True)
    , TestName -> Assertion -> TestTree
testCase
        TestName
"after a crash, the ImmutableDB drops a block starting a chunk that certifies an endorser block the LeiosDB lost"
        (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ (forall s. IOSim s (Point LeiosTestBlock, Bool))
-> (Point LeiosTestBlock, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ([LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
forall s.
[LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
afterCrash [LeiosTestBlock]
chunkStartChain LeiosPoint
chunkStartPointA AOnRestart
LostA)
          (Point LeiosTestBlock, Bool)
-> (Point LeiosTestBlock, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
chunkStartAnnouncingA, Bool
False)
    , TestName -> Assertion -> TestTree
testCase
        TestName
"after a crash, the ImmutableDB keeps a block starting a chunk that certifies an endorser block the LeiosDB holds"
        (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ (forall s. IOSim s (Point LeiosTestBlock, Bool))
-> (Point LeiosTestBlock, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ([LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
forall s.
[LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
afterCrash [LeiosTestBlock]
chunkStartChain LeiosPoint
chunkStartPointA AOnRestart
KeptA)
          (Point LeiosTestBlock, Bool)
-> (Point LeiosTestBlock, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint ([LeiosTestBlock]
chunkStartChain [LeiosTestBlock] -> Int -> LeiosTestBlock
forall a. HasCallStack => [a] -> Int -> a
!! Int
13), Bool
True)
    , TestName -> Assertion -> TestTree
testCase
        TestName
"after losing an endorser block's row, the LeiosDB drops the later ones and takes it again"
        (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$ (forall s. IOSim s (Point LeiosTestBlock, Bool, Bool))
-> (Point LeiosTestBlock, Bool, Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim s (Point LeiosTestBlock, Bool, Bool)
forall s. IOSim s (Point LeiosTestBlock, Bool, Bool)
lostRowBeforeAnother (Point LeiosTestBlock, Bool, Bool)
-> (Point LeiosTestBlock, Bool, Bool) -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
announcingA, Bool
False, Bool
True)
    , TestName -> Assertion -> TestTree
testCase TestName
"a verified certificate has LeiosFetch fetch the endorser block it certifies" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (forall s.
 IOSim s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool))
-> ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
forall s.
IOSim s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
certifiedIsFetched ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
-> ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
-> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= ([(LeiosTestBlock -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot LeiosTestBlock
announcingA, LeiosPoint -> Hash HASH EraIndependentEbReferences
pointEbHash LeiosPoint
pointA)], Bool
True)
    ]

-- | The node adds 'announcingA' and 'certifyingA', whose certificate ChainSel
-- verifies. For each new valid claim the ChainDB then passes on, its slot and
-- the endorser block its announcement names; and whether LeiosFetch is
-- fetching A.
certifiedIsFetched :: IOSim s ([(SlotNo, EndorserBlockHash)], Bool)
certifiedIsFetched :: forall s.
IOSim s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
certifiedIsFetched = (NodeUnderTest (IOSim s)
 -> IOSim
      s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool))
-> IOSim s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
forall s a. (NodeUnderTest (IOSim s) -> IOSim s a) -> IOSim s a
withNode ((NodeUnderTest (IOSim s)
  -> IOSim
       s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool))
 -> IOSim
      s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool))
-> (NodeUnderTest (IOSim s)
    -> IOSim
         s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool))
-> IOSim s ([(SlotNo, Hash HASH EraIndependentEbReferences)], Bool)
forall a b. (a -> b) -> a -> b
$ \NodeUnderTest (IOSim s)
node -> do
  -- Let the slots of the chain's blocks pass, so none is from the future.
  DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
20
  NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock
announcingA, LeiosTestBlock
certifyingA]
  DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
1
  validClaims <- STM (IOSim s) (Map LeiosClaim LeiosAnnouncement)
-> IOSim s (Map LeiosClaim LeiosAnnouncement)
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM (IOSim s) (Map LeiosClaim LeiosAnnouncement)
 -> IOSim s (Map LeiosClaim LeiosAnnouncement))
-> STM (IOSim s) (Map LeiosClaim LeiosAnnouncement)
-> IOSim s (Map LeiosClaim LeiosAnnouncement)
forall a b. (a -> b) -> a -> b
$ NodeUnderTest (IOSim s)
-> STM (IOSim s) (Map LeiosClaim LeiosAnnouncement)
forall (m :: * -> *).
NodeUnderTest m -> STM m (Map LeiosClaim LeiosAnnouncement)
nodeNewLeiosValidClaims NodeUnderTest (IOSim s)
node
  outstanding <- readMVar (lfcOutstanding (lfkContext (getLeiosFetchKernel (nodeKernel node))))
  pure
    ( [ (leiosClaimSlot claim, announcementEbHash ann)
      | (claim, ann) <- Map.toList validClaims
      ]
    , isFocusedPoint pointA outstanding
    )

-- | The node holds endorser blocks A and B, with their transactions. The
-- chain's first block announces A, its second certifies A, and its third
-- announces B, which nothing certifies. Once the immutable tip has passed both
-- announcements and the GC has run, these are the bodies of A and B that the
-- LeiosDB still holds.
bodiesAfterGc :: IOSim s (Maybe (Serialised EndorserBlock), Maybe (Serialised EndorserBlock))
bodiesAfterGc :: forall s.
IOSim
  s
  (Maybe (Serialised EndorserBlock),
   Maybe (Serialised EndorserBlock))
bodiesAfterGc = do
  leiosDb <- IOSim s (LeiosDbHandle (IOSim s))
forall (m :: * -> *). IOLike m => m (LeiosDbHandle m)
newLeiosDBInMemory
  withWriter leiosDb $ \LeiosDbWriter (IOSim s)
writer ->
    [(LeiosPoint, EndorserBlock, ByteString)]
-> ((LeiosPoint, EndorserBlock, ByteString) -> IOSim s ())
-> IOSim s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(LeiosPoint
pointA, EndorserBlock
ebA, ByteString
txA), (LeiosPoint
pointB, EndorserBlock
ebB, ByteString
txB)] (((LeiosPoint, EndorserBlock, ByteString) -> IOSim s ())
 -> IOSim s ())
-> ((LeiosPoint, EndorserBlock, ByteString) -> IOSim s ())
-> IOSim s ()
forall a b. (a -> b) -> a -> b
$ \(LeiosPoint
point, EndorserBlock
eb, ByteString
tx) -> do
      IOSim s (Set Int) -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s (Set Int) -> IOSim s ())
-> IOSim s (Set Int) -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ Promise (IOSim s) (Set Int) -> IOSim s (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise (IOSim s) (Set Int) -> IOSim s (Set Int))
-> IOSim s (Promise (IOSim s) (Set Int)) -> IOSim s (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock
   -> Map Int TxLocation
   -> IOSim s (Promise (IOSim s) (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody LeiosDbWriter (IOSim s)
writer LeiosPoint
point EndorserBlock
eb Map Int TxLocation
forall a. Monoid a => a
mempty
      IOSim s () -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s () -> IOSim s ()) -> IOSim s () -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ Promise (IOSim s) () -> IOSim s ()
forall (m :: * -> *) a. Promise m a -> m a
await (Promise (IOSim s) () -> IOSim s ())
-> IOSim s (Promise (IOSim s) ()) -> IOSim s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> IOSim s (Promise (IOSim s) ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs LeiosDbWriter (IOSim s)
writer LeiosPoint
point (Int -> ByteString -> Map Int ByteString
forall k a. k -> a -> Map k a
Map.singleton Int
0 ByteString
tx)
  withNodeOver leiosDb $ \NodeUnderTest (IOSim s)
node -> do
    -- Let the slots of the chain's blocks pass, so none is from the future.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
20
    NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock]
immutableChain
    -- Long enough for the copy to the ImmutableDB and for the delayed GC.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
100
    LeiosDbHandle (IOSim s)
-> (LeiosDbReader (IOSim s)
    -> IOSim
         s
         (Maybe (Serialised EndorserBlock),
          Maybe (Serialised EndorserBlock)))
-> IOSim
     s
     (Maybe (Serialised EndorserBlock),
      Maybe (Serialised EndorserBlock))
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbReader m -> m a) -> m a
withReader LeiosDbHandle (IOSim s)
leiosDb ((LeiosDbReader (IOSim s)
  -> IOSim
       s
       (Maybe (Serialised EndorserBlock),
        Maybe (Serialised EndorserBlock)))
 -> IOSim
      s
      (Maybe (Serialised EndorserBlock),
       Maybe (Serialised EndorserBlock)))
-> (LeiosDbReader (IOSim s)
    -> IOSim
         s
         (Maybe (Serialised EndorserBlock),
          Maybe (Serialised EndorserBlock)))
-> IOSim
     s
     (Maybe (Serialised EndorserBlock),
      Maybe (Serialised EndorserBlock))
forall a b. (a -> b) -> a -> b
$ \LeiosDbReader (IOSim s)
reader ->
      (,)
        (Maybe (Serialised EndorserBlock)
 -> Maybe (Serialised EndorserBlock)
 -> (Maybe (Serialised EndorserBlock),
     Maybe (Serialised EndorserBlock)))
-> IOSim s (Maybe (Serialised EndorserBlock))
-> IOSim
     s
     (Maybe (Serialised EndorserBlock)
      -> (Maybe (Serialised EndorserBlock),
          Maybe (Serialised EndorserBlock)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LeiosDbReader (IOSim s)
-> HasCallStack =>
   LeiosPoint -> IOSim s (Maybe (Serialised EndorserBlock))
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack =>
   LeiosPoint -> m (Maybe (Serialised EndorserBlock))
lookupEbBody LeiosDbReader (IOSim s)
reader LeiosPoint
pointA
        IOSim
  s
  (Maybe (Serialised EndorserBlock)
   -> (Maybe (Serialised EndorserBlock),
       Maybe (Serialised EndorserBlock)))
-> IOSim s (Maybe (Serialised EndorserBlock))
-> IOSim
     s
     (Maybe (Serialised EndorserBlock),
      Maybe (Serialised EndorserBlock))
forall a b. IOSim s (a -> b) -> IOSim s a -> IOSim s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> LeiosDbReader (IOSim s)
-> HasCallStack =>
   LeiosPoint -> IOSim s (Maybe (Serialised EndorserBlock))
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack =>
   LeiosPoint -> m (Maybe (Serialised EndorserBlock))
lookupEbBody LeiosDbReader (IOSim s)
reader LeiosPoint
pointB

-- | Sixteen blocks: as k is 10, the immutable tip is the sixth, past the
-- announcements of A and B, and past the block certifying A.
immutableChain :: [LeiosTestBlock]
immutableChain :: [LeiosTestBlock]
immutableChain = Int -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. Int -> [a] -> [a]
take Int
16 ([LeiosTestBlock] -> [LeiosTestBlock])
-> [LeiosTestBlock] -> [LeiosTestBlock]
forall a b. (a -> b) -> a -> b
$ LeiosTestBlock
announcingA LeiosTestBlock -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. a -> [a] -> [a]
: LeiosTestBlock
certifyingA LeiosTestBlock -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. a -> [a] -> [a]
: (LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> [LeiosTestBlock]
forall a. (a -> a) -> a -> [a]
iterate LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
announcingB

-- | What the LeiosDB holds of A after a crash
data AOnRestart
  = -- | Nothing
    LostA
  | -- | Its promoted closure
    KeptA

-- | The node adds the chain, which certifies A at the given point, over a
-- LeiosDB holding A's closure there, so A is promoted. Then it restarts after a
-- crash over a LeiosDB holding the given part of A. Its ImmutableDB tip after
-- the restart, and whether the LeiosDB then holds A's closure.
afterCrash ::
  [LeiosTestBlock] -> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
afterCrash :: forall s.
[LeiosTestBlock]
-> LeiosPoint -> AOnRestart -> IOSim s (Point LeiosTestBlock, Bool)
afterCrash [LeiosTestBlock]
chain LeiosPoint
point AOnRestart
aOnRestart = do
  nodeDBs <- IOSim s (NodeDBs (StrictTMVar (IOSim s) MockFS))
forall (m :: * -> *).
MonadSTM m =>
m (NodeDBs (StrictTMVar m MockFS))
emptyNodeDBs
  leiosDb <- newLeiosDBInMemory
  writeClosureOfA leiosDb
  withNodeOverDBs nodeDBs leiosDb $ \NodeUnderTest (IOSim s)
node -> do
    -- Let the slots of the chain's blocks pass, so none is from the future.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
30
    NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock]
chain
    -- Long enough for the copy to the ImmutableDB and for the delayed GC.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
100
  leiosDb' <- case aOnRestart of
    AOnRestart
KeptA -> LeiosDbHandle (IOSim s) -> IOSim s (LeiosDbHandle (IOSim s))
forall a. a -> IOSim s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure LeiosDbHandle (IOSim s)
leiosDb
    AOnRestart
LostA -> IOSim s (LeiosDbHandle (IOSim s))
forall (m :: * -> *). IOLike m => m (LeiosDbHandle m)
newLeiosDBInMemory
  withNodeOverDBsAfterUncleanShutdown nodeDBs leiosDb' $ \NodeUnderTest (IOSim s)
node ->
    (,)
      (Point LeiosTestBlock -> Bool -> (Point LeiosTestBlock, Bool))
-> IOSim s (Point LeiosTestBlock)
-> IOSim s (Bool -> (Point LeiosTestBlock, Bool))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM (IOSim s) (Point LeiosTestBlock)
-> IOSim s (Point LeiosTestBlock)
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (Point (Header LeiosTestBlock) -> Point LeiosTestBlock
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point (Header LeiosTestBlock) -> Point LeiosTestBlock)
-> (AnchoredSeq
      (WithOrigin SlotNo)
      (Anchor (Header LeiosTestBlock))
      (Header LeiosTestBlock)
    -> Point (Header LeiosTestBlock))
-> AnchoredSeq
     (WithOrigin SlotNo)
     (Anchor (Header LeiosTestBlock))
     (Header LeiosTestBlock)
-> Point LeiosTestBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredSeq
  (WithOrigin SlotNo)
  (Anchor (Header LeiosTestBlock))
  (Header LeiosTestBlock)
-> Point (Header LeiosTestBlock)
forall block. AnchoredFragment block -> Point block
AF.anchorPoint (AnchoredSeq
   (WithOrigin SlotNo)
   (Anchor (Header LeiosTestBlock))
   (Header LeiosTestBlock)
 -> Point LeiosTestBlock)
-> STM
     s
     (AnchoredSeq
        (WithOrigin SlotNo)
        (Anchor (Header LeiosTestBlock))
        (Header LeiosTestBlock))
-> STM s (Point LeiosTestBlock)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChainDB (IOSim s) LeiosTestBlock
-> STM
     (IOSim s)
     (AnchoredSeq
        (WithOrigin SlotNo)
        (Anchor (Header LeiosTestBlock))
        (Header LeiosTestBlock))
forall (m :: * -> *) blk.
ChainDB m blk -> STM m (AnchoredFragment (Header blk))
ChainDB.getCurrentChain (NodeUnderTest (IOSim s) -> ChainDB (IOSim s) LeiosTestBlock
forall (m :: * -> *). NodeUnderTest m -> ChainDB m LeiosTestBlock
nodeChainDB NodeUnderTest (IOSim s)
node))
      IOSim s (Bool -> (Point LeiosTestBlock, Bool))
-> IOSim s Bool -> IOSim s (Point LeiosTestBlock, Bool)
forall a b. IOSim s (a -> b) -> IOSim s a -> IOSim s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> LeiosDbHandle (IOSim s)
-> (LeiosDbReader (IOSim s) -> IOSim s Bool) -> IOSim s Bool
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbReader m -> m a) -> m a
withReader LeiosDbHandle (IOSim s)
leiosDb' (\LeiosDbReader (IOSim s)
reader -> Maybe [LeiosTx] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [LeiosTx] -> Bool)
-> IOSim s (Maybe [LeiosTx]) -> IOSim s Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LeiosDbReader (IOSim s)
-> HasCallStack => LeiosPoint -> IOSim s (Maybe [LeiosTx])
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack => LeiosPoint -> m (Maybe [LeiosTx])
lookupEbClosure LeiosDbReader (IOSim s)
reader LeiosPoint
point)
 where
  writeClosureOfA :: LeiosDbHandle m -> m ()
writeClosureOfA LeiosDbHandle m
db =
    LeiosDbHandle m -> (LeiosDbWriter m -> m ()) -> m ()
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbWriter m -> m a) -> m a
withWriter LeiosDbHandle m
db ((LeiosDbWriter m -> m ()) -> m ())
-> (LeiosDbWriter m -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \LeiosDbWriter m
writer -> do
      m (Set Int) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Set Int) -> m ()) -> m (Set Int) -> m ()
forall a b. (a -> b) -> a -> b
$ Promise m (Set Int) -> m (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise m (Set Int) -> m (Set Int))
-> m (Promise m (Set Int)) -> m (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody LeiosDbWriter m
writer LeiosPoint
point EndorserBlock
ebA Map Int TxLocation
forall a. Monoid a => a
mempty
      m () -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Promise m () -> m ()
forall (m :: * -> *) a. Promise m a -> m a
await (Promise m () -> m ()) -> m (Promise m ()) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs LeiosDbWriter m
writer LeiosPoint
point (Int -> ByteString -> Map Int ByteString
forall k a. k -> a -> Map k a
Map.singleton Int
0 ByteString
txA)

-- | The node adds 'twoCertifiedChain' over a LeiosDB holding the closures of
-- A and B, so both are promoted, and restarts after a crash over a LeiosDB that
-- lost A's row but kept B's promoted one, as from a damaged file. The
-- ImmutableDB tip after the restart, whether the immutable partition still
-- holds B, and whether A's closure lands when fetched again.
lostRowBeforeAnother :: IOSim s (Point LeiosTestBlock, Bool, Bool)
lostRowBeforeAnother :: forall s. IOSim s (Point LeiosTestBlock, Bool, Bool)
lostRowBeforeAnother = do
  nodeDBs <- IOSim s (NodeDBs (StrictTMVar (IOSim s) MockFS))
forall (m :: * -> *).
MonadSTM m =>
m (NodeDBs (StrictTMVar m MockFS))
emptyNodeDBs
  leiosDb <- newLeiosDBInMemory
  writeClosure leiosDb pointA ebA txA
  writeClosure leiosDb pointB ebB txB
  withNodeOverDBs nodeDBs leiosDb $ \NodeUnderTest (IOSim s)
node -> do
    -- Let the slots of the chain's blocks pass, so none is from the future.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
30
    NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock]
twoCertifiedChain
    -- Long enough for the copy to the ImmutableDB and for the delayed GC.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
100
  damaged <- newLeiosDBInMemory
  writeClosure damaged pointB ebB txB
  leiosDbPromoteToImmutable damaged (blockSlot certifyingB) [pointB]
  withNodeOverDBsAfterUncleanShutdown nodeDBs damaged $ \NodeUnderTest (IOSim s)
node -> do
    immTip <- STM (IOSim s) (Point LeiosTestBlock)
-> IOSim s (Point LeiosTestBlock)
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (Point (Header LeiosTestBlock) -> Point LeiosTestBlock
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point (Header LeiosTestBlock) -> Point LeiosTestBlock)
-> (AnchoredSeq
      (WithOrigin SlotNo)
      (Anchor (Header LeiosTestBlock))
      (Header LeiosTestBlock)
    -> Point (Header LeiosTestBlock))
-> AnchoredSeq
     (WithOrigin SlotNo)
     (Anchor (Header LeiosTestBlock))
     (Header LeiosTestBlock)
-> Point LeiosTestBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredSeq
  (WithOrigin SlotNo)
  (Anchor (Header LeiosTestBlock))
  (Header LeiosTestBlock)
-> Point (Header LeiosTestBlock)
forall block. AnchoredFragment block -> Point block
AF.anchorPoint (AnchoredSeq
   (WithOrigin SlotNo)
   (Anchor (Header LeiosTestBlock))
   (Header LeiosTestBlock)
 -> Point LeiosTestBlock)
-> STM
     s
     (AnchoredSeq
        (WithOrigin SlotNo)
        (Anchor (Header LeiosTestBlock))
        (Header LeiosTestBlock))
-> STM s (Point LeiosTestBlock)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChainDB (IOSim s) LeiosTestBlock
-> STM
     (IOSim s)
     (AnchoredSeq
        (WithOrigin SlotNo)
        (Anchor (Header LeiosTestBlock))
        (Header LeiosTestBlock))
forall (m :: * -> *) blk.
ChainDB m blk -> STM m (AnchoredFragment (Header blk))
ChainDB.getCurrentChain (NodeUnderTest (IOSim s) -> ChainDB (IOSim s) LeiosTestBlock
forall (m :: * -> *). NodeUnderTest m -> ChainDB m LeiosTestBlock
nodeChainDB NodeUnderTest (IOSim s)
node))
    keptB <- withReader damaged (\LeiosDbReader (IOSim s)
reader -> LeiosDbReader (IOSim s)
-> HasCallStack => LeiosPoint -> IOSim s Bool
forall (m :: * -> *).
LeiosDbReader m -> HasCallStack => LeiosPoint -> m Bool
isImmutable LeiosDbReader (IOSim s)
reader LeiosPoint
pointB)
    writeClosure damaged pointA ebA txA
    landedA <- withReader damaged (\LeiosDbReader (IOSim s)
reader -> Maybe [LeiosTx] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [LeiosTx] -> Bool)
-> IOSim s (Maybe [LeiosTx]) -> IOSim s Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LeiosDbReader (IOSim s)
-> HasCallStack => LeiosPoint -> IOSim s (Maybe [LeiosTx])
forall (m :: * -> *).
LeiosDbReader m
-> HasCallStack => LeiosPoint -> m (Maybe [LeiosTx])
lookupEbClosure LeiosDbReader (IOSim s)
reader LeiosPoint
pointA)
    pure (immTip, keptB, landedA)
 where
  writeClosure :: LeiosDbHandle m
-> LeiosPoint -> EndorserBlock -> ByteString -> m ()
writeClosure LeiosDbHandle m
db LeiosPoint
point EndorserBlock
eb ByteString
tx =
    LeiosDbHandle m -> (LeiosDbWriter m -> m ()) -> m ()
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbWriter m -> m a) -> m a
withWriter LeiosDbHandle m
db ((LeiosDbWriter m -> m ()) -> m ())
-> (LeiosDbWriter m -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \LeiosDbWriter m
writer -> do
      m (Set Int) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Set Int) -> m ()) -> m (Set Int) -> m ()
forall a b. (a -> b) -> a -> b
$ Promise m (Set Int) -> m (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise m (Set Int) -> m (Set Int))
-> m (Promise m (Set Int)) -> m (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody LeiosDbWriter m
writer LeiosPoint
point EndorserBlock
eb Map Int TxLocation
forall a. Monoid a => a
mempty
      m () -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Promise m () -> m ()
forall (m :: * -> *) a. Promise m a -> m a
await (Promise m () -> m ()) -> m (Promise m ()) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs LeiosDbWriter m
writer LeiosPoint
point (Int -> ByteString -> Map Int ByteString
forall k a. k -> a -> Map k a
Map.singleton Int
0 ByteString
tx)

-- | 'immutableChain', but its fourth block certifies B. With sixteen blocks,
-- the immutable tip is past both certificates.
twoCertifiedChain :: [LeiosTestBlock]
twoCertifiedChain :: [LeiosTestBlock]
twoCertifiedChain =
  [LeiosTestBlock
announcingA, LeiosTestBlock
certifyingA, LeiosTestBlock
announcingB, LeiosTestBlock
certifyingB]
    [LeiosTestBlock] -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. [a] -> [a] -> [a]
++ Int -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. Int -> [a] -> [a]
take Int
12 (Int -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. Int -> [a] -> [a]
drop Int
1 ((LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> [LeiosTestBlock]
forall a. (a -> a) -> a -> [a]
iterate LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
certifyingB))

certifyingB :: LeiosTestBlock
certifyingB :: LeiosTestBlock
certifyingB =
  LeiosCert -> LeiosTestBlock -> LeiosTestBlock
setLeiosCert (RankingBlockHash -> [Word16] -> LeiosCert
certOf (LeiosTestBlock -> RankingBlockHash
rbHashOf LeiosTestBlock
announcingB) [Word16
0, Word16
1, Word16
2]) (LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
announcingB)

-- | 'immutableChain', but announcing A in slot 9 and certifying it in slot 10,
-- where the ImmutableDB's second chunk starts, as its chunks span ten slots.
-- With 24 blocks, the immutable tip is in slot 14.
chunkStartChain :: [LeiosTestBlock]
chunkStartChain :: [LeiosTestBlock]
chunkStartChain =
  [LeiosTestBlock]
prefix
    [LeiosTestBlock] -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. [a] -> [a] -> [a]
++ [LeiosTestBlock
chunkStartAnnouncingA, LeiosTestBlock
certifying]
    [LeiosTestBlock] -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. [a] -> [a] -> [a]
++ Int -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. Int -> [a] -> [a]
take Int
14 (Int -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. Int -> [a] -> [a]
drop Int
1 ((LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> [LeiosTestBlock]
forall a. (a -> a) -> a -> [a]
iterate LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
certifying))
 where
  prefix :: [LeiosTestBlock]
prefix = Int -> [LeiosTestBlock] -> [LeiosTestBlock]
forall a. Int -> [a] -> [a]
take Int
8 ((LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> [LeiosTestBlock]
forall a. (a -> a) -> a -> [a]
iterate LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock (Word64 -> LeiosTestBlock
firstLeiosBlock Word64
2))
  certifying :: LeiosTestBlock
certifying =
    LeiosCert -> LeiosTestBlock -> LeiosTestBlock
setLeiosCert
      (RankingBlockHash -> [Word16] -> LeiosCert
certOf (LeiosTestBlock -> RankingBlockHash
rbHashOf LeiosTestBlock
chunkStartAnnouncingA) [Word16
0, Word16
1, Word16
2])
      (LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
chunkStartAnnouncingA)

-- | The block of 'chunkStartChain' announcing A, in slot 9.
chunkStartAnnouncingA :: LeiosTestBlock
chunkStartAnnouncingA :: LeiosTestBlock
chunkStartAnnouncingA =
  Hash HASH EraIndependentEbReferences
-> BytesSize -> LeiosTestBlock -> LeiosTestBlock
setAnnouncement
    (LeiosPoint -> Hash HASH EraIndependentEbReferences
pointEbHash LeiosPoint
pointA)
    (EndorserBlock -> BytesSize
encodeEndorserBlockSize EndorserBlock
ebA)
    ((LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> [LeiosTestBlock]
forall a. (a -> a) -> a -> [a]
iterate LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock (Word64 -> LeiosTestBlock
firstLeiosBlock Word64
2) [LeiosTestBlock] -> Int -> LeiosTestBlock
forall a. HasCallStack => [a] -> Int -> a
!! Int
8)

chunkStartPointA :: LeiosPoint
chunkStartPointA :: LeiosPoint
chunkStartPointA = LeiosPoint
pointA{pointSlotNo = blockSlot chunkStartAnnouncingA}

-- | Add the blocks with the given action, then report why the ChainDB judges
-- the given block invalid, if it does.
whyInvalid ::
  (NodeUnderTest (IOSim s) -> IOSim s ()) ->
  LeiosTestBlock ->
  IOSim s (Maybe (ExtValidationError LeiosTestBlock))
whyInvalid :: forall s.
(NodeUnderTest (IOSim s) -> IOSim s ())
-> LeiosTestBlock
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
whyInvalid NodeUnderTest (IOSim s) -> IOSim s ()
add LeiosTestBlock
blk = (NodeUnderTest (IOSim s)
 -> IOSim s (Maybe (ExtValidationError LeiosTestBlock)))
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
forall s a. (NodeUnderTest (IOSim s) -> IOSim s a) -> IOSim s a
withNode ((NodeUnderTest (IOSim s)
  -> IOSim s (Maybe (ExtValidationError LeiosTestBlock)))
 -> IOSim s (Maybe (ExtValidationError LeiosTestBlock)))
-> (NodeUnderTest (IOSim s)
    -> IOSim s (Maybe (ExtValidationError LeiosTestBlock)))
-> IOSim s (Maybe (ExtValidationError LeiosTestBlock))
forall a b. (a -> b) -> a -> b
$ \NodeUnderTest (IOSim s)
node -> do
  -- Let the slots of the blocks pass, so none is from the future.
  DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
20
  NodeUnderTest (IOSim s) -> IOSim s ()
add NodeUnderTest (IOSim s)
node
  DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
1
  isInvalid <- STM
  (IOSim s) (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
-> IOSim s (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM
   (IOSim s) (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
 -> IOSim s (TestHash -> Maybe (ExtValidationError LeiosTestBlock)))
-> STM
     (IOSim s) (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
-> IOSim s (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
forall a b. (a -> b) -> a -> b
$ WithFingerprint
  (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
-> TestHash -> Maybe (ExtValidationError LeiosTestBlock)
forall a. WithFingerprint a -> a
forgetFingerprint (WithFingerprint
   (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
 -> TestHash -> Maybe (ExtValidationError LeiosTestBlock))
-> STM
     (IOSim s)
     (WithFingerprint
        (TestHash -> Maybe (ExtValidationError LeiosTestBlock)))
-> STM
     (IOSim s) (TestHash -> Maybe (ExtValidationError LeiosTestBlock))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChainDB (IOSim s) LeiosTestBlock
-> STM
     (IOSim s)
     (WithFingerprint
        (HeaderHash LeiosTestBlock
         -> Maybe (ExtValidationError LeiosTestBlock)))
forall (m :: * -> *) blk.
ChainDB m blk
-> STM
     m
     (WithFingerprint
        (HeaderHash blk -> Maybe (ExtValidationError blk)))
ChainDB.getIsInvalidBlock (NodeUnderTest (IOSim s) -> ChainDB (IOSim s) LeiosTestBlock
forall (m :: * -> *). NodeUnderTest m -> ChainDB m LeiosTestBlock
nodeChainDB NodeUnderTest (IOSim s)
node)
  pure $ isInvalid (blockHash blk)

-- | The node adds 'announcingA' and 'certifyingA', restarts, and then adds
-- 'certifyingA' again. Whether the ChainDB holds 'certifyingA' before the
-- restart, after it, and after the second add.
--
-- Without A's transactions, 'certifyingA' is parked and so not on the
-- selection.
heldAcrossRestart :: WhatOfA -> IOSim s (Bool, Bool, Bool)
heldAcrossRestart :: forall s. WhatOfA -> IOSim s (Bool, Bool, Bool)
heldAcrossRestart WhatOfA
whatOfA = do
  nodeDBs <- IOSim s (NodeDBs (StrictTMVar (IOSim s) MockFS))
forall (m :: * -> *).
MonadSTM m =>
m (NodeDBs (StrictTMVar m MockFS))
emptyNodeDBs
  leiosDb <- newLeiosDBInMemory
  withWriter leiosDb $ \LeiosDbWriter (IOSim s)
writer -> do
    IOSim s (Set Int) -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s (Set Int) -> IOSim s ())
-> IOSim s (Set Int) -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ Promise (IOSim s) (Set Int) -> IOSim s (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise (IOSim s) (Set Int) -> IOSim s (Set Int))
-> IOSim s (Promise (IOSim s) (Set Int)) -> IOSim s (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock
   -> Map Int TxLocation
   -> IOSim s (Promise (IOSim s) (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody LeiosDbWriter (IOSim s)
writer LeiosPoint
pointA EndorserBlock
ebA Map Int TxLocation
forall a. Monoid a => a
mempty
    case WhatOfA
whatOfA of
      WhatOfA
BodyOfA -> () -> IOSim s ()
forall a. a -> IOSim s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      WhatOfA
ClosureOfA ->
        IOSim s () -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s () -> IOSim s ()) -> IOSim s () -> IOSim s ()
forall a b. (a -> b) -> a -> b
$
          Promise (IOSim s) () -> IOSim s ()
forall (m :: * -> *) a. Promise m a -> m a
await
            (Promise (IOSim s) () -> IOSim s ())
-> IOSim s (Promise (IOSim s) ()) -> IOSim s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> IOSim s (Promise (IOSim s) ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs
              LeiosDbWriter (IOSim s)
writer
              LeiosPoint
pointA
              (Int -> ByteString -> Map Int ByteString
forall k a. k -> a -> Map k a
Map.singleton Int
0 ByteString
txA)
  beforeRestart <- withNodeOverDBs nodeDBs leiosDb $ \NodeUnderTest (IOSim s)
node -> do
    -- Let the slots of the chain's blocks pass, so none is from the future.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
20
    NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock
announcingA, LeiosTestBlock
certifyingA]
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
1
    NodeUnderTest (IOSim s) -> IOSim s Bool
forall {m :: * -> *}. MonadSTM m => NodeUnderTest m -> m Bool
holds NodeUnderTest (IOSim s)
node
  withNodeOverDBs nodeDBs leiosDb $ \NodeUnderTest (IOSim s)
node -> do
    afterRestart <- NodeUnderTest (IOSim s) -> IOSim s Bool
forall {m :: * -> *}. MonadSTM m => NodeUnderTest m -> m Bool
holds NodeUnderTest (IOSim s)
node
    addBlocksAfter node (NotOrigin announcingA) [certifyingA]
    threadDelay 1
    afterAddingAgain <- holds node
    pure (beforeRestart, afterRestart, afterAddingAgain)
 where
  holds :: NodeUnderTest m -> m Bool
holds NodeUnderTest m
node = STM m Bool -> m Bool
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m Bool -> m Bool) -> STM m Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ ((Point LeiosTestBlock -> Bool) -> Point LeiosTestBlock -> Bool
forall a b. (a -> b) -> a -> b
$ LeiosTestBlock -> Point LeiosTestBlock
forall block. HasHeader block => block -> Point block
blockPoint LeiosTestBlock
certifyingA) ((Point LeiosTestBlock -> Bool) -> Bool)
-> STM m (Point LeiosTestBlock -> Bool) -> STM m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChainDB m LeiosTestBlock -> STM m (Point LeiosTestBlock -> Bool)
forall (m :: * -> *) blk.
ChainDB m blk -> STM m (Point blk -> Bool)
ChainDB.getIsFetched (NodeUnderTest m -> ChainDB m LeiosTestBlock
forall (m :: * -> *). NodeUnderTest m -> ChainDB m LeiosTestBlock
nodeChainDB NodeUnderTest m
node)

-- | What the LeiosDB holds of A
data WhatOfA
  = -- | Its body, but not its transactions
    BodyOfA
  | -- | Its body and its transactions
    ClosureOfA

-- | 'certifyingA', except a minority made its certificate.
minorityCertifyingA :: LeiosTestBlock
minorityCertifyingA :: LeiosTestBlock
minorityCertifyingA =
  LeiosCert -> LeiosTestBlock -> LeiosTestBlock
setLeiosCert (RankingBlockHash -> [Word16] -> LeiosCert
certOf (LeiosTestBlock -> RankingBlockHash
rbHashOf LeiosTestBlock
announcingA) [Word16
0]) (LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
announcingA)

-- | A sibling of 'certifyingA', making the same claim, with a certificate a
-- minority made.
minoritySibling :: LeiosTestBlock
minoritySibling :: LeiosTestBlock
minoritySibling =
  LeiosCert -> LeiosTestBlock -> LeiosTestBlock
setLeiosCert (RankingBlockHash -> [Word16] -> LeiosCert
certOf (LeiosTestBlock -> RankingBlockHash
rbHashOf LeiosTestBlock
announcingA) [Word16
0]) (LeiosTestBlock -> LeiosTestBlock
forkLeiosBlock LeiosTestBlock
certifyingA)

-- | Add 'certifyingA', which establishes its claim, and then 'minoritySibling'.
addSibling :: NodeUnderTest (IOSim s) -> IOSim s ()
addSibling :: forall s. NodeUnderTest (IOSim s) -> IOSim s ()
addSibling NodeUnderTest (IOSim s)
node = do
  NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock
announcingA, LeiosTestBlock
certifyingA]
  NodeUnderTest (IOSim s)
-> WithOrigin LeiosTestBlock -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m
-> WithOrigin LeiosTestBlock -> [LeiosTestBlock] -> m ()
addBlocksAfter NodeUnderTest (IOSim s)
node (LeiosTestBlock -> WithOrigin LeiosTestBlock
forall t. t -> WithOrigin t
NotOrigin LeiosTestBlock
announcingA) [LeiosTestBlock
minoritySibling]

rbHashOf :: LeiosTestBlock -> RankingBlockHash
rbHashOf :: LeiosTestBlock -> RankingBlockHash
rbHashOf = Proxy LeiosTestBlock
-> HeaderHash LeiosTestBlock -> RankingBlockHash
forall blk.
ConvertRawHash blk =>
Proxy blk -> HeaderHash blk -> RankingBlockHash
toRankingBlockHash (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @LeiosTestBlock) (TestHash -> RankingBlockHash)
-> (LeiosTestBlock -> TestHash)
-> LeiosTestBlock
-> RankingBlockHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LeiosTestBlock -> HeaderHash LeiosTestBlock
LeiosTestBlock -> TestHash
forall b. HasHeader b => b -> HeaderHash b
blockHash

pointA, pointB :: LeiosPoint
pointA :: LeiosPoint
pointA = SlotNo -> Hash HASH EraIndependentEbReferences -> LeiosPoint
MkLeiosPoint (LeiosTestBlock -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot LeiosTestBlock
announcingA) (ShortByteString -> Hash HASH EraIndependentEbReferences
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
1))
pointB :: LeiosPoint
pointB = SlotNo -> Hash HASH EraIndependentEbReferences -> LeiosPoint
MkLeiosPoint (LeiosTestBlock -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot LeiosTestBlock
announcingB) (ShortByteString -> Hash HASH EraIndependentEbReferences
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
2))

-- | The first block, which announces A; the second, which certifies A; and
-- the third, which announces B.
announcingA, certifyingA, announcingB :: LeiosTestBlock
announcingA :: LeiosTestBlock
announcingA = Hash HASH EraIndependentEbReferences
-> BytesSize -> LeiosTestBlock -> LeiosTestBlock
setAnnouncement (LeiosPoint -> Hash HASH EraIndependentEbReferences
pointEbHash LeiosPoint
pointA) (EndorserBlock -> BytesSize
encodeEndorserBlockSize EndorserBlock
ebA) (Word64 -> LeiosTestBlock
firstLeiosBlock Word64
1)
certifyingA :: LeiosTestBlock
certifyingA =
  LeiosCert -> LeiosTestBlock -> LeiosTestBlock
setLeiosCert (RankingBlockHash -> [Word16] -> LeiosCert
certOf (LeiosTestBlock -> RankingBlockHash
rbHashOf LeiosTestBlock
announcingA) [Word16
0, Word16
1, Word16
2]) (LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
announcingA)
announcingB :: LeiosTestBlock
announcingB =
  Hash HASH EraIndependentEbReferences
-> BytesSize -> LeiosTestBlock -> LeiosTestBlock
setAnnouncement (LeiosPoint -> Hash HASH EraIndependentEbReferences
pointEbHash LeiosPoint
pointB) (EndorserBlock -> BytesSize
encodeEndorserBlockSize EndorserBlock
ebB) (LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock LeiosTestBlock
certifyingA)

-- | A and B each name one transaction of ten bytes.
ebA, ebB :: EndorserBlock
ebA :: EndorserBlock
ebA = NonEmpty ByteString -> EndorserBlock
endorserBlockFromTxBytes (ByteString
txA ByteString -> [ByteString] -> NonEmpty ByteString
forall a. a -> [a] -> NonEmpty a
:| [])
ebB :: EndorserBlock
ebB = NonEmpty ByteString -> EndorserBlock
endorserBlockFromTxBytes (ByteString
txB ByteString -> [ByteString] -> NonEmpty ByteString
forall a. a -> [a] -> NonEmpty a
:| [])

txA, txB :: BS.ByteString
txA :: ByteString
txA = Int -> Word8 -> ByteString
BS.replicate Int
10 Word8
1
txB :: ByteString
txB = Int -> Word8 -> ByteString
BS.replicate Int
10 Word8
2

-- | The chain announces A and then certifies it, while the LeiosDB holds A's
-- body but not its transactions. The tip of the selected chain before they
-- arrive and the node emits their acquisition, and after.
tipsAroundClosure :: IOSim s (Point LeiosTestBlock, Point LeiosTestBlock)
tipsAroundClosure :: forall s. IOSim s (Point LeiosTestBlock, Point LeiosTestBlock)
tipsAroundClosure = (NodeUnderTest (IOSim s)
 -> IOSim s (Point LeiosTestBlock, Point LeiosTestBlock))
-> IOSim s (Point LeiosTestBlock, Point LeiosTestBlock)
forall s a. (NodeUnderTest (IOSim s) -> IOSim s a) -> IOSim s a
withNode ((NodeUnderTest (IOSim s)
  -> IOSim s (Point LeiosTestBlock, Point LeiosTestBlock))
 -> IOSim s (Point LeiosTestBlock, Point LeiosTestBlock))
-> (NodeUnderTest (IOSim s)
    -> IOSim s (Point LeiosTestBlock, Point LeiosTestBlock))
-> IOSim s (Point LeiosTestBlock, Point LeiosTestBlock)
forall a b. (a -> b) -> a -> b
$ \NodeUnderTest (IOSim s)
node -> do
  let leiosDb :: LeiosDbHandle (IOSim s)
leiosDb = NodeKernel (IOSim s) PeerAddr (ConnectionId ()) LeiosTestBlock
-> LeiosDbHandle (IOSim s)
forall (m :: * -> *) addrNTN addrNTC blk.
NodeKernel m addrNTN addrNTC blk -> LeiosDbHandle m
getLeiosDB (NodeUnderTest (IOSim s)
-> NodeKernel (IOSim s) PeerAddr (ConnectionId ()) LeiosTestBlock
forall (m :: * -> *).
NodeUnderTest m
-> NodeKernel m PeerAddr (ConnectionId ()) LeiosTestBlock
nodeKernel NodeUnderTest (IOSim s)
node)
      chainDB :: ChainDB (IOSim s) LeiosTestBlock
chainDB = NodeUnderTest (IOSim s) -> ChainDB (IOSim s) LeiosTestBlock
forall (m :: * -> *). NodeUnderTest m -> ChainDB m LeiosTestBlock
nodeChainDB NodeUnderTest (IOSim s)
node
  LeiosDbHandle (IOSim s)
-> (LeiosDbWriter (IOSim s) -> IOSim s ()) -> IOSim s ()
forall (m :: * -> *) a.
MonadThrow m =>
LeiosDbHandle m -> (LeiosDbWriter m -> m a) -> m a
withWriter LeiosDbHandle (IOSim s)
leiosDb ((LeiosDbWriter (IOSim s) -> IOSim s ()) -> IOSim s ())
-> (LeiosDbWriter (IOSim s) -> IOSim s ()) -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ \LeiosDbWriter (IOSim s)
writer -> do
    IOSim s (Set Int) -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s (Set Int) -> IOSim s ())
-> IOSim s (Set Int) -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ Promise (IOSim s) (Set Int) -> IOSim s (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise (IOSim s) (Set Int) -> IOSim s (Set Int))
-> IOSim s (Promise (IOSim s) (Set Int)) -> IOSim s (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock
   -> Map Int TxLocation
   -> IOSim s (Promise (IOSim s) (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody LeiosDbWriter (IOSim s)
writer LeiosPoint
pointA EndorserBlock
ebA Map Int TxLocation
forall a. Monoid a => a
mempty
  -- Let the slots of the chain's blocks pass, so none is from the future.
  DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
20
  NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock
announcingA, LeiosTestBlock
certifyingA]
  DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
1
  tipBefore <- STM (IOSim s) (Point LeiosTestBlock)
-> IOSim s (Point LeiosTestBlock)
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM (IOSim s) (Point LeiosTestBlock)
 -> IOSim s (Point LeiosTestBlock))
-> STM (IOSim s) (Point LeiosTestBlock)
-> IOSim s (Point LeiosTestBlock)
forall a b. (a -> b) -> a -> b
$ ChainDB (IOSim s) LeiosTestBlock
-> STM (IOSim s) (Point LeiosTestBlock)
forall (m :: * -> *) blk. ChainDB m blk -> STM m (Point blk)
ChainDB.getTipPoint ChainDB (IOSim s) LeiosTestBlock
chainDB
  withWriter leiosDb $ \LeiosDbWriter (IOSim s)
writer ->
    IOSim s () -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s () -> IOSim s ()) -> IOSim s () -> IOSim s ()
forall a b. (a -> b) -> a -> b
$
      Promise (IOSim s) () -> IOSim s ()
forall (m :: * -> *) a. Promise m a -> m a
await
        (Promise (IOSim s) () -> IOSim s ())
-> IOSim s (Promise (IOSim s) ()) -> IOSim s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> IOSim s (Promise (IOSim s) ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs
          LeiosDbWriter (IOSim s)
writer
          LeiosPoint
pointA
          ( [(Int, ByteString)] -> Map Int ByteString
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
              [ (Int
offset, Int -> Word8 -> ByteString
BS.replicate (BytesSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral BytesSize
size) Word8
0)
              | (Int
offset, (TxHash
_txHash, BytesSize
size)) <- [Int] -> [(TxHash, BytesSize)] -> [(Int, (TxHash, BytesSize))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] (EndorserBlock -> [(TxHash, BytesSize)]
endorserBlockReferences EndorserBlock
ebA)
              ]
          )
  atomically $
    emitLeiosAcquisition (nodeLeiosAcquisitions node) (AcquiredEbTxs pointA)
  threadDelay 1
  tipAfter <- atomically $ ChainDB.getTipPoint chainDB
  pure (tipBefore, tipAfter)

-- | As 'tipsAroundClosure', except that the LeiosDB holds A's closure before
-- the node starts. The tip of the selected chain.
tipWithClosureBeforeStart :: IOSim s (Point LeiosTestBlock)
tipWithClosureBeforeStart :: forall s. IOSim s (Point LeiosTestBlock)
tipWithClosureBeforeStart = do
  leiosDb <- IOSim s (LeiosDbHandle (IOSim s))
forall (m :: * -> *). IOLike m => m (LeiosDbHandle m)
newLeiosDBInMemory
  withWriter leiosDb $ \LeiosDbWriter (IOSim s)
writer -> do
    IOSim s (Set Int) -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s (Set Int) -> IOSim s ())
-> IOSim s (Set Int) -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ Promise (IOSim s) (Set Int) -> IOSim s (Set Int)
forall (m :: * -> *) a. Promise m a -> m a
await (Promise (IOSim s) (Set Int) -> IOSim s (Set Int))
-> IOSim s (Promise (IOSim s) (Set Int)) -> IOSim s (Set Int)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock
   -> Map Int TxLocation
   -> IOSim s (Promise (IOSim s) (Set Int))
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint
   -> EndorserBlock -> Map Int TxLocation -> m (Promise m (Set Int))
writeEbBody LeiosDbWriter (IOSim s)
writer LeiosPoint
pointA EndorserBlock
ebA Map Int TxLocation
forall a. Monoid a => a
mempty
    IOSim s () -> IOSim s ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IOSim s () -> IOSim s ()) -> IOSim s () -> IOSim s ()
forall a b. (a -> b) -> a -> b
$
      Promise (IOSim s) () -> IOSim s ()
forall (m :: * -> *) a. Promise m a -> m a
await
        (Promise (IOSim s) () -> IOSim s ())
-> IOSim s (Promise (IOSim s) ()) -> IOSim s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LeiosDbWriter (IOSim s)
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> IOSim s (Promise (IOSim s) ())
forall (m :: * -> *).
LeiosDbWriter m
-> HasCallStack =>
   LeiosPoint -> Map Int ByteString -> m (Promise m ())
writeTxs
          LeiosDbWriter (IOSim s)
writer
          LeiosPoint
pointA
          (Int -> ByteString -> Map Int ByteString
forall k a. k -> a -> Map k a
Map.singleton Int
0 ByteString
txA)
  withNodeOver leiosDb $ \NodeUnderTest (IOSim s)
node -> do
    -- Let the slots of the chain's blocks pass, so none is from the future.
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
20
    NodeUnderTest (IOSim s) -> [LeiosTestBlock] -> IOSim s ()
forall (m :: * -> *).
IOLike m =>
NodeUnderTest m -> [LeiosTestBlock] -> m ()
addBlocks NodeUnderTest (IOSim s)
node [LeiosTestBlock
announcingA, LeiosTestBlock
certifyingA]
    DiffTime -> IOSim s ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
1
    STM (IOSim s) (Point LeiosTestBlock)
-> IOSim s (Point LeiosTestBlock)
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM (IOSim s) (Point LeiosTestBlock)
 -> IOSim s (Point LeiosTestBlock))
-> STM (IOSim s) (Point LeiosTestBlock)
-> IOSim s (Point LeiosTestBlock)
forall a b. (a -> b) -> a -> b
$ ChainDB (IOSim s) LeiosTestBlock
-> STM (IOSim s) (Point LeiosTestBlock)
forall (m :: * -> *) blk. ChainDB m blk -> STM m (Point blk)
ChainDB.getTipPoint (NodeUnderTest (IOSim s) -> ChainDB (IOSim s) LeiosTestBlock
forall (m :: * -> *). NodeUnderTest m -> ChainDB m LeiosTestBlock
nodeChainDB NodeUnderTest (IOSim s)
node)