-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ScriptErrorEvaluationFailed with DebugPlutusFailure
- Loading branch information
Showing
4 changed files
with
81 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Cardano.Api.Plutus | ||
( DebugPlutusFailure (..) | ||
, renderDebugPlutusFailure | ||
) | ||
where | ||
|
||
import Cardano.Api.Pretty | ||
|
||
import qualified Cardano.Ledger.Api as L | ||
import Cardano.Ledger.Binary.Plain (serializeAsHexText) | ||
import qualified Cardano.Ledger.Plutus.Evaluate as Plutus | ||
import qualified Cardano.Ledger.Plutus.ExUnits as Plutus | ||
import qualified Cardano.Ledger.Plutus.Language as Plutus | ||
import qualified PlutusLedgerApi.V1 as Plutus | ||
|
||
import qualified Data.ByteString.Base64 as B64 | ||
import Data.ByteString.Short as BSS | ||
import qualified Data.Map as Map | ||
import Data.Text (Text) | ||
import qualified Data.Text as Text | ||
import qualified Data.Text.Encoding as Text | ||
|
||
import qualified PlutusTx.Builtins.HasOpaque as PlutusTx | ||
import PlutusTx.ErrorCodes (plutusPreludeErrorCodes) | ||
|
||
data DebugPlutusFailure | ||
= DebugPlutusFailure | ||
{ dpfEvaluationError :: Plutus.EvaluationError | ||
, dpfScriptWithContext :: Plutus.PlutusWithContext L.StandardCrypto | ||
, dpfExecutionUnits :: Plutus.ExUnits | ||
, dpfExecutionLogs :: [Text] | ||
} | ||
deriving (Eq, Show) | ||
|
||
renderDebugPlutusFailure :: DebugPlutusFailure -> Text | ||
renderDebugPlutusFailure dpf = | ||
let pwc = dpfScriptWithContext dpf | ||
lang = case pwc of | ||
Plutus.PlutusWithContext{Plutus.pwcScript = script} -> | ||
either Plutus.plutusLanguage Plutus.plutusLanguage script | ||
|
||
scriptArgs = case pwc of | ||
Plutus.PlutusWithContext{Plutus.pwcArgs = args} -> | ||
pretty args | ||
|
||
evalError = dpfEvaluationError dpf | ||
binaryScript = case pwc of | ||
Plutus.PlutusWithContext{Plutus.pwcScript = scr} -> | ||
let Plutus.Plutus bytes = either id Plutus.plutusFromRunnable scr | ||
in Text.decodeUtf8 . B64.encode . BSS.fromShort $ Plutus.unPlutusBinary bytes | ||
in Text.unlines | ||
[ "Script hash: " <> serializeAsHexText (Plutus.pwcScriptHash pwc) | ||
, "Script language: " <> Text.pack (show lang) | ||
, "Protocol version: " <> Text.pack (show (Plutus.pwcProtocolVersion pwc)) | ||
, "Script arguments: " <> docToText scriptArgs | ||
, "Script evaluation error: " <> docToText (pretty evalError) | ||
, "Script execution logs: " <> Text.unlines (Prelude.map lookupPlutusErrorCode $ dpfExecutionLogs dpf) | ||
, "Script base64 encoded bytes: " <> binaryScript | ||
] | ||
|
||
lookupPlutusErrorCode :: Text -> Text | ||
lookupPlutusErrorCode code = | ||
let codeString = PlutusTx.stringToBuiltinString $ Text.unpack code | ||
in case Map.lookup codeString plutusPreludeErrorCodes of | ||
Just err -> Text.pack err | ||
Nothing -> "Unknown error code: " <> code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters