{-# LANGUAGE OverloadedRecordDot #-} {-# OPTIONS_GHC -Wno-ambiguous-fields #-} module Hydra.HeadLogic.RulesSpec (spec) where import Hydra.HeadLogic.Program (traceB) import Hydra.Prelude import Test.Hydra.HeadLogic.Interpreter (testInterpreterConfig) import Test.Hydra.Prelude (Spec, describe, it, prop, shouldBe, shouldSatisfy) import Crem.Inspect (checkStates, inspectStates) import Crem.StateInspection.NoThunks (StateCheck (NotInspected)) import Crem.StateMachine qualified as Crem import Data.Map.Strict qualified as Map import Hydra.HeadLogic.Interpreter qualified as Interpreter import Hydra.HeadLogic.Rules.Network.ReqTx qualified as ReqTx import Hydra.HeadLogic.Trace (TraceStep (BooleanTraced)) import Hydra.Ledger.Simple (simpleLedger) import Hydra.Network.Message (Message (ReqTx), NetworkEvent (ReceivedMessage)) import Hydra.Node.Environment (Environment (party)) import Hydra.Node.HeadLogic (Input (NetworkInput), Outcome (Continue), StateChanged (DepositRecovered), aggregateState, ruleEnvironment, update) import Hydra.Node.State (NodeState (chainPointTime), initialChainTime) import Test.Hydra.HeadLogic.InputFixtures qualified as Inputs import Test.Hydra.HeadLogic.Scenario qualified as Scenario import Test.QuickCheck (counterexample, elements, forAll, vectorOf) spec :: Spec spec :: Spec spec = String -> Spec -> Spec forall a. HasCallStack => String -> SpecWith a -> SpecWith a describe String "free programs and crem phases" (Spec -> Spec) -> Spec -> Spec forall a b. (a -> b) -> a -> b $ do String -> Spec -> Spec forall a. HasCallStack => String -> SpecWith a -> SpecWith a describe String "workflow inspection" (Spec -> Spec) -> Spec -> Spec forall a b. (a -> b) -> a -> b $ do [(SimpleTx, Text)] -> ((SimpleTx, Text) -> Spec) -> Spec forall (t :: * -> *) (m :: * -> *) a b. (Foldable t, Monad m) => t a -> (a -> m b) -> m () forM_ [(SimpleTx Scenario.transactionOne, Text "CheckDeposits"), (SimpleTx Scenario.transactionTwo, Text "RetryOrInvalidate")] (((SimpleTx, Text) -> Spec) -> Spec) -> ((SimpleTx, Text) -> Spec) -> Spec forall a b. (a -> b) -> a -> b $ \(SimpleTx tx, Text phase) -> String -> IO () -> SpecWith (Arg (IO ())) forall a. (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) it (String "inspects the ledger-selected phase: " String -> String -> String forall a. Semigroup a => a -> a -> a <> Text -> String forall a. ToString a => a -> String toString Text phase) (IO () -> SpecWith (Arg (IO ()))) -> IO () -> SpecWith (Arg (IO ())) forall a b. (a -> b) -> a -> b $ do let before :: NodeState SimpleTx before = NodeState SimpleTx Scenario.initialNode machine :: StateMachineT (Program SimpleTx) () (Maybe ()) machine = BaseMachineT (Program SimpleTx) ReqTxTopology () (Maybe ()) -> StateMachineT (Program SimpleTx) () (Maybe ()) forall {vertex} (m :: * -> *) (topology :: Topology vertex) input output. BaseMachineT m topology input output -> StateMachineT m input output Crem.Executable (BaseMachineT (Program SimpleTx) ReqTxTopology () (Maybe ()) -> StateMachineT (Program SimpleTx) () (Maybe ())) -> BaseMachineT (Program SimpleTx) ReqTxTopology () (Maybe ()) -> StateMachineT (Program SimpleTx) () (Maybe ()) forall a b. (a -> b) -> a -> b $ Args SimpleTx -> BaseMachineT (Program SimpleTx) ReqTxTopology () (Maybe ()) forall tx. IsChainState tx => Args tx -> Workflow tx ReqTxTopology () ReqTx.workflow ReqTx.Args { $sel:env:Args :: RuleEnvironment env = Environment -> RuleEnvironment ruleEnvironment (Environment -> RuleEnvironment) -> Environment -> RuleEnvironment forall a b. (a -> b) -> a -> b $ Int -> Environment Scenario.environment Int 0 , $sel:chainPointTime:Args :: ChainPointTime chainPointTime = NodeState SimpleTx before.chainPointTime , $sel:openState:Args :: OpenState SimpleTx openState = NodeState SimpleTx -> OpenState SimpleTx Scenario.openState NodeState SimpleTx before , $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 tx } program :: Program SimpleTx () program = do (Maybe () _, StateMachineT (Program SimpleTx) () (Maybe ()) outputCheck) <- StateMachineT (Program SimpleTx) () (Maybe ()) -> () -> Program SimpleTx (Maybe (), StateMachineT (Program SimpleTx) () (Maybe ())) forall (m :: * -> *) a b. Monad m => StateMachineT m a b -> a -> m (b, StateMachineT m a b) Crem.run StateMachineT (Program SimpleTx) () (Maybe ()) machine () (Maybe () _, StateMachineT (Program SimpleTx) () (Maybe ()) selected) <- StateMachineT (Program SimpleTx) () (Maybe ()) -> () -> Program SimpleTx (Maybe (), StateMachineT (Program SimpleTx) () (Maybe ())) forall (m :: * -> *) a b. Monad m => StateMachineT m a b -> a -> m (b, StateMachineT m a b) Crem.run StateMachineT (Program SimpleTx) () (Maybe ()) outputCheck () Program SimpleTx Bool -> Program SimpleTx () forall (f :: * -> *) a. Functor f => f a -> f () void (Program SimpleTx Bool -> Program SimpleTx ()) -> Program SimpleTx Bool -> Program SimpleTx () forall a b. (a -> b) -> a -> b $ Text -> Bool -> Program SimpleTx Bool forall tx. Text -> Bool -> Program tx Bool traceB Text "selected phase" (StateMachineT (Program SimpleTx) () (Maybe ()) -> [([Component], Maybe Text)] forall (m :: * -> *) a b. StateMachineT m a b -> [([Component], Maybe Text)] inspectStates StateMachineT (Program SimpleTx) () (Maybe ()) selected [([Component], Maybe Text)] -> [([Component], Maybe Text)] -> Bool forall a. Eq a => a -> a -> Bool == [([], Text -> Maybe Text forall a. a -> Maybe a Just Text phase)]) (Outcome SimpleTx _, [TraceStep] steps) = 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 [TraceStep] steps [TraceStep] -> ([TraceStep] -> Bool) -> IO () forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO () `shouldSatisfy` TraceStep -> [TraceStep] -> Bool forall (f :: * -> *) a. (Foldable f, DisallowElem f, Eq a) => a -> f a -> Bool elem (Text -> Bool -> TraceStep BooleanTraced Text "selected phase" Bool True) [([Component], StateCheck)] checks <- Context -> StateMachineT (Program SimpleTx) () (Maybe ()) -> IO [([Component], StateCheck)] forall (m :: * -> *) a b. Context -> StateMachineT m a b -> IO [([Component], StateCheck)] checkStates [] StateMachineT (Program SimpleTx) () (Maybe ()) machine [([Component], StateCheck)] checks [([Component], StateCheck)] -> ([([Component], StateCheck)] -> Bool) -> IO () forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO () `shouldSatisfy` \case [([], StateCheck NotInspected)] -> Bool True [([Component], StateCheck)] _ -> Bool False String -> Spec -> Spec forall a. HasCallStack => String -> SpecWith a -> SpecWith a describe String "complete input workflow" (Spec -> Spec) -> Spec -> Spec forall a b. (a -> b) -> a -> b $ do [(Text, NodeState SimpleTx, Input SimpleTx)] -> ((Text, NodeState SimpleTx, Input SimpleTx) -> Spec) -> Spec forall (t :: * -> *) (m :: * -> *) a b. (Foldable t, Monad m) => t a -> (a -> m b) -> m () forM_ [(Text, NodeState SimpleTx, Input SimpleTx)] Inputs.examples (((Text, NodeState SimpleTx, Input SimpleTx) -> Spec) -> Spec) -> ((Text, NodeState SimpleTx, Input SimpleTx) -> Spec) -> Spec forall a b. (a -> b) -> a -> b $ \(Text name, NodeState SimpleTx before, Input SimpleTx input) -> String -> IO () -> SpecWith (Arg (IO ())) forall a. (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) it (Text -> String forall a. ToString a => a -> String toString Text name String -> String -> String forall a. Semigroup a => a -> a -> a <> String ": preserves execution, event replay and checkpoint consistency") (IO () -> SpecWith (Arg (IO ()))) -> IO () -> SpecWith (Arg (IO ())) forall a b. (a -> b) -> a -> b $ Environment -> UTCTime -> NodeState SimpleTx -> Input SimpleTx -> Either Text (Outcome SimpleTx, [TraceStep]) Inputs.checkInput (Int -> Environment Scenario.environment Int 0) UTCTime initialChainTime NodeState SimpleTx before Input SimpleTx input Either Text (Outcome SimpleTx, [TraceStep]) -> (Either Text (Outcome SimpleTx, [TraceStep]) -> Bool) -> IO () forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO () `shouldSatisfy` Either Text (Outcome SimpleTx, [TraceStep]) -> Bool forall a b. Either a b -> Bool isRight String -> Property -> Spec forall prop. (HasCallStack, Testable prop) => String -> prop -> Spec prop String "scheduled inputs preserve execution and replay consistency at every prefix" (Property -> Spec) -> Property -> Spec forall a b. (a -> b) -> a -> b $ Gen [Input SimpleTx] -> ([Input SimpleTx] -> Property) -> Property forall a prop. (Show a, Testable prop) => Gen a -> (a -> prop) -> Property forAll (Int -> Gen (Input SimpleTx) -> Gen [Input SimpleTx] forall a. Int -> Gen a -> Gen [a] vectorOf Int 20 (Gen (Input SimpleTx) -> Gen [Input SimpleTx]) -> Gen (Input SimpleTx) -> Gen [Input SimpleTx] forall a b. (a -> b) -> a -> b $ [Input SimpleTx] -> Gen (Input SimpleTx) forall a. HasCallStack => [a] -> Gen a elements [Input SimpleTx input | (Text _, NodeState SimpleTx _, Input SimpleTx input) <- [(Text, NodeState SimpleTx, Input SimpleTx)] Inputs.examples]) (([Input SimpleTx] -> Property) -> Property) -> ([Input SimpleTx] -> Property) -> Property forall a b. (a -> b) -> a -> b $ \[Input SimpleTx] inputs -> let result :: Either Text (NodeState SimpleTx) result = (NodeState SimpleTx -> Input SimpleTx -> Either Text (NodeState SimpleTx)) -> NodeState SimpleTx -> [Input SimpleTx] -> Either Text (NodeState SimpleTx) forall (t :: * -> *) (m :: * -> *) b a. (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b foldlM NodeState SimpleTx -> Input SimpleTx -> Either Text (NodeState SimpleTx) compareFullInput NodeState SimpleTx Scenario.initialNode [Input SimpleTx] inputs in String -> Bool -> Property forall prop. Testable prop => String -> prop -> Property counterexample (Either Text (NodeState SimpleTx) -> String forall b a. (Show a, IsString b) => a -> b show Either Text (NodeState SimpleTx) result) (Either Text (NodeState SimpleTx) -> Bool forall a b. Either a b -> Bool isRight Either Text (NodeState SimpleTx) result) String -> IO () -> SpecWith (Arg (IO ())) forall a. (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) it String "removes a previous head deposit after its recovery is observed" (IO () -> SpecWith (Arg (IO ()))) -> IO () -> SpecWith (Arg (IO ())) forall a b. (a -> b) -> a -> b $ do let witnesses :: [(NodeState SimpleTx, Input SimpleTx)] witnesses = [(NodeState SimpleTx before, Input SimpleTx input) | (Text "recover-previous-head", NodeState SimpleTx before, Input SimpleTx input) <- [(Text, NodeState SimpleTx, Input SimpleTx)] Inputs.examples] [(NodeState SimpleTx, Input SimpleTx)] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [(NodeState SimpleTx, Input SimpleTx)] witnesses Int -> Int -> IO () forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO () `shouldBe` Int 1 [(NodeState SimpleTx, Input SimpleTx)] -> ((NodeState SimpleTx, Input SimpleTx) -> IO ()) -> IO () forall (t :: * -> *) (m :: * -> *) a b. (Foldable t, Monad m) => t a -> (a -> m b) -> m () forM_ [(NodeState SimpleTx, Input SimpleTx)] witnesses (((NodeState SimpleTx, Input SimpleTx) -> IO ()) -> IO ()) -> ((NodeState SimpleTx, Input SimpleTx) -> IO ()) -> IO () forall a b. (a -> b) -> a -> b $ \(NodeState SimpleTx before, Input SimpleTx input) -> do let outcome :: Outcome SimpleTx outcome = Environment -> Ledger SimpleTx -> UTCTime -> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx forall tx. IsChainState tx => Environment -> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx update (Int -> Environment Scenario.environment Int 0) Ledger SimpleTx simpleLedger UTCTime initialChainTime NodeState SimpleTx before Input SimpleTx input Outcome SimpleTx outcome Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO () forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO () `shouldSatisfy` \case Continue [DepositRecovered{}] [] -> Bool True Outcome SimpleTx _ -> Bool False SimpleId -> Map SimpleId (Deposit SimpleTx) -> Bool forall k a. Ord k => k -> Map k a -> Bool Map.member SimpleId 42 (NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx forall tx. IsChainState tx => NodeState tx -> Outcome tx -> NodeState tx aggregateState NodeState SimpleTx before Outcome SimpleTx outcome).pendingDeposits Bool -> Bool -> IO () forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO () `shouldBe` Bool False String -> Property -> Spec forall prop. (HasCallStack, Testable prop) => String -> prop -> Spec prop String "dependent transactions, expiry and duplicates preserve replay consistency" (Property -> Spec) -> Property -> Spec forall a b. (a -> b) -> a -> b $ Gen [(SimpleTx, TTL)] -> ([(SimpleTx, TTL)] -> Property) -> Property forall a prop. (Show a, Testable prop) => Gen a -> (a -> prop) -> Property forAll (Int -> Gen (SimpleTx, TTL) -> Gen [(SimpleTx, TTL)] forall a. Int -> Gen a -> Gen [a] vectorOf Int 20 (Gen (SimpleTx, TTL) -> Gen [(SimpleTx, TTL)]) -> Gen (SimpleTx, TTL) -> Gen [(SimpleTx, TTL)] forall a b. (a -> b) -> a -> b $ (,) (SimpleTx -> TTL -> (SimpleTx, TTL)) -> Gen SimpleTx -> Gen (TTL -> (SimpleTx, TTL)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> [SimpleTx] -> Gen SimpleTx forall a. HasCallStack => [a] -> Gen a elements [SimpleTx Scenario.transactionOne, SimpleTx Scenario.transactionTwo] Gen (TTL -> (SimpleTx, TTL)) -> Gen TTL -> Gen (SimpleTx, TTL) forall a b. Gen (a -> b) -> Gen a -> Gen b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b <*> [TTL] -> Gen TTL forall a. HasCallStack => [a] -> Gen a elements [TTL 0, TTL 1]) (([(SimpleTx, TTL)] -> Property) -> Property) -> ([(SimpleTx, TTL)] -> Property) -> Property forall a b. (a -> b) -> a -> b $ \[(SimpleTx, TTL)] inputs -> let result :: Either Text (NodeState SimpleTx) result = (NodeState SimpleTx -> (SimpleTx, TTL) -> Either Text (NodeState SimpleTx)) -> NodeState SimpleTx -> [(SimpleTx, TTL)] -> Either Text (NodeState SimpleTx) forall (t :: * -> *) (m :: * -> *) b a. (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b foldlM NodeState SimpleTx -> (SimpleTx, TTL) -> Either Text (NodeState SimpleTx) compareInput NodeState SimpleTx Scenario.initialNode [(SimpleTx, TTL)] inputs in String -> Bool -> Property forall prop. Testable prop => String -> prop -> Property counterexample (Either Text (NodeState SimpleTx) -> String forall b a. (Show a, IsString b) => a -> b show Either Text (NodeState SimpleTx) result) (Bool -> Property) -> Bool -> Property forall a b. (a -> b) -> a -> b $ Either Text (NodeState SimpleTx) -> Bool forall a b. Either a b -> Bool isRight Either Text (NodeState SimpleTx) result where compareFullInput :: NodeState SimpleTx -> Input SimpleTx -> Either Text (NodeState SimpleTx) compareFullInput NodeState SimpleTx before Input SimpleTx input = do (Outcome SimpleTx outcome, [TraceStep] _) <- Environment -> UTCTime -> NodeState SimpleTx -> Input SimpleTx -> Either Text (Outcome SimpleTx, [TraceStep]) Inputs.checkInput (Int -> Environment Scenario.environment Int 0) UTCTime initialChainTime NodeState SimpleTx before Input SimpleTx input NodeState SimpleTx -> Either Text (NodeState SimpleTx) forall a. a -> Either Text a forall (f :: * -> *) a. Applicative f => a -> f a pure (NodeState SimpleTx -> Either Text (NodeState SimpleTx)) -> NodeState SimpleTx -> Either Text (NodeState SimpleTx) forall a b. (a -> b) -> a -> b $ NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx forall tx. IsChainState tx => NodeState tx -> Outcome tx -> NodeState tx aggregateState NodeState SimpleTx before Outcome SimpleTx outcome compareInput :: NodeState SimpleTx -> (SimpleTx, TTL) -> Either Text (NodeState SimpleTx) compareInput NodeState SimpleTx before (SimpleTx tx, TTL ttl) = NodeState SimpleTx -> Input SimpleTx -> Either Text (NodeState SimpleTx) compareFullInput NodeState SimpleTx before (Input SimpleTx -> Either Text (NodeState SimpleTx)) -> Input SimpleTx -> Either Text (NodeState SimpleTx) forall a b. (a -> b) -> a -> b $ TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx forall tx. TTL -> NetworkEvent (Message tx) -> Input tx NetworkInput TTL ttl (NetworkEvent (Message SimpleTx) -> Input SimpleTx) -> NetworkEvent (Message SimpleTx) -> Input SimpleTx forall a b. (a -> b) -> a -> b $ Party -> Message SimpleTx -> NetworkEvent (Message SimpleTx) forall msg. Party -> msg -> NetworkEvent msg ReceivedMessage (Int -> Environment Scenario.environment Int 0).party (Message SimpleTx -> NetworkEvent (Message SimpleTx)) -> Message SimpleTx -> NetworkEvent (Message SimpleTx) forall a b. (a -> b) -> a -> b $ SimpleTx -> Message SimpleTx forall tx. tx -> Message tx ReqTx SimpleTx tx