Skip to content

Commit 1b26a94

Browse files
committed
Render errors in evaluateTx more pretty
1 parent c4a3601 commit 1b26a94

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

hydra-tx/hydra-tx.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ library
9999
, ouroboros-consensus
100100
, ouroboros-consensus-cardano
101101
, plutus-ledger-api
102+
, prettyprinter
102103
, QuickCheck
103104
, quickcheck-instances
104105
, serialise

hydra-tx/src/Hydra/Ledger/Cardano/Evaluate.hs

+17-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import Hydra.Cardano.Api (
5656
UTxO,
5757
evaluateTransactionExecutionUnits,
5858
getTxBody,
59+
prettyError,
5960
toLedgerExUnits,
6061
)
6162
import Hydra.Cardano.Api.Pretty (renderTxWithUTxO)
@@ -73,6 +74,8 @@ import Ouroboros.Consensus.HardFork.History (
7374
initBound,
7475
mkInterpreter,
7576
)
77+
import Prettyprinter (defaultLayoutOptions, layoutPretty)
78+
import Prettyprinter.Render.Text (renderStrict)
7679
import Test.QuickCheck (Property, choose, counterexample, property)
7780
import Test.QuickCheck.Gen (chooseWord64)
7881

@@ -158,6 +161,19 @@ data EvaluationError
158161
type EvaluationReport =
159162
(Map ScriptWitnessIndex (Either ScriptExecutionError ExecutionUnits))
160163

164+
-- | Render the 'EvaluationReport' as a pretty multi-line text.
165+
renderEvaluationReport :: EvaluationReport -> Text
166+
renderEvaluationReport =
167+
unlines . map render . Map.toList
168+
where
169+
render (ix, Right exunits) =
170+
"- " <> show ix <> " OK and used " <> show exunits
171+
render (ix, Left err) =
172+
unlines
173+
[ "- " <> show ix <> " FAIL with error: "
174+
, renderStrict $ layoutPretty defaultLayoutOptions $ prettyError err
175+
]
176+
161177
-- | Get the total used 'ExecutionUnits' from an 'EvaluationReport'. Useful to
162178
-- further process the result of 'evaluateTx'.
163179
usedExecutionUnits :: EvaluationReport -> ExecutionUnits
@@ -333,7 +349,7 @@ propTransactionEvaluates (tx, lookupUTxO) =
333349
Right redeemerReport ->
334350
all isRight (Map.elems redeemerReport)
335351
& counterexample ("Transaction: " <> renderTxWithUTxO lookupUTxO tx)
336-
& counterexample ("Redeemer report: " <> show redeemerReport)
352+
& counterexample ("Redeemer report:\n " <> toString (renderEvaluationReport redeemerReport))
337353
& counterexample "Phase-2 validation failed"
338354

339355
-- | Expect a given 'Tx' and 'UTxO' to fail phase 1 or phase 2 evaluation.

0 commit comments

Comments
 (0)