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

module Hydra.HeadLogic.CoordinatedSpec (spec) where

import Hydra.HeadLogic.Cardano (cardanoSnapshotLimits)
import Hydra.HeadLogic.Program (reject, suspend)
import Hydra.HeadLogic.Rules.Coordinated qualified as Staging
import Hydra.Prelude
import Test.Hydra.HeadLogic.Interpreter (testInterpreterConfig)
import Test.Hydra.HeadLogic.Validated (checkedDecommit, checkedSnapshot, checkedTransactionWith)
import Test.Hydra.Prelude (Spec, describe, it, shouldBe, shouldSatisfy)

import Cardano.Binary (decodeFull', serialize')
import Data.Map.Strict qualified as Map
import Data.Sequence qualified as Seq
import Hydra.API.ClientInput qualified as Client
import Hydra.Agda.OffChainReference qualified as Agda
import Hydra.HeadLogic.Error (LogicError (AssertionFailed, RequireFailed), RequirementFailure (InvalidMultisignature))
import Hydra.HeadLogic.Interpreter qualified as Interpreter
import Hydra.HeadLogic.Outcome (Outcome (Continue, Error, Wait), StateChanged (..), WaitReason (WaitOnSeenSnapshot))
import Hydra.HeadLogic.Program qualified as Program
import Hydra.HeadLogic.Snapshot.Request (DecommitStatus (..), chainedSnapshotRequest, decommitStatus, nextSnapshotRequest)
import Hydra.HeadLogic.Snapshot.Verification (candidateEvidence, verifyCandidate)
import Hydra.HeadLogic.State (CoordinatedHeadState (..), OpenState (..), mkSeenSnapshot)
import Hydra.HeadLogic.State.Coordinated qualified as Coordinated
import Hydra.Ledger (Ledger (..))
import Hydra.Ledger.Simple (SimpleChainState (..), SimpleTx (..), simpleLedger)
import Hydra.Network.Message (Message (ReqDec), NetworkEvent (ReceivedMessage))
import Hydra.Node.HeadLogic (aggregateNodeState)
import Hydra.Node.HeadLogic qualified as Head
import Hydra.Node.State (Deposit (..), DepositStatus (Active), NodeState (..), initialChainTime, trackedFromPending)
import Hydra.Tx.Accumulator qualified as Accumulator
import Hydra.Tx.Crypto (aggregateInOrder, sign)
import Hydra.Tx.HeadId (HeadId (UnsafeHeadId))
import Hydra.Tx.HeadParameters (HeadParameters (parties))
import Hydra.Tx.Party (Party)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (ConfirmedSnapshot), Snapshot (..), getSnapshot)
import Test.Hydra.HeadLogic.ConflictingDecommit qualified as Conflict
import Test.Hydra.HeadLogic.CoordinatedAudit qualified as Audit
import Test.Hydra.HeadLogic.Scenario qualified as Scenario
import Test.Hydra.Ledger.Simple (utxoRef)
import Test.Hydra.Tx.Fixture (alice, aliceSk, bob, bobSk, carol, carolSk, testHeadId)

spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"coordinated state owners" (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
"inspects retained owner phases without rebuilding the local UTxO" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let initial :: Runtime SimpleTx
initial = OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open
        ([StateChanged SimpleTx]
_, Runtime SimpleTx
applied) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step Runtime SimpleTx
initial (Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx))
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ ValidatedTransaction SimpleTx -> Command SimpleTx
forall tx. ValidatedTransaction tx -> Command tx
Coordinated.applyTransaction (Ledger SimpleTx
-> UTxOType SimpleTx -> SimpleTx -> ValidatedTransaction SimpleTx
checkedTransactionWith ((ChainSlot
 -> UTxOType SimpleTx
 -> [SimpleTx]
 -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx
forall tx.
(ChainSlot
 -> UTxOType tx
 -> [tx]
 -> Either (tx, ValidationError) (UTxOType tx))
-> Ledger tx
Ledger ((ChainSlot
  -> UTxOType SimpleTx
  -> [SimpleTx]
  -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
 -> Ledger SimpleTx)
-> (ChainSlot
    -> UTxOType SimpleTx
    -> [SimpleTx]
    -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx
forall a b. (a -> b) -> a -> b
$ \ChainSlot
_ UTxOType SimpleTx
_ [SimpleTx]
_ -> UTxOType SimpleTx
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx)
forall a b. b -> Either a b
Right (UTxOType SimpleTx
 -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> UTxOType SimpleTx
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx)
forall a b. (a -> b) -> a -> b
$ Text -> Set SimpleTxOut
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"inspection demands after UTxO") (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) SimpleTx
Scenario.transactionOne)
        ([StateChanged SimpleTx]
_, Runtime SimpleTx
collecting) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step Runtime SimpleTx
applied (Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx))
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ ValidatedSnapshot SimpleTx -> Seq SimpleTx -> Command SimpleTx
forall tx. ValidatedSnapshot tx -> Seq tx -> Command tx
Coordinated.acceptSnapshot (Snapshot SimpleTx -> ValidatedSnapshot SimpleTx
checkedSnapshot Snapshot SimpleTx
candidate) (SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton (SimpleTx -> Seq SimpleTx) -> SimpleTx -> Seq SimpleTx
forall a b. (a -> b) -> a -> b
$ Text -> SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"inspection rebuilds transactions")
    Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect Runtime SimpleTx
applied [(Text, Maybe Text)] -> [(Text, Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [(Text
"Snapshots", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"AwaitingRequest"), (Text
"Transactions", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"NoDecommit"), (Text
"Commits", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"NoCommit")]
    Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect Runtime SimpleTx
collecting [(Text, Maybe Text)] -> [(Text, Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [(Text
"Snapshots", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"CollectingSignatures"), (Text
"Transactions", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"NoDecommit"), (Text
"Commits", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"NoCommit")]

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"inspects reservation and certificate phases after reconstruction" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let pending :: OpenState SimpleTx
pending = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{decommitTx = Just Scenario.transactionOne}}
        certified :: OpenState SimpleTx
certified = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{confirmedSnapshot = ConfirmedSnapshot candidate{utxoToDecommit = Just $ utxoRef 2} signatures}}
        descriptions :: [[(Text, Maybe Text)]]
descriptions = (OpenState SimpleTx -> [(Text, Maybe Text)])
-> [OpenState SimpleTx] -> [[(Text, Maybe Text)]]
forall a b. (a -> b) -> [a] -> [b]
map (Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect (Runtime SimpleTx -> [(Text, Maybe Text)])
-> (OpenState SimpleTx -> Runtime SimpleTx)
-> OpenState SimpleTx
-> [(Text, Maybe Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start) [OpenState SimpleTx
open, OpenState SimpleTx
pending, OpenState SimpleTx
certified]
    [[(Text, Maybe Text)]]
descriptions
      [[(Text, Maybe Text)]] -> [[(Text, Maybe Text)]] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [ [(Text
"Snapshots", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"AwaitingRequest"), (Text
"Transactions", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
phaseName), (Text
"Commits", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"NoCommit")]
                 | Text
phaseName <- [Text
"NoDecommit", Text
"DecommitPending", Text
"AwaitingDecommitSettlement"]
                 ]

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"compares the independent ownership checks with Agda over bounded UTxOs" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [([SimpleId], [SimpleId], [SimpleId], Bool)]
-> (([SimpleId], [SimpleId], [SimpleId], Bool) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [([SimpleId]
reserved, [SimpleId]
available, [SimpleId]
confirmed, Bool
carried) | [SimpleId]
reserved <- [SimpleId] -> [[SimpleId]]
forall a. [a] -> [[a]]
subsequences [SimpleId
1, SimpleId
2, SimpleId
3], [SimpleId]
available <- [SimpleId] -> [[SimpleId]]
forall a. [a] -> [[a]]
subsequences [SimpleId
1, SimpleId
2, SimpleId
3], [SimpleId]
confirmed <- [SimpleId] -> [[SimpleId]]
forall a. [a] -> [[a]]
subsequences [SimpleId
1, SimpleId
2, SimpleId
3], Bool
carried <- [Bool
False, Bool
True]] ((([SimpleId], [SimpleId], [SimpleId], Bool) -> IO ()) -> IO ())
-> (([SimpleId], [SimpleId], [SimpleId], Bool) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \([SimpleId]
reserved, [SimpleId]
available, [SimpleId]
confirmed, Bool
carried) -> do
      let tx :: SimpleTx
tx = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
3 ([Item (UTxOType SimpleTx)] -> UTxOType SimpleTx
forall l. IsList l => [Item l] -> l
fromList ([Item (UTxOType SimpleTx)] -> UTxOType SimpleTx)
-> [Item (UTxOType SimpleTx)] -> UTxOType SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> SimpleTxOut
forall a. Num a => SimpleId -> a
fromInteger (SimpleId -> SimpleTxOut) -> [SimpleId] -> [SimpleTxOut]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SimpleId]
reserved) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4)
          base :: Snapshot SimpleTx
          base :: Snapshot SimpleTx
base = Snapshot SimpleTx
Scenario.initialSnapshot{utxo = fromList $ fromInteger <$> confirmed, utxoToDecommit = if carried then Just tx.txOutputs else Nothing}
          chs :: CoordinatedHeadState SimpleTx
chs = OpenState SimpleTx
open.coordinatedHeadState{decommitTx = Just tx, localUTxO = fromList $ fromInteger <$> available, confirmedSnapshot = ConfirmedSnapshot base mempty}
      CoordinatedHeadState SimpleTx -> Bool
Audit.reservationHeld CoordinatedHeadState SimpleTx
chs Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [SimpleId] -> [SimpleId] -> Bool
Agda.reservationHeldRef [SimpleId]
reserved [SimpleId]
available
      CoordinatedHeadState SimpleTx -> SimpleTx -> Bool
Audit.decommitReady CoordinatedHeadState SimpleTx
chs SimpleTx
tx Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> [SimpleId] -> [SimpleId] -> Bool
Agda.decommitReadyRef Bool
carried [SimpleId]
reserved [SimpleId]
confirmed
      let eligible :: Bool
eligible = case SnapshotVersion
-> Snapshot SimpleTx -> Maybe SimpleTx -> DecommitStatus SimpleTx
forall tx.
IsTx tx =>
SnapshotVersion -> Snapshot tx -> Maybe tx -> DecommitStatus tx
decommitStatus SnapshotVersion
0 Snapshot SimpleTx
base (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
tx) of
            ReadyToDecommit{} -> Bool
True
            CarryDecommit{} -> Bool
True
            DecommitStatus SimpleTx
_ -> Bool
False
      Bool
eligible Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> [SimpleId] -> [SimpleId] -> Bool
Agda.decommitReadyRef Bool
carried [SimpleId]
reserved [SimpleId]
confirmed
      Maybe (SnapshotRequest SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust (SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
chainedSnapshotRequest SnapshotLimits
cardanoSnapshotLimits SnapshotVersion
0 Snapshot SimpleTx
base Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing (SnapshotVersion
-> Snapshot SimpleTx -> Maybe SimpleTx -> DecommitStatus SimpleTx
forall tx.
IsTx tx =>
SnapshotVersion -> Snapshot tx -> Maybe tx -> DecommitStatus tx
decommitStatus SnapshotVersion
0 Snapshot SimpleTx
base (Maybe SimpleTx -> DecommitStatus SimpleTx)
-> Maybe SimpleTx -> DecommitStatus SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
tx) Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing)
        Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> [SimpleId] -> [SimpleId] -> Bool
Agda.newDecommitReadyRef Bool
carried [SimpleId]
reserved [SimpleId]
confirmed
      let rebased :: Snapshot SimpleTx
          rebased :: Snapshot SimpleTx
rebased = Snapshot SimpleTx
base{utxo = fromList $ fromInteger <$> available}
          next :: Runtime SimpleTx
next = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty (Snapshot SimpleTx
-> Seq SimpleTx
-> Maybe (TxIdType SimpleTx)
-> StateChanged SimpleTx
forall tx.
Snapshot tx -> Seq tx -> Maybe (TxIdType tx) -> StateChanged tx
SnapshotRequested Snapshot SimpleTx
rebased Seq SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing) (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{coordinatedHeadState = chs})
      (Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
next).localUTxO UTxOType SimpleTx -> UTxOType SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [Item (UTxOType SimpleTx)] -> UTxOType SimpleTx
forall l. IsList l => [Item l] -> l
fromList (SimpleId -> Item (UTxOType SimpleTx)
forall a. Num a => SimpleId -> a
fromInteger (SimpleId -> Item (UTxOType SimpleTx))
-> [SimpleId] -> [Item (UTxOType SimpleTx)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SimpleId] -> [SimpleId] -> [SimpleId]
Agda.reserveRebaseRef [SimpleId]
reserved [SimpleId]
available)

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"agrees between continuous replay and checkpoint recovery, including old duplicate signatures" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let events :: [StateChanged SimpleTx]
events =
          [ SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
Scenario.transactionOne
          , HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
TransactionAppliedToLocalUTxO HeadId
testHeadId SimpleTx
Scenario.transactionOne
          , SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
Scenario.transactionTwo
          , HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
TransactionAppliedToLocalUTxO HeadId
testHeadId SimpleTx
Scenario.transactionTwo
          , HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
DecommitRecorded HeadId
testHeadId (SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
3 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4))
          , SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1
          , Snapshot SimpleTx
-> Seq SimpleTx
-> Maybe (TxIdType SimpleTx)
-> StateChanged SimpleTx
forall tx.
Snapshot tx -> Seq tx -> Maybe (TxIdType tx) -> StateChanged tx
SnapshotRequested Snapshot SimpleTx
candidate (SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton SimpleTx
Scenario.transactionTwo) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
          , SnapshotNumber
-> Party -> Signature (Snapshot SimpleTx) -> StateChanged SimpleTx
forall tx.
SnapshotNumber
-> Party -> Signature (Snapshot tx) -> StateChanged tx
PartySignedSnapshot SnapshotNumber
1 Party
alice (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)
          , SnapshotNumber
-> Party -> Signature (Snapshot SimpleTx) -> StateChanged SimpleTx
forall tx.
SnapshotNumber
-> Party -> Signature (Snapshot tx) -> StateChanged tx
PartySignedSnapshot SnapshotNumber
1 Party
alice (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)
          , SnapshotNumber
-> Party -> Signature (Snapshot SimpleTx) -> StateChanged SimpleTx
forall tx.
SnapshotNumber
-> Party -> Signature (Snapshot tx) -> StateChanged tx
PartySignedSnapshot SnapshotNumber
1 Party
bob (Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
bobSk Snapshot SimpleTx
candidate)
          , SnapshotNumber
-> Party -> Signature (Snapshot SimpleTx) -> StateChanged SimpleTx
forall tx.
SnapshotNumber
-> Party -> Signature (Snapshot tx) -> StateChanged tx
PartySignedSnapshot SnapshotNumber
1 Party
carol (Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
carolSk Snapshot SimpleTx
candidate)
          , HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing MultiSignature (Snapshot SimpleTx)
signatures
          ]
    IO (NodeState SimpleTx, Runtime SimpleTx) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (NodeState SimpleTx, Runtime SimpleTx) -> IO ())
-> IO (NodeState SimpleTx, Runtime SimpleTx) -> IO ()
forall a b. (a -> b) -> a -> b
$ ((NodeState SimpleTx, Runtime SimpleTx)
 -> StateChanged SimpleTx
 -> IO (NodeState SimpleTx, Runtime SimpleTx))
-> (NodeState SimpleTx, Runtime SimpleTx)
-> [StateChanged SimpleTx]
-> IO (NodeState SimpleTx, Runtime SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM (NodeState SimpleTx, Runtime SimpleTx)
-> StateChanged SimpleTx
-> IO (NodeState SimpleTx, Runtime SimpleTx)
check (NodeState SimpleTx
Scenario.initialNode, OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open) [StateChanged SimpleTx]
events

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"does not reconstruct the local UTxO merely to stage a snapshot event" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let command :: Command SimpleTx
command = ValidatedSnapshot SimpleTx -> Seq SimpleTx -> Command SimpleTx
forall tx. ValidatedSnapshot tx -> Seq tx -> Command tx
Coordinated.acceptSnapshot (Snapshot SimpleTx -> ValidatedSnapshot SimpleTx
checkedSnapshot Snapshot SimpleTx
candidate) (SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton (SimpleTx -> Seq SimpleTx) -> SimpleTx -> Seq SimpleTx
forall a b. (a -> b) -> a -> b
$ Text -> SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"staging reconstructs the local UTxO")
    case InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open) Command SimpleTx
command) of
      Continue [StateChanged SimpleTx]
events [] -> [StateChanged SimpleTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [StateChanged SimpleTx]
events Int -> Int -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Int
1
      Outcome SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"snapshot staging does not complete"

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"carries a live continuation without forcing the validated UTxO projection" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    SnapshotRequest SimpleTx
request <- IO (SnapshotRequest SimpleTx)
-> (SnapshotRequest SimpleTx -> IO (SnapshotRequest SimpleTx))
-> Maybe (SnapshotRequest SimpleTx)
-> IO (SnapshotRequest SimpleTx)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> IO (SnapshotRequest SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no request") SnapshotRequest SimpleTx -> IO (SnapshotRequest SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (SnapshotRequest SimpleTx) -> IO (SnapshotRequest SimpleTx))
-> Maybe (SnapshotRequest SimpleTx)
-> IO (SnapshotRequest SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
nextSnapshotRequest SnapshotLimits
cardanoSnapshotLimits SnapshotVersion
0 Snapshot SimpleTx
Scenario.initialSnapshot Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing
    let program :: Program.Program SimpleTx ()
        program :: Program SimpleTx ()
program = do
          Runtime SimpleTx
next <- Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open) (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ ValidatedTransaction SimpleTx -> Command SimpleTx
forall tx. ValidatedTransaction tx -> Command tx
Coordinated.applyTransaction (Ledger SimpleTx
-> UTxOType SimpleTx -> SimpleTx -> ValidatedTransaction SimpleTx
checkedTransactionWith ((ChainSlot
 -> UTxOType SimpleTx
 -> [SimpleTx]
 -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx
forall tx.
(ChainSlot
 -> UTxOType tx
 -> [tx]
 -> Either (tx, ValidationError) (UTxOType tx))
-> Ledger tx
Ledger ((ChainSlot
  -> UTxOType SimpleTx
  -> [SimpleTx]
  -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
 -> Ledger SimpleTx)
-> (ChainSlot
    -> UTxOType SimpleTx
    -> [SimpleTx]
    -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx
forall a b. (a -> b) -> a -> b
$ \ChainSlot
_ UTxOType SimpleTx
_ [SimpleTx]
_ -> UTxOType SimpleTx
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx)
forall a b. b -> Either a b
Right (UTxOType SimpleTx
 -> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> UTxOType SimpleTx
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx)
forall a b. (a -> b) -> a -> b
$ Text -> Set SimpleTxOut
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"after demanded") (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) SimpleTx
Scenario.transactionOne)
          Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
next (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotRequest SimpleTx -> Command SimpleTx
forall tx. SnapshotRequest tx -> Command tx
Coordinated.requestSnapshot SnapshotRequest SimpleTx
request
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) Program SimpleTx ()
program
      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 [HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
TransactionAppliedToLocalUTxO HeadId
testHeadId SimpleTx
Scenario.transactionOne, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1] []

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"owns settlement, reobservation, rollback, adoption and clearing across recovery" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let depositId :: SimpleId
depositId = SimpleId
7
        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 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
8) UTCTime
initialChainTime (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
100 UTCTime
initialChainTime) DepositStatus
Active
        initial :: NodeState SimpleTx
initial = NodeState SimpleTx
Scenario.initialNode{deposits = trackedFromPending $ Map.singleton depositId deposit}
        adopted :: Snapshot SimpleTx
        adopted :: Snapshot SimpleTx
adopted = Snapshot SimpleTx
candidate{utxoToCommit = Just (utxoRef 8), depositTxId = Just depositId, appliedAccumulator = Accumulator.buildFromUTxO @SimpleTx (utxoRef 2 <> utxoRef 8)}
        events :: [StateChanged SimpleTx]
events =
          [ TxIdType SimpleTx
-> UTCTime -> Deposit SimpleTx -> StateChanged SimpleTx
forall tx. TxIdType tx -> UTCTime -> Deposit tx -> StateChanged tx
DepositActivated SimpleId
TxIdType SimpleTx
depositId UTCTime
initialChainTime Deposit SimpleTx
deposit
          , ChainStateType SimpleTx
-> HeadId
-> SnapshotVersion
-> TxIdType SimpleTx
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> SnapshotVersion -> TxIdType tx -> StateChanged tx
CommitFinalized (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) HeadId
testHeadId SnapshotVersion
1 SimpleId
TxIdType SimpleTx
depositId
          , HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId (Snapshot SimpleTx -> Maybe (Snapshot SimpleTx)
forall a. a -> Maybe a
Just Snapshot SimpleTx
adopted) MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty
          , ChainStateType SimpleTx
-> HeadId
-> SnapshotVersion
-> TxIdType SimpleTx
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> SnapshotVersion -> TxIdType tx -> StateChanged tx
CommitFinalized (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
4) HeadId
testHeadId SnapshotVersion
1 SimpleId
TxIdType SimpleTx
depositId
          , ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2)
          , TxIdType SimpleTx
-> UTCTime -> Deposit SimpleTx -> StateChanged SimpleTx
forall tx. TxIdType tx -> UTCTime -> Deposit tx -> StateChanged tx
DepositActivated SimpleId
TxIdType SimpleTx
depositId UTCTime
initialChainTime Deposit SimpleTx
deposit
          , HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
DecommitRecorded HeadId
testHeadId (SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
3 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4))
          , ChainStateType SimpleTx
-> HeadId
-> UTxOType SimpleTx
-> SnapshotVersion
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> UTxOType tx -> SnapshotVersion -> StateChanged tx
DecommitFinalized (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
5) HeadId
testHeadId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4) SnapshotVersion
2
          , HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
DecommitRecorded HeadId
testHeadId (SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
4 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
8) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
9))
          , ChainStateType SimpleTx
-> HeadId
-> UTxOType SimpleTx
-> SnapshotVersion
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> UTxOType tx -> SnapshotVersion -> StateChanged tx
DecommitFinalized (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
6) HeadId
testHeadId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4) SnapshotVersion
2
          , HeadId -> SnapshotNumber -> StateChanged SimpleTx
forall tx. HeadId -> SnapshotNumber -> StateChanged tx
LocalStateCleared HeadId
testHeadId SnapshotNumber
1
          , ChainStateType SimpleTx
-> HeadId
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> TxIdType tx -> UTxOType tx -> StateChanged tx
DepositRecovered (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
7) HeadId
testHeadId SimpleId
TxIdType SimpleTx
depositId (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
8)
          ]
    IO (NodeState SimpleTx, Runtime SimpleTx) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (NodeState SimpleTx, Runtime SimpleTx) -> IO ())
-> IO (NodeState SimpleTx, Runtime SimpleTx) -> IO ()
forall a b. (a -> b) -> a -> b
$ ((NodeState SimpleTx, Runtime SimpleTx)
 -> StateChanged SimpleTx
 -> IO (NodeState SimpleTx, Runtime SimpleTx))
-> (NodeState SimpleTx, Runtime SimpleTx)
-> [StateChanged SimpleTx]
-> IO (NodeState SimpleTx, Runtime SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM (NodeState SimpleTx, Runtime SimpleTx)
-> StateChanged SimpleTx
-> IO (NodeState SimpleTx, Runtime SimpleTx)
check (NodeState SimpleTx
initial, OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start (OpenState SimpleTx -> Runtime SimpleTx)
-> OpenState SimpleTx -> Runtime SimpleTx
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
initial) [StateChanged SimpleTx]
events

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"matches the Agda settlement projection, including repeated observations" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [(SimpleId, SimpleId, Bool)]
-> ((SimpleId, SimpleId, Bool) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(SimpleId
old, SimpleId
new, Bool
pending) | SimpleId
old <- [SimpleId
0 .. SimpleId
3], SimpleId
new <- [SimpleId
0 .. SimpleId
3], Bool
pending <- [Bool
False, Bool
True]] (((SimpleId, SimpleId, Bool) -> IO ()) -> IO ())
-> ((SimpleId, SimpleId, Bool) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(SimpleId
old, SimpleId
new, Bool
pending) -> do
      let tx :: SimpleTx
tx = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
3 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4)
          available :: [SimpleId]
available = [SimpleId
1 | Bool -> Bool
not Bool
pending]
          startingUTxO :: Set SimpleTxOut
startingUTxO = [Item (Set SimpleTxOut)] -> Set SimpleTxOut
forall l. IsList l => [Item l] -> l
fromList ([Item (Set SimpleTxOut)] -> Set SimpleTxOut)
-> [Item (Set SimpleTxOut)] -> Set SimpleTxOut
forall a b. (a -> b) -> a -> b
$ SimpleId -> Item (Set SimpleTxOut)
forall a. Num a => SimpleId -> a
fromInteger (SimpleId -> Item (Set SimpleTxOut))
-> [SimpleId] -> [Item (Set SimpleTxOut)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SimpleId]
available
          runtime :: Runtime SimpleTx
runtime = OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{version = fromInteger old, decommitTx = tx <$ guard pending, localUTxO = startingUTxO}}
          ([StateChanged SimpleTx]
_, Runtime SimpleTx
next) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step Runtime SimpleTx
runtime (Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx))
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ ChainStateType SimpleTx
-> SnapshotVersion -> UTxOType SimpleTx -> Command SimpleTx
forall tx.
ChainStateType tx -> SnapshotVersion -> UTxOType tx -> Command tx
Coordinated.recordDecommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) (SimpleId -> SnapshotVersion
forall a. Num a => SimpleId -> a
fromInteger SimpleId
new) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4)
          actual :: CoordinatedHeadState SimpleTx
actual = Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
next
      SnapshotVersion -> SimpleId
forall a. Integral a => a -> SimpleId
toInteger CoordinatedHeadState SimpleTx
actual.version SimpleId -> SimpleId -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> SimpleId -> SimpleId
Agda.settlementVersionRef SimpleId
old SimpleId
new
      Maybe SimpleTx -> Bool
forall a. Maybe a -> Bool
isJust CoordinatedHeadState SimpleTx
actual.decommitTx Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> SimpleId -> SimpleId -> Bool
Agda.settlementReservationRef Bool
pending SimpleId
old SimpleId
new
      CoordinatedHeadState SimpleTx
actual.localUTxO UTxOType SimpleTx -> UTxOType SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [Item (UTxOType SimpleTx)] -> UTxOType SimpleTx
forall l. IsList l => [Item l] -> l
fromList (SimpleId -> Item (UTxOType SimpleTx)
forall a. Num a => SimpleId -> a
fromInteger (SimpleId -> Item (UTxOType SimpleTx))
-> [SimpleId] -> [Item (UTxOType SimpleTx)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool
-> SimpleId -> SimpleId -> [SimpleId] -> [SimpleId] -> [SimpleId]
Agda.settlementUTxORef Bool
pending SimpleId
old SimpleId
new [SimpleId]
available [SimpleId
1])

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"does not demand signing bytes when replaying a retained confirmation" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let poisoned :: Snapshot SimpleTx
poisoned = Snapshot SimpleTx
candidate{accumulator = error "replay computes signing bytes", appliedAccumulator = error "replay computes signing bytes"}
        before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{seenSnapshot = mkSeenSnapshot poisoned mempty}}
        event :: StateChanged SimpleTx
event = HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing MultiSignature (Snapshot SimpleTx)
signatures
    let next :: Runtime SimpleTx
next = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty StateChanged SimpleTx
event (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before)
    case (Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
next).confirmedSnapshot of
      ConfirmedSnapshot Snapshot SimpleTx
retained MultiSignature (Snapshot SimpleTx)
proof -> do
        Snapshot SimpleTx
retained.number SnapshotNumber -> SnapshotNumber -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Snapshot SimpleTx
candidate.number
        Snapshot SimpleTx
retained.utxo UTxOType SimpleTx -> UTxOType SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Snapshot SimpleTx
candidate.utxo
        MultiSignature (Snapshot SimpleTx)
proof MultiSignature (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` MultiSignature (Snapshot SimpleTx)
signatures
      ConfirmedSnapshot SimpleTx
_ -> String -> IO ()
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"replay did not retain the confirmed snapshot"

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retains a staged signature on wait and discards it on failed verification" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let runtime :: Runtime SimpleTx
runtime = OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{seenSnapshot = seen}}
        signed :: Command SimpleTx
signed = Candidate SimpleTx
-> Party -> Signature (Snapshot SimpleTx) -> Command SimpleTx
forall tx.
Candidate tx -> Party -> Signature (Snapshot tx) -> Command tx
Coordinated.recordSignature (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate Runtime SimpleTx
runtime) Party
alice (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)
        stage :: Program.Program SimpleTx (Coordinated.Runtime SimpleTx)
        stage :: Program SimpleTx (Runtime SimpleTx)
stage = Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
runtime Command SimpleTx
signed
        event :: StateChanged SimpleTx
event = SnapshotNumber
-> Party -> Signature (Snapshot SimpleTx) -> StateChanged SimpleTx
forall tx.
SnapshotNumber
-> Party -> Signature (Snapshot tx) -> StateChanged tx
PartySignedSnapshot SnapshotNumber
1 Party
alice (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)
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx (Runtime SimpleTx)
stage Program SimpleTx (Runtime SimpleTx)
-> Program SimpleTx () -> Program SimpleTx ()
forall a b.
Program SimpleTx a -> Program SimpleTx b -> Program SimpleTx b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> WaitReason SimpleTx -> Program SimpleTx ()
forall tx a. WaitReason tx -> Program tx a
suspend WaitReason SimpleTx
forall tx. WaitReason tx
WaitOnSeenSnapshot)
      Outcome SimpleTx -> Outcome SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` WaitReason SimpleTx -> [StateChanged SimpleTx] -> Outcome SimpleTx
forall tx. WaitReason tx -> [StateChanged tx] -> Outcome tx
Wait WaitReason SimpleTx
forall tx. WaitReason tx
WaitOnSeenSnapshot [StateChanged SimpleTx
event]
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram
      (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger)
      ( do
          Runtime SimpleTx
next <- Program SimpleTx (Runtime SimpleTx)
stage
          Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
result <- Candidate SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
     SimpleTx
     (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall tx.
Candidate tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either (LogicError tx) (VerifiedCandidate tx))
verifyCandidate (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate Runtime SimpleTx
next) (Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder (Party
-> Signature (Snapshot SimpleTx)
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. k -> a -> Map k a
Map.singleton Party
alice (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)) [Party]
Scenario.scenarioParties)
          (LogicError SimpleTx -> Program SimpleTx ())
-> (VerifiedCandidate SimpleTx -> Program SimpleTx ())
-> Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> Program SimpleTx ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> (VerifiedCandidate SimpleTx
    -> Program SimpleTx (Runtime SimpleTx))
-> VerifiedCandidate SimpleTx
-> Program SimpleTx ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
next (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> (VerifiedCandidate SimpleTx -> Command SimpleTx)
-> VerifiedCandidate SimpleTx
-> Program SimpleTx (Runtime SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerifiedCandidate SimpleTx -> Command SimpleTx
forall tx. VerifiedCandidate tx -> Command tx
Coordinated.confirmSnapshot) Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
result
      )
      Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Error (RequireFailed InvalidMultisignature{}) -> Bool
True; Outcome SimpleTx
_ -> Bool
False

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"binds live confirmation evidence to the candidate's signed representation" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let runtime :: Snapshot SimpleTx -> Runtime SimpleTx
runtime Snapshot SimpleTx
snapshot = OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{seenSnapshot = mkSeenSnapshot snapshot mempty}}
        confirm :: Coordinated.Runtime SimpleTx -> Program.Program SimpleTx ()
        confirm :: Runtime SimpleTx -> Program SimpleTx ()
confirm Runtime SimpleTx
target = do
          VerifiedCandidate SimpleTx
verified <- Candidate SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
     SimpleTx
     (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall tx.
Candidate tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either (LogicError tx) (VerifiedCandidate tx))
verifyCandidate (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate (Runtime SimpleTx -> Candidate SimpleTx)
-> Runtime SimpleTx -> Candidate SimpleTx
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx -> Runtime SimpleTx
runtime Snapshot SimpleTx
candidate) MultiSignature (Snapshot SimpleTx)
signatures Program
  SimpleTx
  (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
-> (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a b.
Program SimpleTx a
-> (a -> Program SimpleTx b) -> Program SimpleTx b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (LogicError SimpleTx
 -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> (VerifiedCandidate SimpleTx
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LogicError SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall tx a. LogicError tx -> Program tx a
reject VerifiedCandidate SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a. a -> Program SimpleTx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
target (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ VerifiedCandidate SimpleTx -> Command SimpleTx
forall tx. VerifiedCandidate tx -> Command tx
Coordinated.confirmSnapshot VerifiedCandidate SimpleTx
verified
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Runtime SimpleTx -> Program SimpleTx ()
confirm (Runtime SimpleTx -> Program SimpleTx ())
-> Runtime SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx -> Runtime SimpleTx
runtime Snapshot SimpleTx
candidate)
      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 [HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing MultiSignature (Snapshot SimpleTx)
signatures] []
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Runtime SimpleTx -> Program SimpleTx ()
confirm (Runtime SimpleTx -> Program SimpleTx ())
-> Runtime SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx -> Runtime SimpleTx
runtime Snapshot SimpleTx
candidate{number = 2})
      Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Error AssertionFailed{} -> Bool
True; Outcome SimpleTx
_ -> Bool
False
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Runtime SimpleTx -> Program SimpleTx ()
confirm (Runtime SimpleTx -> Program SimpleTx ())
-> Runtime SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open)
      Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Error AssertionFailed{} -> Bool
True; Outcome SimpleTx
_ -> Bool
False
  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"binds confirmation and adoption to the Head and ordered parties" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [[Party]] -> ([Party] -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[], [Party
alice], [Party
bob, Party
alice, Party
carol]] (([Party] -> IO ()) -> IO ()) -> ([Party] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[Party]
parties ->
      [VerifiedCandidate SimpleTx -> Command SimpleTx]
-> ((VerifiedCandidate SimpleTx -> Command SimpleTx) -> IO ())
-> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [VerifiedCandidate SimpleTx -> Command SimpleTx
forall tx. VerifiedCandidate tx -> Command tx
Coordinated.confirmSnapshot, VerifiedSnapshot SimpleTx -> Command SimpleTx
forall tx. VerifiedSnapshot tx -> Command tx
Coordinated.adoptSnapshot (VerifiedSnapshot SimpleTx -> Command SimpleTx)
-> (VerifiedCandidate SimpleTx -> VerifiedSnapshot SimpleTx)
-> VerifiedCandidate SimpleTx
-> Command SimpleTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerifiedCandidate SimpleTx -> VerifiedSnapshot SimpleTx
forall tx. VerifiedCandidate tx -> VerifiedSnapshot tx
candidateEvidence] (((VerifiedCandidate SimpleTx -> Command SimpleTx) -> IO ())
 -> IO ())
-> ((VerifiedCandidate SimpleTx -> Command SimpleTx) -> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \VerifiedCandidate SimpleTx -> Command SimpleTx
command -> do
        let signed :: MultiSignature (Snapshot SimpleTx)
signed = Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder ([(Party, Signature (Snapshot SimpleTx))]
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Party
p, Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
candidate) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [(Party
alice, Secret (SigningKey HydraKey)
aliceSk), (Party
bob, Secret (SigningKey HydraKey)
bobSk), (Party
carol, Secret (SigningKey HydraKey)
carolSk)]]) [Party]
parties
            program :: Program.Program SimpleTx ()
            program :: Program SimpleTx ()
program = do
              let target :: Runtime SimpleTx
target = Snapshot SimpleTx -> [Party] -> Runtime SimpleTx
owner Snapshot SimpleTx
candidate [Party]
Scenario.scenarioParties
              Runtime SimpleTx
staged <- Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
target (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ Candidate SimpleTx
-> Party -> Signature (Snapshot SimpleTx) -> Command SimpleTx
forall tx.
Candidate tx -> Party -> Signature (Snapshot tx) -> Command tx
Coordinated.recordSignature (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate Runtime SimpleTx
target) Party
alice (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)
              VerifiedCandidate SimpleTx
evidence <- Candidate SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
     SimpleTx
     (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall tx.
Candidate tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either (LogicError tx) (VerifiedCandidate tx))
verifyCandidate (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate (Runtime SimpleTx -> Candidate SimpleTx)
-> Runtime SimpleTx -> Candidate SimpleTx
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx -> [Party] -> Runtime SimpleTx
owner Snapshot SimpleTx
candidate [Party]
parties) MultiSignature (Snapshot SimpleTx)
signed Program
  SimpleTx
  (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
-> (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a b.
Program SimpleTx a
-> (a -> Program SimpleTx b) -> Program SimpleTx b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (LogicError SimpleTx
 -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> (VerifiedCandidate SimpleTx
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LogicError SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall tx a. LogicError tx -> Program tx a
reject VerifiedCandidate SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a. a -> Program SimpleTx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
              Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
staged (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ VerifiedCandidate SimpleTx -> Command SimpleTx
command VerifiedCandidate SimpleTx
evidence
        InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) Program SimpleTx ()
program
          Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Error AssertionFailed{} -> Bool
True; Outcome SimpleTx
_ -> Bool
False

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"rejects correctly signed evidence from another Head, including adoption" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [VerifiedCandidate SimpleTx -> Command SimpleTx]
-> ((VerifiedCandidate SimpleTx -> Command SimpleTx) -> IO ())
-> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [VerifiedCandidate SimpleTx -> Command SimpleTx
forall tx. VerifiedCandidate tx -> Command tx
Coordinated.confirmSnapshot, VerifiedSnapshot SimpleTx -> Command SimpleTx
forall tx. VerifiedSnapshot tx -> Command tx
Coordinated.adoptSnapshot (VerifiedSnapshot SimpleTx -> Command SimpleTx)
-> (VerifiedCandidate SimpleTx -> VerifiedSnapshot SimpleTx)
-> VerifiedCandidate SimpleTx
-> Command SimpleTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerifiedCandidate SimpleTx -> VerifiedSnapshot SimpleTx
forall tx. VerifiedCandidate tx -> VerifiedSnapshot tx
candidateEvidence] (((VerifiedCandidate SimpleTx -> Command SimpleTx) -> IO ())
 -> IO ())
-> ((VerifiedCandidate SimpleTx -> Command SimpleTx) -> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \VerifiedCandidate SimpleTx -> Command SimpleTx
command -> do
      let foreignSnapshot :: Snapshot SimpleTx
          foreignSnapshot :: Snapshot SimpleTx
foreignSnapshot = Snapshot SimpleTx
candidate{headId = UnsafeHeadId "another head"}
          signed :: MultiSignature (Snapshot SimpleTx)
signed = Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder ([(Party, Signature (Snapshot SimpleTx))]
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Party
p, Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
foreignSnapshot) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [(Party
alice, Secret (SigningKey HydraKey)
aliceSk), (Party
bob, Secret (SigningKey HydraKey)
bobSk), (Party
carol, Secret (SigningKey HydraKey)
carolSk)]]) [Party]
Scenario.scenarioParties
          program :: Program.Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            VerifiedCandidate SimpleTx
evidence <- Candidate SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
     SimpleTx
     (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall tx.
Candidate tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either (LogicError tx) (VerifiedCandidate tx))
verifyCandidate (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate (Runtime SimpleTx -> Candidate SimpleTx)
-> Runtime SimpleTx -> Candidate SimpleTx
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx -> [Party] -> Runtime SimpleTx
owner Snapshot SimpleTx
foreignSnapshot [Party]
Scenario.scenarioParties) MultiSignature (Snapshot SimpleTx)
signed Program
  SimpleTx
  (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
-> (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a b.
Program SimpleTx a
-> (a -> Program SimpleTx b) -> Program SimpleTx b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (LogicError SimpleTx
 -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> (VerifiedCandidate SimpleTx
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LogicError SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall tx a. LogicError tx -> Program tx a
reject VerifiedCandidate SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a. a -> Program SimpleTx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{seenSnapshot = seen}}) (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ VerifiedCandidate SimpleTx -> Command SimpleTx
command VerifiedCandidate SimpleTx
evidence
      InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) Program SimpleTx ()
program
        Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Error AssertionFailed{} -> Bool
True; Outcome SimpleTx
_ -> Bool
False

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"releases a conflicting reservation in all 24 delivery orders and admits the next decommit" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [Either Text (World, Trace)]
-> (Either Text (World, Trace) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Either Text (World, Trace)]
Conflict.schedules ((Either Text (World, Trace) -> IO ()) -> IO ())
-> (Either Text (World, Trace) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Either Text (World, Trace)
schedule -> do
      (World
world, Trace
_) <- (Text -> IO (World, Trace))
-> ((World, Trace) -> IO (World, Trace))
-> Either Text (World, Trace)
-> IO (World, Trace)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (World, Trace)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (World, Trace))
-> (Text -> String) -> Text -> IO (World, Trace)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString) (World, Trace) -> IO (World, Trace)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either Text (World, Trace)
schedule
      World
world.pending [Delivery] -> [Delivery] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` []
      World
world.retries [Delivery] -> [Delivery] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` []
      [(Int, NodeState SimpleTx)]
-> ((Int, NodeState SimpleTx) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([Int] -> [NodeState SimpleTx] -> [(Int, NodeState SimpleTx)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] World
world.nodes) (((Int, NodeState SimpleTx) -> IO ()) -> IO ())
-> ((Int, NodeState SimpleTx) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Int
partyIndex, NodeState SimpleTx
node) -> do
        let current :: CoordinatedHeadState SimpleTx
current = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
node).coordinatedHeadState
        CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleTx
forall a. Maybe a
Nothing
        (ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot CoordinatedHeadState SimpleTx
current.confirmedSnapshot).utxo Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2
        let 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
Head.update (Int -> Environment
Scenario.environment Int
partyIndex) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
Head.NetworkInput TTL
0 (NetworkEvent (Message SimpleTx) -> Input SimpleTx)
-> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ Party -> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall msg. Party -> msg -> NetworkEvent msg
ReceivedMessage Party
alice (Message SimpleTx -> NetworkEvent (Message SimpleTx))
-> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec (SimpleTx -> Message SimpleTx) -> SimpleTx -> Message SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)
        Outcome SimpleTx
outcome Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Continue (DecommitRecorded{} : [StateChanged SimpleTx]
_) [Effect SimpleTx]
_ -> Bool
True; Outcome SimpleTx
_ -> Bool
False

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"restores other reserved inputs on conflict, but keeps a retained dependency" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [Bool] -> (Bool -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> IO ()) -> IO ()) -> (Bool -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Bool
keepDependency -> do
      let tx :: SimpleTx
tx = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
10 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
9) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
10)
          base :: Snapshot SimpleTx
          base :: Snapshot SimpleTx
base = Snapshot SimpleTx
candidate{utxo = utxoRef 2 <> utxoRef 9, accumulator = Accumulator.buildFromUTxO @SimpleTx (utxoRef 2 <> utxoRef 9), appliedAccumulator = Accumulator.buildFromUTxO @SimpleTx (utxoRef 2 <> utxoRef 9)}
          pending :: Seq SimpleTx
pending = if Bool
keepDependency then SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton (SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1)) else Seq SimpleTx
forall a. Monoid a => a
mempty
          before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{decommitTx = Just tx, localTxs = pending, localUTxO = if keepDependency then mempty else utxoRef 2, seenSnapshot = mkSeenSnapshot base mempty}}
          event :: StateChanged SimpleTx
          event :: StateChanged SimpleTx
event = HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty
          runtime :: Runtime SimpleTx
runtime = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty StateChanged SimpleTx
event (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before)
          current :: CoordinatedHeadState SimpleTx
current = Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
runtime
      CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` if Bool
keepDependency then SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
tx else Maybe SimpleTx
forall a. Maybe a
Nothing
      CoordinatedHeadState SimpleTx
current.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` if Bool
keepDependency then Set SimpleTxOut
forall a. Monoid a => a
mempty else SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
9
      let node :: NodeState SimpleTx
node = NodeState SimpleTx
Scenario.initialNode{headState = Head.Open before}
          expected :: NodeState SimpleTx
expected = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
aggregateNodeState NodeState SimpleTx
node StateChanged SimpleTx
event
      CoordinatedHeadState SimpleTx
current CoordinatedHeadState SimpleTx
-> CoordinatedHeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
expected).coordinatedHeadState
      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
expected) Either DecoderError (NodeState SimpleTx)
-> Either DecoderError (NodeState SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> Either DecoderError (NodeState SimpleTx)
forall a b. b -> Either a b
Right NodeState SimpleTx
expected

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"protects an authorised decommit even when no ReqDec was received" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
    let authorised :: Snapshot SimpleTx
        authorised :: Snapshot SimpleTx
authorised = Snapshot SimpleTx
candidate{utxoToDecommit = Just (utxoRef 10)}
        before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{confirmedSnapshot = ConfirmedSnapshot authorised mempty, localUTxO = utxoRef 2}}
        runtime :: Runtime SimpleTx
runtime = OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before
        command :: Command SimpleTx
command = ValidatedDecommit SimpleTx -> Command SimpleTx
forall tx. ValidatedDecommit tx -> Command tx
Coordinated.recordDecommit (UTxOType SimpleTx -> SimpleTx -> ValidatedDecommit SimpleTx
checkedDecommit (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)))
        stage :: Coordinated.Runtime SimpleTx -> Outcome SimpleTx
        stage :: Runtime SimpleTx -> Outcome SimpleTx
stage Runtime SimpleTx
target = InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage Runtime SimpleTx
target Command SimpleTx
command)
    Runtime SimpleTx -> Outcome SimpleTx
stage Runtime SimpleTx
runtime Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Error AssertionFailed{} -> Bool
True; Outcome SimpleTx
_ -> Bool
False
    let ([StateChanged SimpleTx]
_, Runtime SimpleTx
settled) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step Runtime SimpleTx
runtime (Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx))
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ ChainStateType SimpleTx
-> SnapshotVersion -> UTxOType SimpleTx -> Command SimpleTx
forall tx.
ChainStateType tx -> SnapshotVersion -> UTxOType tx -> Command tx
Coordinated.recordDecommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) SnapshotVersion
1 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
10)
    Runtime SimpleTx -> Outcome SimpleTx
stage Runtime SimpleTx
settled Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Continue [DecommitRecorded{}] [] -> Bool
True; Outcome SimpleTx
_ -> Bool
False

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"compares confirmation disposition with Agda over bounded input sets" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [([SimpleId], [SimpleId], Bool, Bool)]
-> (([SimpleId], [SimpleId], Bool, Bool) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [([SimpleId]
inputs, [SimpleId]
available, Bool
carried, Bool
other) | [SimpleId]
inputs <- [SimpleId] -> [[SimpleId]]
forall a. [a] -> [[a]]
subsequences [SimpleId
1, SimpleId
2, SimpleId
3], [SimpleId]
available <- [SimpleId] -> [[SimpleId]]
forall a. [a] -> [[a]]
subsequences [SimpleId
1, SimpleId
2, SimpleId
3], (Bool
carried, Bool
other) <- [(Bool
False, Bool
False), (Bool
True, Bool
False), (Bool
False, Bool
True)]] ((([SimpleId], [SimpleId], Bool, Bool) -> IO ()) -> IO ())
-> (([SimpleId], [SimpleId], Bool, Bool) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \([SimpleId]
inputs, [SimpleId]
available, Bool
carried, Bool
other) -> do
      let tx :: SimpleTx
tx = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
10 ([Item (UTxOType SimpleTx)] -> UTxOType SimpleTx
forall l. IsList l => [Item l] -> l
fromList ([Item (UTxOType SimpleTx)] -> UTxOType SimpleTx)
-> [Item (UTxOType SimpleTx)] -> UTxOType SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> SimpleTxOut
forall a. Num a => SimpleId -> a
fromInteger (SimpleId -> SimpleTxOut) -> [SimpleId] -> [SimpleTxOut]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SimpleId]
inputs) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
4)
          snapshot :: Snapshot SimpleTx
          snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
candidate{utxo = fromList $ fromInteger <$> available, utxoToDecommit = if carried then Just (utxoRef 4) else if other then Just (utxoRef 5) else Nothing}
          before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{decommitTx = Just tx, seenSnapshot = mkSeenSnapshot snapshot mempty}}
          next :: Runtime SimpleTx
next = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty (HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty) (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before)
      Maybe SimpleTx -> Bool
forall a. Maybe a -> Bool
isJust (Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
next).decommitTx Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Bool -> [SimpleId] -> [SimpleId] -> Bool
Agda.retainReservationRef Bool
carried Bool
other [SimpleId]
inputs [SimpleId]
available

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"compares client and network admission with Agda, including certificates without ReqDec" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [(Bool, Bool)] -> ((Bool, Bool) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
pending, Bool
authorised) | Bool
pending <- [Bool
False, Bool
True], Bool
authorised <- [Bool
False, Bool
True]] (((Bool, Bool) -> IO ()) -> IO ())
-> ((Bool, Bool) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
pending, Bool
authorised) -> do
      let snapshot :: Snapshot SimpleTx
          snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
candidate{utxoToDecommit = utxoRef 10 <$ guard authorised}
          chs :: CoordinatedHeadState SimpleTx
chs = OpenState SimpleTx
open.coordinatedHeadState{confirmedSnapshot = ConfirmedSnapshot snapshot mempty, decommitTx = SimpleTx 10 (utxoRef 1) (utxoRef 10) <$ guard pending, localUTxO = utxoRef 2}
          node :: NodeState SimpleTx
node = NodeState SimpleTx
Scenario.initialNode{headState = Head.Open open{coordinatedHeadState = chs}}
          tx :: SimpleTx
tx = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)
          run :: Input SimpleTx -> Outcome SimpleTx
run = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
Head.update (Int -> Environment
Scenario.environment Int
0) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node
          accepts :: Outcome SimpleTx -> Bool
          accepts :: Outcome SimpleTx -> Bool
accepts = \case Continue (DecommitInvalid{} : [StateChanged SimpleTx]
_) [Effect SimpleTx]
_ -> Bool
False; Continue{} -> Bool
True; Outcome SimpleTx
_ -> Bool
False
      Outcome SimpleTx -> Bool
accepts (Input SimpleTx -> Outcome SimpleTx
run (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
Head.NetworkInput TTL
0 (NetworkEvent (Message SimpleTx) -> Input SimpleTx)
-> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ Party -> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall msg. Party -> msg -> NetworkEvent msg
ReceivedMessage Party
alice (Message SimpleTx -> NetworkEvent (Message SimpleTx))
-> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec SimpleTx
tx) Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Bool -> Bool
Agda.decommitAdmissionRef Bool
pending Bool
authorised
      Outcome SimpleTx -> Bool
accepts (Input SimpleTx -> Outcome SimpleTx
run (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
Head.ClientInput (ClientInput SimpleTx -> Input SimpleTx)
-> ClientInput SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> ClientInput SimpleTx
forall tx. tx -> ClientInput tx
Client.Decommit SimpleTx
tx) Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Bool -> Bool
Agda.decommitAdmissionRef Bool
pending Bool
authorised

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"compares certificate context with Agda using real signatures" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    [(Bool, [(SimpleId, Party)])]
-> ((Bool, [(SimpleId, Party)]) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
sameHead, [(SimpleId, Party)]
parties) | Bool
sameHead <- [Bool
False, Bool
True], [(SimpleId, Party)]
parties <- [] [(SimpleId, Party)]
-> [[(SimpleId, Party)]] -> [[(SimpleId, Party)]]
forall a. a -> [a] -> [a]
: [[(SimpleId
1, Party
alice)]] [[(SimpleId, Party)]]
-> [[(SimpleId, Party)]] -> [[(SimpleId, Party)]]
forall a. Semigroup a => a -> a -> a
<> [(SimpleId, Party)] -> [[(SimpleId, Party)]]
forall a. [a] -> [[a]]
permutations [(SimpleId
1, Party
alice), (SimpleId
2, Party
bob), (SimpleId
3, Party
carol)]] (((Bool, [(SimpleId, Party)]) -> IO ()) -> IO ())
-> ((Bool, [(SimpleId, Party)]) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
sameHead, [(SimpleId, Party)]
parties) -> do
      let snapshot :: Snapshot SimpleTx
          snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
candidate{headId = if sameHead then testHeadId else UnsafeHeadId "another head"}
          signed :: MultiSignature (Snapshot SimpleTx)
signed = Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder ([(Party, Signature (Snapshot SimpleTx))]
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Party
p, Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
snapshot) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [(Party
alice, Secret (SigningKey HydraKey)
aliceSk), (Party
bob, Secret (SigningKey HydraKey)
bobSk), (Party
carol, Secret (SigningKey HydraKey)
carolSk)]]) ((SimpleId, Party) -> Party
forall a b. (a, b) -> b
snd ((SimpleId, Party) -> Party) -> [(SimpleId, Party)] -> [Party]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(SimpleId, Party)]
parties)
          program :: Program.Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            VerifiedCandidate SimpleTx
evidence <- Candidate SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
     SimpleTx
     (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall tx.
Candidate tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either (LogicError tx) (VerifiedCandidate tx))
verifyCandidate (Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate (Runtime SimpleTx -> Candidate SimpleTx)
-> Runtime SimpleTx -> Candidate SimpleTx
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx -> [Party] -> Runtime SimpleTx
owner Snapshot SimpleTx
snapshot ((SimpleId, Party) -> Party
forall a b. (a, b) -> b
snd ((SimpleId, Party) -> Party) -> [(SimpleId, Party)] -> [Party]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(SimpleId, Party)]
parties)) MultiSignature (Snapshot SimpleTx)
signed Program
  SimpleTx
  (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
-> (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a b.
Program SimpleTx a
-> (a -> Program SimpleTx b) -> Program SimpleTx b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (LogicError SimpleTx
 -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> (VerifiedCandidate SimpleTx
    -> Program SimpleTx (VerifiedCandidate SimpleTx))
-> Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either LogicError SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall tx a. LogicError tx -> Program tx a
reject VerifiedCandidate SimpleTx
-> Program SimpleTx (VerifiedCandidate SimpleTx)
forall a. a -> Program SimpleTx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
            Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ())
-> Program SimpleTx (Runtime SimpleTx) -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall tx. Runtime tx -> Command tx -> Program tx (Runtime tx)
Staging.stage (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{seenSnapshot = mkSeenSnapshot snapshot mempty}}) (Command SimpleTx -> Program SimpleTx (Runtime SimpleTx))
-> Command SimpleTx -> Program SimpleTx (Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ VerifiedCandidate SimpleTx -> Command SimpleTx
forall tx. VerifiedCandidate tx -> Command tx
Coordinated.confirmSnapshot VerifiedCandidate SimpleTx
evidence
          accepted :: Bool
accepted = case InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) Program SimpleTx ()
program of Continue [SnapshotConfirmed{}] [] -> Bool
True; Outcome SimpleTx
_ -> Bool
False
      Bool
accepted Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> SimpleId -> [SimpleId] -> [SimpleId] -> Bool
Agda.verificationContextRef SimpleId
0 (if Bool
sameHead then SimpleId
0 else SimpleId
1) [SimpleId
1, SimpleId
2, SimpleId
3] ((SimpleId, Party) -> SimpleId
forall a b. (a, b) -> a
fst ((SimpleId, Party) -> SimpleId)
-> [(SimpleId, Party)] -> [SimpleId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(SimpleId, Party)]
parties)
 where
  open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
Scenario.initialNode
  candidate :: Snapshot SimpleTx
  candidate :: Snapshot SimpleTx
candidate = Snapshot SimpleTx
Scenario.initialSnapshot{number = 1, confirmed = [Scenario.transactionOne], utxo = utxoRef 2, accumulator = Accumulator.buildFromUTxO @SimpleTx (utxoRef 2), appliedAccumulator = Accumulator.buildFromUTxO @SimpleTx (utxoRef 2)}
  seen :: SeenSnapshot SimpleTx
seen = Snapshot SimpleTx
-> Map Party (Signature (Snapshot SimpleTx))
-> SeenSnapshot SimpleTx
forall tx.
IsTx tx =>
Snapshot tx
-> Map Party (Signature (Snapshot tx)) -> SeenSnapshot tx
mkSeenSnapshot Snapshot SimpleTx
candidate Map Party (Signature (Snapshot SimpleTx))
forall a. Monoid a => a
mempty
  signatures :: MultiSignature (Snapshot SimpleTx)
signatures = Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder ([(Party, Signature (Snapshot SimpleTx))]
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Party
p, Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
candidate) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [(Party
alice, Secret (SigningKey HydraKey)
aliceSk), (Party
bob, Secret (SigningKey HydraKey)
bobSk), (Party
carol, Secret (SigningKey HydraKey)
carolSk)]]) [Party]
Scenario.scenarioParties

  owner :: Snapshot SimpleTx -> [Party] -> Coordinated.Runtime SimpleTx
  owner :: Snapshot SimpleTx -> [Party] -> Runtime SimpleTx
owner Snapshot SimpleTx
snapshot [Party]
parties =
    let parameters :: HeadParameters
        parameters :: HeadParameters
parameters = OpenState SimpleTx
open.parameters{parties = parties}
     in OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open{headId = snapshot.headId, parameters, coordinatedHeadState = open.coordinatedHeadState{seenSnapshot = mkSeenSnapshot snapshot mempty}}

  ownedCandidate :: Coordinated.Runtime SimpleTx -> Coordinated.Candidate SimpleTx
  ownedCandidate :: Runtime SimpleTx -> Candidate SimpleTx
ownedCandidate = Candidate SimpleTx
-> Maybe (Candidate SimpleTx) -> Candidate SimpleTx
forall a. a -> Maybe a -> a
fromMaybe (Text -> Candidate SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"fixture has no owned candidate") (Maybe (Candidate SimpleTx) -> Candidate SimpleTx)
-> (Runtime SimpleTx -> Maybe (Candidate SimpleTx))
-> Runtime SimpleTx
-> Candidate SimpleTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Runtime SimpleTx -> Maybe (Candidate SimpleTx)
forall tx. Runtime tx -> Maybe (Candidate tx)
Coordinated.candidate

  check :: (NodeState SimpleTx, Coordinated.Runtime SimpleTx) -> StateChanged SimpleTx -> IO (NodeState SimpleTx, Coordinated.Runtime SimpleTx)
  check :: (NodeState SimpleTx, Runtime SimpleTx)
-> StateChanged SimpleTx
-> IO (NodeState SimpleTx, Runtime SimpleTx)
check (NodeState SimpleTx
node, Runtime SimpleTx
runtime) StateChanged SimpleTx
event = do
    let before :: OpenState SimpleTx
before = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
node
        deposited :: Set SimpleTxOut
deposited = case StateChanged SimpleTx
event of
          CommitFinalized{TxIdType SimpleTx
depositTxId :: TxIdType SimpleTx
$sel:depositTxId:NetworkConnected :: forall tx. StateChanged tx -> TxIdType tx
depositTxId} -> Set SimpleTxOut
-> (Deposit SimpleTx -> Set SimpleTxOut)
-> Maybe (Deposit SimpleTx)
-> Set SimpleTxOut
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set SimpleTxOut
forall a. Monoid a => a
mempty (.deposited) (Maybe (Deposit SimpleTx) -> Set SimpleTxOut)
-> Maybe (Deposit SimpleTx) -> Set SimpleTxOut
forall a b. (a -> b) -> a -> b
$ SimpleId
-> Map SimpleId (Deposit SimpleTx) -> Maybe (Deposit SimpleTx)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SimpleId
TxIdType SimpleTx
depositTxId NodeState SimpleTx
node.pendingDeposits
          StateChanged SimpleTx
_ -> Set SimpleTxOut
forall a. Monoid a => a
mempty
    let next :: Runtime SimpleTx
next = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
deposited StateChanged SimpleTx
event Runtime SimpleTx
runtime
        restoredNext :: Runtime SimpleTx
restoredNext = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
deposited StateChanged SimpleTx
event (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before)
        after :: NodeState SimpleTx
after = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
aggregateNodeState NodeState SimpleTx
node StateChanged SimpleTx
event
    Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
next CoordinatedHeadState SimpleTx
-> CoordinatedHeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
after).coordinatedHeadState
    Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
restoredNext CoordinatedHeadState SimpleTx
-> CoordinatedHeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
next
    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
after) Either DecoderError (NodeState SimpleTx)
-> Either DecoderError (NodeState SimpleTx) -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> Either DecoderError (NodeState SimpleTx)
forall a b. b -> Either a b
Right NodeState SimpleTx
after
    (NodeState SimpleTx, Runtime SimpleTx)
-> IO (NodeState SimpleTx, Runtime SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NodeState SimpleTx
after, Runtime SimpleTx
next)