{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StrictData #-}

module Convex.TestingInterface.Trace (
  -- * Test run trace
  TestRunTrace (..),
  TestCategory (..),

  -- * Iteration trace
  IterationTrace (..),
  IterationStatus (..),

  -- * State transitions
  Transition (..),
  TransitionResult (..),

  -- * Transaction summary
  TxSummary (..),
  TxInputSummary (..),
  TxOutputSummary (..),

  -- * Value representation
  ValueSummary (..),
  AssetSummary (..),

  -- * Threat model trace
  ThreatModelTrace (..),
  ThreatModelTraceOutcome (..),
) where

import Data.Aeson (ToJSON (..), Value, object, (.=))
import Data.Text (Text)
import GHC.Generics (Generic)

{- | Complete trace of a test run (one QuickCheck property execution = N iterations).
Links to the Tasty test tree via 'trtTestId'.
-}
data TestRunTrace = TestRunTrace
  { TestRunTrace -> Int
trtTestId :: !Int
  -- ^ Tasty test ID (links to the @test_done@ event in the NDJSON stream)
  , TestRunTrace -> Text
trtTestName :: !Text
  -- ^ e.g. "Positive tests"
  , TestRunTrace -> [Text]
trtPath :: ![Text]
  -- ^ Tasty test path, e.g. @["MyContract", "Positive tests"]@
  , TestRunTrace -> TestCategory
trtCategory :: !TestCategory
  , TestRunTrace -> [IterationTrace]
trtIterations :: ![IterationTrace]
  }
  deriving (TestRunTrace -> TestRunTrace -> Bool
(TestRunTrace -> TestRunTrace -> Bool)
-> (TestRunTrace -> TestRunTrace -> Bool) -> Eq TestRunTrace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TestRunTrace -> TestRunTrace -> Bool
== :: TestRunTrace -> TestRunTrace -> Bool
$c/= :: TestRunTrace -> TestRunTrace -> Bool
/= :: TestRunTrace -> TestRunTrace -> Bool
Eq, Int -> TestRunTrace -> ShowS
[TestRunTrace] -> ShowS
TestRunTrace -> String
(Int -> TestRunTrace -> ShowS)
-> (TestRunTrace -> String)
-> ([TestRunTrace] -> ShowS)
-> Show TestRunTrace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TestRunTrace -> ShowS
showsPrec :: Int -> TestRunTrace -> ShowS
$cshow :: TestRunTrace -> String
show :: TestRunTrace -> String
$cshowList :: [TestRunTrace] -> ShowS
showList :: [TestRunTrace] -> ShowS
Show, (forall x. TestRunTrace -> Rep TestRunTrace x)
-> (forall x. Rep TestRunTrace x -> TestRunTrace)
-> Generic TestRunTrace
forall x. Rep TestRunTrace x -> TestRunTrace
forall x. TestRunTrace -> Rep TestRunTrace x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TestRunTrace -> Rep TestRunTrace x
from :: forall x. TestRunTrace -> Rep TestRunTrace x
$cto :: forall x. Rep TestRunTrace x -> TestRunTrace
to :: forall x. Rep TestRunTrace x -> TestRunTrace
Generic)

-- | Whether this test run is a positive or negative test property.
data TestCategory
  = Positive
  | Negative
  deriving (TestCategory -> TestCategory -> Bool
(TestCategory -> TestCategory -> Bool)
-> (TestCategory -> TestCategory -> Bool) -> Eq TestCategory
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TestCategory -> TestCategory -> Bool
== :: TestCategory -> TestCategory -> Bool
$c/= :: TestCategory -> TestCategory -> Bool
/= :: TestCategory -> TestCategory -> Bool
Eq, Int -> TestCategory -> ShowS
[TestCategory] -> ShowS
TestCategory -> String
(Int -> TestCategory -> ShowS)
-> (TestCategory -> String)
-> ([TestCategory] -> ShowS)
-> Show TestCategory
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TestCategory -> ShowS
showsPrec :: Int -> TestCategory -> ShowS
$cshow :: TestCategory -> String
show :: TestCategory -> String
$cshowList :: [TestCategory] -> ShowS
showList :: [TestCategory] -> ShowS
Show, (forall x. TestCategory -> Rep TestCategory x)
-> (forall x. Rep TestCategory x -> TestCategory)
-> Generic TestCategory
forall x. Rep TestCategory x -> TestCategory
forall x. TestCategory -> Rep TestCategory x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TestCategory -> Rep TestCategory x
from :: forall x. TestCategory -> Rep TestCategory x
$cto :: forall x. Rep TestCategory x -> TestCategory
to :: forall x. Rep TestCategory x -> TestCategory
Generic)

instance ToJSON TestCategory where
  toJSON :: TestCategory -> Value
toJSON TestCategory
Positive = Value
"positive"
  toJSON TestCategory
Negative = Value
"negative"

-- | Trace of a single QuickCheck iteration within a test run.
data IterationTrace = IterationTrace
  { IterationTrace -> Int
itIndex :: !Int
  -- ^ 0-based iteration number
  , IterationTrace -> IterationStatus
itStatus :: !IterationStatus
  , IterationTrace -> [Transition]
itTransitions :: ![Transition]
  -- ^ Ordered sequence of actions performed
  , IterationTrace -> [ThreatModelTrace]
itThreatModels :: ![ThreatModelTrace]
  {- ^ Threat model results applied to this iteration's transactions.
  Only populated for positive tests.
  -}
  }
  deriving (IterationTrace -> IterationTrace -> Bool
(IterationTrace -> IterationTrace -> Bool)
-> (IterationTrace -> IterationTrace -> Bool) -> Eq IterationTrace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IterationTrace -> IterationTrace -> Bool
== :: IterationTrace -> IterationTrace -> Bool
$c/= :: IterationTrace -> IterationTrace -> Bool
/= :: IterationTrace -> IterationTrace -> Bool
Eq, Int -> IterationTrace -> ShowS
[IterationTrace] -> ShowS
IterationTrace -> String
(Int -> IterationTrace -> ShowS)
-> (IterationTrace -> String)
-> ([IterationTrace] -> ShowS)
-> Show IterationTrace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> IterationTrace -> ShowS
showsPrec :: Int -> IterationTrace -> ShowS
$cshow :: IterationTrace -> String
show :: IterationTrace -> String
$cshowList :: [IterationTrace] -> ShowS
showList :: [IterationTrace] -> ShowS
Show, (forall x. IterationTrace -> Rep IterationTrace x)
-> (forall x. Rep IterationTrace x -> IterationTrace)
-> Generic IterationTrace
forall x. Rep IterationTrace x -> IterationTrace
forall x. IterationTrace -> Rep IterationTrace x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. IterationTrace -> Rep IterationTrace x
from :: forall x. IterationTrace -> Rep IterationTrace x
$cto :: forall x. Rep IterationTrace x -> IterationTrace
to :: forall x. Rep IterationTrace x -> IterationTrace
Generic)

-- | Outcome of a single iteration.
data IterationStatus
  = IterationSuccess
  | IterationFailure !Text
  | IterationDiscarded !Text
  deriving (IterationStatus -> IterationStatus -> Bool
(IterationStatus -> IterationStatus -> Bool)
-> (IterationStatus -> IterationStatus -> Bool)
-> Eq IterationStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IterationStatus -> IterationStatus -> Bool
== :: IterationStatus -> IterationStatus -> Bool
$c/= :: IterationStatus -> IterationStatus -> Bool
/= :: IterationStatus -> IterationStatus -> Bool
Eq, Int -> IterationStatus -> ShowS
[IterationStatus] -> ShowS
IterationStatus -> String
(Int -> IterationStatus -> ShowS)
-> (IterationStatus -> String)
-> ([IterationStatus] -> ShowS)
-> Show IterationStatus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> IterationStatus -> ShowS
showsPrec :: Int -> IterationStatus -> ShowS
$cshow :: IterationStatus -> String
show :: IterationStatus -> String
$cshowList :: [IterationStatus] -> ShowS
showList :: [IterationStatus] -> ShowS
Show, (forall x. IterationStatus -> Rep IterationStatus x)
-> (forall x. Rep IterationStatus x -> IterationStatus)
-> Generic IterationStatus
forall x. Rep IterationStatus x -> IterationStatus
forall x. IterationStatus -> Rep IterationStatus x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. IterationStatus -> Rep IterationStatus x
from :: forall x. IterationStatus -> Rep IterationStatus x
$cto :: forall x. Rep IterationStatus x -> IterationStatus
to :: forall x. Rep IterationStatus x -> IterationStatus
Generic)

{- | One step in an iteration: an action was performed, the model state changed,
and a transaction was (possibly) submitted.
-}
data Transition = Transition
  { Transition -> Int
trStepIndex :: !Int
  -- ^ 0-based index within the iteration
  , Transition -> Text
trAction :: !Text
  -- ^ @show@ of the @Action state@ value
  , Transition -> Value
trStateBefore :: !Value
  -- ^ @toJSON@ of the model state before @perform@
  , Transition -> Value
trStateAfter :: !Value
  -- ^ @toJSON@ of the model state after @perform@
  , Transition -> Maybe TxSummary
trTransaction :: !(Maybe TxSummary)
  {- ^ The transaction produced, if any. @Nothing@ if @perform@ failed
  before building a transaction.
  -}
  , Transition -> TransitionResult
trResult :: !TransitionResult
  }
  deriving (Transition -> Transition -> Bool
(Transition -> Transition -> Bool)
-> (Transition -> Transition -> Bool) -> Eq Transition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Transition -> Transition -> Bool
== :: Transition -> Transition -> Bool
$c/= :: Transition -> Transition -> Bool
/= :: Transition -> Transition -> Bool
Eq, Int -> Transition -> ShowS
[Transition] -> ShowS
Transition -> String
(Int -> Transition -> ShowS)
-> (Transition -> String)
-> ([Transition] -> ShowS)
-> Show Transition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Transition -> ShowS
showsPrec :: Int -> Transition -> ShowS
$cshow :: Transition -> String
show :: Transition -> String
$cshowList :: [Transition] -> ShowS
showList :: [Transition] -> ShowS
Show, (forall x. Transition -> Rep Transition x)
-> (forall x. Rep Transition x -> Transition) -> Generic Transition
forall x. Rep Transition x -> Transition
forall x. Transition -> Rep Transition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Transition -> Rep Transition x
from :: forall x. Transition -> Rep Transition x
$cto :: forall x. Rep Transition x -> Transition
to :: forall x. Rep Transition x -> Transition
Generic)

-- | Whether the transaction was successfully submitted to the mockchain.
data TransitionResult
  = -- | TxId as text
    TransitionSuccess !Text
  | -- | Error description
    TransitionFailure !Text
  deriving (TransitionResult -> TransitionResult -> Bool
(TransitionResult -> TransitionResult -> Bool)
-> (TransitionResult -> TransitionResult -> Bool)
-> Eq TransitionResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TransitionResult -> TransitionResult -> Bool
== :: TransitionResult -> TransitionResult -> Bool
$c/= :: TransitionResult -> TransitionResult -> Bool
/= :: TransitionResult -> TransitionResult -> Bool
Eq, Int -> TransitionResult -> ShowS
[TransitionResult] -> ShowS
TransitionResult -> String
(Int -> TransitionResult -> ShowS)
-> (TransitionResult -> String)
-> ([TransitionResult] -> ShowS)
-> Show TransitionResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TransitionResult -> ShowS
showsPrec :: Int -> TransitionResult -> ShowS
$cshow :: TransitionResult -> String
show :: TransitionResult -> String
$cshowList :: [TransitionResult] -> ShowS
showList :: [TransitionResult] -> ShowS
Show, (forall x. TransitionResult -> Rep TransitionResult x)
-> (forall x. Rep TransitionResult x -> TransitionResult)
-> Generic TransitionResult
forall x. Rep TransitionResult x -> TransitionResult
forall x. TransitionResult -> Rep TransitionResult x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TransitionResult -> Rep TransitionResult x
from :: forall x. TransitionResult -> Rep TransitionResult x
$cto :: forall x. Rep TransitionResult x -> TransitionResult
to :: forall x. Rep TransitionResult x -> TransitionResult
Generic)

{- | Compact representation of a transaction for visualization.
Values are structured JSON for full fidelity.
-}
data TxSummary = TxSummary
  { TxSummary -> Maybe Text
txsId :: !(Maybe Text)
  -- ^ TxId if submitted successfully, @Nothing@ otherwise
  , TxSummary -> [TxInputSummary]
txsInputs :: ![TxInputSummary]
  , TxSummary -> [TxOutputSummary]
txsOutputs :: ![TxOutputSummary]
  , TxSummary -> Maybe ValueSummary
txsMint :: !(Maybe ValueSummary)
  -- ^ Structured mint value, @Nothing@ if no minting
  , TxSummary -> Integer
txsFee :: !Integer
  -- ^ Fee in lovelace
  , TxSummary -> [Text]
txsSigners :: ![Text]
  -- ^ Required signer key hashes
  , TxSummary -> Maybe Text
txsValidRange :: !(Maybe Text)
  -- ^ Rendered validity interval
  }
  deriving (TxSummary -> TxSummary -> Bool
(TxSummary -> TxSummary -> Bool)
-> (TxSummary -> TxSummary -> Bool) -> Eq TxSummary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxSummary -> TxSummary -> Bool
== :: TxSummary -> TxSummary -> Bool
$c/= :: TxSummary -> TxSummary -> Bool
/= :: TxSummary -> TxSummary -> Bool
Eq, Int -> TxSummary -> ShowS
[TxSummary] -> ShowS
TxSummary -> String
(Int -> TxSummary -> ShowS)
-> (TxSummary -> String)
-> ([TxSummary] -> ShowS)
-> Show TxSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxSummary -> ShowS
showsPrec :: Int -> TxSummary -> ShowS
$cshow :: TxSummary -> String
show :: TxSummary -> String
$cshowList :: [TxSummary] -> ShowS
showList :: [TxSummary] -> ShowS
Show, (forall x. TxSummary -> Rep TxSummary x)
-> (forall x. Rep TxSummary x -> TxSummary) -> Generic TxSummary
forall x. Rep TxSummary x -> TxSummary
forall x. TxSummary -> Rep TxSummary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxSummary -> Rep TxSummary x
from :: forall x. TxSummary -> Rep TxSummary x
$cto :: forall x. Rep TxSummary x -> TxSummary
to :: forall x. Rep TxSummary x -> TxSummary
Generic)

-- | Summary of a transaction input.
data TxInputSummary = TxInputSummary
  { TxInputSummary -> Text
tisUtxo :: !Text
  -- ^ @"txid#index"@
  , TxInputSummary -> Text
tisAddress :: !Text
  -- ^ Bech32 or hex address
  , TxInputSummary -> ValueSummary
tisValue :: !ValueSummary
  -- ^ Structured value (ada + tokens)
  }
  deriving (TxInputSummary -> TxInputSummary -> Bool
(TxInputSummary -> TxInputSummary -> Bool)
-> (TxInputSummary -> TxInputSummary -> Bool) -> Eq TxInputSummary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxInputSummary -> TxInputSummary -> Bool
== :: TxInputSummary -> TxInputSummary -> Bool
$c/= :: TxInputSummary -> TxInputSummary -> Bool
/= :: TxInputSummary -> TxInputSummary -> Bool
Eq, Int -> TxInputSummary -> ShowS
[TxInputSummary] -> ShowS
TxInputSummary -> String
(Int -> TxInputSummary -> ShowS)
-> (TxInputSummary -> String)
-> ([TxInputSummary] -> ShowS)
-> Show TxInputSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxInputSummary -> ShowS
showsPrec :: Int -> TxInputSummary -> ShowS
$cshow :: TxInputSummary -> String
show :: TxInputSummary -> String
$cshowList :: [TxInputSummary] -> ShowS
showList :: [TxInputSummary] -> ShowS
Show, (forall x. TxInputSummary -> Rep TxInputSummary x)
-> (forall x. Rep TxInputSummary x -> TxInputSummary)
-> Generic TxInputSummary
forall x. Rep TxInputSummary x -> TxInputSummary
forall x. TxInputSummary -> Rep TxInputSummary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxInputSummary -> Rep TxInputSummary x
from :: forall x. TxInputSummary -> Rep TxInputSummary x
$cto :: forall x. Rep TxInputSummary x -> TxInputSummary
to :: forall x. Rep TxInputSummary x -> TxInputSummary
Generic)

-- | Summary of a transaction output.
data TxOutputSummary = TxOutputSummary
  { TxOutputSummary -> Text
tosUtxo :: !Text
  -- ^ @"txid#index"@ – the UTxO reference for this output
  , TxOutputSummary -> Text
tosAddress :: !Text
  , TxOutputSummary -> ValueSummary
tosValue :: !ValueSummary
  , TxOutputSummary -> Maybe Text
tosDatum :: !(Maybe Text)
  -- ^ @"inline:\<hash\>"@, @"hash:\<hash\>"@, or @Nothing@
  }
  deriving (TxOutputSummary -> TxOutputSummary -> Bool
(TxOutputSummary -> TxOutputSummary -> Bool)
-> (TxOutputSummary -> TxOutputSummary -> Bool)
-> Eq TxOutputSummary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxOutputSummary -> TxOutputSummary -> Bool
== :: TxOutputSummary -> TxOutputSummary -> Bool
$c/= :: TxOutputSummary -> TxOutputSummary -> Bool
/= :: TxOutputSummary -> TxOutputSummary -> Bool
Eq, Int -> TxOutputSummary -> ShowS
[TxOutputSummary] -> ShowS
TxOutputSummary -> String
(Int -> TxOutputSummary -> ShowS)
-> (TxOutputSummary -> String)
-> ([TxOutputSummary] -> ShowS)
-> Show TxOutputSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxOutputSummary -> ShowS
showsPrec :: Int -> TxOutputSummary -> ShowS
$cshow :: TxOutputSummary -> String
show :: TxOutputSummary -> String
$cshowList :: [TxOutputSummary] -> ShowS
showList :: [TxOutputSummary] -> ShowS
Show, (forall x. TxOutputSummary -> Rep TxOutputSummary x)
-> (forall x. Rep TxOutputSummary x -> TxOutputSummary)
-> Generic TxOutputSummary
forall x. Rep TxOutputSummary x -> TxOutputSummary
forall x. TxOutputSummary -> Rep TxOutputSummary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxOutputSummary -> Rep TxOutputSummary x
from :: forall x. TxOutputSummary -> Rep TxOutputSummary x
$cto :: forall x. Rep TxOutputSummary x -> TxOutputSummary
to :: forall x. Rep TxOutputSummary x -> TxOutputSummary
Generic)

-- | Structured representation of a Cardano value for JSON serialization.
data ValueSummary = ValueSummary
  { ValueSummary -> Integer
vsLovelace :: !Integer
  , ValueSummary -> [AssetSummary]
vsAssets :: ![AssetSummary]
  }
  deriving (ValueSummary -> ValueSummary -> Bool
(ValueSummary -> ValueSummary -> Bool)
-> (ValueSummary -> ValueSummary -> Bool) -> Eq ValueSummary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ValueSummary -> ValueSummary -> Bool
== :: ValueSummary -> ValueSummary -> Bool
$c/= :: ValueSummary -> ValueSummary -> Bool
/= :: ValueSummary -> ValueSummary -> Bool
Eq, Int -> ValueSummary -> ShowS
[ValueSummary] -> ShowS
ValueSummary -> String
(Int -> ValueSummary -> ShowS)
-> (ValueSummary -> String)
-> ([ValueSummary] -> ShowS)
-> Show ValueSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ValueSummary -> ShowS
showsPrec :: Int -> ValueSummary -> ShowS
$cshow :: ValueSummary -> String
show :: ValueSummary -> String
$cshowList :: [ValueSummary] -> ShowS
showList :: [ValueSummary] -> ShowS
Show, (forall x. ValueSummary -> Rep ValueSummary x)
-> (forall x. Rep ValueSummary x -> ValueSummary)
-> Generic ValueSummary
forall x. Rep ValueSummary x -> ValueSummary
forall x. ValueSummary -> Rep ValueSummary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ValueSummary -> Rep ValueSummary x
from :: forall x. ValueSummary -> Rep ValueSummary x
$cto :: forall x. Rep ValueSummary x -> ValueSummary
to :: forall x. Rep ValueSummary x -> ValueSummary
Generic)

data AssetSummary = AssetSummary
  { AssetSummary -> Text
asPolicyId :: !Text
  , AssetSummary -> Text
asName :: !Text
  , AssetSummary -> Integer
asQuantity :: !Integer
  }
  deriving (AssetSummary -> AssetSummary -> Bool
(AssetSummary -> AssetSummary -> Bool)
-> (AssetSummary -> AssetSummary -> Bool) -> Eq AssetSummary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AssetSummary -> AssetSummary -> Bool
== :: AssetSummary -> AssetSummary -> Bool
$c/= :: AssetSummary -> AssetSummary -> Bool
/= :: AssetSummary -> AssetSummary -> Bool
Eq, Int -> AssetSummary -> ShowS
[AssetSummary] -> ShowS
AssetSummary -> String
(Int -> AssetSummary -> ShowS)
-> (AssetSummary -> String)
-> ([AssetSummary] -> ShowS)
-> Show AssetSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AssetSummary -> ShowS
showsPrec :: Int -> AssetSummary -> ShowS
$cshow :: AssetSummary -> String
show :: AssetSummary -> String
$cshowList :: [AssetSummary] -> ShowS
showList :: [AssetSummary] -> ShowS
Show, (forall x. AssetSummary -> Rep AssetSummary x)
-> (forall x. Rep AssetSummary x -> AssetSummary)
-> Generic AssetSummary
forall x. Rep AssetSummary x -> AssetSummary
forall x. AssetSummary -> Rep AssetSummary x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AssetSummary -> Rep AssetSummary x
from :: forall x. AssetSummary -> Rep AssetSummary x
$cto :: forall x. Rep AssetSummary x -> AssetSummary
to :: forall x. Rep AssetSummary x -> AssetSummary
Generic)

instance ToJSON ValueSummary where
  toJSON :: ValueSummary -> Value
toJSON ValueSummary
v =
    [Pair] -> Value
object
      [ Key
"lovelace" Key -> Integer -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ValueSummary -> Integer
vsLovelace ValueSummary
v
      , Key
"assets" Key -> [AssetSummary] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ValueSummary -> [AssetSummary]
vsAssets ValueSummary
v
      ]

instance ToJSON AssetSummary where
  toJSON :: AssetSummary -> Value
toJSON AssetSummary
a =
    [Pair] -> Value
object
      [ Key
"policyId" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= AssetSummary -> Text
asPolicyId AssetSummary
a
      , Key
"name" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= AssetSummary -> Text
asName AssetSummary
a
      , Key
"quantity" Key -> Integer -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= AssetSummary -> Integer
asQuantity AssetSummary
a
      ]

{- | What happened when a threat model was applied to a specific transaction
in this iteration.
-}
data ThreatModelTrace = ThreatModelTrace
  { ThreatModelTrace -> Text
tmtName :: !Text
  -- ^ Name of the threat model (e.g. "unprotectedScriptOutput")
  , ThreatModelTrace -> Int
tmtTargetTxIndex :: !Int
  -- ^ Index into 'itTransitions' identifying which transaction was targeted
  , ThreatModelTrace -> [Value]
tmtModifications :: ![Value]
  -- ^ Structured JSON descriptions of each modification applied
  , ThreatModelTrace -> TxSummary
tmtOriginalTx :: !TxSummary
  -- ^ The original transaction before modification
  , ThreatModelTrace -> Maybe TxSummary
tmtModifiedTx :: !(Maybe TxSummary)
  -- ^ The modified transaction, @Nothing@ if the modification couldn't produce a valid tx body
  , ThreatModelTrace -> ThreatModelTraceOutcome
tmtOutcome :: !ThreatModelTraceOutcome
  }
  deriving (ThreatModelTrace -> ThreatModelTrace -> Bool
(ThreatModelTrace -> ThreatModelTrace -> Bool)
-> (ThreatModelTrace -> ThreatModelTrace -> Bool)
-> Eq ThreatModelTrace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ThreatModelTrace -> ThreatModelTrace -> Bool
== :: ThreatModelTrace -> ThreatModelTrace -> Bool
$c/= :: ThreatModelTrace -> ThreatModelTrace -> Bool
/= :: ThreatModelTrace -> ThreatModelTrace -> Bool
Eq, Int -> ThreatModelTrace -> ShowS
[ThreatModelTrace] -> ShowS
ThreatModelTrace -> String
(Int -> ThreatModelTrace -> ShowS)
-> (ThreatModelTrace -> String)
-> ([ThreatModelTrace] -> ShowS)
-> Show ThreatModelTrace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ThreatModelTrace -> ShowS
showsPrec :: Int -> ThreatModelTrace -> ShowS
$cshow :: ThreatModelTrace -> String
show :: ThreatModelTrace -> String
$cshowList :: [ThreatModelTrace] -> ShowS
showList :: [ThreatModelTrace] -> ShowS
Show, (forall x. ThreatModelTrace -> Rep ThreatModelTrace x)
-> (forall x. Rep ThreatModelTrace x -> ThreatModelTrace)
-> Generic ThreatModelTrace
forall x. Rep ThreatModelTrace x -> ThreatModelTrace
forall x. ThreatModelTrace -> Rep ThreatModelTrace x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ThreatModelTrace -> Rep ThreatModelTrace x
from :: forall x. ThreatModelTrace -> Rep ThreatModelTrace x
$cto :: forall x. Rep ThreatModelTrace x -> ThreatModelTrace
to :: forall x. Rep ThreatModelTrace x -> ThreatModelTrace
Generic)

-- | Outcome of applying a threat model to a transaction.
data ThreatModelTraceOutcome
  = -- | Modified tx was correctly rejected by the ledger (good!)
    TMTOPassed
  | -- | Modified tx was ACCEPTED by the ledger (vulnerability found!)
    TMTOFailed !Text
  | -- | Couldn't test: rebalancing failed or precondition not met
    TMTOSkipped !Text
  | -- | Unexpected error during threat model execution
    TMTOError !Text
  deriving (ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool
(ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool)
-> (ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool)
-> Eq ThreatModelTraceOutcome
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool
== :: ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool
$c/= :: ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool
/= :: ThreatModelTraceOutcome -> ThreatModelTraceOutcome -> Bool
Eq, Int -> ThreatModelTraceOutcome -> ShowS
[ThreatModelTraceOutcome] -> ShowS
ThreatModelTraceOutcome -> String
(Int -> ThreatModelTraceOutcome -> ShowS)
-> (ThreatModelTraceOutcome -> String)
-> ([ThreatModelTraceOutcome] -> ShowS)
-> Show ThreatModelTraceOutcome
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ThreatModelTraceOutcome -> ShowS
showsPrec :: Int -> ThreatModelTraceOutcome -> ShowS
$cshow :: ThreatModelTraceOutcome -> String
show :: ThreatModelTraceOutcome -> String
$cshowList :: [ThreatModelTraceOutcome] -> ShowS
showList :: [ThreatModelTraceOutcome] -> ShowS
Show, (forall x.
 ThreatModelTraceOutcome -> Rep ThreatModelTraceOutcome x)
-> (forall x.
    Rep ThreatModelTraceOutcome x -> ThreatModelTraceOutcome)
-> Generic ThreatModelTraceOutcome
forall x. Rep ThreatModelTraceOutcome x -> ThreatModelTraceOutcome
forall x. ThreatModelTraceOutcome -> Rep ThreatModelTraceOutcome x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ThreatModelTraceOutcome -> Rep ThreatModelTraceOutcome x
from :: forall x. ThreatModelTraceOutcome -> Rep ThreatModelTraceOutcome x
$cto :: forall x. Rep ThreatModelTraceOutcome x -> ThreatModelTraceOutcome
to :: forall x. Rep ThreatModelTraceOutcome x -> ThreatModelTraceOutcome
Generic)

-- ---------------------------------------------------------------------
-- ToJSON instances
-- ---------------------------------------------------------------------

instance ToJSON TestRunTrace where
  toJSON :: TestRunTrace -> Value
toJSON TestRunTrace
t =
    [Pair] -> Value
object
      [ Key
"testId" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TestRunTrace -> Int
trtTestId TestRunTrace
t
      , Key
"testName" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TestRunTrace -> Text
trtTestName TestRunTrace
t
      , Key
"path" Key -> [Text] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TestRunTrace -> [Text]
trtPath TestRunTrace
t
      , Key
"category" Key -> TestCategory -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TestRunTrace -> TestCategory
trtCategory TestRunTrace
t
      , Key
"iterations" Key -> [IterationTrace] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TestRunTrace -> [IterationTrace]
trtIterations TestRunTrace
t
      ]

instance ToJSON IterationTrace where
  toJSON :: IterationTrace -> Value
toJSON IterationTrace
t =
    [Pair] -> Value
object
      [ Key
"index" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= IterationTrace -> Int
itIndex IterationTrace
t
      , Key
"status" Key -> IterationStatus -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= IterationTrace -> IterationStatus
itStatus IterationTrace
t
      , Key
"transitions" Key -> [Transition] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= IterationTrace -> [Transition]
itTransitions IterationTrace
t
      , Key
"threatModels" Key -> [ThreatModelTrace] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= IterationTrace -> [ThreatModelTrace]
itThreatModels IterationTrace
t
      ]

instance ToJSON IterationStatus where
  toJSON :: IterationStatus -> Value
toJSON IterationStatus
IterationSuccess =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"success" :: Text)]
  toJSON (IterationFailure Text
msg) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"failure" :: Text), Key
"message" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
msg]
  toJSON (IterationDiscarded Text
msg) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"discarded" :: Text), Key
"message" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
msg]

instance ToJSON Transition where
  toJSON :: Transition -> Value
toJSON Transition
t =
    [Pair] -> Value
object
      [ Key
"stepIndex" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Transition -> Int
trStepIndex Transition
t
      , Key
"action" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Transition -> Text
trAction Transition
t
      , Key
"stateBefore" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Transition -> Value
trStateBefore Transition
t
      , Key
"stateAfter" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Transition -> Value
trStateAfter Transition
t
      , Key
"transaction" Key -> Maybe TxSummary -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Transition -> Maybe TxSummary
trTransaction Transition
t
      , Key
"result" Key -> TransitionResult -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Transition -> TransitionResult
trResult Transition
t
      ]

instance ToJSON TransitionResult where
  toJSON :: TransitionResult -> Value
toJSON (TransitionSuccess Text
txId) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"success" :: Text), Key
"txId" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
txId]
  toJSON (TransitionFailure Text
err) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"failure" :: Text), Key
"error" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
err]

instance ToJSON TxSummary where
  toJSON :: TxSummary -> Value
toJSON TxSummary
t =
    [Pair] -> Value
object
      [ Key
"id" Key -> Maybe Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> Maybe Text
txsId TxSummary
t
      , Key
"inputs" Key -> [TxInputSummary] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> [TxInputSummary]
txsInputs TxSummary
t
      , Key
"outputs" Key -> [TxOutputSummary] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> [TxOutputSummary]
txsOutputs TxSummary
t
      , Key
"mint" Key -> Maybe ValueSummary -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> Maybe ValueSummary
txsMint TxSummary
t
      , Key
"fee" Key -> Integer -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> Integer
txsFee TxSummary
t
      , Key
"signers" Key -> [Text] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> [Text]
txsSigners TxSummary
t
      , Key
"validRange" Key -> Maybe Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxSummary -> Maybe Text
txsValidRange TxSummary
t
      ]

instance ToJSON TxInputSummary where
  toJSON :: TxInputSummary -> Value
toJSON TxInputSummary
t =
    [Pair] -> Value
object
      [ Key
"utxo" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxInputSummary -> Text
tisUtxo TxInputSummary
t
      , Key
"address" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxInputSummary -> Text
tisAddress TxInputSummary
t
      , Key
"value" Key -> ValueSummary -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxInputSummary -> ValueSummary
tisValue TxInputSummary
t
      ]

instance ToJSON TxOutputSummary where
  toJSON :: TxOutputSummary -> Value
toJSON TxOutputSummary
t =
    [Pair] -> Value
object
      [ Key
"utxo" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxOutputSummary -> Text
tosUtxo TxOutputSummary
t
      , Key
"address" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxOutputSummary -> Text
tosAddress TxOutputSummary
t
      , Key
"value" Key -> ValueSummary -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxOutputSummary -> ValueSummary
tosValue TxOutputSummary
t
      , Key
"datum" Key -> Maybe Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= TxOutputSummary -> Maybe Text
tosDatum TxOutputSummary
t
      ]

instance ToJSON ThreatModelTrace where
  toJSON :: ThreatModelTrace -> Value
toJSON ThreatModelTrace
t =
    [Pair] -> Value
object
      [ Key
"name" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ThreatModelTrace -> Text
tmtName ThreatModelTrace
t
      , Key
"targetTxIndex" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ThreatModelTrace -> Int
tmtTargetTxIndex ThreatModelTrace
t
      , Key
"modifications" Key -> [Value] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ThreatModelTrace -> [Value]
tmtModifications ThreatModelTrace
t
      , Key
"originalTx" Key -> TxSummary -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ThreatModelTrace -> TxSummary
tmtOriginalTx ThreatModelTrace
t
      , Key
"modifiedTx" Key -> Maybe TxSummary -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ThreatModelTrace -> Maybe TxSummary
tmtModifiedTx ThreatModelTrace
t
      , Key
"outcome" Key -> ThreatModelTraceOutcome -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ThreatModelTrace -> ThreatModelTraceOutcome
tmtOutcome ThreatModelTrace
t
      ]

instance ToJSON ThreatModelTraceOutcome where
  toJSON :: ThreatModelTraceOutcome -> Value
toJSON ThreatModelTraceOutcome
TMTOPassed =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"passed" :: Text)]
  toJSON (TMTOFailed Text
reason) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"failed" :: Text), Key
"reason" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
reason]
  toJSON (TMTOSkipped Text
reason) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"skipped" :: Text), Key
"reason" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
reason]
  toJSON (TMTOError Text
msg) =
    [Pair] -> Value
object [Key
"status" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"error" :: Text), Key
"message" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
msg]