{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}

module Hydra.HeadLogic.LifecycleSpec (spec) where

import Hydra.Prelude
import Test.Hydra.Prelude (Expectation, Spec, arbitrary, describe, generateWith, it, shouldBe)

import Cardano.Binary (decodeFull', serialize')
import Control.Monad (foldM)
import Crem.Inspect (inspectStates)
import Crem.StateMachine qualified as Crem
import Data.List qualified as List
import Hydra.API.ClientInput qualified as Client
import Hydra.Chain (ChainEvent (PostTxError, Tick), OnChainTx (OnContestTx, OnInitTx, OnPartialFanoutTx), PostChainTx (FinalPartialFanoutTx, InitTx, PartialFanoutTx, utxoForProof, utxoToDistribute), PostTxError (FailedToConstructFanoutTx, FailedToConstructPartialFanoutTx))
import Hydra.HeadLogic.Input qualified as Input
import Hydra.HeadLogic.Outcome (Effect (OnChainEffect), Outcome (Continue), StateChanged (..))
import Hydra.HeadLogic.State (ClosedState (..), FanoutMode (..), FinalizedState (..), HeadState (..), OnChainFanoutDatum (..), OpenState (..), PartialFanoutState (..), headIdOf)
import Hydra.HeadLogic.State.Head qualified as Head
import Hydra.HeadLogic.State.Node qualified as Machine
import Hydra.HeadLogic.State.Types (DepositOutput (..))
import Hydra.HeadLogicSpec (inClosedState', inIdleState, inOpenState, observeTx, testSnapshot)
import Hydra.Ledger.Simple (SimpleChainState (..), SimpleTx, simpleLedger)
import Hydra.Node.Environment (Environment (..))
import Hydra.Node.HeadLogic (Input (ClientInput), aggregateState, update)
import Hydra.Node.State (ChainPointTime (..), NodeState (..), SyncedStatus (..), initialChainTime, syncedStatus)
import Hydra.Tx (UTxOType)
import Hydra.Tx.Crypto (aggregate, sign)
import Hydra.Tx.HeadId (HeadId (UnsafeHeadId))
import Hydra.Tx.HeadParameters (HeadParameters (..))
import Hydra.Tx.Snapshot (ConfirmedSnapshot (ConfirmedSnapshot), Snapshot)
import Test.Hydra.Ledger.Simple (utxoRef, utxoRefs)
import Test.Hydra.Node.Fixture (testEnvironment)
import Test.Hydra.Tx.Fixture (alice, aliceSk, testHeadId)

spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"head lifecycle" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
  [(String, Text, NodeState SimpleTx)]
-> ((String, Text, NodeState SimpleTx) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"Open", Text
"Open", NodeState SimpleTx
opened), (String
"Closed", Text
"Closed", NodeState SimpleTx
closed), (String
"Closed with pending fanout", Text
"Closed", NodeState SimpleTx
pending), (String
"PartialFanout", Text
"PartialFanout", NodeState SimpleTx
partial), (String
"Finalized", Text
"Finalized", NodeState SimpleTx
finalized)] (((String, Text, NodeState SimpleTx) -> Spec) -> Spec)
-> ((String, Text, NodeState SimpleTx) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
name, Text
expected, NodeState SimpleTx
before) ->
    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"does not reopen or reset " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" from a retained HeadOpened") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      let after :: NodeState SimpleTx
after = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
before StateChanged SimpleTx
openEvent
      NodeState SimpleTx
after NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
before
      NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
before [(StateChanged SimpleTx
openEvent, Text
expected)]

  [(String, StateChanged SimpleTx)]
-> ((String, StateChanged SimpleTx) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"automatic", StateChanged SimpleTx
initiated), (String
"selective", StateChanged SimpleTx
selected)] (((String, StateChanged SimpleTx) -> Spec) -> Spec)
-> ((String, StateChanged SimpleTx) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
name, StateChanged SimpleTx
intent) -> do
    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"cancels a failed " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" request before any distribution is observed") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
closed [(StateChanged SimpleTx
intent, Text
"Closed"), (StateChanged SimpleTx
reverted, Text
"Closed")]
      let cancelled :: NodeState SimpleTx
cancelled = NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
closed [StateChanged SimpleTx
intent, StateChanged SimpleTx
reverted]
      case NodeState SimpleTx
cancelled.headState of
        Closed{} -> NodeState SimpleTx
cancelled NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
closed
        HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected a closed head after cancelling pending fanout"

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"records partial distribution after a pending " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" request") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
      NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
closed [(StateChanged SimpleTx
intent, Text
"Closed"), (FanoutMode SimpleTx -> StateChanged SimpleTx
observed FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection, Text
"PartialFanout"), (StateChanged SimpleTx
finished, Text
"Finalized")]

  [(String, ClientInput SimpleTx)]
-> ((String, ClientInput SimpleTx) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"automatic", ClientInput SimpleTx
forall tx. ClientInput tx
Client.Fanout), (String
"selective", UTxOType SimpleTx -> ClientInput SimpleTx
forall tx. UTxOType tx -> ClientInput tx
Client.PartialFanout (UTxOType SimpleTx -> ClientInput SimpleTx)
-> UTxOType SimpleTx -> ClientInput SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1)] (((String, ClientInput SimpleTx) -> Spec) -> Spec)
-> ((String, ClientInput SimpleTx) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
name, ClientInput SimpleTx
command) -> do
    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"accepts a contest and adopts its snapshot during pending " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" fanout") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      let before :: NodeState SimpleTx
before = Bool -> NodeState SimpleTx
closedWithCandidate Bool
False
          requested :: NodeState SimpleTx
requested = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
before (Input SimpleTx -> NodeState SimpleTx)
-> Input SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
command
          signatures :: MultiSignature (Snapshot SimpleTx)
signatures = [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk Snapshot SimpleTx
candidate]
          deadline :: UTCTime
deadline = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
1200 UTCTime
initialChainTime
          contested :: NodeState SimpleTx
contested = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
requested (Input SimpleTx -> NodeState SimpleTx)
-> Input SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ OnChainTx SimpleTx -> Input SimpleTx
observeTx (OnChainTx SimpleTx -> Input SimpleTx)
-> OnChainTx SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadId
-> SnapshotNumber
-> UTCTime
-> MultiSignature (Snapshot SimpleTx)
-> OnChainTx SimpleTx
forall tx.
HeadId
-> SnapshotNumber
-> UTCTime
-> MultiSignature (Snapshot tx)
-> OnChainTx tx
OnContestTx HeadId
testHeadId SnapshotNumber
2 UTCTime
deadline MultiSignature (Snapshot SimpleTx)
signatures
      case NodeState SimpleTx
contested.headState of
        Closed ClosedState SimpleTx
st -> do
          ClosedState SimpleTx
st.confirmedSnapshot ConfirmedSnapshot SimpleTx -> ConfirmedSnapshot SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot Snapshot SimpleTx
candidate MultiSignature (Snapshot SimpleTx)
signatures
          ClosedState SimpleTx
st.unconfirmedSnapshot Maybe (Snapshot SimpleTx) -> Maybe (Snapshot SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing
          ClosedState SimpleTx
st.contestationDeadline UTCTime -> UTCTime -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` UTCTime
deadline
        HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"a contest cancels stale local fanout work while the Head remains Closed"
      NodeState SimpleTx
restored <- (DecoderError -> IO (NodeState SimpleTx))
-> (NodeState SimpleTx -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (NodeState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (NodeState SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (NodeState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (NodeState SimpleTx)
 -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (NodeState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (NodeState SimpleTx))
-> ByteString -> Either DecoderError (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' NodeState SimpleTx
requested
      NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
restored (OnChainTx SimpleTx -> Input SimpleTx
observeTx (OnChainTx SimpleTx -> Input SimpleTx)
-> OnChainTx SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadId
-> SnapshotNumber
-> UTCTime
-> MultiSignature (Snapshot SimpleTx)
-> OnChainTx SimpleTx
forall tx.
HeadId
-> SnapshotNumber
-> UTCTime
-> MultiSignature (Snapshot tx)
-> OnChainTx tx
OnContestTx HeadId
testHeadId SnapshotNumber
2 UTCTime
deadline MultiSignature (Snapshot SimpleTx)
signatures) NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
contested

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"ignores a pre-contest failure after a new " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" fanout request") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      let before :: NodeState SimpleTx
before = Bool -> NodeState SimpleTx
closedWithCandidate Bool
False
          oldRequest :: Outcome SimpleTx
oldRequest = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
testEnvironment Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
before (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
command
          pendingFirst :: NodeState SimpleTx
pendingFirst = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
before Outcome SimpleTx
oldRequest
          signatures :: MultiSignature (Snapshot SimpleTx)
signatures = [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk Snapshot SimpleTx
candidate]
          contested :: NodeState SimpleTx
contested = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
pendingFirst (Input SimpleTx -> NodeState SimpleTx)
-> Input SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ OnChainTx SimpleTx -> Input SimpleTx
observeTx (OnChainTx SimpleTx -> Input SimpleTx)
-> OnChainTx SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadId
-> SnapshotNumber
-> UTCTime
-> MultiSignature (Snapshot SimpleTx)
-> OnChainTx SimpleTx
forall tx.
HeadId
-> SnapshotNumber
-> UTCTime
-> MultiSignature (Snapshot tx)
-> OnChainTx tx
OnContestTx HeadId
testHeadId SnapshotNumber
2 (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
1200 UTCTime
initialChainTime) MultiSignature (Snapshot SimpleTx)
signatures
          newRequest :: Outcome SimpleTx
newRequest = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
testEnvironment Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
contested (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
command
          pendingSecond :: NodeState SimpleTx
pendingSecond = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
contested Outcome SimpleTx
newRequest
      case (Outcome SimpleTx
oldRequest, Outcome SimpleTx
newRequest) of
        (Continue [StateChanged SimpleTx]
_ [OnChainEffect PostChainTx SimpleTx
oldTx], Continue [StateChanged SimpleTx]
_ [OnChainEffect PostChainTx SimpleTx
_]) ->
          NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
pendingSecond (ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Input.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ PostChainTx SimpleTx
-> PostTxError SimpleTx -> Maybe SimpleTx -> ChainEvent SimpleTx
forall tx.
PostChainTx tx -> PostTxError tx -> Maybe tx -> ChainEvent tx
PostTxError PostChainTx SimpleTx
oldTx PostTxError SimpleTx
forall tx. PostTxError tx
FailedToConstructFanoutTx Maybe SimpleTx
forall a. Maybe a
Nothing) NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
pendingSecond
        (Outcome SimpleTx, Outcome SimpleTx)
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected fanout requests before and after the contest"

    [Bool] -> (Bool -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> Spec) -> Spec) -> (Bool -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \Bool
ready ->
      String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"preserves the candidate and readiness after a failed " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" fanout, ready=" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Bool -> String
forall b a. (Show a, IsString b) => a -> b
show Bool
ready) (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
        let before :: NodeState SimpleTx
before = Bool -> NodeState SimpleTx
closedWithCandidate Bool
ready
            outcome :: Outcome SimpleTx
outcome = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
testEnvironment Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
before (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
command
            requested :: NodeState SimpleTx
requested = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
before Outcome SimpleTx
outcome
        case Outcome SimpleTx
outcome of
          Continue [StateChanged SimpleTx]
_ [OnChainEffect PostChainTx SimpleTx
tx] -> do
            NodeState SimpleTx
restored <- (DecoderError -> IO (NodeState SimpleTx))
-> (NodeState SimpleTx -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (NodeState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (NodeState SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (NodeState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (NodeState SimpleTx)
 -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (NodeState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (NodeState SimpleTx))
-> ByteString -> Either DecoderError (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' NodeState SimpleTx
requested
            let cancelled :: NodeState SimpleTx
cancelled = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
restored (Input SimpleTx -> NodeState SimpleTx)
-> Input SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Input.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ PostChainTx SimpleTx
-> PostTxError SimpleTx -> Maybe SimpleTx -> ChainEvent SimpleTx
forall tx.
PostChainTx tx -> PostTxError tx -> Maybe tx -> ChainEvent tx
PostTxError PostChainTx SimpleTx
tx PostTxError SimpleTx
forall tx. PostTxError tx
FailedToConstructFanoutTx Maybe SimpleTx
forall a. Maybe a
Nothing
            NodeState SimpleTx
cancelled NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
before
            let tick :: Input SimpleTx
tick = ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Input.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ UTCTime -> ChainPointType SimpleTx -> ChainEvent SimpleTx
forall tx. UTCTime -> ChainPointType tx -> ChainEvent tx
Tick (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
601 UTCTime
initialChainTime) ChainPointType SimpleTx
1
                notified :: NodeState SimpleTx
notified = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
cancelled Input SimpleTx
tick
            case NodeState SimpleTx
notified.headState of
              Closed ClosedState SimpleTx
st -> ClosedState SimpleTx
st.readyToFanoutSent Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool
True
              HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected Closed after the deadline tick"
          Outcome SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"expected a pending fanout transaction, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Outcome SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Outcome SimpleTx
outcome

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"notifies readiness while " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" fanout is pending") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      let before :: NodeState SimpleTx
before = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput (Bool -> NodeState SimpleTx
closedWithCandidate Bool
False) (Input SimpleTx -> NodeState SimpleTx)
-> Input SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
command
          outcome :: Outcome SimpleTx
outcome = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
testEnvironment Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
before (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Input.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ UTCTime -> ChainPointType SimpleTx -> ChainEvent SimpleTx
forall tx. UTCTime -> ChainPointType tx -> ChainEvent tx
Tick (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
601 UTCTime
initialChainTime) ChainSlot
ChainPointType SimpleTx
1
          after :: NodeState SimpleTx
after = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
before Outcome SimpleTx
outcome
          cancelled :: NodeState SimpleTx
cancelled = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
after StateChanged SimpleTx
reverted
      case Outcome SimpleTx
outcome of
        Continue [StateChanged SimpleTx]
changes [Effect SimpleTx]
_ -> (StateChanged SimpleTx -> Bool)
-> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. (a -> Bool) -> [a] -> [a]
filter (\case HeadIsReadyToFanout{} -> Bool
True; StateChanged SimpleTx
_ -> Bool
False) [StateChanged SimpleTx]
changes [StateChanged SimpleTx] -> [StateChanged SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [HeadId -> StateChanged SimpleTx
forall tx. HeadId -> StateChanged tx
HeadIsReadyToFanout HeadId
testHeadId]
        Outcome SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected a readiness notification while pending"
      case NodeState SimpleTx
cancelled.headState of
        Closed ClosedState SimpleTx
st -> ClosedState SimpleTx
st.readyToFanoutSent Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool
True
        HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected Closed after cancelling the request"

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"observes another party's partial fanout without a local request" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
closed [(FanoutMode SimpleTx -> StateChanged SimpleTx
observed FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection, Text
"PartialFanout"), (StateChanged SimpleTx
finished, Text
"Finalized")]
    NodeState SimpleTx
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> FanoutMode SimpleTx
-> IO ()
checkProgress NodeState SimpleTx
partial (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
2, SimpleId
3]) FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection

  [(String, NodeState SimpleTx, FanoutMode SimpleTx)]
-> ((String, NodeState SimpleTx, FanoutMode SimpleTx) -> Spec)
-> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"observer", NodeState SimpleTx
closed, FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection), (String
"pending request", NodeState SimpleTx
pending, FanoutMode SimpleTx
forall tx. FanoutMode tx
AutoDrain)] (((String, NodeState SimpleTx, FanoutMode SimpleTx) -> Spec)
 -> Spec)
-> ((String, NodeState SimpleTx, FanoutMode SimpleTx) -> Spec)
-> Spec
forall a b. (a -> b) -> a -> b
$ \(String
name, NodeState SimpleTx
initial, FanoutMode SimpleTx
expectedMode) ->
    [(String, Set SimpleTxOut, Bool)]
-> ((String, Set SimpleTxOut, Bool) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"the whole remainder", [SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3], Bool
True), (String
"a strict subset", SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1, Bool
False)] (((String, Set SimpleTxOut, Bool) -> Spec) -> Spec)
-> ((String, Set SimpleTxOut, Bool) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
selectionName, Set SimpleTxOut
selection, Bool
isFinal) ->
      String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"retains observed fanout with no locally matching outputs after " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" when selecting " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
selectionName) (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
        let remaining :: UTxOType SimpleTx
remaining = [SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3]
            observation :: Input SimpleTx
observation = OnChainTx SimpleTx -> Input SimpleTx
observeTx (OnChainTx SimpleTx -> Input SimpleTx)
-> OnChainTx SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadId -> UTxOType SimpleTx -> OnChainTx SimpleTx
forall tx. HeadId -> UTxOType tx -> OnChainTx tx
OnPartialFanoutTx HeadId
testHeadId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
999)
            observedNode :: NodeState SimpleTx
observedNode = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
initial Input SimpleTx
observation
        NodeState SimpleTx
restored <- (DecoderError -> IO (NodeState SimpleTx))
-> (NodeState SimpleTx -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (NodeState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (NodeState SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (NodeState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (NodeState SimpleTx)
 -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (NodeState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (NodeState SimpleTx))
-> ByteString -> Either DecoderError (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' NodeState SimpleTx
observedNode
        NodeState SimpleTx
restored NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
observedNode
        NodeState SimpleTx -> Maybe Text
restoredPhase NodeState SimpleTx
restored Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"PartialFanout"
        NodeState SimpleTx
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> FanoutMode SimpleTx
-> IO ()
checkProgress NodeState SimpleTx
restored Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty Set SimpleTxOut
UTxOType SimpleTx
remaining FanoutMode SimpleTx
expectedMode
        case NodeState SimpleTx
restored.headState of
          FanoutProgress PartialFanoutState SimpleTx
st -> PartialFanoutState SimpleTx
st.fanoutDatum OnChainFanoutDatum -> OnChainFanoutDatum -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` OnChainFanoutDatum
DatumFanoutProgress
          HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected observed fanout progress"
        let command :: Input SimpleTx
command = ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput (ClientInput SimpleTx -> Input SimpleTx)
-> ClientInput SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ UTxOType SimpleTx -> ClientInput SimpleTx
forall tx. UTxOType tx -> ClientInput tx
Client.PartialFanout Set SimpleTxOut
UTxOType SimpleTx
selection
            outcome :: Outcome SimpleTx
outcome = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
testEnvironment Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
restored Input SimpleTx
command
            selectedNode :: NodeState SimpleTx
selectedNode = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
restored Outcome SimpleTx
outcome
        NodeState SimpleTx
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> FanoutMode SimpleTx
-> IO ()
checkProgress NodeState SimpleTx
selectedNode Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty Set SimpleTxOut
UTxOType SimpleTx
remaining (UTxOType SimpleTx -> FanoutMode SimpleTx
forall tx. UTxOType tx -> FanoutMode tx
DistributingSelection Set SimpleTxOut
UTxOType SimpleTx
selection)
        case NodeState SimpleTx
selectedNode.headState of
          FanoutProgress PartialFanoutState SimpleTx
st -> PartialFanoutState SimpleTx
st.fanoutDatum OnChainFanoutDatum -> OnChainFanoutDatum -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` OnChainFanoutDatum
DatumFanoutProgress
          HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected observed fanout progress after selection"
        case Outcome SimpleTx
outcome of
          Continue [StateChanged SimpleTx]
_ [OnChainEffect PostChainTx SimpleTx
tx] -> do
            case (Bool
isFinal, PostChainTx SimpleTx
tx) of
              (Bool
True, FinalPartialFanoutTx{UTxOType SimpleTx
$sel:utxoToDistribute:InitTx :: forall tx. PostChainTx tx -> UTxOType tx
utxoToDistribute :: UTxOType SimpleTx
utxoToDistribute}) ->
                Set SimpleTxOut
UTxOType SimpleTx
utxoToDistribute Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Set SimpleTxOut
selection
              (Bool
False, PartialFanoutTx{UTxOType SimpleTx
$sel:utxoToDistribute:InitTx :: forall tx. PostChainTx tx -> UTxOType tx
utxoToDistribute :: UTxOType SimpleTx
utxoToDistribute, UTxOType SimpleTx
$sel:utxoForProof:InitTx :: forall tx. PostChainTx tx -> UTxOType tx
utxoForProof :: UTxOType SimpleTx
utxoForProof}) -> do
                Set SimpleTxOut
UTxOType SimpleTx
utxoToDistribute Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Set SimpleTxOut
selection
                Set SimpleTxOut
UTxOType SimpleTx
utxoForProof Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Set SimpleTxOut
remaining
              (Bool, PostChainTx SimpleTx)
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"unexpected fanout transaction for " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
selectionName String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> PostChainTx SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show PostChainTx SimpleTx
tx
            let constructionError :: PostTxError SimpleTx
constructionError = if Bool
isFinal then PostTxError SimpleTx
forall tx. PostTxError tx
FailedToConstructFanoutTx else PostTxError SimpleTx
forall tx. PostTxError tx
FailedToConstructPartialFanoutTx
                failed :: NodeState SimpleTx
failed = NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
selectedNode (Input SimpleTx -> NodeState SimpleTx)
-> Input SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Input.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ PostChainTx SimpleTx
-> PostTxError SimpleTx -> Maybe SimpleTx -> ChainEvent SimpleTx
forall tx.
PostChainTx tx -> PostTxError tx -> Maybe tx -> ChainEvent tx
PostTxError PostChainTx SimpleTx
tx PostTxError SimpleTx
constructionError Maybe SimpleTx
forall a. Maybe a
Nothing
            NodeState SimpleTx
failed NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
selectedNode
            NodeState SimpleTx -> Maybe Text
restoredPhase NodeState SimpleTx
failed Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"PartialFanout"
          Outcome SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"expected one fanout transaction, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Outcome SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Outcome SimpleTx
outcome
        NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
restored [(StateChanged SimpleTx
reverted, Text
"PartialFanout"), (StateChanged SimpleTx
finished, Text
"Finalized")]

  [(String, FanoutMode SimpleTx)]
-> ((String, FanoutMode SimpleTx) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"automatic", FanoutMode SimpleTx
forall tx. FanoutMode tx
AutoDrain), (String
"selective", UTxOType SimpleTx -> FanoutMode SimpleTx
forall tx. UTxOType tx -> FanoutMode tx
DistributingSelection (UTxOType SimpleTx -> FanoutMode SimpleTx)
-> UTxOType SimpleTx -> FanoutMode SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2), (String
"waiting", FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection)] (((String, FanoutMode SimpleTx) -> Spec) -> Spec)
-> ((String, FanoutMode SimpleTx) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
name, FanoutMode SimpleTx
mode) ->
    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"does not revert observed partial fanout while " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
name) (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      let before :: NodeState SimpleTx
before = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
closed (StateChanged SimpleTx -> NodeState SimpleTx)
-> StateChanged SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ FanoutMode SimpleTx -> StateChanged SimpleTx
observed FanoutMode SimpleTx
mode
      NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
before [(StateChanged SimpleTx
reverted, Text
"PartialFanout")]
      NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
before StateChanged SimpleTx
reverted NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
before

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves distributed outputs across selections, further observations and rollback" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let nextSelection :: StateChanged SimpleTx
nextSelection = HeadId
-> UTxOType SimpleTx -> UTxOType SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> UTxOType tx -> UTxOType tx -> StateChanged tx
HeadPartialFanoutSelected HeadId
testHeadId ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
2, SimpleId
3]) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2)
        nextObservation :: StateChanged SimpleTx
nextObservation = HeadId
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> ChainStateType SimpleTx
-> FanoutMode SimpleTx
-> StateChanged SimpleTx
forall tx.
HeadId
-> UTxOType tx
-> UTxOType tx
-> ChainStateType tx
-> FanoutMode tx
-> StateChanged tx
HeadPartialFannedOut HeadId
testHeadId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3) (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection
        rolledBack :: StateChanged SimpleTx
rolledBack = ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2)
        events :: [StateChanged SimpleTx]
events = [StateChanged SimpleTx
nextSelection, StateChanged SimpleTx
nextObservation, StateChanged SimpleTx
rolledBack, HeadId -> UTxOType SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> UTxOType tx -> StateChanged tx
HeadFanoutInitiated HeadId
testHeadId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3), StateChanged SimpleTx
finished]
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
partial ([(StateChanged SimpleTx, Text)] -> IO ())
-> [(StateChanged SimpleTx, Text)] -> IO ()
forall a b. (a -> b) -> a -> b
$ [StateChanged SimpleTx]
-> [Text] -> [(StateChanged SimpleTx, Text)]
forall a b. [a] -> [b] -> [(a, b)]
zip [StateChanged SimpleTx]
events [Text
"PartialFanout", Text
"PartialFanout", Text
"PartialFanout", Text
"PartialFanout", Text
"Finalized"]
    NodeState SimpleTx
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> FanoutMode SimpleTx
-> IO ()
checkProgress (NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
partial StateChanged SimpleTx
nextSelection) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
2, SimpleId
3]) (UTxOType SimpleTx -> FanoutMode SimpleTx
forall tx. UTxOType tx -> FanoutMode tx
DistributingSelection (UTxOType SimpleTx -> FanoutMode SimpleTx)
-> UTxOType SimpleTx -> FanoutMode SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2)
    NodeState SimpleTx
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> FanoutMode SimpleTx
-> IO ()
checkProgress (NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
partial [StateChanged SimpleTx
nextSelection, StateChanged SimpleTx
nextObservation, StateChanged SimpleTx
rolledBack]) ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2]) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3) FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"can finish a full fanout without entering partial fanout" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
closed [(StateChanged SimpleTx
finished, Text
"Finalized")]
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
pending [(StateChanged SimpleTx
finished, Text
"Finalized")]

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"updates the contest deadline without leaving Closed" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let deadline :: UTCTime
deadline = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
60 UTCTime
initialChainTime
        contested :: StateChanged SimpleTx
contested = HeadId
-> ChainStateType SimpleTx
-> UTCTime
-> SnapshotNumber
-> StateChanged SimpleTx
forall tx.
HeadId
-> ChainStateType tx
-> UTCTime
-> SnapshotNumber
-> StateChanged tx
HeadContested HeadId
testHeadId (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) UTCTime
deadline SnapshotNumber
1
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
closed [(StateChanged SimpleTx
contested, Text
"Closed"), (StateChanged SimpleTx
finished, Text
"Finalized")]
    case (NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
closed StateChanged SimpleTx
contested).headState of
      Closed ClosedState SimpleTx
st -> (ClosedState SimpleTx
st.contestationDeadline, ClosedState SimpleTx
st.chainState) (UTCTime, SimpleChainState) -> (UTCTime, SimpleChainState) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` (UTCTime
deadline, ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3)
      HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"contest must preserve Closed"

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retains the completed identity across checkpoints and rollback" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let rollback :: StateChanged SimpleTx
rollback = ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainStateType SimpleTx -> StateChanged SimpleTx)
-> ChainStateType SimpleTx -> StateChanged SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
partial [(StateChanged SimpleTx
finished, Text
"Finalized"), (StateChanged SimpleTx
rollback, Text
"Finalized")]
    NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
partial [StateChanged SimpleTx
finished, NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
finalized, StateChanged SimpleTx
rollback]
      NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
finalized StateChanged SimpleTx
rollback
    HeadState SimpleTx -> Maybe HeadId
forall tx. HeadState tx -> Maybe HeadId
headIdOf NodeState SimpleTx
finalized.headState Maybe HeadId -> Maybe HeadId -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` HeadId -> Maybe HeadId
forall a. a -> Maybe a
Just HeadId
testHeadId
    (NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
finalized (StateChanged SimpleTx -> NodeState SimpleTx)
-> StateChanged SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainStateType SimpleTx -> StateChanged SimpleTx)
-> ChainStateType SimpleTx -> StateChanged SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2).headState
      HeadState SimpleTx -> HeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` FinalizedState SimpleTx -> HeadState SimpleTx
forall tx. FinalizedState tx -> HeadState tx
Finalized FinalizedState{$sel:headId:FinalizedState :: HeadId
headId = HeadId
testHeadId, $sel:chainState:FinalizedState :: ChainStateType SimpleTx
chainState = ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2}

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"does not reopen a finalized Head even for a different identity" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let input :: DepositOutput SimpleTx
input = DepositOutput{$sel:event:DepositOutput :: StateChanged SimpleTx
event = StateChanged SimpleTx
nextOpenEvent, $sel:deposits:DepositOutput :: Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
deposits = Map SimpleId (TrackedDeposit SimpleTx)
Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
forall a. Monoid a => a
mempty, $sel:settledDeposit:DepositOutput :: UTxOType SimpleTx
settledDeposit = Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty}
        (HeadState SimpleTx
result, StateMachineT
  Identity (DepositOutput SimpleTx) (HeadState SimpleTx)
next) = Identity
  (HeadState SimpleTx,
   StateMachineT
     Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
-> (HeadState SimpleTx,
    StateMachineT
      Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
forall a. Identity a -> a
runIdentity (Identity
   (HeadState SimpleTx,
    StateMachineT
      Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
 -> (HeadState SimpleTx,
     StateMachineT
       Identity (DepositOutput SimpleTx) (HeadState SimpleTx)))
-> Identity
     (HeadState SimpleTx,
      StateMachineT
        Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
-> (HeadState SimpleTx,
    StateMachineT
      Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
forall a b. (a -> b) -> a -> b
$ StateMachineT
  Identity (DepositOutput SimpleTx) (HeadState SimpleTx)
-> DepositOutput SimpleTx
-> Identity
     (HeadState SimpleTx,
      StateMachineT
        Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
forall (m :: * -> *) a b.
Monad m =>
StateMachineT m a b -> a -> m (b, StateMachineT m a b)
Crem.run (BaseMachineT
  Identity
  ('Topology
     '[ '( 'Idle, '[ 'Open]), '( 'Open, '[ 'Open, 'Closed]),
        '( 'Closed, '[ 'Closed, 'PartialFanout, 'Finalized]),
        '( 'PartialFanout, '[ 'PartialFanout, 'Finalized]),
        '( 'Finalized, '[])])
  (DepositOutput SimpleTx)
  (HeadState SimpleTx)
-> StateMachineT
     Identity (DepositOutput SimpleTx) (HeadState SimpleTx)
forall (m :: * -> *) vertex (topology :: Topology vertex) input
       output.
(Demote vertex ~ vertex, SingKind vertex, SingI topology,
 Eq vertex, Show vertex, RenderableVertices vertex) =>
BaseMachineT m topology input output
-> StateMachineT m input output
Crem.Basic (BaseMachineT
   Identity
   ('Topology
      '[ '( 'Idle, '[ 'Open]), '( 'Open, '[ 'Open, 'Closed]),
         '( 'Closed, '[ 'Closed, 'PartialFanout, 'Finalized]),
         '( 'PartialFanout, '[ 'PartialFanout, 'Finalized]),
         '( 'Finalized, '[])])
   (DepositOutput SimpleTx)
   (HeadState SimpleTx)
 -> StateMachineT
      Identity (DepositOutput SimpleTx) (HeadState SimpleTx))
-> BaseMachineT
     Identity
     ('Topology
        '[ '( 'Idle, '[ 'Open]), '( 'Open, '[ 'Open, 'Closed]),
           '( 'Closed, '[ 'Closed, 'PartialFanout, 'Finalized]),
           '( 'PartialFanout, '[ 'PartialFanout, 'Finalized]),
           '( 'Finalized, '[])])
     (DepositOutput SimpleTx)
     (HeadState SimpleTx)
-> StateMachineT
     Identity (DepositOutput SimpleTx) (HeadState SimpleTx)
forall a b. (a -> b) -> a -> b
$ HeadState SimpleTx
-> BaseMachineT
     Identity
     LifecycleTopology
     (DepositOutput SimpleTx)
     (HeadState SimpleTx)
forall tx (m :: * -> *).
(IsChainState tx, Applicative m) =>
HeadState tx
-> BaseMachineT
     m LifecycleTopology (DepositOutput tx) (HeadState tx)
Head.machine NodeState SimpleTx
finalized.headState) DepositOutput SimpleTx
input
    HeadState SimpleTx
result HeadState SimpleTx -> HeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
finalized.headState
    StateMachineT
  Identity (DepositOutput SimpleTx) (HeadState SimpleTx)
-> [([Component], Maybe Text)]
forall (m :: * -> *) a b.
StateMachineT m a b -> [([Component], Maybe Text)]
inspectStates StateMachineT
  Identity (DepositOutput SimpleTx) (HeadState SimpleTx)
next [([Component], Maybe Text)] -> [([Component], Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [([], Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"Finalized")]

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"starts a separate Head at the node boundary and preserves tracked deposits" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let before :: NodeState SimpleTx
        before :: NodeState SimpleTx
before = NodeState SimpleTx
finalized{deposits = generateWith arbitrary 42}
        after :: NodeState SimpleTx
after = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
before StateChanged SimpleTx
nextOpenEvent
    NodeState SimpleTx -> Maybe Text
restoredPhase NodeState SimpleTx
after Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"Open"
    NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
before [StateChanged SimpleTx
nextOpenEvent, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1]
      NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
after (SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1)
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
after [(SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1, Text
"Open")]
    HeadState SimpleTx -> Maybe HeadId
forall tx. HeadState tx -> Maybe HeadId
headIdOf NodeState SimpleTx
after.headState Maybe HeadId -> Maybe HeadId -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` HeadId -> Maybe HeadId
forall a. a -> Maybe a
Just HeadId
nextHeadId
    NodeState SimpleTx
after.deposits Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
-> Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
before.deposits

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"accepts Init and observes initialization of a different Head after finalization" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let env :: Environment
env = Environment
testEnvironment
        initInput :: Input SimpleTx
        initInput :: Input SimpleTx
initInput = ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
forall tx. ClientInput tx
Client.Init
        parameters :: HeadParameters
parameters = HeadParameters{$sel:parties:HeadParameters :: [Party]
parties = Environment
env.party Party -> [Party] -> [Party]
forall a. a -> [a] -> [a]
: Environment
env.otherParties, $sel:contestationPeriod:HeadParameters :: ContestationPeriod
contestationPeriod = Environment
env.contestationPeriod, $sel:depositPeriod:HeadParameters :: DepositPeriod
depositPeriod = Environment
env.depositPeriod}
        input :: Input SimpleTx
input = OnChainTx SimpleTx -> Input SimpleTx
observeTx (OnChainTx SimpleTx -> Input SimpleTx)
-> OnChainTx SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadId
-> HeadSeed -> HeadParameters -> [OnChainId] -> OnChainTx SimpleTx
forall tx.
HeadId -> HeadSeed -> HeadParameters -> [OnChainId] -> OnChainTx tx
OnInitTx HeadId
nextHeadId HeadSeed
"new-seed" HeadParameters
parameters Environment
env.participants
        outcome :: Outcome SimpleTx
outcome = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
env Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
finalized Input SimpleTx
input
    Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
env Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
finalized Input SimpleTx
initInput
      Outcome SimpleTx -> Outcome SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [] [PostChainTx SimpleTx -> Effect SimpleTx
forall tx. PostChainTx tx -> Effect tx
OnChainEffect (PostChainTx SimpleTx -> Effect SimpleTx)
-> PostChainTx SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ [OnChainId] -> HeadParameters -> PostChainTx SimpleTx
forall tx. [OnChainId] -> HeadParameters -> PostChainTx tx
InitTx Environment
env.participants HeadParameters
parameters]
    HeadState SimpleTx -> Maybe HeadId
forall tx. HeadState tx -> Maybe HeadId
headIdOf (NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
finalized Outcome SimpleTx
outcome).headState Maybe HeadId -> Maybe HeadId -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` HeadId -> Maybe HeadId
forall a. a -> Maybe a
Just HeadId
nextHeadId
    NodeState SimpleTx -> Maybe Text
restoredPhase (NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
finalized Outcome SimpleTx
outcome) Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"Open"

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"opens from Idle and restores checkpoints independently of lifecycle edges" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
inIdleState [(StateChanged SimpleTx
openEvent, Text
"Open")]
    NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
partial (NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
opened) NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
opened
    NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
partial [NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
opened, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
2]
      NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
opened (SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
2)
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
opened [(SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
2, Text
"Open")]

  [(String, NodeState SimpleTx)]
-> ((String, NodeState SimpleTx) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"Idle", NodeState SimpleTx
inIdleState), (String
"Open", NodeState SimpleTx
opened), (String
"Closed", NodeState SimpleTx
closed), (String
"PartialFanout", NodeState SimpleTx
partial), (String
"Finalized", NodeState SimpleTx
finalized)] (((String, NodeState SimpleTx) -> Spec) -> Spec)
-> ((String, NodeState SimpleTx) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
phase, NodeState SimpleTx
initial) ->
    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"preserves Head and deposits across chain-time and sync updates in " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
phase) (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      let before :: NodeState SimpleTx
          before :: NodeState SimpleTx
before = NodeState SimpleTx
initial{deposits = generateWith arbitrary 42}
          nextTime :: UTCTime
nextTime = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
10 UTCTime
initialChainTime
          unsynced :: StateChanged SimpleTx
          unsynced :: StateChanged SimpleTx
unsynced = ChainSlot -> UTCTime -> NominalDiffTime -> StateChanged SimpleTx
forall tx.
ChainSlot -> UTCTime -> NominalDiffTime -> StateChanged tx
NodeUnsynced ChainSlot
5 UTCTime
initialChainTime NominalDiffTime
60
          tick :: StateChanged SimpleTx
tick = ChainPointType SimpleTx -> UTCTime -> StateChanged SimpleTx
forall tx. ChainPointType tx -> UTCTime -> StateChanged tx
TickObserved ChainPointType SimpleTx
6 UTCTime
nextTime
          synced :: StateChanged SimpleTx
          synced :: StateChanged SimpleTx
synced = ChainSlot -> UTCTime -> NominalDiffTime -> StateChanged SimpleTx
forall tx.
ChainSlot -> UTCTime -> NominalDiffTime -> StateChanged tx
NodeSynced ChainSlot
6 UTCTime
nextTime NominalDiffTime
2
          catchingUp :: NodeState SimpleTx
catchingUp = NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
before [StateChanged SimpleTx
unsynced, StateChanged SimpleTx
tick]
          caughtUp :: NodeState SimpleTx
caughtUp = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
catchingUp StateChanged SimpleTx
synced
      NodeState SimpleTx
catchingUp NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` HeadState SimpleTx
-> Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
-> ChainPointTime
-> NodeState SimpleTx
forall tx.
HeadState tx
-> TrackedDeposits tx -> ChainPointTime -> NodeState tx
NodeCatchingUp NodeState SimpleTx
before.headState NodeState SimpleTx
before.deposits (ChainSlot -> UTCTime -> NominalDiffTime -> ChainPointTime
ChainPointTime ChainSlot
6 UTCTime
nextTime NominalDiffTime
60)
      NodeState SimpleTx
caughtUp NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` HeadState SimpleTx
-> Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
-> ChainPointTime
-> NodeState SimpleTx
forall tx.
HeadState tx
-> TrackedDeposits tx -> ChainPointTime -> NodeState tx
NodeInSync NodeState SimpleTx
before.headState NodeState SimpleTx
before.deposits (ChainSlot -> UTCTime -> NominalDiffTime -> ChainPointTime
ChainPointTime ChainSlot
6 UTCTime
nextTime NominalDiffTime
2)
      NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
before [(StateChanged SimpleTx
event, String -> Text
forall a. ToText a => a -> Text
toText String
phase) | StateChanged SimpleTx
event <- [StateChanged SimpleTx
unsynced, StateChanged SimpleTx
tick, StateChanged SimpleTx
synced]]

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves observed time across opening and rollback and restores it from a checkpoint" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let before :: NodeState SimpleTx
before = HeadState SimpleTx
-> Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
-> ChainPointTime
-> NodeState SimpleTx
forall tx.
HeadState tx
-> TrackedDeposits tx -> ChainPointTime -> NodeState tx
NodeCatchingUp NodeState SimpleTx
inIdleState.headState Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
forall a. Monoid a => a
mempty (ChainSlot -> UTCTime -> NominalDiffTime -> ChainPointTime
ChainPointTime ChainSlot
10 UTCTime
initialChainTime NominalDiffTime
15)
        opening :: StateChanged SimpleTx
        opening :: StateChanged SimpleTx
opening = StateChanged SimpleTx
openEvent{chainState = SimpleChainState 8}
        rollback :: StateChanged SimpleTx
rollback = ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainStateType SimpleTx -> StateChanged SimpleTx)
-> ChainStateType SimpleTx -> StateChanged SimpleTx
forall a b. (a -> b) -> a -> b
$ ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
4
        tick :: StateChanged SimpleTx
tick = ChainPointType SimpleTx -> UTCTime -> StateChanged SimpleTx
forall tx. ChainPointType tx -> UTCTime -> StateChanged tx
TickObserved ChainPointType SimpleTx
12 (UTCTime -> StateChanged SimpleTx)
-> UTCTime -> StateChanged SimpleTx
forall a b. (a -> b) -> a -> b
$ NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
10 UTCTime
initialChainTime
        newHead :: NodeState SimpleTx
newHead = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
before StateChanged SimpleTx
opening
        rewound :: NodeState SimpleTx
rewound = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
newHead StateChanged SimpleTx
rollback
        advanced :: NodeState SimpleTx
advanced = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
rewound StateChanged SimpleTx
tick
        restored :: NodeState SimpleTx
restored = NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
advanced [NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
rewound, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1]
    NodeState SimpleTx
newHead.chainPointTime ChainPointTime -> ChainPointTime -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` ChainSlot -> UTCTime -> NominalDiffTime -> ChainPointTime
ChainPointTime ChainSlot
8 UTCTime
initialChainTime NominalDiffTime
15
    NodeState SimpleTx
rewound.chainPointTime ChainPointTime -> ChainPointTime -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` ChainSlot -> UTCTime -> NominalDiffTime -> ChainPointTime
ChainPointTime ChainSlot
4 UTCTime
initialChainTime NominalDiffTime
15
    NodeState SimpleTx
restored.chainPointTime ChainPointTime -> ChainPointTime -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
rewound.chainPointTime
    NodeState SimpleTx -> SyncedStatus
forall tx. NodeState tx -> SyncedStatus
syncedStatus NodeState SimpleTx
restored SyncedStatus -> SyncedStatus -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SyncedStatus
CatchingUp
    NodeState SimpleTx
restored NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
rewound (SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1)
    NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
before [(StateChanged SimpleTx
opening, Text
"Open"), (StateChanged SimpleTx
rollback, Text
"Open"), (StateChanged SimpleTx
tick, Text
"Open")]

-- Retain the node runtime and inspect its component states, comparing with
-- serialized recovery after each event. Checkpoints and new-Head
-- resets belong to the separate node-boundary tests above.
checkSteps :: NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> Expectation
checkSteps :: NodeState SimpleTx -> [(StateChanged SimpleTx, Text)] -> IO ()
checkSteps NodeState SimpleTx
initial = IO (Runtime SimpleTx) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Runtime SimpleTx) -> IO ())
-> ([(StateChanged SimpleTx, Text)] -> IO (Runtime SimpleTx))
-> [(StateChanged SimpleTx, Text)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Runtime SimpleTx
 -> (StateChanged SimpleTx, Text) -> IO (Runtime SimpleTx))
-> Runtime SimpleTx
-> [(StateChanged SimpleTx, Text)]
-> IO (Runtime SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Runtime SimpleTx
-> (StateChanged SimpleTx, Text) -> IO (Runtime SimpleTx)
step (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start NodeState SimpleTx
initial)
 where
  step :: Runtime SimpleTx
-> (StateChanged SimpleTx, Text) -> IO (Runtime SimpleTx)
step Runtime SimpleTx
runtime (StateChanged SimpleTx
event, Text
expected) = do
    NodeState SimpleTx
restored <- (DecoderError -> IO (NodeState SimpleTx))
-> (NodeState SimpleTx -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (NodeState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (NodeState SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (NodeState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (NodeState SimpleTx)
 -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (NodeState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (NodeState SimpleTx))
-> ByteString -> Either DecoderError (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' (NodeState SimpleTx -> ByteString)
-> NodeState SimpleTx -> ByteString
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
runtime
    StateChanged SimpleTx
retained <- (DecoderError -> IO (StateChanged SimpleTx))
-> (StateChanged SimpleTx -> IO (StateChanged SimpleTx))
-> Either DecoderError (StateChanged SimpleTx)
-> IO (StateChanged SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (StateChanged SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (StateChanged SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (StateChanged SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) StateChanged SimpleTx -> IO (StateChanged SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (StateChanged SimpleTx)
 -> IO (StateChanged SimpleTx))
-> Either DecoderError (StateChanged SimpleTx)
-> IO (StateChanged SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (StateChanged SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (StateChanged SimpleTx))
-> ByteString -> Either DecoderError (StateChanged SimpleTx)
forall a b. (a -> b) -> a -> b
$ StateChanged SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' StateChanged SimpleTx
event
    let updated :: Runtime SimpleTx
updated = Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step Runtime SimpleTx
runtime StateChanged SimpleTx
event
        node :: NodeState SimpleTx
node = Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
updated
    NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
initial [NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
restored, StateChanged SimpleTx
retained] NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
node
    Runtime SimpleTx -> Maybe Text
forall tx. Runtime tx -> Maybe Text
machinePhase Runtime SimpleTx
updated Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
expected
    Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Machine.inspect Runtime SimpleTx
updated [(Text, Maybe Text)] -> [(Text, Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Machine.inspect (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start NodeState SimpleTx
node)
    Runtime SimpleTx -> IO (Runtime SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Runtime SimpleTx
updated

machinePhase :: Machine.Runtime tx -> Maybe Text
machinePhase :: forall tx. Runtime tx -> Maybe Text
machinePhase = Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Text) -> Maybe Text)
-> (Runtime tx -> Maybe (Maybe Text)) -> Runtime tx -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [(Text, Maybe Text)] -> Maybe (Maybe Text)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
List.lookup Text
"Head" ([(Text, Maybe Text)] -> Maybe (Maybe Text))
-> (Runtime tx -> [(Text, Maybe Text)])
-> Runtime tx
-> Maybe (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Runtime tx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Machine.inspect

restoredPhase :: NodeState SimpleTx -> Maybe Text
restoredPhase :: NodeState SimpleTx -> Maybe Text
restoredPhase = Runtime SimpleTx -> Maybe Text
forall tx. Runtime tx -> Maybe Text
machinePhase (Runtime SimpleTx -> Maybe Text)
-> (NodeState SimpleTx -> Runtime SimpleTx)
-> NodeState SimpleTx
-> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start

checkProgress :: NodeState SimpleTx -> UTxOType SimpleTx -> UTxOType SimpleTx -> FanoutMode SimpleTx -> Expectation
checkProgress :: NodeState SimpleTx
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> FanoutMode SimpleTx
-> IO ()
checkProgress NodeState SimpleTx
node UTxOType SimpleTx
distributed UTxOType SimpleTx
remaining FanoutMode SimpleTx
mode = case NodeState SimpleTx
node.headState of
  FanoutProgress PartialFanoutState SimpleTx
st -> (PartialFanoutState SimpleTx
st.distributedOutputs, PartialFanoutState SimpleTx
st.remainingOutputs, PartialFanoutState SimpleTx
st.mode) (Set SimpleTxOut, Set SimpleTxOut, FanoutMode SimpleTx)
-> (Set SimpleTxOut, Set SimpleTxOut, FanoutMode SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` (Set SimpleTxOut
UTxOType SimpleTx
distributed, Set SimpleTxOut
UTxOType SimpleTx
remaining, FanoutMode SimpleTx
mode)
  HeadState SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected partial fanout progress"

opened, closed, pending, partial, finalized :: NodeState SimpleTx
opened :: NodeState SimpleTx
opened = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply ([Party] -> NodeState SimpleTx
inOpenState [Party
alice]) (StateChanged SimpleTx -> NodeState SimpleTx)
-> StateChanged SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1
closed :: NodeState SimpleTx
closed = [Party] -> ConfirmedSnapshot SimpleTx -> NodeState SimpleTx
inClosedState' [Party
alice] (ConfirmedSnapshot SimpleTx -> NodeState SimpleTx)
-> ConfirmedSnapshot SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot (SnapshotNumber
-> SnapshotVersion
-> [SimpleTx]
-> UTxOType SimpleTx
-> Snapshot SimpleTx
forall tx.
IsTx tx =>
SnapshotNumber
-> SnapshotVersion -> [tx] -> UTxOType tx -> Snapshot tx
testSnapshot SnapshotNumber
1 SnapshotVersion
0 [] (UTxOType SimpleTx -> Snapshot SimpleTx)
-> UTxOType SimpleTx -> Snapshot SimpleTx
forall a b. (a -> b) -> a -> b
$ [SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3]) MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty
pending :: NodeState SimpleTx
pending = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
closed StateChanged SimpleTx
initiated
partial :: NodeState SimpleTx
partial = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
closed (StateChanged SimpleTx -> NodeState SimpleTx)
-> StateChanged SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ FanoutMode SimpleTx -> StateChanged SimpleTx
observed FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection
finalized :: NodeState SimpleTx
finalized = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
partial StateChanged SimpleTx
finished

nextHeadId :: HeadId
nextHeadId :: HeadId
nextHeadId = ByteString -> HeadId
UnsafeHeadId ByteString
"another-head"

nextOpenEvent :: StateChanged SimpleTx
nextOpenEvent :: StateChanged SimpleTx
nextOpenEvent = StateChanged SimpleTx
openEvent{headId = nextHeadId, headSeed = "new-seed"}

openEvent, initiated, selected, reverted, finished :: StateChanged SimpleTx
openEvent :: StateChanged SimpleTx
openEvent = case ([Party] -> NodeState SimpleTx
inOpenState [Party
alice]).headState of
  Open OpenState SimpleTx
st -> HeadParameters
-> ChainStateType SimpleTx
-> HeadId
-> HeadSeed
-> [Party]
-> StateChanged SimpleTx
forall tx.
HeadParameters
-> ChainStateType tx
-> HeadId
-> HeadSeed
-> [Party]
-> StateChanged tx
HeadOpened OpenState SimpleTx
st.parameters OpenState SimpleTx
st.chainState OpenState SimpleTx
st.headId OpenState SimpleTx
st.headSeed [Party
alice]
  HeadState SimpleTx
_ -> Text -> StateChanged SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"fixture does not open a head"
initiated :: StateChanged SimpleTx
initiated = HeadId -> UTxOType SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> UTxOType tx -> StateChanged tx
HeadFanoutInitiated HeadId
testHeadId (UTxOType SimpleTx -> StateChanged SimpleTx)
-> UTxOType SimpleTx -> StateChanged SimpleTx
forall a b. (a -> b) -> a -> b
$ [SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3]
selected :: StateChanged SimpleTx
selected = HeadId
-> UTxOType SimpleTx -> UTxOType SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> UTxOType tx -> UTxOType tx -> StateChanged tx
HeadPartialFanoutSelected HeadId
testHeadId ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3]) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1)
reverted :: StateChanged SimpleTx
reverted = HeadId -> StateChanged SimpleTx
forall tx. HeadId -> StateChanged tx
HeadFanoutReverted HeadId
testHeadId
finished :: StateChanged SimpleTx
finished = HeadId
-> UTxOType SimpleTx
-> ChainStateType SimpleTx
-> StateChanged SimpleTx
forall tx.
HeadId -> UTxOType tx -> ChainStateType tx -> StateChanged tx
HeadFannedOut HeadId
testHeadId ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3]) (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
4)

observed :: FanoutMode SimpleTx -> StateChanged SimpleTx
observed :: FanoutMode SimpleTx -> StateChanged SimpleTx
observed = HeadId
-> UTxOType SimpleTx
-> UTxOType SimpleTx
-> ChainStateType SimpleTx
-> FanoutMode SimpleTx
-> StateChanged SimpleTx
forall tx.
HeadId
-> UTxOType tx
-> UTxOType tx
-> ChainStateType tx
-> FanoutMode tx
-> StateChanged tx
HeadPartialFannedOut HeadId
testHeadId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) ([SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
2, SimpleId
3]) (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2)

-- A fanout command can arrive before the deadline, while a candidate still
-- needs the signatures in a later contest to become confirmed.
closedWithCandidate :: Bool -> NodeState SimpleTx
closedWithCandidate :: Bool -> NodeState SimpleTx
closedWithCandidate Bool
ready = case NodeState SimpleTx
closed.headState of
  Closed ClosedState SimpleTx
st -> NodeState SimpleTx
closed{headState = Closed st{contestationDeadline = addUTCTime 600 initialChainTime, readyToFanoutSent = ready, unconfirmedSnapshot = Just candidate}}
  HeadState SimpleTx
_ -> Text -> NodeState SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"fixture does not close a head"

candidate :: Snapshot SimpleTx
candidate :: Snapshot SimpleTx
candidate = SnapshotNumber
-> SnapshotVersion
-> [SimpleTx]
-> UTxOType SimpleTx
-> Snapshot SimpleTx
forall tx.
IsTx tx =>
SnapshotNumber
-> SnapshotVersion -> [tx] -> UTxOType tx -> Snapshot tx
testSnapshot SnapshotNumber
2 SnapshotVersion
0 [] (UTxOType SimpleTx -> Snapshot SimpleTx)
-> UTxOType SimpleTx -> Snapshot SimpleTx
forall a b. (a -> b) -> a -> b
$ [SimpleId] -> UTxOType SimpleTx
utxoRefs [SimpleId
1, SimpleId
2, SimpleId
3]

applyInput :: NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput :: NodeState SimpleTx -> Input SimpleTx -> NodeState SimpleTx
applyInput NodeState SimpleTx
node Input SimpleTx
input = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
node (Outcome SimpleTx -> NodeState SimpleTx)
-> Outcome SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
testEnvironment Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node Input SimpleTx
input