{-# LANGUAGE OverloadedRecordDot #-}

module Hydra.HeadLogic.InterpreterSpec (spec) where

import Hydra.HeadLogic.Interpreter qualified as Interpreter
import Hydra.HeadLogic.Program (
  Instruction (ApplyTransaction),
  Program,
  applyTransaction,
  change,
  emit,
  foldProgram,
  instruction,
  reject,
  signSnapshot,
  suspend,
  traceB,
  verifySnapshot,
 )
import Hydra.HeadLogic.Trace qualified as Trace
import Hydra.Ledger (Ledger (..), ValidationError (..))
import Hydra.Ledger.Simple (SimpleTx, simpleLedger)
import Hydra.Network.Message (Message (ReqTx))
import Hydra.Node.HeadLogic (
  Effect (NetworkEffect),
  LogicError (AssertionFailed),
  Outcome (..),
  StateChanged (TransactionAppliedToLocalUTxO, TransactionReceived),
  WaitReason (WaitOnSnapshotNumber),
 )
import Hydra.Prelude hiding (trace)
import Hydra.Prelude qualified as Prelude
import Hydra.Tx.Crypto (VerificationError (..), aggregate, getSignableRepresentation, getVerificationKey, sign)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (InitialSnapshot), Snapshot, getSnapshot)
import Test.Hydra.HeadLogic.Interpreter (testInterpreterConfig)
import Test.Hydra.Ledger.Simple (aValidTx, utxoRef)
import Test.Hydra.Prelude (Spec, describe, it, shouldBe)
import Test.Hydra.Tx.Fixture (aliceSk, testHeadId)

spec :: Spec
spec :: Spec
spec = do
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"program termination" (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
"preserves change and effect order on completion" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
            Effect SimpleTx -> Program SimpleTx ()
forall tx. Effect tx -> Program tx ()
emit Effect SimpleTx
firstEffect
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
applied
            Effect SimpleTx -> Program SimpleTx ()
forall tx. Effect tx -> Program tx ()
emit Effect SimpleTx
secondEffect
      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 -> 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] [Effect SimpleTx
firstEffect, Effect SimpleTx
secondEffect]

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"commits the change prefix and discards effects when suspended" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
            Effect SimpleTx -> Program SimpleTx ()
forall tx. Effect tx -> Program tx ()
emit Effect SimpleTx
firstEffect
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
applied
            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
"suspension evaluates its continuation"
      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 -> 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 WaitReason SimpleTx
waitReason [StateChanged SimpleTx
received, StateChanged SimpleTx
applied]

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"discards all changes and effects when rejected" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
            Effect SimpleTx -> Program SimpleTx ()
forall tx. Effect tx -> Program tx ()
emit Effect SimpleTx
firstEffect
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
applied
            Any
_ <- LogicError SimpleTx -> Program SimpleTx Any
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
            Text -> Program SimpleTx ()
forall a t. (HasCallStack, IsText t) => t -> a
Prelude.error Text
"rejection evaluates its continuation"
      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 -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` LogicError SimpleTx -> Outcome SimpleTx
forall tx. LogicError tx -> Outcome tx
Error LogicError SimpleTx
rejection

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"does not evaluate a rejection after suspension" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
            Any
_ <- WaitReason SimpleTx -> Program SimpleTx Any
forall tx a. WaitReason tx -> Program tx a
suspend WaitReason SimpleTx
waitReason
            LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
      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 -> 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 WaitReason SimpleTx
waitReason [StateChanged SimpleTx
received]

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves the traced Boolean when selecting its continuation" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      [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
predicate -> do
        let program :: Program SimpleTx ()
            program :: Program SimpleTx ()
program = do
              Bool
accepted <- Text -> Bool -> Program SimpleTx Bool
forall tx. Text -> Bool -> Program tx Bool
traceB Text
"example guard" Bool
predicate
              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
$ if Bool
accepted then StateChanged SimpleTx
applied else StateChanged SimpleTx
received
        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 -> 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 [if Bool
predicate then StateChanged SimpleTx
applied else StateChanged SimpleTx
received] []

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"passes the requested slot, UTxO, and transaction to the ledger" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let oldUTxO :: UTxOType SimpleTx
oldUTxO = Integer -> UTxOType SimpleTx
utxoRef Integer
10
          returnedUTxO :: UTxOType SimpleTx
returnedUTxO = Integer -> UTxOType SimpleTx
utxoRef Integer
20
          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
slot UTxOType SimpleTx
utxo [SimpleTx]
txs ->
            if ChainSlot
slot ChainSlot -> ChainSlot -> Bool
forall a. Eq a => a -> a -> Bool
== ChainSlot
7 Bool -> Bool -> Bool
&& Set SimpleTxOut
UTxOType SimpleTx
utxo Set SimpleTxOut -> Set SimpleTxOut -> Bool
forall a. Eq a => a -> a -> Bool
== Set SimpleTxOut
oldUTxO Bool -> Bool -> Bool
&& [SimpleTx]
txs [SimpleTx] -> [SimpleTx] -> Bool
forall a. Eq a => a -> a -> Bool
== [SimpleTx
sampleTx]
              then Set SimpleTxOut
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. b -> Either a b
Right Set SimpleTxOut
returnedUTxO
              else Text -> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a t. (HasCallStack, IsText t) => t -> a
Prelude.error Text
"ledger query arguments differ from the instruction"
          program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = 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
7 Set SimpleTxOut
UTxOType SimpleTx
oldUTxO SimpleTx
sampleTx
            if Either ValidationError (Set SimpleTxOut)
result Either ValidationError (Set SimpleTxOut)
-> Either ValidationError (Set SimpleTxOut) -> Bool
forall a. Eq a => a -> a -> Bool
== Set SimpleTxOut -> Either ValidationError (Set SimpleTxOut)
forall a b. b -> Either a b
Right Set SimpleTxOut
returnedUTxO
              then StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
applied
              else LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
      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` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [StateChanged SimpleTx
applied] []

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"returns ledger failures to the rule without rejecting the attempt" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = 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
sampleTx
            if Either ValidationError (Set SimpleTxOut)
result Either ValidationError (Set SimpleTxOut)
-> Either ValidationError (Set SimpleTxOut) -> Bool
forall a. Eq a => a -> a -> Bool
== ValidationError -> Either ValidationError (Set SimpleTxOut)
forall a b. a -> Either a b
Left ValidationError
ledgerFailure
              then StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
              else LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
      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) 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 [StateChanged SimpleTx
received] []

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"traces tentative work and the disposition that discards it" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
            Effect SimpleTx -> Program SimpleTx ()
forall tx. Effect tx -> Program tx ()
emit Effect SimpleTx
firstEffect
            LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
          (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
simpleLedger) Program SimpleTx ()
program
      Outcome SimpleTx
outcome 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 LogicError SimpleTx
rejection
      [TraceStep]
trace [TraceStep] -> [TraceStep] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [Text -> TraceStep
Trace.Changed Text
"transaction received", Text -> TraceStep
Trace.Emitted Text
"broadcast ReqTx", TraceStep
Trace.Rejected]

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"uses the configured signer and verifier" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let snapshot :: Snapshot SimpleTx
snapshot = ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot (HeadId -> ConfirmedSnapshot SimpleTx
forall tx. HeadId -> ConfirmedSnapshot tx
InitialSnapshot HeadId
testHeadId) :: Snapshot SimpleTx
          key :: VerificationKey HydraKey
key = Secret (SigningKey HydraKey) -> VerificationKey HydraKey
forall s k. HasVerificationKey s k => s -> VerificationKey k
getVerificationKey Secret (SigningKey HydraKey)
aliceSk
          signature :: Signature (Snapshot SimpleTx)
signature = 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
snapshot
          bytes :: ByteString
bytes = Snapshot SimpleTx -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation Snapshot SimpleTx
snapshot
          config :: InterpreterConfig SimpleTx
config =
            Interpreter.InterpreterConfig
              { $sel:transactionApplication:InterpreterConfig :: ChainSlot
-> UTxOType SimpleTx
-> SimpleTx
-> Either ValidationError (UTxOType SimpleTx)
Interpreter.transactionApplication = \ChainSlot
_ UTxOType SimpleTx
_ SimpleTx
_ -> Text -> Either ValidationError (Set SimpleTxOut)
forall a t. (HasCallStack, IsText t) => t -> a
Prelude.error Text
"unexpected ledger query"
              , $sel:snapshotSigner:InterpreterConfig :: SnapshotSigner SimpleTx
Interpreter.snapshotSigner = (Snapshot SimpleTx -> Signature (Snapshot SimpleTx))
-> VerificationKey HydraKey -> SnapshotSigner SimpleTx
forall tx.
(Snapshot tx -> Signature (Snapshot tx))
-> VerificationKey HydraKey -> SnapshotSigner tx
Interpreter.SnapshotSigner (Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk) VerificationKey HydraKey
key
              , $sel:snapshotVerifier:InterpreterConfig :: [VerificationKey HydraKey]
-> MultiSignature (Snapshot SimpleTx)
-> ByteString
-> Either VerificationError ()
Interpreter.snapshotVerifier = \[VerificationKey HydraKey]
vkeys MultiSignature (Snapshot SimpleTx)
signatures ByteString
contents ->
                  if [VerificationKey HydraKey]
vkeys [VerificationKey HydraKey] -> [VerificationKey HydraKey] -> Bool
forall a. Eq a => a -> a -> Bool
== [VerificationKey HydraKey
key] Bool -> Bool -> Bool
&& MultiSignature (Snapshot SimpleTx)
signatures MultiSignature (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx) -> Bool
forall a. Eq a => a -> a -> Bool
== [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Signature (Snapshot SimpleTx)
signature] Bool -> Bool -> Bool
&& ByteString
contents ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes
                    then () -> Either VerificationError ()
forall a b. b -> Either a b
Right ()
                    else VerificationError -> Either VerificationError ()
forall a b. a -> Either a b
Left (VerificationError -> Either VerificationError ())
-> VerificationError -> Either VerificationError ()
forall a b. (a -> b) -> a -> b
$ NonEmpty (VerificationKey HydraKey) -> VerificationError
InvalidSignatures (VerificationKey HydraKey
key VerificationKey HydraKey
-> [VerificationKey HydraKey]
-> NonEmpty (VerificationKey HydraKey)
forall a. a -> [a] -> NonEmpty a
:| [])
              }
          program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = do
            Signature (Snapshot SimpleTx)
signed <- Snapshot SimpleTx
-> Program SimpleTx (Signature (Snapshot SimpleTx))
forall tx. Snapshot tx -> Program tx (Signature (Snapshot tx))
signSnapshot Snapshot SimpleTx
snapshot
            Either VerificationError ()
result <- [VerificationKey HydraKey]
-> MultiSignature (Snapshot SimpleTx)
-> ByteString
-> Program SimpleTx (Either VerificationError ())
forall tx.
[VerificationKey HydraKey]
-> MultiSignature (Snapshot tx)
-> ByteString
-> Program tx (Either VerificationError ())
verifySnapshot [VerificationKey HydraKey
key] ([Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Signature (Snapshot SimpleTx)
signed]) ByteString
bytes
            case Either VerificationError ()
result of
              Right () -> StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
              Left VerificationError
_ -> LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
      Execution SimpleTx -> Outcome SimpleTx
forall tx. Execution tx -> Outcome tx
Interpreter.runExecution (InterpreterConfig SimpleTx
-> Program SimpleTx () -> Execution SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Execution tx
Interpreter.Execution InterpreterConfig SimpleTx
config 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 [StateChanged SimpleTx
received] []
      SnapshotSigner SimpleTx -> VerificationKey HydraKey
forall tx. SnapshotSigner tx -> VerificationKey HydraKey
Interpreter.signingIdentity InterpreterConfig SimpleTx
config.snapshotSigner VerificationKey HydraKey -> VerificationKey HydraKey -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` VerificationKey HydraKey
key

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"returns configured verification failures to the rule without rejecting the attempt" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let key :: VerificationKey HydraKey
key = Secret (SigningKey HydraKey) -> VerificationKey HydraKey
forall s k. HasVerificationKey s k => s -> VerificationKey k
getVerificationKey Secret (SigningKey HydraKey)
aliceSk
          snapshot :: Snapshot SimpleTx
snapshot = ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot (HeadId -> ConfirmedSnapshot SimpleTx
forall tx. HeadId -> ConfirmedSnapshot tx
InitialSnapshot HeadId
testHeadId) :: Snapshot SimpleTx
          signatures :: MultiSignature (Snapshot SimpleTx)
signatures = [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk Snapshot SimpleTx
snapshot]
      [VerificationError]
-> (VerificationError -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [NonEmpty (VerificationKey HydraKey) -> VerificationError
InvalidSignatures (VerificationKey HydraKey
key VerificationKey HydraKey
-> [VerificationKey HydraKey]
-> NonEmpty (VerificationKey HydraKey)
forall a. a -> [a] -> NonEmpty a
:| []), VerificationError
SignatureCountMismatch] ((VerificationError -> Expectation) -> Expectation)
-> (VerificationError -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \VerificationError
failure -> do
        let program :: Program SimpleTx ()
            program :: Program SimpleTx ()
program = do
              Either VerificationError ()
result <- [VerificationKey HydraKey]
-> MultiSignature (Snapshot SimpleTx)
-> ByteString
-> Program SimpleTx (Either VerificationError ())
forall tx.
[VerificationKey HydraKey]
-> MultiSignature (Snapshot tx)
-> ByteString
-> Program tx (Either VerificationError ())
verifySnapshot [VerificationKey HydraKey
key] MultiSignature (Snapshot SimpleTx)
signatures (Snapshot SimpleTx -> ByteString
forall a. SignableRepresentation a => a -> ByteString
getSignableRepresentation Snapshot SimpleTx
snapshot)
              if Either VerificationError ()
result Either VerificationError () -> Either VerificationError () -> Bool
forall a. Eq a => a -> a -> Bool
== VerificationError -> Either VerificationError ()
forall a b. a -> Either a b
Left VerificationError
failure
                then StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received
                else LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection
            config :: Interpreter.InterpreterConfig SimpleTx
            config :: InterpreterConfig SimpleTx
config = (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger){Interpreter.snapshotVerifier = \[VerificationKey HydraKey]
_ MultiSignature (Snapshot SimpleTx)
_ ByteString
_ -> VerificationError -> Either VerificationError ()
forall a b. a -> Either a b
Left VerificationError
failure}
        InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram InterpreterConfig SimpleTx
config 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 [StateChanged SimpleTx
received] []

    String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"can replace a ledger reply while preserving the program continuation" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
      let program :: Program SimpleTx ()
          program :: Program SimpleTx ()
program = 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
sampleTx
            (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
$ StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change StateChanged SimpleTx
received) (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
$ LogicError SimpleTx -> Program SimpleTx ()
forall tx a. LogicError tx -> Program tx a
reject LogicError SimpleTx
rejection) Either ValidationError (Set SimpleTxOut)
result
          replace :: Instruction SimpleTx a -> Program SimpleTx a
          replace :: forall a. Instruction SimpleTx a -> Program SimpleTx a
replace = \case
            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

      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)
        ((forall a. Instruction SimpleTx a -> Program SimpleTx a)
-> 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 a. Instruction SimpleTx a -> Program SimpleTx a
replace 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 [StateChanged SimpleTx
received] []

sampleTx :: SimpleTx
sampleTx :: SimpleTx
sampleTx = Integer -> SimpleTx
aValidTx Integer
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

firstEffect, secondEffect :: Effect SimpleTx
firstEffect :: Effect SimpleTx
firstEffect = 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
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
sampleTx
secondEffect :: Effect SimpleTx
secondEffect = 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
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx (Integer -> SimpleTx
aValidTx Integer
43)

waitReason :: WaitReason SimpleTx
waitReason :: WaitReason SimpleTx
waitReason = SnapshotNumber -> WaitReason SimpleTx
forall tx. SnapshotNumber -> WaitReason tx
WaitOnSnapshotNumber SnapshotNumber
1

rejection :: LogicError SimpleTx
rejection :: LogicError SimpleTx
rejection = Text -> LogicError SimpleTx
forall tx. Text -> LogicError tx
AssertionFailed Text
"test rejection"

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
"expected one ledger transaction"