{-# LANGUAGE DataKinds #-}

module Hydra.HeadLogic.WorkflowSpec (spec) where

import Crem.StateMachine qualified as Crem
import Crem.Topology (Topology (Topology))
import Hydra.HeadLogic.Interpreter (runProgram, traceProgram)
import Hydra.HeadLogic.Program (Program, applyTransaction, change, reject, suspend, traceB)
import Hydra.HeadLogic.Workflow qualified as Workflow
import Hydra.Ledger (Ledger (..), ValidationError (..))
import Hydra.Ledger.Simple (SimpleTx)
import Hydra.Node.HeadLogic (
  LogicError (AssertionFailed),
  Outcome (Continue, Error, Wait),
  StateChanged (TransactionReceived),
  WaitReason (WaitOnSnapshotNumber),
 )
import Hydra.Prelude hiding (State, state)
import Test.Hydra.HeadLogic.Interpreter (testInterpreterConfig)
import Test.Hydra.Ledger.Simple (aValidTx)
import Test.Hydra.Prelude (Spec, describe, it, shouldBe)

data Phase = Deciding | Applying | Completed

type TopologyOf =
  'Topology
    '[ '( 'Deciding, '[ 'Applying])
     , '( 'Applying, '[ 'Completed])
     ]

data State (phase :: Phase) where
  Decide :: State 'Deciding
  Apply :: State 'Applying
  Complete :: State 'Completed

spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"workflow execution" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
  String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"uses primitive replies to advance to completion" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
    let ledger :: Ledger SimpleTx
        ledger :: Ledger SimpleTx
ledger = (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
utxo [SimpleTx]
_ -> Set SimpleTxOut
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. b -> Either a b
Right Set SimpleTxOut
UTxOType SimpleTx
utxo
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) (Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx TopologyOf () -> Program SimpleTx ())
-> Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Bool -> Workflow SimpleTx TopologyOf ()
example Bool
True) Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
transaction] []
  String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"returns a typed value to the enclosing program before continuing" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
    let ledger :: Ledger SimpleTx
        ledger :: Ledger SimpleTx
ledger = (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]
_ -> Text -> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"completed workflow queries the ledger"
        completed :: Workflow.Workflow SimpleTx TopologyOf SimpleTx
        completed :: Workflow SimpleTx TopologyOf SimpleTx
completed = (forall (phase :: Phase). State phase -> Text)
-> State 'Completed
-> (forall (from :: Phase).
    State from -> Transition SimpleTx TopologyOf State from SimpleTx)
-> Workflow SimpleTx TopologyOf SimpleTx
forall {k} (state :: k -> *) (initial :: k) tx
       (topology :: Topology k) result.
(forall (phase :: k). state phase -> Text)
-> state initial
-> (forall (from :: k).
    state from -> Transition tx topology state from result)
-> Workflow tx topology result
Workflow.workflow (Text -> State phase -> Text
forall a b. a -> b -> a
const Text
"completed") State 'Completed
Complete (State from
-> SimpleTx -> Transition SimpleTx TopologyOf State from SimpleTx
forall {vertex} (state :: vertex -> *) (phase :: vertex) result tx
       (topology :: Topology vertex).
state phase -> result -> Transition tx topology state phase result
`Workflow.finish` SimpleTx
transaction)
        program :: Program SimpleTx ()
program = Workflow SimpleTx TopologyOf SimpleTx -> Program SimpleTx SimpleTx
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run Workflow SimpleTx TopologyOf SimpleTx
completed Program SimpleTx SimpleTx
-> (SimpleTx -> Program SimpleTx ()) -> Program 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
>>= StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change (StateChanged SimpleTx -> Program SimpleTx ())
-> (SimpleTx -> StateChanged SimpleTx)
-> SimpleTx
-> Program SimpleTx ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) Program SimpleTx ()
program Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
transaction] []
  String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"suspends without evaluating a later primitive" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
    let ledger :: Ledger SimpleTx
        ledger :: Ledger SimpleTx
ledger = (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]
_ -> Text -> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"unreachable ledger query"
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) (Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx TopologyOf () -> Program SimpleTx ())
-> Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Bool -> Workflow SimpleTx TopologyOf ()
example Bool
False) Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` WaitReason SimpleTx -> [StateChanged SimpleTx] -> Outcome SimpleTx
forall tx. WaitReason tx -> [StateChanged tx] -> Outcome tx
Wait (SnapshotNumber -> WaitReason SimpleTx
forall tx. SnapshotNumber -> WaitReason tx
WaitOnSnapshotNumber SnapshotNumber
1) []
  String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"rejects through Program before a later workflow phase" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
    let ledger :: Ledger SimpleTx
        ledger :: Ledger SimpleTx
ledger = (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]
_ -> (SimpleTx, ValidationError)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. a -> Either a b
Left (SimpleTx
transaction, Text -> ValidationError
ValidationError Text
"controlled rejection")
    InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) (Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx TopologyOf () -> Program SimpleTx ())
-> Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Bool -> Workflow SimpleTx TopologyOf ()
example Bool
True) Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` LogicError SimpleTx -> Outcome SimpleTx
forall tx. LogicError tx -> Outcome tx
Error (Text -> LogicError SimpleTx
forall tx. Text -> LogicError tx
AssertionFailed Text
"ledger rejected")
  String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves the generic Crem runner's outcome and complete instruction trace" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
    [(Bool, Bool)] -> ((Bool, Bool) -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
eligible, Bool
valid) | Bool
eligible <- [Bool
False, Bool
True], Bool
valid <- [Bool
False, Bool
True]] (((Bool, Bool) -> Expectation) -> Expectation)
-> ((Bool, Bool) -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \(Bool
eligible, Bool
valid) -> do
      let ledger :: Ledger SimpleTx
          ledger :: Ledger SimpleTx
ledger = (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
utxo [SimpleTx]
_ ->
            if Bool
valid then Set SimpleTxOut
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. b -> Either a b
Right Set SimpleTxOut
UTxOType SimpleTx
utxo else (SimpleTx, ValidationError)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. a -> Either a b
Left (SimpleTx
transaction, Text -> ValidationError
ValidationError Text
"controlled rejection")
          config :: InterpreterConfig SimpleTx
config = Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger
          workflow :: Workflow SimpleTx TopologyOf ()
workflow = Bool -> Workflow SimpleTx TopologyOf ()
example Bool
eligible
      InterpreterConfig SimpleTx
-> Program SimpleTx () -> (Outcome SimpleTx, [TraceStep])
forall tx.
InterpreterConfig tx -> Program tx () -> (Outcome tx, [TraceStep])
traceProgram InterpreterConfig SimpleTx
config (Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run Workflow SimpleTx TopologyOf ()
workflow)
        (Outcome SimpleTx, [TraceStep])
-> (Outcome SimpleTx, [TraceStep]) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` InterpreterConfig SimpleTx
-> Program SimpleTx () -> (Outcome SimpleTx, [TraceStep])
forall tx.
InterpreterConfig tx -> Program tx () -> (Outcome tx, [TraceStep])
traceProgram InterpreterConfig SimpleTx
config (Workflow SimpleTx TopologyOf () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
runThroughCrem Workflow SimpleTx TopologyOf ()
workflow)

-- Keep the general runner as an independent oracle for the direct driver.
runThroughCrem :: Workflow.Workflow tx topology result -> Program tx result
runThroughCrem :: forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
runThroughCrem = StateMachineT (Program tx) () (Maybe result) -> Program tx result
forall (m :: * -> *) result.
Monad m =>
StateMachineT m () (Maybe result) -> m result
drive (StateMachineT (Program tx) () (Maybe result) -> Program tx result)
-> (Workflow tx topology result
    -> StateMachineT (Program tx) () (Maybe result))
-> Workflow tx topology result
-> Program tx result
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Workflow tx topology result
-> StateMachineT (Program tx) () (Maybe result)
forall {vertex} (m :: * -> *) (topology :: Topology vertex) input
       output.
BaseMachineT m topology input output
-> StateMachineT m input output
Crem.Executable
 where
  drive :: Monad m => Crem.StateMachineT m () (Maybe result) -> m result
  drive :: forall (m :: * -> *) result.
Monad m =>
StateMachineT m () (Maybe result) -> m result
drive StateMachineT m () (Maybe result)
machine = do
    (Maybe result
result, StateMachineT m () (Maybe result)
remaining) <- StateMachineT m () (Maybe result)
-> () -> m (Maybe result, StateMachineT m () (Maybe result))
forall (m :: * -> *) a b.
Monad m =>
StateMachineT m a b -> a -> m (b, StateMachineT m a b)
Crem.run StateMachineT m () (Maybe result)
machine ()
    m result -> (result -> m result) -> Maybe result -> m result
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (StateMachineT m () (Maybe result) -> m result
forall (m :: * -> *) result.
Monad m =>
StateMachineT m () (Maybe result) -> m result
drive StateMachineT m () (Maybe result)
remaining) result -> m result
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe result
result

example :: Bool -> Workflow.Workflow SimpleTx TopologyOf ()
example :: Bool -> Workflow SimpleTx TopologyOf ()
example Bool
eligible = (forall (phase :: Phase). State phase -> Text)
-> State 'Deciding
-> (forall (from :: Phase).
    State from -> Transition SimpleTx TopologyOf State from ())
-> Workflow SimpleTx TopologyOf ()
forall {k} (state :: k -> *) (initial :: k) tx
       (topology :: Topology k) result.
(forall (phase :: k). state phase -> Text)
-> state initial
-> (forall (from :: k).
    state from -> Transition tx topology state from result)
-> Workflow tx topology result
Workflow.workflow State phase -> Text
forall (phase :: Phase). State phase -> Text
describePhase State 'Deciding
Decide State from -> Transition SimpleTx TopologyOf State from ()
forall (from :: Phase).
State from -> Transition SimpleTx TopologyOf State from ()
step
 where
  describePhase :: State phase -> Text
  describePhase :: forall (phase :: Phase). State phase -> Text
describePhase = \case
    State phase
Decide -> Text
"deciding"
    State phase
Apply -> Text
"applying"
    State phase
Complete -> Text
"completed"

  step :: State phase -> Workflow.Transition SimpleTx TopologyOf State phase ()
  step :: forall (from :: Phase).
State from -> Transition SimpleTx TopologyOf State from ()
step = \case
    State phase
Decide -> Program SimpleTx (Destination TopologyOf State phase ())
-> Transition SimpleTx TopologyOf State phase ()
forall {vertex} tx (topology :: Topology vertex)
       (state :: vertex -> *) (from :: vertex) result.
Program tx (Destination topology state from result)
-> Transition tx topology state from result
Workflow.choose (Program SimpleTx (Destination TopologyOf State phase ())
 -> Transition SimpleTx TopologyOf State phase ())
-> Program SimpleTx (Destination TopologyOf State phase ())
-> Transition SimpleTx TopologyOf State phase ()
forall a b. (a -> b) -> a -> b
$ do
      Bool
proceed <- Text -> Bool -> Program SimpleTx Bool
forall tx. Text -> Bool -> Program tx Bool
traceB Text
"eligible" Bool
eligible
      if Bool
proceed
        then Destination TopologyOf State phase ()
-> Program SimpleTx (Destination TopologyOf State phase ())
forall a. a -> Program SimpleTx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Destination TopologyOf State phase ()
 -> Program SimpleTx (Destination TopologyOf State phase ()))
-> Destination TopologyOf State phase ()
-> Program SimpleTx (Destination TopologyOf State phase ())
forall a b. (a -> b) -> a -> b
$ State 'Applying -> Destination TopologyOf State phase ()
forall {vertex} (topology :: Topology vertex) (from :: vertex)
       (to :: vertex) (state :: vertex -> *) result.
AllowedTransition topology from to =>
state to -> Destination topology state from result
Workflow.next State 'Applying
Apply
        else WaitReason SimpleTx
-> Program SimpleTx (Destination TopologyOf State phase ())
forall tx a. WaitReason tx -> Program tx a
suspend (WaitReason SimpleTx
 -> Program SimpleTx (Destination TopologyOf State phase ()))
-> WaitReason SimpleTx
-> Program SimpleTx (Destination TopologyOf State phase ())
forall a b. (a -> b) -> a -> b
$ SnapshotNumber -> WaitReason SimpleTx
forall tx. SnapshotNumber -> WaitReason tx
WaitOnSnapshotNumber SnapshotNumber
1
    State phase
Apply -> (() -> State 'Completed)
-> Program SimpleTx ()
-> Transition SimpleTx TopologyOf State phase ()
forall {vertex} (topology :: Topology vertex) (from :: vertex)
       (to :: vertex) reply (state :: vertex -> *) tx result.
AllowedTransition topology from to =>
(reply -> state to)
-> Program tx reply -> Transition tx topology state from result
Workflow.continue (State 'Completed -> () -> State 'Completed
forall a b. a -> b -> a
const State 'Completed
Complete) (Program SimpleTx ()
 -> Transition SimpleTx TopologyOf State phase ())
-> Program SimpleTx ()
-> Transition SimpleTx TopologyOf State phase ()
forall a b. (a -> b) -> a -> b
$ do
      Either ValidationError (Set SimpleTxOut)
result <- ChainSlot
-> UTxOType SimpleTx
-> SimpleTx
-> Program SimpleTx (Either ValidationError (UTxOType SimpleTx))
forall tx.
ChainSlot
-> UTxOType tx
-> tx
-> Program tx (Either ValidationError (UTxOType tx))
applyTransaction ChainSlot
0 Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SimpleTx
transaction
      (ValidationError -> Program SimpleTx ())
-> (Set SimpleTxOut -> Program SimpleTx ())
-> Either ValidationError (Set SimpleTxOut)
-> Program SimpleTx ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
        (Program SimpleTx () -> ValidationError -> Program SimpleTx ()
forall a b. a -> b -> a
const (Program SimpleTx () -> ValidationError -> Program SimpleTx ())
-> Program SimpleTx () -> ValidationError -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject (LogicError SimpleTx -> Program SimpleTx ())
-> LogicError SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Text -> LogicError SimpleTx
forall tx. Text -> LogicError tx
AssertionFailed Text
"ledger rejected")
        (Program SimpleTx () -> Set SimpleTxOut -> Program SimpleTx ()
forall a b. a -> b -> a
const (Program SimpleTx () -> Set SimpleTxOut -> Program SimpleTx ())
-> Program SimpleTx () -> Set SimpleTxOut -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change (StateChanged SimpleTx -> Program SimpleTx ())
-> StateChanged SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
transaction)
        Either ValidationError (Set SimpleTxOut)
result
    State phase
Complete -> State phase -> () -> Transition SimpleTx TopologyOf State phase ()
forall {vertex} (state :: vertex -> *) (phase :: vertex) result tx
       (topology :: Topology vertex).
state phase -> result -> Transition tx topology state phase result
Workflow.finish State phase
State 'Completed
Complete ()

transaction :: SimpleTx
transaction :: SimpleTx
transaction = Integer -> SimpleTx
aValidTx Integer
42