{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Tests that the node writes each endorser block that it forges to the
-- LeiosDB, and acquires and relays it as it would one fetched from a peer.
module Test.Consensus.Leios.Forge (tests) where

import Control.Monad.Class.MonadTimer.SI (threadDelay)
import Control.Monad.IOSim (IOSim, runSimOrThrow)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS8
import Data.List (sort)
import Data.List.NonEmpty (NonEmpty (..))
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Ledger.Abstract (GetTip (..))
import Ouroboros.Consensus.Leios.Acquisitions
  ( LeiosAcquisition (..)
  , LeiosAcquisitions (..)
  )
import Ouroboros.Consensus.Leios.NotifyLog (Notification (..), nextNotifyLog, notifyLogStart)
import Ouroboros.Consensus.Leios.Protocol (LeiosAnnouncement (..))
import Ouroboros.Consensus.Leios.Types
import Ouroboros.Consensus.MiniProtocol.LeiosNotify.Client.Announcements.Validation
  ( AnnouncingHeader (..)
  )
import Ouroboros.Consensus.NodeKernel (NodeKernel (..))
import Ouroboros.Consensus.Storage.ChainDB.API (getCurrentChain)
import Ouroboros.Consensus.Storage.LeiosDB.API (LeiosDbReader (..), withReader)
import Ouroboros.Consensus.Util.IOLike (atomically, orElse)
import qualified Ouroboros.Network.AnchoredFragment as AF
import Test.Consensus.Leios.NodeUnderTest
import Test.Consensus.Leios.TestBlock
import Test.Tasty
import Test.Tasty.HUnit
import Test.Util.TestBlock (TestHash (..))

tests :: TestTree
tests :: TestTree
tests =
  [Char] -> [TestTree] -> TestTree
testGroup
    [Char]
"Forging"
    [ [Char] -> Assertion -> TestTree
testCase
        [Char]
"the node stores, acquires and relays each endorser block that it forges, at the point that the header announces"
        Assertion
unit_storesForgedEbs
    ]

-- | The node runs 'forkBlockForging' with 'ebForging' for slots 0 to 3. Then
-- the test reads the chain and the LeiosDB.
--
-- * If the node forges no block, the other checks pass with no data. The
--   first check catches this.
-- * If 'forkBlockForging' does not hand the endorser block on from 'forge', or
--   if the node writes it at a slot or a hash that differs from the
--   announcement in the header, the second check fails.
-- * If the node does not write the transaction bytes, the third check fails.
-- * If the node does not emit both acquisitions at each such point, as it would
--   for an endorser block fetched from a peer, the fourth check fails.
-- * If the node does not relay the announcement and offer both the body and the
--   closure at each such point, the fifth check fails.
unit_storesForgedEbs :: Assertion
unit_storesForgedEbs :: Assertion
unit_storesForgedEbs = do
  let ForgeOutcome{[AnnouncedEb]
announcedEbs :: [AnnouncedEb]
announcedEbs :: ForgeOutcome -> [AnnouncedEb]
announcedEbs, [LeiosPoint]
storedPoints :: [LeiosPoint]
storedPoints :: ForgeOutcome -> [LeiosPoint]
storedPoints, [Acquired]
acquisitions :: [Acquired]
acquisitions :: ForgeOutcome -> [Acquired]
acquisitions, [Relayed]
relayed :: [Relayed]
relayed :: ForgeOutcome -> [Relayed]
relayed} = (forall s. IOSim s ForgeOutcome) -> ForgeOutcome
forall a. (forall s. IOSim s a) -> a
runSimOrThrow IOSim s ForgeOutcome
forall s. IOSim s ForgeOutcome
forgedEbs
      points :: [LeiosPoint]
points = [SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (AnnouncedEb -> SlotNo
announcedSlot AnnouncedEb
eb) (AnnouncedEb -> EndorserBlockHash
announcedHash AnnouncedEb
eb) | AnnouncedEb
eb <- [AnnouncedEb]
announcedEbs]
  HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"the node forged no block" (Bool -> Bool
not ([AnnouncedEb] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [AnnouncedEb]
announcedEbs))
  [LeiosPoint] -> [LeiosPoint]
forall a. Ord a => [a] -> [a]
sort [LeiosPoint]
storedPoints [LeiosPoint] -> [LeiosPoint] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [LeiosPoint] -> [LeiosPoint]
forall a. Ord a => [a] -> [a]
sort [LeiosPoint]
points
  (AnnouncedEb -> Maybe [ByteString])
-> [AnnouncedEb] -> [Maybe [ByteString]]
forall a b. (a -> b) -> [a] -> [b]
map AnnouncedEb -> Maybe [ByteString]
storedTxs [AnnouncedEb]
announcedEbs [Maybe [ByteString]] -> [Maybe [ByteString]] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= (AnnouncedEb -> Maybe [ByteString])
-> [AnnouncedEb] -> [Maybe [ByteString]]
forall a b. (a -> b) -> [a] -> [b]
map ([ByteString] -> Maybe [ByteString]
forall a. a -> Maybe a
Just ([ByteString] -> Maybe [ByteString])
-> (AnnouncedEb -> [ByteString])
-> AnnouncedEb
-> Maybe [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnnouncedEb -> [ByteString]
forgedTxs) [AnnouncedEb]
announcedEbs
  [Acquired] -> [Acquired]
forall a. Ord a => [a] -> [a]
sort [Acquired]
acquisitions
    [Acquired] -> [Acquired] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Acquired] -> [Acquired]
forall a. Ord a => [a] -> [a]
sort ([[Acquired]] -> [Acquired]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[LeiosPoint -> Acquired
AcquiredBody LeiosPoint
point, LeiosPoint -> Acquired
AcquiredClosure LeiosPoint
point] | LeiosPoint
point <- [LeiosPoint]
points])
  [Relayed] -> [Relayed]
forall a. Ord a => [a] -> [a]
sort [Relayed]
relayed
    [Relayed] -> [Relayed] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Relayed] -> [Relayed]
forall a. Ord a => [a] -> [a]
sort
      ([[Relayed]] -> [Relayed]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[LeiosPoint -> Relayed
RelayedAnnouncement LeiosPoint
point, LeiosPoint -> Relayed
RelayedBody LeiosPoint
point, LeiosPoint -> Relayed
RelayedClosure LeiosPoint
point] | LeiosPoint
point <- [LeiosPoint]
points])

-- | What the chain and the LeiosDB hold after the node forges.
data ForgeOutcome = ForgeOutcome
  { ForgeOutcome -> [AnnouncedEb]
announcedEbs :: [AnnouncedEb]
  -- ^ The endorser blocks that the blocks on the chain announce, oldest first.
  , ForgeOutcome -> [LeiosPoint]
storedPoints :: [LeiosPoint]
  -- ^ The points whose closures the LeiosDB holds.
  , ForgeOutcome -> [Acquired]
acquisitions :: [Acquired]
  -- ^ What the node emitted as acquired.
  , ForgeOutcome -> [Relayed]
relayed :: [Relayed]
  -- ^ What the node's NotifyLog holds.
  }

-- | An acquisition, comparably.
data Acquired = AcquiredBody LeiosPoint | AcquiredClosure LeiosPoint
  deriving (Acquired -> Acquired -> Bool
(Acquired -> Acquired -> Bool)
-> (Acquired -> Acquired -> Bool) -> Eq Acquired
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Acquired -> Acquired -> Bool
== :: Acquired -> Acquired -> Bool
$c/= :: Acquired -> Acquired -> Bool
/= :: Acquired -> Acquired -> Bool
Eq, Eq Acquired
Eq Acquired =>
(Acquired -> Acquired -> Ordering)
-> (Acquired -> Acquired -> Bool)
-> (Acquired -> Acquired -> Bool)
-> (Acquired -> Acquired -> Bool)
-> (Acquired -> Acquired -> Bool)
-> (Acquired -> Acquired -> Acquired)
-> (Acquired -> Acquired -> Acquired)
-> Ord Acquired
Acquired -> Acquired -> Bool
Acquired -> Acquired -> Ordering
Acquired -> Acquired -> Acquired
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Acquired -> Acquired -> Ordering
compare :: Acquired -> Acquired -> Ordering
$c< :: Acquired -> Acquired -> Bool
< :: Acquired -> Acquired -> Bool
$c<= :: Acquired -> Acquired -> Bool
<= :: Acquired -> Acquired -> Bool
$c> :: Acquired -> Acquired -> Bool
> :: Acquired -> Acquired -> Bool
$c>= :: Acquired -> Acquired -> Bool
>= :: Acquired -> Acquired -> Bool
$cmax :: Acquired -> Acquired -> Acquired
max :: Acquired -> Acquired -> Acquired
$cmin :: Acquired -> Acquired -> Acquired
min :: Acquired -> Acquired -> Acquired
Ord, Int -> Acquired -> ShowS
[Acquired] -> ShowS
Acquired -> [Char]
(Int -> Acquired -> ShowS)
-> (Acquired -> [Char]) -> ([Acquired] -> ShowS) -> Show Acquired
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Acquired -> ShowS
showsPrec :: Int -> Acquired -> ShowS
$cshow :: Acquired -> [Char]
show :: Acquired -> [Char]
$cshowList :: [Acquired] -> ShowS
showList :: [Acquired] -> ShowS
Show)

-- | A notification about an endorser block, comparably.
data Relayed
  = RelayedAnnouncement LeiosPoint
  | RelayedBody LeiosPoint
  | RelayedClosure LeiosPoint
  deriving (Relayed -> Relayed -> Bool
(Relayed -> Relayed -> Bool)
-> (Relayed -> Relayed -> Bool) -> Eq Relayed
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Relayed -> Relayed -> Bool
== :: Relayed -> Relayed -> Bool
$c/= :: Relayed -> Relayed -> Bool
/= :: Relayed -> Relayed -> Bool
Eq, Eq Relayed
Eq Relayed =>
(Relayed -> Relayed -> Ordering)
-> (Relayed -> Relayed -> Bool)
-> (Relayed -> Relayed -> Bool)
-> (Relayed -> Relayed -> Bool)
-> (Relayed -> Relayed -> Bool)
-> (Relayed -> Relayed -> Relayed)
-> (Relayed -> Relayed -> Relayed)
-> Ord Relayed
Relayed -> Relayed -> Bool
Relayed -> Relayed -> Ordering
Relayed -> Relayed -> Relayed
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Relayed -> Relayed -> Ordering
compare :: Relayed -> Relayed -> Ordering
$c< :: Relayed -> Relayed -> Bool
< :: Relayed -> Relayed -> Bool
$c<= :: Relayed -> Relayed -> Bool
<= :: Relayed -> Relayed -> Bool
$c> :: Relayed -> Relayed -> Bool
> :: Relayed -> Relayed -> Bool
$c>= :: Relayed -> Relayed -> Bool
>= :: Relayed -> Relayed -> Bool
$cmax :: Relayed -> Relayed -> Relayed
max :: Relayed -> Relayed -> Relayed
$cmin :: Relayed -> Relayed -> Relayed
min :: Relayed -> Relayed -> Relayed
Ord, Int -> Relayed -> ShowS
[Relayed] -> ShowS
Relayed -> [Char]
(Int -> Relayed -> ShowS)
-> (Relayed -> [Char]) -> ([Relayed] -> ShowS) -> Show Relayed
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Relayed -> ShowS
showsPrec :: Int -> Relayed -> ShowS
$cshow :: Relayed -> [Char]
show :: Relayed -> [Char]
$cshowList :: [Relayed] -> ShowS
showList :: [Relayed] -> ShowS
Show)

-- | An endorser block that a block on the chain announces.
data AnnouncedEb = AnnouncedEb
  { AnnouncedEb -> SlotNo
announcedSlot :: SlotNo
  , AnnouncedEb -> EndorserBlockHash
announcedHash :: EndorserBlockHash
  , AnnouncedEb -> [ByteString]
forgedTxs :: [ByteString]
  -- ^ The bytes of the transactions that 'ebForging' puts in the endorser block.
  , AnnouncedEb -> Maybe [ByteString]
storedTxs :: Maybe [ByteString]
  -- ^ The bytes of the transactions that the LeiosDB holds for 'announcedHash',
  -- or 'Nothing' if it does not hold all of them.
  }

-- | The node forges in each of the first slots.
forgedEbs :: IOSim s ForgeOutcome
forgedEbs :: forall s. IOSim s ForgeOutcome
forgedEbs = (NodeUnderTest (IOSim s) -> IOSim s ForgeOutcome)
-> IOSim s ForgeOutcome
forall s a. (NodeUnderTest (IOSim s) -> IOSim s a) -> IOSim s a
withNode ((NodeUnderTest (IOSim s) -> IOSim s ForgeOutcome)
 -> IOSim s ForgeOutcome)
-> (NodeUnderTest (IOSim s) -> IOSim s ForgeOutcome)
-> IOSim s ForgeOutcome
forall a b. (a -> b) -> a -> b
$ \NodeUnderTest (IOSim s)
node -> do
  next <- LeiosAcquisitions (IOSim s)
-> IOSim s (STM (IOSim s) LeiosAcquisition)
forall (m :: * -> *).
LeiosAcquisitions m -> m (STM m LeiosAcquisition)
subscribeLeiosAcquisitions (NodeUnderTest (IOSim s) -> LeiosAcquisitions (IOSim s)
forall (m :: * -> *). NodeUnderTest m -> LeiosAcquisitions m
nodeLeiosAcquisitions NodeUnderTest (IOSim s)
node)
  setBlockForging (nodeKernel node) [MkBlockForging (pure ebForging)]
  -- A slot is one second, so the node forges in slots 0 to 3. The half slot
  -- lets the forge of slot 3 add its block to the ChainDB.
  threadDelay 3.5
  chain <- atomically $ getCurrentChain (nodeChainDB node)
  withReader (getLeiosDB (nodeKernel node)) $ \LeiosDbReader (IOSim s)
reader -> do
    announcedEbs <-
      [IOSim s AnnouncedEb] -> IOSim s [AnnouncedEb]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
        [ SlotNo
-> EndorserBlockHash
-> [ByteString]
-> Maybe [ByteString]
-> AnnouncedEb
AnnouncedEb (Header LeiosTestBlock -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot Header LeiosTestBlock
hdr) EndorserBlockHash
ebHash [SlotNo -> ByteString
txInSlot (Header LeiosTestBlock -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot Header LeiosTestBlock
hdr)]
            (Maybe [ByteString] -> AnnouncedEb)
-> IOSim s (Maybe [ByteString]) -> IOSim s AnnouncedEb
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (([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])
-> IOSim s (Maybe [LeiosTx]) -> IOSim s (Maybe [ByteString])
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 (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (Header LeiosTestBlock -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot Header LeiosTestBlock
hdr) EndorserBlockHash
ebHash))
        | Header LeiosTestBlock
hdr <- AnchoredSeq
  (WithOrigin SlotNo)
  (Anchor (Header LeiosTestBlock))
  (Header LeiosTestBlock)
-> [Header LeiosTestBlock]
forall v a b. AnchoredSeq v a b -> [b]
AF.toOldestFirst AnchoredSeq
  (WithOrigin SlotNo)
  (Anchor (Header LeiosTestBlock))
  (Header LeiosTestBlock)
chain
        , Just (EndorserBlockHash
ebHash, BytesSize
_size) <- [Header LeiosTestBlock -> Maybe (EndorserBlockHash, BytesSize)
lthAnnouncement Header LeiosTestBlock
hdr]
        ]
    storedPoints <- scanCompleteEbClosuresNotOlderThanSlot reader 0
    acquisitions <- drain next
    relayed <- atomically $ notifications (getLeiosNotifyLog (nodeKernel node)) notifyLogStart
    pure ForgeOutcome{announcedEbs, storedPoints, acquisitions, relayed}
 where
  drain :: STM m LeiosAcquisition -> m [Acquired]
drain STM m LeiosAcquisition
next =
    STM m (Maybe LeiosAcquisition) -> m (Maybe LeiosAcquisition)
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically ((LeiosAcquisition -> Maybe LeiosAcquisition
forall a. a -> Maybe a
Just (LeiosAcquisition -> Maybe LeiosAcquisition)
-> STM m LeiosAcquisition -> STM m (Maybe LeiosAcquisition)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM m LeiosAcquisition
next) STM m (Maybe LeiosAcquisition)
-> STM m (Maybe LeiosAcquisition) -> STM m (Maybe LeiosAcquisition)
forall a. STM m a -> STM m a -> STM m a
forall (m :: * -> *) a. MonadSTM m => STM m a -> STM m a -> STM m a
`orElse` Maybe LeiosAcquisition -> STM m (Maybe LeiosAcquisition)
forall a. a -> STM m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe LeiosAcquisition
forall a. Maybe a
Nothing) m (Maybe LeiosAcquisition)
-> (Maybe LeiosAcquisition -> m [Acquired]) -> m [Acquired]
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Maybe LeiosAcquisition
Nothing -> [Acquired] -> m [Acquired]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
      Just LeiosAcquisition
notification -> (LeiosAcquisition -> Acquired
acquiredOf LeiosAcquisition
notification Acquired -> [Acquired] -> [Acquired]
forall a. a -> [a] -> [a]
:) ([Acquired] -> [Acquired]) -> m [Acquired] -> m [Acquired]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM m LeiosAcquisition -> m [Acquired]
drain STM m LeiosAcquisition
next
  acquiredOf :: LeiosAcquisition -> Acquired
acquiredOf = \case
    AcquiredEb LeiosPoint
point BytesSize
_size -> LeiosPoint -> Acquired
AcquiredBody LeiosPoint
point
    AcquiredEbTxs LeiosPoint
point -> LeiosPoint -> Acquired
AcquiredClosure LeiosPoint
point
  notifications :: NotifyLog m blk -> NotifyCursor -> STM m [Relayed]
notifications NotifyLog m blk
notifyLog NotifyCursor
cursor =
    ( NotifyLog m blk
-> NotifyCursor -> STM m (Notification blk, NotifyCursor)
forall (m :: * -> *) blk.
IOLike m =>
NotifyLog m blk
-> NotifyCursor -> STM m (Notification blk, NotifyCursor)
nextNotifyLog NotifyLog m blk
notifyLog NotifyCursor
cursor STM m (Notification blk, NotifyCursor)
-> ((Notification blk, NotifyCursor) -> STM m [Relayed])
-> STM m [Relayed]
forall a b. STM m a -> (a -> STM m b) -> STM m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(Notification blk
n, NotifyCursor
cursor') ->
        (Notification blk -> [Relayed]
forall {blk}. Notification blk -> [Relayed]
relayedOf Notification blk
n [Relayed] -> [Relayed] -> [Relayed]
forall a. [a] -> [a] -> [a]
++) ([Relayed] -> [Relayed]) -> STM m [Relayed] -> STM m [Relayed]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NotifyLog m blk -> NotifyCursor -> STM m [Relayed]
notifications NotifyLog m blk
notifyLog NotifyCursor
cursor'
    )
      STM m [Relayed] -> STM m [Relayed] -> STM m [Relayed]
forall a. STM m a -> STM m a -> STM m a
forall (m :: * -> *) a. MonadSTM m => STM m a -> STM m a -> STM m a
`orElse` [Relayed] -> STM m [Relayed]
forall a. a -> STM m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
  relayedOf :: Notification blk -> [Relayed]
relayedOf = \case
    NotifyAnnouncement AnnouncingHeader blk
anc ->
      [ LeiosPoint -> Relayed
RelayedAnnouncement (LeiosPoint -> Relayed) -> LeiosPoint -> Relayed
forall a b. (a -> b) -> a -> b
$
          SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint
            (ElectionId -> SlotNo
electionSlot (AnnouncingHeader blk -> ElectionId
forall blk. AnnouncingHeader blk -> ElectionId
ancElectionId AnnouncingHeader blk
anc))
            (LeiosAnnouncement -> EndorserBlockHash
announcementEbHash (AnnouncingHeader blk -> LeiosAnnouncement
forall blk. AnnouncingHeader blk -> LeiosAnnouncement
ancAnnouncement AnnouncingHeader blk
anc))
      ]
    NotifyBlockOffer LeiosPoint
point BytesSize
_size -> [LeiosPoint -> Relayed
RelayedBody LeiosPoint
point]
    NotifyBlockTxsOffer LeiosPoint
point -> [LeiosPoint -> Relayed
RelayedClosure LeiosPoint
point]
    -- The node votes for nothing here.
    NotifyVote LeiosVote
_ -> []

-- | A forging that leads in every slot. Each block extends the ledger tip and
-- announces an endorser block with one transaction, whose bytes depend on the
-- slot, so that each endorser block has its own hash.
ebForging :: BlockForging (IOSim s) LeiosTestBlock
ebForging :: forall s. BlockForging (IOSim s) LeiosTestBlock
ebForging =
  BlockForging
    { forgeLabel :: Text
forgeLabel = Text
"ebForging"
    , canBeLeader :: CanBeLeader (BlockProtocol LeiosTestBlock)
canBeLeader = ()
    , updateForgeState :: TopLevelConfig LeiosTestBlock
-> SlotNo
-> Ticked (ChainDepState (BlockProtocol LeiosTestBlock))
-> IOSim s (ForgeStateUpdateInfo LeiosTestBlock)
updateForgeState = \TopLevelConfig LeiosTestBlock
_ SlotNo
_ Ticked (ChainDepState (BlockProtocol LeiosTestBlock))
_ -> ForgeStateUpdateInfo LeiosTestBlock
-> IOSim s (ForgeStateUpdateInfo LeiosTestBlock)
forall a. a -> IOSim s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForgeStateUpdateInfo LeiosTestBlock
 -> IOSim s (ForgeStateUpdateInfo LeiosTestBlock))
-> ForgeStateUpdateInfo LeiosTestBlock
-> IOSim s (ForgeStateUpdateInfo LeiosTestBlock)
forall a b. (a -> b) -> a -> b
$ ForgeStateInfo LeiosTestBlock
-> ForgeStateUpdateInfo LeiosTestBlock
forall blk. ForgeStateInfo blk -> ForgeStateUpdateInfo blk
ForgeStateUpdated ()
    , checkCanForge :: TopLevelConfig LeiosTestBlock
-> SlotNo
-> Ticked (ChainDepState (BlockProtocol LeiosTestBlock))
-> IsLeader (BlockProtocol LeiosTestBlock)
-> ForgeStateInfo LeiosTestBlock
-> Either (CannotForge LeiosTestBlock) ()
checkCanForge = \TopLevelConfig LeiosTestBlock
_ SlotNo
_ Ticked (ChainDepState (BlockProtocol LeiosTestBlock))
_ IsLeader (BlockProtocol LeiosTestBlock)
_ ForgeStateInfo LeiosTestBlock
_ -> () -> Either Void ()
forall a b. b -> Either a b
Right ()
    , forgeBlock :: ForgeBlockArgs LeiosTestBlock
-> IOSim s (ForgeBlockResult LeiosTestBlock)
forgeBlock = \ForgeBlockArgs{SlotNo
fbCurrentSlotNo :: SlotNo
fbCurrentSlotNo :: forall blk. ForgeBlockArgs blk -> SlotNo
fbCurrentSlotNo, TickedLedgerState LeiosTestBlock EmptyMK
fbCurrentTickedLedgerState :: TickedLedgerState LeiosTestBlock EmptyMK
fbCurrentTickedLedgerState :: forall blk. ForgeBlockArgs blk -> TickedLedgerState blk EmptyMK
fbCurrentTickedLedgerState} -> do
        let forgedEb :: ForgedEndorserBlock
forgedEb = Version -> NonEmpty ByteString -> ForgedEndorserBlock
forgeLeiosEb Version
forall a. Bounded a => a
minBound (SlotNo -> ByteString
txInSlot SlotNo
fbCurrentSlotNo ByteString -> [ByteString] -> NonEmpty ByteString
forall a. a -> [a] -> NonEmpty a
:| [])
            eb :: EndorserBlock
eb = ForgedEndorserBlock -> EndorserBlock
forgedEbBody ForgedEndorserBlock
forgedEb
            ebHash :: EndorserBlockHash
ebHash = EndorserBlock -> EndorserBlockHash
endorserBlockHash EndorserBlock
eb
            tip :: Point LeiosTestBlock
tip = Point (Ticked LedgerState LeiosTestBlock) -> Point LeiosTestBlock
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (TickedLedgerState LeiosTestBlock EmptyMK
-> Point (Ticked LedgerState LeiosTestBlock)
forall (mk :: MapKind).
Ticked LedgerState LeiosTestBlock mk
-> Point (Ticked LedgerState LeiosTestBlock)
forall (l :: LedgerStateKind) (mk :: MapKind).
GetTip l =>
l mk -> Point l
getTip TickedLedgerState LeiosTestBlock EmptyMK
fbCurrentTickedLedgerState)
        ForgeBlockResult LeiosTestBlock
-> IOSim s (ForgeBlockResult LeiosTestBlock)
forall a. a -> IOSim s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          ForgeBlockResult
            { rankingBlock :: LeiosTestBlock
rankingBlock =
                EndorserBlockHash -> BytesSize -> LeiosTestBlock -> LeiosTestBlock
setAnnouncement EndorserBlockHash
ebHash (EndorserBlock -> BytesSize
encodeEndorserBlockSize EndorserBlock
eb) (LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> LeiosTestBlock
forall a b. (a -> b) -> a -> b
$
                  SlotNo -> LeiosTestBlock -> LeiosTestBlock
setSlot SlotNo
fbCurrentSlotNo (LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> LeiosTestBlock
forall a b. (a -> b) -> a -> b
$
                    Point LeiosTestBlock -> LeiosTestBlock
blockAfter Point LeiosTestBlock
tip
            , endorserBlock :: Maybe ForgedEndorserBlock
endorserBlock = ForgedEndorserBlock -> Maybe ForgedEndorserBlock
forall a. a -> Maybe a
Just ForgedEndorserBlock
forgedEb
            }
    , finalize :: IOSim s ()
finalize = () -> IOSim s ()
forall a. a -> IOSim s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    }

-- | A block whose predecessor is the given point. 'forgeBlock' sets its slot.
blockAfter :: Point LeiosTestBlock -> LeiosTestBlock
blockAfter :: Point LeiosTestBlock -> LeiosTestBlock
blockAfter = \case
  Point LeiosTestBlock
GenesisPoint -> Word64 -> LeiosTestBlock
firstLeiosBlock Word64
0
  BlockPoint SlotNo
_ (TestHash NonEmpty Word64
path) ->
    LeiosTestBlock -> LeiosTestBlock
successorLeiosBlock (LeiosTestBlock -> LeiosTestBlock)
-> LeiosTestBlock -> LeiosTestBlock
forall a b. (a -> b) -> a -> b
$
      Header LeiosTestBlock -> LeiosTestBlock
LeiosTestBlock (LeiosTestBlock -> Header LeiosTestBlock
ltbHeader (Word64 -> LeiosTestBlock
firstLeiosBlock Word64
0)){lthHash = TestHash path}

txInSlot :: SlotNo -> ByteString
txInSlot :: SlotNo -> ByteString
txInSlot SlotNo
slot = [Char] -> ByteString
BS8.pack ([Char]
"tx in slot " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> Word64 -> [Char]
forall a. Show a => a -> [Char]
show (SlotNo -> Word64
unSlotNo SlotNo
slot))