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

module Hydra.HeadLogic.MachineSpec (spec) where

import Hydra.Prelude
import Test.Hydra.Prelude (
  Arbitrary (arbitrary),
  Spec,
  describe,
  genericArbitrary,
  it,
  prop,
  shouldBe,
 )

import Cardano.Binary (decodeFull', serialize')
import Hydra.Agda.OffChainReference qualified as Agda
import Hydra.Chain.ChainState (ChainSlot)
import Hydra.HeadLogic (
  CoordinatedHeadState (
    CoordinatedHeadState,
    confirmedSnapshot,
    currentDepositTxId,
    decommitTx,
    version
  ),
  FinalizedState (headId),
  HeadState (Finalized, Open),
  OpenState (OpenState, coordinatedHeadState),
  Outcome (Continue, Error, Wait, stateChanges),
  StateChanged (
    Checkpoint,
    CommitApproved,
    CommitFinalized,
    DecommitApproved,
    DecommitFinalized,
    DecommitInvalid,
    DecommitRecorded,
    DepositRecorded,
    HeadClosed,
    HeadContested,
    HeadFannedOut,
    HeadFanoutInitiated,
    HeadFanoutReverted,
    HeadIsReadyToFanout,
    HeadOpened,
    HeadPartialFannedOut,
    HeadPartialFanoutSelected,
    LocalStateCleared,
    SnapshotConfirmed,
    SnapshotRequestDecided,
    SnapshotRequested,
    TransactionAppliedToLocalUTxO,
    TxInvalid,
    newVersion,
    snapshot
  ),
  aggregateState,
  eventHeadId,
  headIdOf,
 )
import Hydra.HeadLogic.Deposit qualified as Deposit
import Hydra.HeadLogic.Outcome qualified as Outcome
import Hydra.HeadLogic.State.Node qualified as Machine
import Hydra.Ledger.Simple (SimpleChainState (..), SimpleTx)
import Hydra.Node.HeadLogic (aggregateNodeState)
import Hydra.Node.State (Deposit (..), DepositStatus (..), NodeState (..), TrackedDeposit (..), initialChainTime)
import Hydra.Tx (HeadId)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..))
import Test.Hydra.HeadLogic.Outcome ()
import Test.Hydra.HeadLogic.Projection.Incremental qualified as Incremental
import Test.Hydra.HeadLogic.Projection.Lifecycle qualified as Lifecycle
import Test.Hydra.HeadLogic.Projection.OffChain qualified as OffChain
import Test.Hydra.HeadLogic.Scenario (initialNode)
import Test.Hydra.HeadLogic.Settlement (SettlementStep (..), settlementSequence)
import Test.Hydra.Ledger.Simple ()
import Test.Hydra.Node.State ()
import Test.Hydra.Tx.Fixture (testHeadId)
import Test.QuickCheck (Property, conjoin, counterexample, forAll, property, resize, vectorOf, (===))

spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"crem event application" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"keeps owner descriptions across execution and state reconstruction" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let event :: StateChanged SimpleTx
event = HeadId
-> SnapshotNumber
-> ChainStateType SimpleTx
-> UTCTime
-> StateChanged SimpleTx
forall tx.
HeadId
-> SnapshotNumber
-> ChainStateType tx
-> UTCTime
-> StateChanged tx
HeadClosed HeadId
testHeadId SnapshotNumber
0 (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
1) UTCTime
initialChainTime
        next :: Runtime SimpleTx
next = Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start NodeState SimpleTx
initialNode) StateChanged SimpleTx
event
        node :: NodeState SimpleTx
node = Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
next
        descriptions :: [(Text, Maybe Text)]
descriptions =
          [ (Text
"Deposits", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"TrackedDeposits")
          , (Text
"Head", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"Closed")
          ]
    NodeState SimpleTx
node 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
initialNode StateChanged SimpleTx
event
    Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Machine.inspect Runtime SimpleTx
next [(Text, Maybe Text)] -> [(Text, Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [(Text, Maybe Text)]
descriptions
    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) [(Text, Maybe Text)] -> [(Text, Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [(Text, Maybe Text)]
descriptions

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retains eligibility through consumption and rollback" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let deposit :: Deposit SimpleTx
        deposit :: Deposit SimpleTx
deposit = HeadId
-> UTxOType SimpleTx
-> UTCTime
-> UTCTime
-> DepositStatus
-> Deposit SimpleTx
forall tx.
HeadId
-> UTxOType tx -> UTCTime -> UTCTime -> DepositStatus -> Deposit tx
Deposit HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty UTCTime
initialChainTime UTCTime
initialChainTime DepositStatus
Active
        active :: Maybe (TrackedDeposit SimpleTx)
active = ChainSlot
-> Deposit SimpleTx
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
ChainSlot
-> Deposit tx
-> Maybe (TrackedDeposit tx)
-> Maybe (TrackedDeposit tx)
Deposit.record ChainSlot
1 Deposit SimpleTx
deposit Maybe (TrackedDeposit SimpleTx)
forall a. Maybe a
Nothing
        consumed :: Maybe (TrackedDeposit SimpleTx)
consumed = ChainSlot
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
ChainSlot -> Maybe (TrackedDeposit tx) -> Maybe (TrackedDeposit tx)
Deposit.consume ChainSlot
2 Maybe (TrackedDeposit SimpleTx)
active
        restored :: Maybe (TrackedDeposit SimpleTx)
restored = ChainSlot
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
ChainSlot -> Maybe (TrackedDeposit tx) -> Maybe (TrackedDeposit tx)
Deposit.rollback ChainSlot
1 Maybe (TrackedDeposit SimpleTx)
consumed
    (Maybe (TrackedDeposit SimpleTx) -> HsTrackedPhase)
-> [Maybe (TrackedDeposit SimpleTx)] -> [HsTrackedPhase]
forall a b. (a -> b) -> [a] -> [b]
map Maybe (TrackedDeposit SimpleTx) -> HsTrackedPhase
projectTracked [Maybe (TrackedDeposit SimpleTx)
forall a. Maybe a
Nothing, Maybe (TrackedDeposit SimpleTx)
active, Maybe (TrackedDeposit SimpleTx)
consumed, Maybe (TrackedDeposit SimpleTx)
restored]
      [HsTrackedPhase] -> [HsTrackedPhase] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [HsTrackedPhase
Agda.MissingDeposit, HsTrackedPhase
Agda.ActiveDeposit, HsTrackedPhase
Agda.ConsumedDeposit, HsTrackedPhase
Agda.ActiveDeposit]
    Maybe (TrackedDeposit SimpleTx)
restored Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe (TrackedDeposit SimpleTx)
active

  String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"agrees between continuous execution, reconstructed execution and CBOR recovery" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
    Gen (NodeState SimpleTx)
-> (NodeState SimpleTx -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx)
forall a. HasCallStack => Int -> Gen a -> Gen a
resize Int
8 (Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx))
-> Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ forall a. Arbitrary a => Gen a
arbitrary @(NodeState SimpleTx)) ((NodeState SimpleTx -> Property) -> Property)
-> (NodeState SimpleTx -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \NodeState SimpleTx
initial ->
      Gen [StateChanged SimpleTx]
-> ([StateChanged SimpleTx] -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx]
forall a. Int -> Gen a -> Gen [a]
vectorOf Int
40 (Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx])
-> Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx]
forall a b. (a -> b) -> a -> b
$ Int -> Gen (StateChanged SimpleTx) -> Gen (StateChanged SimpleTx)
forall a. HasCallStack => Int -> Gen a -> Gen a
resize Int
8 Gen (StateChanged SimpleTx)
forall a.
(Generic a, GA UnsizedOpts (Rep a),
 UniformWeight (Weights_ (Rep a))) =>
Gen a
genericArbitrary) (([StateChanged SimpleTx] -> Property) -> Property)
-> ([StateChanged SimpleTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[StateChanged SimpleTx]
events ->
        NodeState SimpleTx -> [StateChanged SimpleTx] -> Property
comparePrefixes NodeState SimpleTx
initial [StateChanged SimpleTx]
events

  String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"shares a retained batch across checkpoints and preserves wait prefixes" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
    Gen (NodeState SimpleTx)
-> (NodeState SimpleTx -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx)
forall a. HasCallStack => Int -> Gen a -> Gen a
resize Int
8 (Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx))
-> Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ forall a. Arbitrary a => Gen a
arbitrary @(NodeState SimpleTx)) ((NodeState SimpleTx -> Property) -> Property)
-> (NodeState SimpleTx -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \NodeState SimpleTx
initial ->
      Gen (NodeState SimpleTx)
-> (NodeState SimpleTx -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen (NodeState SimpleTx) -> Gen (NodeState SimpleTx)
forall a. HasCallStack => Int -> Gen a -> Gen a
resize Int
8 Gen (NodeState SimpleTx)
forall a. Arbitrary a => Gen a
arbitrary) ((NodeState SimpleTx -> Property) -> Property)
-> (NodeState SimpleTx -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \NodeState SimpleTx
checkpoint ->
        Gen [StateChanged SimpleTx]
-> ([StateChanged SimpleTx] -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int -> Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx]
forall a. Int -> Gen a -> Gen [a]
vectorOf Int
40 (Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx])
-> Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx]
forall a b. (a -> b) -> a -> b
$ Int -> Gen (StateChanged SimpleTx) -> Gen (StateChanged SimpleTx)
forall a. HasCallStack => Int -> Gen a -> Gen a
resize Int
8 Gen (StateChanged SimpleTx)
forall a.
(Generic a, GA UnsizedOpts (Rep a),
 UniformWeight (Weights_ (Rep a))) =>
Gen a
genericArbitrary) (([StateChanged SimpleTx] -> Property) -> Property)
-> ([StateChanged SimpleTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[StateChanged SimpleTx]
changes ->
          let events :: [StateChanged SimpleTx]
events = Int -> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. Int -> [a] -> [a]
take Int
20 [StateChanged SimpleTx]
changes [StateChanged SimpleTx]
-> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. Semigroup a => a -> a -> a
<> [NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
checkpoint] [StateChanged SimpleTx]
-> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. Semigroup a => a -> a -> a
<> Int -> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. Int -> [a] -> [a]
drop Int
20 [StateChanged SimpleTx]
changes
              expected :: NodeState SimpleTx
expected = (NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx)
-> NodeState SimpleTx
-> [StateChanged SimpleTx]
-> NodeState SimpleTx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
aggregateNodeState NodeState SimpleTx
initial [StateChanged SimpleTx]
events
           in [Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
                [ NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents NodeState SimpleTx
initial [StateChanged SimpleTx]
events NodeState SimpleTx -> NodeState SimpleTx -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NodeState SimpleTx
expected
                , NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
initial ([StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [StateChanged SimpleTx]
events []) NodeState SimpleTx -> NodeState SimpleTx -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NodeState SimpleTx
expected
                , NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
initial (WaitReason SimpleTx -> [StateChanged SimpleTx] -> Outcome SimpleTx
forall tx. WaitReason tx -> [StateChanged tx] -> Outcome tx
Wait WaitReason SimpleTx
forall tx. WaitReason tx
Outcome.WaitOnSeenSnapshot [StateChanged SimpleTx]
events) NodeState SimpleTx -> NodeState SimpleTx -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NodeState SimpleTx
expected
                ]

  String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"matches Agda for tracked-deposit updates, consumption and rollback" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
    Gen (Maybe (TrackedDeposit SimpleTx))
-> (Maybe (TrackedDeposit SimpleTx) -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll (Int
-> Gen (Maybe (TrackedDeposit SimpleTx))
-> Gen (Maybe (TrackedDeposit SimpleTx))
forall a. HasCallStack => Int -> Gen a -> Gen a
resize Int
8 Gen (Maybe (TrackedDeposit SimpleTx))
forall a. Arbitrary a => Gen a
arbitrary) ((Maybe (TrackedDeposit SimpleTx) -> Property) -> Property)
-> (Maybe (TrackedDeposit SimpleTx) -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Maybe (TrackedDeposit SimpleTx)
tracked ->
      Gen (Deposit SimpleTx)
-> (Deposit SimpleTx -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen (Deposit SimpleTx)
forall a. Arbitrary a => Gen a
arbitrary ((Deposit SimpleTx -> Property) -> Property)
-> (Deposit SimpleTx -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Deposit SimpleTx
payload ->
        Gen ChainSlot -> (ChainSlot -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen ChainSlot
forall a. Arbitrary a => Gen a
arbitrary ((ChainSlot -> Property) -> Property)
-> (ChainSlot -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \ChainSlot
slot ->
          [Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin [Maybe (TrackedDeposit SimpleTx)
-> Deposit SimpleTx -> ChainSlot -> Integer -> Property
depositReference Maybe (TrackedDeposit SimpleTx)
tracked Deposit SimpleTx
payload ChainSlot
slot Integer
action | Integer
action <- [Integer
0 .. Integer
3]]

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves all 24 settlement and rollback schedules" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [(Bool, Int, [Int])] -> ((Bool, Int, [Int]) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
confirmed, Int
slot, [Int]
recipients) | Bool
confirmed <- [Bool
False, Bool
True], Int
slot <- [Int
2, Int
3], [Int]
recipients <- [Int] -> [[Int]]
forall a. [a] -> [[a]]
permutations [Int
0, Int
1, Int
2]] (((Bool, Int, [Int]) -> IO ()) -> IO ())
-> ((Bool, Int, [Int]) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
confirmed, Int
slot, [Int]
recipients) -> do
      [SettlementStep]
steps <- (Text -> IO [SettlementStep])
-> ([SettlementStep] -> IO [SettlementStep])
-> Either Text [SettlementStep]
-> IO [SettlementStep]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO [SettlementStep]
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO [SettlementStep])
-> (Text -> String) -> Text -> IO [SettlementStep]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString) [SettlementStep] -> IO [SettlementStep]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Text [SettlementStep] -> IO [SettlementStep])
-> Either Text [SettlementStep] -> IO [SettlementStep]
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> [Int] -> Either Text [SettlementStep]
settlementSequence Bool
confirmed Int
slot [Int]
recipients
      [SettlementStep] -> (SettlementStep -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SettlementStep]
steps ((SettlementStep -> IO ()) -> IO ())
-> (SettlementStep -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \SettlementStep
entry ->
        (NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx)
-> NodeState SimpleTx
-> [StateChanged SimpleTx]
-> NodeState SimpleTx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
aggregateNodeState SettlementStep
entry.before (Outcome SimpleTx -> [StateChanged SimpleTx]
forall tx. Outcome tx -> [StateChanged tx]
retained SettlementStep
entry.outcome) NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SettlementStep
entry.after

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"tracks optimistic fanout, reversion, contest and completion" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let events :: [StateChanged SimpleTx]
events =
          [ HeadId
-> SnapshotNumber
-> ChainStateType SimpleTx
-> UTCTime
-> StateChanged SimpleTx
forall tx.
HeadId
-> SnapshotNumber
-> ChainStateType tx
-> UTCTime
-> StateChanged tx
HeadClosed HeadId
testHeadId SnapshotNumber
0 (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
1) UTCTime
initialChainTime
          , HeadId -> StateChanged SimpleTx
forall tx. HeadId -> StateChanged tx
HeadIsReadyToFanout HeadId
testHeadId
          , HeadId -> UTxOType SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> UTxOType tx -> StateChanged tx
HeadFanoutInitiated HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty
          , HeadId -> StateChanged SimpleTx
forall tx. HeadId -> StateChanged tx
HeadFanoutReverted HeadId
testHeadId
          , HeadId
-> ChainStateType SimpleTx
-> UTCTime
-> SnapshotNumber
-> StateChanged SimpleTx
forall tx.
HeadId
-> ChainStateType tx
-> UTCTime
-> SnapshotNumber
-> StateChanged tx
HeadContested HeadId
testHeadId (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2) (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
100 UTCTime
initialChainTime) SnapshotNumber
0
          , HeadId
-> UTxOType SimpleTx
-> ChainStateType SimpleTx
-> StateChanged SimpleTx
forall tx.
HeadId -> UTxOType tx -> ChainStateType tx -> StateChanged tx
HeadFannedOut HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3)
          ]
        nodes :: [NodeState SimpleTx]
nodes = (Runtime SimpleTx -> NodeState SimpleTx)
-> [Runtime SimpleTx] -> [NodeState SimpleTx]
forall a b. (a -> b) -> [a] -> [b]
map Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState ([Runtime SimpleTx] -> [NodeState SimpleTx])
-> [Runtime SimpleTx] -> [NodeState SimpleTx]
forall a b. (a -> b) -> a -> b
$ (Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx)
-> Runtime SimpleTx
-> [StateChanged SimpleTx]
-> [Runtime SimpleTx]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start NodeState SimpleTx
initialNode) [StateChanged SimpleTx]
events
    (NodeState SimpleTx -> Phase) -> [NodeState SimpleTx] -> [Phase]
forall a b. (a -> b) -> [a] -> [b]
map (HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Lifecycle.phase (HeadState SimpleTx -> Phase)
-> (NodeState SimpleTx -> HeadState SimpleTx)
-> NodeState SimpleTx
-> Phase
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NodeState SimpleTx -> HeadState SimpleTx
forall tx. NodeState tx -> HeadState tx
headState) [NodeState SimpleTx]
nodes
      [Phase] -> [Phase] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [Phase
Lifecycle.Open, Phase
Lifecycle.Closed, Phase
Lifecycle.Closed, Phase
Lifecycle.Closed, Phase
Lifecycle.Closed, Phase
Lifecycle.Closed, Phase
Lifecycle.Finalized]
    [NodeState SimpleTx]
nodes [NodeState SimpleTx] -> [NodeState SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` (NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx)
-> NodeState SimpleTx
-> [StateChanged SimpleTx]
-> [NodeState SimpleTx]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
aggregateNodeState NodeState SimpleTx
initialNode [StateChanged SimpleTx]
events

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reconstructs every component after checkpoint restoration" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let waiting :: Runtime SimpleTx
waiting = Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start NodeState SimpleTx
initialNode) (SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1)
        restored :: Runtime SimpleTx
restored = Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step Runtime SimpleTx
waiting (NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
initialNode)
        node :: NodeState SimpleTx
node = Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
restored
    NodeState SimpleTx
node NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
initialNode
    HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Lifecycle.phase NodeState SimpleTx
node.headState Phase -> Phase -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Phase
Lifecycle.Open
    HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Incremental.phase NodeState SimpleTx
node.headState Phase -> Phase -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Phase
Incremental.NoIncrement
    HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
OffChain.phase NodeState SimpleTx
node.headState Phase -> Phase -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Phase
OffChain.NoRequestSeen
    Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState (Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step Runtime SimpleTx
restored (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
aggregateNodeState NodeState SimpleTx
initialNode (SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1)

-- Rebase head-scoped events onto the current Head for three out of four steps;
-- leave the rest foreign to exercise the filter. Checkpoints remain arbitrary.
-- Generic generation includes every StateChanged constructor, unlike the
-- environment-constrained generator used by node hydration tests.
comparePrefixes :: NodeState SimpleTx -> [StateChanged SimpleTx] -> Property
comparePrefixes :: NodeState SimpleTx -> [StateChanged SimpleTx] -> Property
comparePrefixes NodeState SimpleTx
initial = Int
-> NodeState SimpleTx
-> Runtime SimpleTx
-> [StateChanged SimpleTx]
-> Property
go Int
0 NodeState SimpleTx
initial (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start NodeState SimpleTx
initial)
 where
  go :: Int -> NodeState SimpleTx -> Machine.Runtime SimpleTx -> [StateChanged SimpleTx] -> Property
  go :: Int
-> NodeState SimpleTx
-> Runtime SimpleTx
-> [StateChanged SimpleTx]
-> Property
go Int
_ NodeState SimpleTx
_ Runtime SimpleTx
_ [] = Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
  go Int
n NodeState SimpleTx
old Runtime SimpleTx
runtime (StateChanged SimpleTx
raw : [StateChanged SimpleTx]
rest) =
    let event :: StateChanged SimpleTx
event = case (HeadState SimpleTx -> Maybe HeadId
forall tx. HeadState tx -> Maybe HeadId
headIdOf NodeState SimpleTx
old.headState, StateChanged SimpleTx -> Maybe HeadId
forall tx. StateChanged tx -> Maybe HeadId
eventHeadId StateChanged SimpleTx
raw) of
          (Just HeadId
headId, Just HeadId
_) | Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
4 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 -> HeadId -> StateChanged SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> StateChanged tx -> StateChanged tx
scope HeadId
headId StateChanged SimpleTx
raw
          (Maybe HeadId, Maybe HeadId)
_ -> StateChanged SimpleTx
raw
        expected :: NodeState SimpleTx
expected = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
aggregateNodeState NodeState SimpleTx
old StateChanged SimpleTx
event
        next :: Runtime SimpleTx
next = Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step Runtime SimpleTx
runtime StateChanged SimpleTx
event
        actual :: NodeState SimpleTx
actual = Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
next
     in String -> Property -> Property
forall prop. Testable prop => String -> prop -> Property
counterexample (String
"prefix " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show Int
n String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> StateChanged SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show StateChanged SimpleTx
event String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\nbefore: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> NodeState SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show NodeState SimpleTx
old) (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
          [Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
            [ NodeState SimpleTx
actual NodeState SimpleTx -> NodeState SimpleTx -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NodeState SimpleTx
expected
            , NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Machine.apply NodeState SimpleTx
old StateChanged SimpleTx
event NodeState SimpleTx -> NodeState SimpleTx -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NodeState SimpleTx
expected
            , ByteString -> Either DecoderError (NodeState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' NodeState SimpleTx
actual) Either DecoderError (NodeState SimpleTx)
-> Either DecoderError (NodeState SimpleTx) -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== NodeState SimpleTx -> Either DecoderError (NodeState SimpleTx)
forall a b. b -> Either a b
Right NodeState SimpleTx
expected
            , NodeState SimpleTx -> Property
incrementalReference NodeState SimpleTx
actual
            , if NodeState SimpleTx -> StateChanged SimpleTx -> Bool
ignored NodeState SimpleTx
old StateChanged SimpleTx
event then Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True else NodeState SimpleTx
-> StateChanged SimpleTx -> NodeState SimpleTx -> Property
lifecycleReference NodeState SimpleTx
old StateChanged SimpleTx
event NodeState SimpleTx
actual
            , if NodeState SimpleTx -> StateChanged SimpleTx -> Bool
ignored NodeState SimpleTx
old StateChanged SimpleTx
event then Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True else NodeState SimpleTx
-> StateChanged SimpleTx -> NodeState SimpleTx -> Property
coordinationReference NodeState SimpleTx
old StateChanged SimpleTx
event NodeState SimpleTx
actual
            , Int
-> NodeState SimpleTx
-> Runtime SimpleTx
-> [StateChanged SimpleTx]
-> Property
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) NodeState SimpleTx
expected Runtime SimpleTx
next [StateChanged SimpleTx]
rest
            ]

retained :: Outcome tx -> [StateChanged tx]
retained :: forall tx. Outcome tx -> [StateChanged tx]
retained = \case
  Continue{[StateChanged tx]
$sel:stateChanges:Continue :: forall tx. Outcome tx -> [StateChanged tx]
stateChanges :: [StateChanged tx]
stateChanges} -> [StateChanged tx]
stateChanges
  Wait{[StateChanged tx]
$sel:stateChanges:Continue :: forall tx. Outcome tx -> [StateChanged tx]
stateChanges :: [StateChanged tx]
stateChanges} -> [StateChanged tx]
stateChanges
  Error{} -> []

-- The Agda pending-action reference projects stored fields; it does not model
-- settlement payloads or admission guards. Compare it at checkpoint prefixes too.
incrementalReference :: NodeState SimpleTx -> Property
incrementalReference :: NodeState SimpleTx -> Property
incrementalReference NodeState SimpleTx
node =
  Phase -> Integer
project (HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Incremental.phase NodeState SimpleTx
node.headState) Integer -> Integer -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== case NodeState SimpleTx
node.headState of
    Open OpenState{$sel:coordinatedHeadState:OpenState :: forall tx. OpenState tx -> CoordinatedHeadState tx
coordinatedHeadState = CoordinatedHeadState SimpleTx
chs} ->
      Bool -> Bool -> Bool -> Integer
Agda.pendingIncrementPhaseRef Bool
True (Maybe (TxIdType SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust CoordinatedHeadState SimpleTx
chs.currentDepositTxId) (Maybe SimpleTx -> Bool
forall a. Maybe a -> Bool
isJust CoordinatedHeadState SimpleTx
chs.decommitTx)
    HeadState SimpleTx
_ -> Bool -> Bool -> Bool -> Integer
Agda.pendingIncrementPhaseRef Bool
False Bool
False Bool
False
 where
  project :: Phase -> Integer
project = \case
    Phase
Incremental.OutsideHead -> Integer
0
    Phase
Incremental.NoIncrement -> Integer
1
    Phase
Incremental.DepositSelected -> Integer
2
    Phase
Incremental.DecommitPending -> Integer
3
    Phase
Incremental.ConflictingSelection -> Integer
4

lifecycleReference :: NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx -> Property
lifecycleReference :: NodeState SimpleTx
-> StateChanged SimpleTx -> NodeState SimpleTx -> Property
lifecycleReference NodeState SimpleTx
before StateChanged SimpleTx
event NodeState SimpleTx
after =
  Phase -> HsLifecyclePhase
project (HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Lifecycle.phase NodeState SimpleTx
after.headState) HsLifecyclePhase -> HsLifecyclePhase -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== HsLifecyclePhase -> HsLifecycleEvent -> HsLifecyclePhase
Agda.lifecyclePhaseRef HsLifecyclePhase
initialPhase HsLifecycleEvent
tag
 where
  -- A different identity starts a fresh lifecycle at the node boundary. It
  -- does not add an outgoing edge to the completed Head's Agda projection.
  initialPhase :: HsLifecyclePhase
initialPhase = if NodeState SimpleTx -> StateChanged SimpleTx -> Bool
forall tx. NodeState tx -> StateChanged tx -> Bool
startsNewHead NodeState SimpleTx
before StateChanged SimpleTx
event then HsLifecyclePhase
Agda.IdleHead else Phase -> HsLifecyclePhase
project (Phase -> HsLifecyclePhase) -> Phase -> HsLifecyclePhase
forall a b. (a -> b) -> a -> b
$ HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Lifecycle.phase NodeState SimpleTx
before.headState
  project :: Phase -> HsLifecyclePhase
project = \case
    Phase
Lifecycle.Idle -> HsLifecyclePhase
Agda.IdleHead
    Phase
Lifecycle.Open -> HsLifecyclePhase
Agda.OpenHead
    Phase
Lifecycle.Closed -> HsLifecyclePhase
Agda.ClosedHead
    Phase
Lifecycle.FanoutProgress -> HsLifecyclePhase
Agda.FanoutProgress
    Phase
Lifecycle.Finalized -> HsLifecyclePhase
Agda.FinalizedHead
  tag :: HsLifecycleEvent
tag = case StateChanged SimpleTx
event of
    HeadOpened{} -> HsLifecycleEvent
Agda.Opened
    HeadClosed{} -> HsLifecycleEvent
Agda.Closed
    HeadContested{} -> HsLifecycleEvent
Agda.Contested
    HeadIsReadyToFanout{} -> HsLifecycleEvent
Agda.Ready
    HeadFanoutInitiated{} -> HsLifecycleEvent
Agda.FanoutStarted
    HeadPartialFanoutSelected{} -> HsLifecycleEvent
Agda.FanoutStarted
    HeadPartialFannedOut{} -> HsLifecycleEvent
Agda.PartialObserved
    HeadFanoutReverted{} -> HsLifecycleEvent
Agda.FanoutReverted
    HeadFannedOut{} -> HsLifecycleEvent
Agda.FannedOut
    StateChanged SimpleTx
_ -> HsLifecycleEvent
Agda.Unchanged

coordinationReference :: NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx -> Property
coordinationReference :: NodeState SimpleTx
-> StateChanged SimpleTx -> NodeState SimpleTx -> Property
coordinationReference NodeState SimpleTx
before StateChanged SimpleTx
event NodeState SimpleTx
after =
  Phase -> HsCoordinationPhase
project (HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
OffChain.phase NodeState SimpleTx
after.headState) HsCoordinationPhase -> HsCoordinationPhase -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== HsCoordinationPhase
-> HsCoordinationEvent
-> Bool
-> Bool
-> Bool
-> HsCoordinationPhase
Agda.coordinationPhaseRef (Phase -> HsCoordinationPhase
project (Phase -> HsCoordinationPhase) -> Phase -> HsCoordinationPhase
forall a b. (a -> b) -> a -> b
$ HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
OffChain.phase NodeState SimpleTx
before.headState) HsCoordinationEvent
tag Bool
initial Bool
explicit Bool
fresh
 where
  project :: Phase -> HsCoordinationPhase
project = \case
    Phase
OffChain.OutsideHead -> HsCoordinationPhase
Agda.OutsideHead
    Phase
OffChain.NoRequestSeen -> HsCoordinationPhase
Agda.NoRequestSeen
    Phase
OffChain.SnapshotIdle -> HsCoordinationPhase
Agda.SnapshotIdle
    Phase
OffChain.RequestSent -> HsCoordinationPhase
Agda.RequestSent
    Phase
OffChain.CollectingSignatures -> HsCoordinationPhase
Agda.Collecting
  tag :: HsCoordinationEvent
tag = case StateChanged SimpleTx
event of
    -- Coordination starts only after the lifecycle accepts an opening.
    HeadOpened{} | HeadState SimpleTx -> Phase
forall tx. HeadState tx -> Phase
Lifecycle.phase NodeState SimpleTx
before.headState Phase -> Phase -> Bool
forall a. Eq a => a -> a -> Bool
== Phase
Lifecycle.Idle Bool -> Bool -> Bool
|| NodeState SimpleTx -> StateChanged SimpleTx -> Bool
forall tx. NodeState tx -> StateChanged tx -> Bool
startsNewHead NodeState SimpleTx
before StateChanged SimpleTx
event -> HsCoordinationEvent
Agda.HeadOpened
    HeadClosed{} -> HsCoordinationEvent
Agda.HeadClosed
    SnapshotRequestDecided{} -> HsCoordinationEvent
Agda.RequestDecided
    SnapshotRequested{} -> HsCoordinationEvent
Agda.SnapshotRequested
    SnapshotConfirmed{} -> HsCoordinationEvent
Agda.SnapshotConfirmed
    LocalStateCleared{} -> HsCoordinationEvent
Agda.LocalCleared
    CommitFinalized{} -> HsCoordinationEvent
Agda.SettlementObserved
    DecommitFinalized{} -> HsCoordinationEvent
Agda.SettlementObserved
    StateChanged SimpleTx
_ -> HsCoordinationEvent
Agda.OtherEvent
  initial :: Bool
initial = case NodeState SimpleTx
before.headState of
    Open OpenState{$sel:coordinatedHeadState:OpenState :: forall tx. OpenState tx -> CoordinatedHeadState tx
coordinatedHeadState = CoordinatedHeadState{$sel:confirmedSnapshot:CoordinatedHeadState :: forall tx. CoordinatedHeadState tx -> ConfirmedSnapshot tx
confirmedSnapshot = InitialSnapshot{}}} -> Bool
True
    HeadState SimpleTx
_ -> Bool
False
  explicit :: Bool
explicit = case StateChanged SimpleTx
event of
    SnapshotConfirmed{Maybe (Snapshot SimpleTx)
$sel:snapshot:NetworkConnected :: forall tx. StateChanged tx -> Maybe (Snapshot tx)
snapshot :: Maybe (Snapshot SimpleTx)
snapshot} -> Maybe (Snapshot SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Snapshot SimpleTx)
snapshot
    StateChanged SimpleTx
_ -> Bool
False
  fresh :: Bool
fresh = case NodeState SimpleTx
before.headState of
    Open OpenState{$sel:coordinatedHeadState:OpenState :: forall tx. OpenState tx -> CoordinatedHeadState tx
coordinatedHeadState = CoordinatedHeadState SimpleTx
chs} -> case StateChanged SimpleTx
event of
      CommitFinalized{SnapshotVersion
$sel:newVersion:NetworkConnected :: forall tx. StateChanged tx -> SnapshotVersion
newVersion :: SnapshotVersion
newVersion} -> SnapshotVersion
newVersion SnapshotVersion -> SnapshotVersion -> Bool
forall a. Ord a => a -> a -> Bool
> CoordinatedHeadState SimpleTx
chs.version
      DecommitFinalized{SnapshotVersion
$sel:newVersion:NetworkConnected :: forall tx. StateChanged tx -> SnapshotVersion
newVersion :: SnapshotVersion
newVersion} -> SnapshotVersion
newVersion SnapshotVersion -> SnapshotVersion -> Bool
forall a. Ord a => a -> a -> Bool
> CoordinatedHeadState SimpleTx
chs.version
      StateChanged SimpleTx
_ -> Bool
False
    HeadState SimpleTx
_ -> Bool
False

depositReference :: Maybe (TrackedDeposit SimpleTx) -> Deposit SimpleTx -> ChainSlot -> Integer -> Property
depositReference :: Maybe (TrackedDeposit SimpleTx)
-> Deposit SimpleTx -> ChainSlot -> Integer -> Property
depositReference Maybe (TrackedDeposit SimpleTx)
tracked Deposit SimpleTx
payload ChainSlot
slot Integer
action =
  Maybe (TrackedDeposit SimpleTx) -> HsTrackedPhase
projectTracked (Maybe (TrackedDeposit SimpleTx) -> Maybe (TrackedDeposit SimpleTx)
change Maybe (TrackedDeposit SimpleTx)
tracked) HsTrackedPhase -> HsTrackedPhase -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== HsTrackedPhase
-> Integer -> HsDepositStatus -> Bool -> Bool -> HsTrackedPhase
Agda.trackedDepositPhaseRef (Maybe (TrackedDeposit SimpleTx) -> HsTrackedPhase
projectTracked Maybe (TrackedDeposit SimpleTx)
tracked) Integer
action HsDepositStatus
status Bool
recordedLater Bool
consumedLater
 where
  change :: Maybe (TrackedDeposit SimpleTx) -> Maybe (TrackedDeposit SimpleTx)
change = case Integer
action of
    Integer
0 -> ChainSlot
-> Deposit SimpleTx
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
ChainSlot
-> Deposit tx
-> Maybe (TrackedDeposit tx)
-> Maybe (TrackedDeposit tx)
Deposit.record ChainSlot
slot Deposit SimpleTx
payload
    Integer
1 -> Deposit SimpleTx
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
Deposit tx
-> Maybe (TrackedDeposit tx) -> Maybe (TrackedDeposit tx)
Deposit.update Deposit SimpleTx
payload
    Integer
2 -> ChainSlot
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
ChainSlot -> Maybe (TrackedDeposit tx) -> Maybe (TrackedDeposit tx)
Deposit.consume ChainSlot
slot
    Integer
_ -> ChainSlot
-> Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall tx.
ChainSlot -> Maybe (TrackedDeposit tx) -> Maybe (TrackedDeposit tx)
Deposit.rollback ChainSlot
slot
  retainedStatus :: DepositStatus
retainedStatus = DepositStatus
-> (TrackedDeposit SimpleTx -> DepositStatus)
-> Maybe (TrackedDeposit SimpleTx)
-> DepositStatus
forall b a. b -> (a -> b) -> Maybe a -> b
maybe DepositStatus
Inactive (\TrackedDeposit SimpleTx
t -> TrackedDeposit SimpleTx
t.deposit.status) Maybe (TrackedDeposit SimpleTx)
tracked
  status :: HsDepositStatus
status = case if Integer
action Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
2 then Deposit SimpleTx
payload.status else DepositStatus
retainedStatus of
    DepositStatus
Inactive -> HsDepositStatus
Agda.InactiveS
    DepositStatus
Active -> HsDepositStatus
Agda.ActiveS
    DepositStatus
Expired -> HsDepositStatus
Agda.ExpiredS
  recordedLater :: Bool
recordedLater = Bool
-> (TrackedDeposit SimpleTx -> Bool)
-> Maybe (TrackedDeposit SimpleTx)
-> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (\TrackedDeposit SimpleTx
t -> TrackedDeposit SimpleTx
t.recordedAt ChainSlot -> ChainSlot -> Bool
forall a. Ord a => a -> a -> Bool
> ChainSlot
slot) Maybe (TrackedDeposit SimpleTx)
tracked
  consumedLater :: Bool
consumedLater = Bool
-> (TrackedDeposit SimpleTx -> Bool)
-> Maybe (TrackedDeposit SimpleTx)
-> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (\TrackedDeposit SimpleTx
t -> Bool -> (ChainSlot -> Bool) -> Maybe ChainSlot -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (ChainSlot -> ChainSlot -> Bool
forall a. Ord a => a -> a -> Bool
> ChainSlot
slot) TrackedDeposit SimpleTx
t.consumedAt) Maybe (TrackedDeposit SimpleTx)
tracked

projectTracked :: Maybe (TrackedDeposit SimpleTx) -> Agda.HsTrackedPhase
projectTracked :: Maybe (TrackedDeposit SimpleTx) -> HsTrackedPhase
projectTracked = \case
  Maybe (TrackedDeposit SimpleTx)
Nothing -> HsTrackedPhase
Agda.MissingDeposit
  Just TrackedDeposit SimpleTx
tracked
    | Maybe ChainSlot -> Bool
forall a. Maybe a -> Bool
isJust TrackedDeposit SimpleTx
tracked.consumedAt -> HsTrackedPhase
Agda.ConsumedDeposit
    | Bool
otherwise -> case TrackedDeposit SimpleTx
tracked.deposit.status of
        DepositStatus
Inactive -> HsTrackedPhase
Agda.InactiveDeposit
        DepositStatus
Active -> HsTrackedPhase
Agda.ActiveDeposit
        DepositStatus
Expired -> HsTrackedPhase
Agda.ExpiredDeposit

ignored :: NodeState SimpleTx -> StateChanged SimpleTx -> Bool
ignored :: NodeState SimpleTx -> StateChanged SimpleTx -> Bool
ignored NodeState SimpleTx
node StateChanged SimpleTx
event = case StateChanged SimpleTx
event of
  Checkpoint{} -> Bool
True
  HeadOpened{} | NodeState SimpleTx -> StateChanged SimpleTx -> Bool
forall tx. NodeState tx -> StateChanged tx -> Bool
startsNewHead NodeState SimpleTx
node StateChanged SimpleTx
event -> Bool
False
  StateChanged SimpleTx
_ -> case (HeadState SimpleTx -> Maybe HeadId
forall tx. HeadState tx -> Maybe HeadId
headIdOf NodeState SimpleTx
node.headState, StateChanged SimpleTx -> Maybe HeadId
forall tx. StateChanged tx -> Maybe HeadId
eventHeadId StateChanged SimpleTx
event) of
    (Just HeadId
ours, Just HeadId
theirs) -> HeadId
ours HeadId -> HeadId -> Bool
forall a. Eq a => a -> a -> Bool
/= HeadId
theirs
    (Maybe HeadId, Maybe HeadId)
_ -> Bool
False

startsNewHead :: NodeState tx -> StateChanged tx -> Bool
startsNewHead :: forall tx. NodeState tx -> StateChanged tx -> Bool
startsNewHead NodeState tx
node StateChanged tx
event = case (NodeState tx
node.headState, StateChanged tx
event) of
  (Finalized FinalizedState tx
previous, HeadOpened{$sel:headId:NetworkConnected :: forall tx. StateChanged tx -> HeadId
Outcome.headId = HeadId
nextHeadId}) -> FinalizedState tx
previous.headId HeadId -> HeadId -> Bool
forall a. Eq a => a -> a -> Bool
/= HeadId
nextHeadId
  (HeadState tx, StateChanged tx)
_ -> Bool
False

-- Only event fields used by the production filter are rebased.
scope :: HeadId -> StateChanged tx -> StateChanged tx
scope :: forall tx. HeadId -> StateChanged tx -> StateChanged tx
scope HeadId
headId = \case
  event :: StateChanged tx
event@HeadOpened{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@TransactionAppliedToLocalUTxO{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@SnapshotConfirmed{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@LocalStateCleared{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@DepositRecorded{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@CommitApproved{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@CommitFinalized{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@DecommitRecorded{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@DecommitApproved{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@DecommitInvalid{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@DecommitFinalized{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadIsReadyToFanout{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadClosed{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadContested{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadFannedOut{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@TxInvalid{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadPartialFannedOut{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadFanoutInitiated{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadPartialFanoutSelected{} -> StateChanged tx
event{Outcome.headId = headId}
  event :: StateChanged tx
event@HeadFanoutReverted{} -> StateChanged tx
event{Outcome.headId = headId}
  StateChanged tx
event -> StateChanged tx
event