{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Hydra.HeadLogic.ProgramSpec (spec) where
import Hydra.HeadLogic.Program (foldProgram, instruction, suspend)
import Hydra.Node.HeadLogic.Configuration qualified as Configuration
import Hydra.Prelude hiding (trace)
import Hydra.Prelude qualified as Prelude
import Test.Hydra.HeadLogic.Interpreter (testInterpreterConfig)
import Test.Hydra.Prelude (
Spec,
describe,
failure,
it,
shouldBe,
shouldNotContain,
shouldSatisfy,
)
import Data.Map.Strict qualified as Map
import Data.Sequence qualified as Seq
import Hydra.Agda.OffChainReference (
HsReqTxChange (..),
HsReqTxEffect (..),
HsReqTxOutcome (..),
HsReqTxTerminal (..),
reqTxOutcomeRef,
)
import Hydra.Chain.ChainState (ChainSlot)
import Hydra.HeadLogic.Interpreter qualified as Interpreter
import Hydra.HeadLogic.Program qualified as Program
import Hydra.HeadLogic.Rules.Network.ReqTx qualified as ReqTx
import Hydra.HeadLogic.Trace qualified as Trace
import Hydra.HeadLogic.Workflow qualified as Workflow
import Hydra.HeadLogicSpec (inOpenState, testSnapshot, withAccumulators, zeroChainPointTime)
import Hydra.Ledger (Ledger (..), ValidationError (..))
import Hydra.Ledger.Simple (SimpleTx, simpleLedger)
import Hydra.Network.Message (Message (..))
import Hydra.Node.Environment (Environment (..))
import Hydra.Node.HeadLogic (
CoordinatedHeadState (..),
Effect (..),
HeadState (..),
OpenState (..),
Outcome (..),
SeenSnapshot (..),
StateChanged (..),
TTL,
WaitReason (..),
aggregateState,
ruleEnvironment,
)
import Hydra.Node.State (Deposit (..), DepositStatus (..), NodeState (..), initialChainTime)
import Hydra.Tx.IsTx (UTxOType, txId)
import Hydra.Tx.Party (Party)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..), Snapshot (..))
import Test.Hydra.HeadLogic.Debugger qualified as Debug
import Test.Hydra.HeadLogic.State ()
import Test.Hydra.Ledger.Simple (aValidTx, utxoRef)
import Test.Hydra.Node.Fixture (testEnvironment)
import Test.Hydra.Node.State ()
import Test.Hydra.Tx.Fixture (alice, bob, carol, testHeadId)
spec :: Spec
spec :: Spec
spec = do
Spec
reificationSpec
Spec
capacityPruningSpec
String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"crem phases with program actions" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ ReqTxRule -> Spec
specWith (Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ())
-> (Args SimpleTx -> Workflow SimpleTx ReqTxTopology ())
-> ReqTxRule
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Args SimpleTx -> Workflow SimpleTx ReqTxTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx ReqTxTopology ()
ReqTx.workflow)
reificationSpec :: Spec
reificationSpec :: Spec
reificationSpec =
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reifies suspension without evaluating its unreachable continuation" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let program :: Program.Program SimpleTx ()
program :: Program SimpleTx ()
program = do
Any
_ <- WaitReason SimpleTx -> Program SimpleTx Any
forall tx a. WaitReason tx -> Program tx a
suspend WaitReason SimpleTx
waitReason
Text -> Program SimpleTx ()
forall a t. (HasCallStack, IsText t) => t -> a
Prelude.error Text
"reification evaluates the suspended continuation"
case Program SimpleTx () -> Cursor SimpleTx ()
forall tx a. Program tx a -> Cursor tx a
Debug.cursor Program SimpleTx ()
program of
Debug.Await (Program.Suspend WaitReason SimpleTx
actualReason) x -> Cursor SimpleTx ()
_ -> WaitReason SimpleTx
actualReason WaitReason SimpleTx -> WaitReason SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` WaitReason SimpleTx
waitReason
Cursor SimpleTx ()
_ -> String -> Expectation
forall (m :: * -> *) a.
(HasCallStack, MonadThrow m) =>
String -> m a
failure String
"expected a suspended instruction" :: IO ()
capacityPruningSpec :: Spec
capacityPruningSpec :: Spec
capacityPruningSpec = String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"requests a pruning round when a settling deposit blocks the first transaction" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let deposited :: UTxOType SimpleTx
deposited = SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
99
pending :: Map SimpleId (Deposit SimpleTx)
pending = SimpleId -> Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton SimpleId
7 (Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx))
-> Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx)
forall a b. (a -> b) -> a -> b
$ DepositStatus -> UTxOType SimpleTx -> Deposit SimpleTx
depositFixture DepositStatus
Active Set SimpleTxOut
UTxOType SimpleTx
deposited
st :: OpenState SimpleTx
st =
OpenState SimpleTx
emptyOpenState
{ coordinatedHeadState =
emptyOpenState.coordinatedHeadState
{ confirmedSnapshot = settlingSnapshot deposited
, localUTxO = deposited
, localTxs = Seq.singleton $ aValidTx 99
, currentDepositTxId = Just 7
}
}
program :: Program SimpleTx ()
program =
Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ())
-> Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$
Args SimpleTx -> Workflow SimpleTx ReqTxTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx ReqTxTopology ()
ReqTx.workflow
ReqTx.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
ruleEnvironment Environment
testEnvironment
, $sel:chainPointTime:Args :: ChainPointTime
chainPointTime = ChainPointTime
zeroChainPointTime
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
st
, $sel:ttl:Args :: TTL
ttl = TTL
1
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
pending
, $sel:transaction:Args :: SimpleTx
transaction = SimpleTx
sampleTx
}
outcome :: Outcome SimpleTx
outcome = 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 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 [StateChanged SimpleTx
received, StateChanged SimpleTx
applied, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1] [Message SimpleTx -> Effect SimpleTx
forall tx. Message tx -> Effect tx
NetworkEffect (Message SimpleTx -> Effect SimpleTx)
-> Message SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] Maybe SimpleTx
forall a. Maybe a
Nothing (SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
7)]
type ReqTxRule = ReqTx.Args SimpleTx -> Program.Program SimpleTx ()
specWith :: ReqTxRule -> Spec
specWith :: ReqTxRule -> Spec
specWith ReqTxRule
runReqTx = do
String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"ReqTx program" (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
"agrees with the production wrapper" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let program :: Program SimpleTx ()
program = Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx
InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram
(Environment -> Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx.
IsTx tx =>
Environment -> Ledger tx -> InterpreterConfig tx
Configuration.interpreterConfig Environment
testEnvironment Ledger SimpleTx
simpleLedger)
( Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ())
-> Workflow SimpleTx ReqTxTopology () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$
Args SimpleTx -> Workflow SimpleTx ReqTxTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx ReqTxTopology ()
ReqTx.workflow
ReqTx.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
Configuration.ruleEnvironment Environment
testEnvironment
, $sel:chainPointTime:Args :: ChainPointTime
chainPointTime = ChainPointTime
zeroChainPointTime
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
emptyOpenState
, $sel:ttl:Args :: TTL
ttl = TTL
1
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty
, $sel:transaction:Args :: SimpleTx
transaction = SimpleTx
sampleTx
}
)
Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` 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
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"a follower applies the transaction without requesting a snapshot" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
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) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
bob TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx)
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 [StateChanged SimpleTx
received, StateChanged SimpleTx
applied] []
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"a leader records the request before broadcasting its transaction IDs" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
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) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx)
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
[StateChanged SimpleTx
received, StateChanged SimpleTx
applied, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1]
[Message SimpleTx -> Effect SimpleTx
forall tx. Message tx -> Effect tx
NetworkEffect (Message SimpleTx -> Effect SimpleTx)
-> Message SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [SimpleTx -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
sampleTx] Maybe SimpleTx
forall a. Maybe a
Nothing Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"a controlled ledger failure retains the transaction while waiting" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let outcome :: Outcome SimpleTx
outcome = 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
rejectingLedger) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx)
Outcome SimpleTx
outcome 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 (ValidationError -> WaitReason SimpleTx
forall tx. ValidationError -> WaitReason tx
WaitOnNotApplicableTx ValidationError
ledgerFailure) [StateChanged SimpleTx
received]
case (NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState ([Party] -> NodeState SimpleTx
inOpenState [Party]
testParties) Outcome SimpleTx
outcome).headState of
Open OpenState SimpleTx
st -> OpenState SimpleTx
st.coordinatedHeadState.allTxs Map SimpleId SimpleTx -> Map SimpleId SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SimpleId -> SimpleTx -> Map SimpleId SimpleTx
forall k a. k -> a -> Map k a
Map.singleton (SimpleTx -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
sampleTx) SimpleTx
sampleTx
HeadState SimpleTx
other -> String -> Expectation
forall (m :: * -> *) a.
(HasCallStack, MonadThrow m) =>
String -> m a
failure (String -> Expectation) -> String -> Expectation
forall a b. (a -> b) -> a -> b
$ String
"expected an open head, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> HeadState SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show HeadState SimpleTx
other
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"an exhausted request records invalidation and finishes normally" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
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
rejectingLedger) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
0 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx)
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
[StateChanged SimpleTx
received, TxInvalid{$sel:headId:NetworkConnected :: HeadId
headId = HeadId
testHeadId, $sel:utxo:NetworkConnected :: UTxOType SimpleTx
utxo = Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty, $sel:transaction:NetworkConnected :: SimpleTx
transaction = SimpleTx
sampleTx, $sel:validationError:NetworkConnected :: ValidationError
validationError = ValidationError
ledgerFailure}]
[]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"the ledger instruction can be intercepted without replacing the rule" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let program :: Program SimpleTx ()
program = (forall x. Instruction SimpleTx x -> Program SimpleTx x)
-> Program SimpleTx () -> Program SimpleTx ()
forall (m :: * -> *) tx a.
Monad m =>
(forall x. Instruction tx x -> m x) -> Program tx a -> m a
foldProgram Instruction SimpleTx x -> Program SimpleTx x
forall x. Instruction SimpleTx x -> Program SimpleTx x
rejectLedgerQuery (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx)
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
Prelude.error Text
"intercepted query reaches the real ledger"
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
ledger) Program SimpleTx ()
program
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 (ValidationError -> WaitReason SimpleTx
forall tx. ValidationError -> WaitReason tx
WaitOnNotApplicableTx ValidationError
ledgerFailure) [StateChanged SimpleTx
received]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"queries the ledger once on both acceptance and ledger rejection" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
[Ledger SimpleTx]
-> (Ledger SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Ledger SimpleTx
simpleLedger, Ledger SimpleTx
rejectingLedger] ((Ledger SimpleTx -> Expectation) -> Expectation)
-> (Ledger SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Ledger SimpleTx
ledger -> do
let (Outcome SimpleTx
_, [TraceStep]
trace) = InterpreterConfig SimpleTx
-> Program SimpleTx () -> (Outcome SimpleTx, [TraceStep])
forall tx.
InterpreterConfig tx -> Program tx () -> (Outcome tx, [TraceStep])
Interpreter.traceProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx)
[()] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [() | Trace.LedgerApplied Bool
_ <- [TraceStep]
trace] Int -> Int -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Int
1
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"rejects an output collision before querying the ledger" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let st :: OpenState SimpleTx
st = OpenState SimpleTx
emptyOpenState{coordinatedHeadState = emptyOpenState.coordinatedHeadState{localUTxO = utxoRef 42}}
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
Prelude.error Text
"output collision reaches the ledger"
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
ledger) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
bob TTL
1 OpenState SimpleTx
st SimpleTx
sampleTx)
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
(ValidationError -> WaitReason SimpleTx
forall tx. ValidationError -> WaitReason tx
WaitOnNotApplicableTx (ValidationError -> WaitReason SimpleTx)
-> ValidationError -> WaitReason SimpleTx
forall a b. (a -> b) -> a -> b
$ Text -> ValidationError
ValidationError Text
"transaction creates an output the head already holds")
[StateChanged SimpleTx
received]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reports ledger failure before testing a pending deposit collision" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let pending :: Map SimpleId (Deposit SimpleTx)
pending = SimpleId -> Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton SimpleId
7 (Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx))
-> Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx)
forall a b. (a -> b) -> a -> b
$ DepositStatus -> UTxOType SimpleTx -> Deposit SimpleTx
depositFixture DepositStatus
Active (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
42)
program :: Program SimpleTx ()
program =
ReqTxRule
runReqTx
ReqTx.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
ruleEnvironment Environment
testEnvironment
, $sel:chainPointTime:Args :: ChainPointTime
chainPointTime = ChainPointTime
zeroChainPointTime
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
emptyOpenState
, $sel:ttl:Args :: TTL
ttl = TTL
1
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
pending
, $sel:transaction:Args :: SimpleTx
transaction = SimpleTx
sampleTx
}
(Outcome SimpleTx
outcome, [TraceStep]
trace) = InterpreterConfig SimpleTx
-> Program SimpleTx () -> (Outcome SimpleTx, [TraceStep])
forall tx.
InterpreterConfig tx -> Program tx () -> (Outcome tx, [TraceStep])
Interpreter.traceProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
rejectingLedger) Program SimpleTx ()
program
Outcome SimpleTx
outcome 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 (ValidationError -> WaitReason SimpleTx
forall tx. ValidationError -> WaitReason tx
WaitOnNotApplicableTx ValidationError
ledgerFailure) [StateChanged SimpleTx
received]
[TraceStep]
trace [TraceStep] -> [TraceStep] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
`shouldNotContain` [Text -> Bool -> TraceStep
Trace.BooleanTraced Text
"outputs do not collide with pending deposits" Bool
False]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"tracing preserves successful, suspended, and timed-out outcomes" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
[Ledger SimpleTx]
-> (Ledger SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Ledger SimpleTx
simpleLedger, Ledger SimpleTx
rejectingLedger] ((Ledger SimpleTx -> Expectation) -> Expectation)
-> (Ledger SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Ledger SimpleTx
ledger ->
[TTL] -> (TTL -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [TTL
0, TTL
1] ((TTL -> Expectation) -> Expectation)
-> (TTL -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \TTL
ttl -> do
let program :: Program SimpleTx ()
program = Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
ttl OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx
(Outcome SimpleTx
outcome, [TraceStep]
trace) = InterpreterConfig SimpleTx
-> Program SimpleTx () -> (Outcome SimpleTx, [TraceStep])
forall tx.
InterpreterConfig tx -> Program tx () -> (Outcome tx, [TraceStep])
Interpreter.traceProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) Program SimpleTx ()
program
Outcome SimpleTx
outcome Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` 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
ledger) Program SimpleTx ()
program
[TraceStep]
trace [TraceStep] -> ([TraceStep] -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` (Bool -> Bool
not (Bool -> Bool) -> ([TraceStep] -> Bool) -> [TraceStep] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TraceStep] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reifying and lowering the actual rule preserves its outcome and trace" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
[Ledger SimpleTx]
-> (Ledger SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Ledger SimpleTx
simpleLedger, Ledger SimpleTx
rejectingLedger] ((Ledger SimpleTx -> Expectation) -> Expectation)
-> (Ledger SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Ledger SimpleTx
ledger ->
[Party] -> (Party -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Party
alice, Party
bob] ((Party -> Expectation) -> Expectation)
-> (Party -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Party
actor ->
[TTL] -> (TTL -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [TTL
0, TTL
1] ((TTL -> Expectation) -> Expectation)
-> (TTL -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \TTL
ttl -> do
let program :: Program SimpleTx ()
program = Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
actor TTL
ttl OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx
restored :: Program SimpleTx ()
restored = Cursor SimpleTx () -> Program SimpleTx ()
forall tx a. Cursor tx a -> Program tx a
lowerProgram (Cursor SimpleTx () -> Program SimpleTx ())
-> Cursor SimpleTx () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Program SimpleTx () -> Cursor SimpleTx ()
forall tx a. Program tx a -> Cursor tx a
Debug.cursor Program SimpleTx ()
program
InterpreterConfig SimpleTx
-> Program SimpleTx () -> (Outcome SimpleTx, [TraceStep])
forall tx.
InterpreterConfig tx -> Program tx () -> (Outcome tx, [TraceStep])
Interpreter.traceProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) Program SimpleTx ()
restored (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])
Interpreter.traceProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) Program SimpleTx ()
program
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"resumes the same reified ledger continuation with either reply" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let program :: Program SimpleTx ()
program = Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
emptyOpenState SimpleTx
sampleTx
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
Prelude.error Text
"resumed continuation queries the ledger again"
case Int -> Cursor SimpleTx () -> Maybe (LedgerPause SimpleTx)
forall tx. Int -> Cursor tx () -> Maybe (LedgerPause tx)
pauseAtLedger Int
4 (Cursor SimpleTx () -> Maybe (LedgerPause SimpleTx))
-> Cursor SimpleTx () -> Maybe (LedgerPause SimpleTx)
forall a b. (a -> b) -> a -> b
$ Program SimpleTx () -> Cursor SimpleTx ()
forall tx a. Program tx a -> Cursor tx a
Debug.cursor Program SimpleTx ()
program of
Maybe (LedgerPause SimpleTx)
Nothing -> String -> Expectation
forall (m :: * -> *) a.
(HasCallStack, MonadThrow m) =>
String -> m a
failure String
"ReqTx does not expose its ledger query within four instructions"
Just (LedgerPause ChainSlot
slot UTxOType SimpleTx
utxo SimpleTx
tx Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ()
resume) -> do
ChainSlot
slot ChainSlot -> ChainSlot -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` ChainSlot
0
Set SimpleTxOut
UTxOType SimpleTx
utxo Set SimpleTxOut -> Set SimpleTxOut -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Set SimpleTxOut
forall a. Monoid a => a
mempty
SimpleTx
tx SimpleTx -> SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SimpleTx
sampleTx
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
ledger) (Cursor SimpleTx () -> Program SimpleTx ()
forall tx a. Cursor tx a -> Program tx a
lowerProgram (Cursor SimpleTx () -> Program SimpleTx ())
-> Cursor SimpleTx () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ()
resume (Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ())
-> Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ()
forall a b. (a -> b) -> a -> b
$ UTxOType SimpleTx -> Either ValidationError (UTxOType SimpleTx)
forall a b. b -> Either a b
Right (UTxOType SimpleTx -> Either ValidationError (UTxOType SimpleTx))
-> UTxOType SimpleTx -> Either ValidationError (UTxOType SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
42)
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
[StateChanged SimpleTx
applied, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1]
[Message SimpleTx -> Effect SimpleTx
forall tx. Message tx -> Effect tx
NetworkEffect (Message SimpleTx -> Effect SimpleTx)
-> Message SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [SimpleTx -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
sampleTx] Maybe SimpleTx
forall a. Maybe a
Nothing Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing]
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
ledger) (Cursor SimpleTx () -> Program SimpleTx ()
forall tx a. Cursor tx a -> Program tx a
lowerProgram (Cursor SimpleTx () -> Program SimpleTx ())
-> Cursor SimpleTx () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ()
resume (Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ())
-> Either ValidationError (UTxOType SimpleTx) -> Cursor SimpleTx ()
forall a b. (a -> b) -> a -> b
$ ValidationError -> Either ValidationError (Set SimpleTxOut)
forall a b. a -> Either a b
Left ValidationError
ledgerFailure)
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 (ValidationError -> WaitReason SimpleTx
forall tx. ValidationError -> WaitReason tx
WaitOnNotApplicableTx ValidationError
ledgerFailure) []
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"handles deposit collisions across status, settlement, and retry budget" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
[DepositStatus] -> (DepositStatus -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [DepositStatus
Inactive, DepositStatus
Active, DepositStatus
Expired] ((DepositStatus -> Expectation) -> Expectation)
-> (DepositStatus -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \DepositStatus
status ->
[Bool] -> (Bool -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> Expectation) -> Expectation)
-> (Bool -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Bool
settling ->
[Bool] -> (Bool -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> Expectation) -> Expectation)
-> (Bool -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Bool
colliding ->
[TTL] -> (TTL -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [TTL
0, TTL
1] ((TTL -> Expectation) -> Expectation)
-> (TTL -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \TTL
ttl -> do
let deposited :: UTxOType SimpleTx
deposited = SimpleId -> UTxOType SimpleTx
utxoRef (SimpleId -> UTxOType SimpleTx) -> SimpleId -> UTxOType SimpleTx
forall a b. (a -> b) -> a -> b
$ if Bool
colliding then SimpleId
42 else SimpleId
99
deposit :: Deposit SimpleTx
deposit = DepositStatus -> UTxOType SimpleTx -> Deposit SimpleTx
depositFixture DepositStatus
status UTxOType SimpleTx
deposited
pending :: Map SimpleId (Deposit SimpleTx)
pending = SimpleId -> Deposit SimpleTx -> Map SimpleId (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton SimpleId
7 Deposit SimpleTx
deposit
confirmed :: ConfirmedSnapshot SimpleTx
confirmed
| Bool
settling = UTxOType SimpleTx -> ConfirmedSnapshot SimpleTx
settlingSnapshot UTxOType SimpleTx
deposited
| Bool
otherwise = HeadId -> ConfirmedSnapshot SimpleTx
forall tx. HeadId -> ConfirmedSnapshot tx
InitialSnapshot HeadId
testHeadId
st :: OpenState SimpleTx
st = OpenState SimpleTx
emptyOpenState{coordinatedHeadState = emptyOpenState.coordinatedHeadState{confirmedSnapshot = confirmed}}
program :: Program SimpleTx ()
program =
ReqTxRule
runReqTx
ReqTx.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
ruleEnvironment Environment
testEnvironment
, $sel:chainPointTime:Args :: ChainPointTime
chainPointTime = ChainPointTime
zeroChainPointTime
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
st
, $sel:ttl:Args :: TTL
ttl = TTL
ttl
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
pending
, $sel:transaction:Args :: SimpleTx
transaction = SimpleTx
sampleTx
}
outcome :: Outcome SimpleTx
outcome = 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
err :: ValidationError
err = Text -> ValidationError
ValidationError Text
"transaction creates an output a pending deposit names"
if Bool
colliding Bool -> Bool -> Bool
&& (DepositStatus
status DepositStatus -> DepositStatus -> Bool
forall a. Eq a => a -> a -> Bool
/= DepositStatus
Expired Bool -> Bool -> Bool
|| Bool
settling)
then
Outcome SimpleTx
outcome
Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` if TTL
ttl TTL -> TTL -> Bool
forall a. Ord a => a -> a -> Bool
> TTL
0
then WaitReason SimpleTx -> [StateChanged SimpleTx] -> Outcome SimpleTx
forall tx. WaitReason tx -> [StateChanged tx] -> Outcome tx
Wait (ValidationError -> WaitReason SimpleTx
forall tx. ValidationError -> WaitReason tx
WaitOnNotApplicableTx ValidationError
err) [StateChanged SimpleTx
received]
else [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [StateChanged SimpleTx
received, TxInvalid{$sel:headId:NetworkConnected :: HeadId
headId = HeadId
testHeadId, $sel:utxo:NetworkConnected :: UTxOType SimpleTx
utxo = Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty, $sel:transaction:NetworkConnected :: SimpleTx
transaction = SimpleTx
sampleTx, $sel:validationError:NetworkConnected :: ValidationError
validationError = ValidationError
err}] []
else case Outcome SimpleTx
outcome of
Continue{[StateChanged SimpleTx]
stateChanges :: [StateChanged SimpleTx]
$sel:stateChanges:Continue :: forall tx. Outcome tx -> [StateChanged tx]
stateChanges} -> Int -> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. Int -> [a] -> [a]
take Int
2 [StateChanged SimpleTx]
stateChanges [StateChanged SimpleTx] -> [StateChanged SimpleTx] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [StateChanged SimpleTx
received, StateChanged SimpleTx
applied]
Outcome SimpleTx
other -> String -> Expectation
forall (m :: * -> *) a.
(HasCallStack, MonadThrow m) =>
String -> m a
failure (String -> Expectation) -> String -> Expectation
forall a b. (a -> b) -> a -> b
$ String
"expected transaction application, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Outcome SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Outcome SimpleTx
other
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"carries an existing decommit in the next request" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let decommit :: SimpleTx
decommit = SimpleId -> SimpleTx
aValidTx SimpleId
99
st :: OpenState SimpleTx
st = OpenState SimpleTx
emptyOpenState{coordinatedHeadState = emptyOpenState.coordinatedHeadState{decommitTx = Just decommit}}
outcome :: Outcome SimpleTx
outcome = 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 () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
alice TTL
1 OpenState SimpleTx
st SimpleTx
sampleTx
Outcome SimpleTx
outcome
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
[StateChanged SimpleTx
received, StateChanged SimpleTx
applied, SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1]
[Message SimpleTx -> Effect SimpleTx
forall tx. Message tx -> Effect tx
NetworkEffect (Message SimpleTx -> Effect SimpleTx)
-> Message SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [SimpleTx -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
sampleTx] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing]
String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"ReqTx Agda agreement" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"compares terminal outcome and ordered change/effect tags over the bounded model" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
[Bool] -> (Bool -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> Expectation) -> Expectation)
-> (Bool -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Bool
applicable ->
[SimpleId] -> (SimpleId -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SimpleId
0, SimpleId
1] ((SimpleId -> Expectation) -> Expectation)
-> (SimpleId -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \SimpleId
ttl ->
[Bool] -> (Bool -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> Expectation) -> Expectation)
-> (Bool -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Bool
inFlight ->
[(SimpleId, Party)]
-> ((SimpleId, Party) -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ ([SimpleId] -> [Party] -> [(SimpleId, Party)]
forall a b. [a] -> [b] -> [(a, b)]
zip [SimpleId
0 ..] [Party]
testParties) (((SimpleId, Party) -> Expectation) -> Expectation)
-> ((SimpleId, Party) -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \(SimpleId
partyIndex, Party
actor) ->
[SimpleId] -> (SimpleId -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SimpleId
1 .. SimpleId
6] ((SimpleId -> Expectation) -> Expectation)
-> (SimpleId -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \SimpleId
nextNumber -> do
let previousNumber :: SnapshotNumber
previousNumber = SimpleId -> SnapshotNumber
forall a. Num a => SimpleId -> a
fromInteger (SimpleId
nextNumber SimpleId -> SimpleId -> SimpleId
forall a. Num a => a -> a -> a
- SimpleId
1)
confirmed :: ConfirmedSnapshot SimpleTx
confirmed
| SnapshotNumber
previousNumber SnapshotNumber -> SnapshotNumber -> Bool
forall a. Eq a => a -> a -> Bool
== SnapshotNumber
0 = HeadId -> ConfirmedSnapshot SimpleTx
forall tx. HeadId -> ConfirmedSnapshot tx
InitialSnapshot HeadId
testHeadId
| Bool
otherwise = Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot (SnapshotNumber
-> SnapshotVersion
-> [SimpleTx]
-> UTxOType SimpleTx
-> Snapshot SimpleTx
forall tx.
IsTx tx =>
SnapshotNumber
-> SnapshotVersion -> [tx] -> UTxOType tx -> Snapshot tx
testSnapshot SnapshotNumber
previousNumber SnapshotVersion
0 [] Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty) MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty
seen :: SeenSnapshot SimpleTx
seen
| Bool
inFlight = SnapshotNumber -> SnapshotNumber -> SeenSnapshot SimpleTx
forall tx. SnapshotNumber -> SnapshotNumber -> SeenSnapshot tx
RequestedSnapshot SnapshotNumber
previousNumber (SimpleId -> SnapshotNumber
forall a. Num a => SimpleId -> a
fromInteger SimpleId
nextNumber)
| Bool
otherwise = SnapshotNumber -> SeenSnapshot SimpleTx
forall tx. SnapshotNumber -> SeenSnapshot tx
LastSeenSnapshot SnapshotNumber
previousNumber
st :: OpenState SimpleTx
st =
OpenState SimpleTx
emptyOpenState
{ coordinatedHeadState =
emptyOpenState.coordinatedHeadState
{ confirmedSnapshot = confirmed
, seenSnapshot = seen
}
}
ledger :: Ledger SimpleTx
ledger = if Bool
applicable then Ledger SimpleTx
simpleLedger else Ledger SimpleTx
rejectingLedger
outcome :: Outcome SimpleTx
outcome = 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
ledger) (Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
actor (SimpleId -> TTL
forall a. Num a => SimpleId -> a
fromInteger SimpleId
ttl) OpenState SimpleTx
st SimpleTx
sampleTx)
expected :: HsReqTxOutcome
expected = Bool
-> SimpleId
-> Bool
-> SimpleId
-> SimpleId
-> SimpleId
-> HsReqTxOutcome
reqTxOutcomeRef Bool
applicable SimpleId
ttl Bool
inFlight SimpleId
2 SimpleId
nextNumber SimpleId
partyIndex
Outcome SimpleTx -> Either String HsReqTxOutcome
projectReqTxOutcome Outcome SimpleTx
outcome Either String HsReqTxOutcome
-> Either String HsReqTxOutcome -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` HsReqTxOutcome -> Either String HsReqTxOutcome
forall a b. b -> Either a b
Right HsReqTxOutcome
expected
where
reqTxProgram :: Party -> TTL -> OpenState SimpleTx -> SimpleTx -> Program.Program SimpleTx ()
reqTxProgram :: Party
-> TTL -> OpenState SimpleTx -> SimpleTx -> Program SimpleTx ()
reqTxProgram Party
actor TTL
ttl OpenState SimpleTx
st SimpleTx
transaction =
ReqTxRule
runReqTx
ReqTx.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
ruleEnvironment Environment
testEnvironment{party = actor}
, $sel:chainPointTime:Args :: ChainPointTime
chainPointTime = ChainPointTime
zeroChainPointTime
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
st
, $sel:ttl:Args :: TTL
ttl = TTL
ttl
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty
, $sel:transaction:Args :: SimpleTx
transaction = SimpleTx
transaction
}
testParties :: [Party]
testParties :: [Party]
testParties = [Party
alice, Party
bob, Party
carol]
emptyOpenState :: OpenState SimpleTx
emptyOpenState :: OpenState SimpleTx
emptyOpenState = case ([Party] -> NodeState SimpleTx
inOpenState [Party]
testParties).headState of
Open OpenState SimpleTx
st -> OpenState SimpleTx
st
HeadState SimpleTx
_ -> Text -> OpenState SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
Prelude.error Text
"inOpenState does not construct an open head"
sampleTx :: SimpleTx
sampleTx :: SimpleTx
sampleTx = SimpleId -> SimpleTx
aValidTx SimpleId
42
received, applied :: StateChanged SimpleTx
received :: StateChanged SimpleTx
received = SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
sampleTx
applied :: StateChanged SimpleTx
applied = HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
TransactionAppliedToLocalUTxO HeadId
testHeadId SimpleTx
sampleTx
ledgerFailure :: ValidationError
ledgerFailure :: ValidationError
ledgerFailure = Text -> ValidationError
ValidationError Text
"controlled ledger rejection"
rejectingLedger :: Ledger SimpleTx
rejectingLedger :: Ledger SimpleTx
rejectingLedger = (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]
txs -> case [SimpleTx]
txs of
[SimpleTx
tx] -> (SimpleTx, ValidationError)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. a -> Either a b
Left (SimpleTx
tx, ValidationError
ledgerFailure)
[SimpleTx]
_ -> Text -> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a t. (HasCallStack, IsText t) => t -> a
Prelude.error Text
"ReqTx sends more than one transaction to the ledger"
depositFixture :: DepositStatus -> UTxOType SimpleTx -> Deposit SimpleTx
depositFixture :: DepositStatus -> UTxOType SimpleTx -> Deposit SimpleTx
depositFixture DepositStatus
status UTxOType SimpleTx
deposited =
Deposit{$sel:headId:Deposit :: HeadId
headId = HeadId
testHeadId, UTxOType SimpleTx
deposited :: UTxOType SimpleTx
$sel:deposited:Deposit :: UTxOType SimpleTx
deposited, $sel:created:Deposit :: UTCTime
created = UTCTime
initialChainTime, $sel:deadline:Deposit :: UTCTime
deadline = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
3600 UTCTime
initialChainTime, DepositStatus
status :: DepositStatus
$sel:status:Deposit :: DepositStatus
status}
settlingSnapshot :: UTxOType SimpleTx -> ConfirmedSnapshot SimpleTx
settlingSnapshot :: UTxOType SimpleTx -> ConfirmedSnapshot SimpleTx
settlingSnapshot UTxOType SimpleTx
deposited =
Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot
(Snapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => Snapshot tx -> Snapshot tx
withAccumulators (Snapshot SimpleTx -> Snapshot SimpleTx)
-> Snapshot SimpleTx -> Snapshot SimpleTx
forall a b. (a -> b) -> a -> b
$ (SnapshotNumber
-> SnapshotVersion
-> [SimpleTx]
-> UTxOType SimpleTx
-> Snapshot SimpleTx
forall tx.
IsTx tx =>
SnapshotNumber
-> SnapshotVersion -> [tx] -> UTxOType tx -> Snapshot tx
testSnapshot SnapshotNumber
0 SnapshotVersion
0 [] Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty){utxoToCommit = Just deposited, depositTxId = Just 7})
MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty
rejectLedgerQuery :: Program.Instruction SimpleTx a -> Program.Program SimpleTx a
rejectLedgerQuery :: forall x. Instruction SimpleTx x -> Program SimpleTx x
rejectLedgerQuery = \case
Program.ApplyTransaction{} -> a -> Program SimpleTx a
forall a. a -> Program SimpleTx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Program SimpleTx a) -> a -> Program SimpleTx a
forall a b. (a -> b) -> a -> b
$ ValidationError -> Either ValidationError (Set SimpleTxOut)
forall a b. a -> Either a b
Left ValidationError
ledgerFailure
Instruction SimpleTx a
op -> Instruction SimpleTx a -> Program SimpleTx a
forall tx a. Instruction tx a -> Program tx a
instruction Instruction SimpleTx a
op
projectReqTxOutcome :: Outcome SimpleTx -> Either String HsReqTxOutcome
projectReqTxOutcome :: Outcome SimpleTx -> Either String HsReqTxOutcome
projectReqTxOutcome = \case
Continue{[StateChanged SimpleTx]
$sel:stateChanges:Continue :: forall tx. Outcome tx -> [StateChanged tx]
stateChanges :: [StateChanged SimpleTx]
stateChanges, [Effect SimpleTx]
effects :: [Effect SimpleTx]
$sel:effects:Continue :: forall tx. Outcome tx -> [Effect tx]
effects} ->
HsReqTxTerminal
-> [HsReqTxChange] -> [HsReqTxEffect] -> HsReqTxOutcome
HsReqTxOutcome HsReqTxTerminal
ReqTxFinished ([HsReqTxChange] -> [HsReqTxEffect] -> HsReqTxOutcome)
-> Either String [HsReqTxChange]
-> Either String ([HsReqTxEffect] -> HsReqTxOutcome)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (StateChanged SimpleTx -> Either String HsReqTxChange)
-> [StateChanged SimpleTx] -> Either String [HsReqTxChange]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse StateChanged SimpleTx -> Either String HsReqTxChange
projectChange [StateChanged SimpleTx]
stateChanges Either String ([HsReqTxEffect] -> HsReqTxOutcome)
-> Either String [HsReqTxEffect] -> Either String HsReqTxOutcome
forall a b.
Either String (a -> b) -> Either String a -> Either String b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Effect SimpleTx -> Either String HsReqTxEffect)
-> [Effect SimpleTx] -> Either String [HsReqTxEffect]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Effect SimpleTx -> Either String HsReqTxEffect
projectEffect [Effect SimpleTx]
effects
Wait{$sel:reason:Continue :: forall tx. Outcome tx -> WaitReason tx
reason = WaitOnNotApplicableTx ValidationError
err, [StateChanged SimpleTx]
$sel:stateChanges:Continue :: forall tx. Outcome tx -> [StateChanged tx]
stateChanges :: [StateChanged SimpleTx]
stateChanges}
| ValidationError
err ValidationError -> ValidationError -> Bool
forall a. Eq a => a -> a -> Bool
== ValidationError
ledgerFailure ->
HsReqTxTerminal
-> [HsReqTxChange] -> [HsReqTxEffect] -> HsReqTxOutcome
HsReqTxOutcome HsReqTxTerminal
ReqTxWaiting ([HsReqTxChange] -> [HsReqTxEffect] -> HsReqTxOutcome)
-> Either String [HsReqTxChange]
-> Either String ([HsReqTxEffect] -> HsReqTxOutcome)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (StateChanged SimpleTx -> Either String HsReqTxChange)
-> [StateChanged SimpleTx] -> Either String [HsReqTxChange]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse StateChanged SimpleTx -> Either String HsReqTxChange
projectChange [StateChanged SimpleTx]
stateChanges Either String ([HsReqTxEffect] -> HsReqTxOutcome)
-> Either String [HsReqTxEffect] -> Either String HsReqTxOutcome
forall a b.
Either String (a -> b) -> Either String a -> Either String b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [HsReqTxEffect] -> Either String [HsReqTxEffect]
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
Outcome SimpleTx
other -> String -> Either String HsReqTxOutcome
forall a b. a -> Either a b
Left (String -> Either String HsReqTxOutcome)
-> String -> Either String HsReqTxOutcome
forall a b. (a -> b) -> a -> b
$ String
"unmodeled outcome: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Outcome SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Outcome SimpleTx
other
where
projectChange :: StateChanged SimpleTx -> Either String HsReqTxChange
projectChange :: StateChanged SimpleTx -> Either String HsReqTxChange
projectChange = \case
TransactionReceived{} -> HsReqTxChange -> Either String HsReqTxChange
forall a b. b -> Either a b
Right HsReqTxChange
ReqTxReceived
TransactionAppliedToLocalUTxO{} -> HsReqTxChange -> Either String HsReqTxChange
forall a b. b -> Either a b
Right HsReqTxChange
ReqTxApplied
TxInvalid{} -> HsReqTxChange -> Either String HsReqTxChange
forall a b. b -> Either a b
Right HsReqTxChange
ReqTxInvalid
SnapshotRequestDecided{} -> HsReqTxChange -> Either String HsReqTxChange
forall a b. b -> Either a b
Right HsReqTxChange
ReqTxSnapshotDecided
StateChanged SimpleTx
other -> String -> Either String HsReqTxChange
forall a b. a -> Either a b
Left (String -> Either String HsReqTxChange)
-> String -> Either String HsReqTxChange
forall a b. (a -> b) -> a -> b
$ String
"unmodeled change: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> StateChanged SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show StateChanged SimpleTx
other
projectEffect :: Effect SimpleTx -> Either String HsReqTxEffect
projectEffect :: Effect SimpleTx -> Either String HsReqTxEffect
projectEffect = \case
NetworkEffect ReqSn{} -> HsReqTxEffect -> Either String HsReqTxEffect
forall a b. b -> Either a b
Right HsReqTxEffect
ReqTxRequestSnapshot
Effect SimpleTx
other -> String -> Either String HsReqTxEffect
forall a b. a -> Either a b
Left (String -> Either String HsReqTxEffect)
-> String -> Either String HsReqTxEffect
forall a b. (a -> b) -> a -> b
$ String
"unmodeled effect: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Effect SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Effect SimpleTx
other
lowerProgram :: Debug.Cursor tx a -> Program.Program tx a
lowerProgram :: forall tx a. Cursor tx a -> Program tx a
lowerProgram = \case
Debug.Done a
result -> a -> Program tx a
forall a. a -> Program tx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result
Debug.Await Instruction tx x
op x -> Cursor tx a
resume -> Instruction tx x -> Program tx x
forall tx a. Instruction tx a -> Program tx a
instruction Instruction tx x
op Program tx x -> (x -> Program tx a) -> Program tx a
forall a b. Program tx a -> (a -> Program tx b) -> Program tx b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Cursor tx a -> Program tx a
forall tx a. Cursor tx a -> Program tx a
lowerProgram (Cursor tx a -> Program tx a)
-> (x -> Cursor tx a) -> x -> Program tx a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Cursor tx a
resume
data LedgerPause tx
= LedgerPause
ChainSlot
(UTxOType tx)
tx
(Either ValidationError (UTxOType tx) -> Debug.Cursor tx ())
pauseAtLedger :: Int -> Debug.Cursor tx () -> Maybe (LedgerPause tx)
pauseAtLedger :: forall tx. Int -> Cursor tx () -> Maybe (LedgerPause tx)
pauseAtLedger Int
fuel Cursor tx ()
view
| Int
fuel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Maybe (LedgerPause tx)
forall a. Maybe a
Nothing
| Bool
otherwise = case Cursor tx ()
view of
Debug.Await Instruction tx x
op x -> Cursor tx ()
resume -> case Instruction tx x
op of
Program.ApplyTransaction ChainSlot
slot UTxOType tx
utxo tx
tx -> LedgerPause tx -> Maybe (LedgerPause tx)
forall a. a -> Maybe a
Just (LedgerPause tx -> Maybe (LedgerPause tx))
-> LedgerPause tx -> Maybe (LedgerPause tx)
forall a b. (a -> b) -> a -> b
$ ChainSlot
-> UTxOType tx
-> tx
-> (Either ValidationError (UTxOType tx) -> Cursor tx ())
-> LedgerPause tx
forall tx.
ChainSlot
-> UTxOType tx
-> tx
-> (Either ValidationError (UTxOType tx) -> Cursor tx ())
-> LedgerPause tx
LedgerPause ChainSlot
slot UTxOType tx
utxo tx
tx x -> Cursor tx ()
Either ValidationError (UTxOType tx) -> Cursor tx ()
resume
Program.Change{} -> Int -> Cursor tx () -> Maybe (LedgerPause tx)
forall tx. Int -> Cursor tx () -> Maybe (LedgerPause tx)
pauseAtLedger (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (x -> Cursor tx ()
resume ())
Program.TraceBoolean Text
_ Bool
answer -> Int -> Cursor tx () -> Maybe (LedgerPause tx)
forall tx. Int -> Cursor tx () -> Maybe (LedgerPause tx)
pauseAtLedger (Int
fuel Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (x -> Cursor tx ()
resume x
Bool
answer)
Instruction tx x
_ -> Maybe (LedgerPause tx)
forall a. Maybe a
Nothing
Debug.Done{} -> Maybe (LedgerPause tx)
forall a. Maybe a
Nothing
waitReason :: WaitReason SimpleTx
waitReason :: WaitReason SimpleTx
waitReason = SnapshotNumber -> WaitReason SimpleTx
forall tx. SnapshotNumber -> WaitReason tx
WaitOnSnapshotNumber SnapshotNumber
1