Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to plutus debug #4503

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eras/conway/impl/cardano-ledger-conway.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-ledger-conway
version: 1.16.0.1
version: 1.16.1.0
lehins marked this conversation as resolved.
Show resolved Hide resolved
license: Apache-2.0
maintainer: operations@iohk.io
author: IOHK
Expand Down
3 changes: 3 additions & 0 deletions eras/conway/impl/cddl-files/extra.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
; second era after Conway. We recommend all the tooling to account for this future breaking
; change sooner rather than later, in order to provide a smooth transition for their users.

; This is an unordered set. Duplicate elements are not allowed and the order of elements is implementation specific.
set<a> = #6.258([* a]) / [* a]

; Just like `set`, but must contain at least one element.
nonempty_set<a> = #6.258([+ a]) / [+ a]

; This is a non-empty ordered set. Duplicate elements are not allowed and the order of elements will be preserved.
nonempty_oset<a> = #6.258([+ a]) / [+ a]

positive_int = 1 .. 18446744073709551615
Expand Down
6 changes: 3 additions & 3 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ cradle:
- path: "libs/cardano-ledger-core/testlib"
component: "cardano-ledger-core:lib:testlib"

- path: "libs/cardano-ledger-core/app/PlutusDebug.hs"
component: "cardano-ledger-core:exe:plutus-debug"

- path: "libs/cardano-ledger-core/test"
component: "cardano-ledger-core:test:tests"

Expand Down Expand Up @@ -285,9 +288,6 @@ cradle:
- path: "libs/plutus-preprocessor/app/Main.hs"
component: "plutus-preprocessor:exe:plutus-preprocessor"

- path: "libs/plutus-preprocessor/src/Debug.hs"
component: "plutus-preprocessor:exe:plutus-debug"

- path: "libs/set-algebra/src"
component: "lib:set-algebra"

Expand Down
3 changes: 3 additions & 0 deletions libs/cardano-ledger-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
## 1.14.0.0

* Add `mkTermToEvaluate` to `PlutusLanguage` class.
* Add a field to `DebugFailure`
* Convert `debugPlutus` to an `IO` action
* Add `plutus-debug` executable

## 1.13.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Main where

import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Plutus.Evaluate (debugPlutus)
import Control.Monad ((<=<))
import System.Environment (getArgs)

main :: IO ()
main = mapM_ (print . debugPlutus @StandardCrypto) =<< getArgs
main = mapM_ (print <=< debugPlutus @StandardCrypto) =<< getArgs
13 changes: 13 additions & 0 deletions libs/cardano-ledger-core/cardano-ledger-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ library testlib
if !impl(ghc >=9.2)
ghc-options: -Wno-name-shadowing

executable plutus-debug
main-is: PlutusDebug.hs
hs-source-dirs: app
default-language: Haskell2010
ghc-options:
-Wall -Wcompat -Wincomplete-record-updates
-Wincomplete-uni-patterns -Wredundant-constraints -Wpartial-fields
-Wunused-packages -threaded -rtsopts -with-rtsopts=-N

build-depends:
base >=4.14 && <5,
cardano-ledger-core

test-suite tests
type: exitcode-stdio-1.0
main-is: Main.hs
Expand Down
53 changes: 40 additions & 13 deletions libs/cardano-ledger-core/src/Cardano/Ledger/Plutus/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -61,8 +62,9 @@ import Cardano.Ledger.Plutus.Language (
withSamePlutusLanguage,
)
import Cardano.Ledger.Plutus.TxInfo
import Control.DeepSeq (NFData (..))
import Control.Monad (unless)
import Control.DeepSeq (NFData (..), force)
import Control.Exception (evaluate)
import Control.Monad (join, unless)
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.UTF8 as BSU
import Data.List.NonEmpty (NonEmpty (..))
Expand All @@ -71,6 +73,7 @@ import Data.Text (Text, pack)
import GHC.Generics (Generic)
import PlutusLedgerApi.Common as P (EvaluationError (CodecError), ExBudget, VerboseMode (..))
import Prettyprinter (Pretty (..))
import System.Timeout (timeout)

-- | This type contains all that is necessary from Ledger to evaluate a plutus script.
data PlutusWithContext c where
Expand Down Expand Up @@ -196,27 +199,51 @@ instance Crypto c => FromCBOR (PlutusWithContext c) where
data PlutusDebugInfo c
= DebugBadHex String
| DebugCannotDecode String
| DebugSuccess [Text] P.ExBudget
| DebugFailure [Text] P.EvaluationError (PlutusWithContext c)
| DebugSuccess
-- | Execution logs from the plutus interpreter
[Text]
-- | Execution budget that was consumed. It will always be less or equal to what was
-- supplied during execution.
P.ExBudget
| DebugFailure
-- | Execution logs from the plutus interpreter
[Text]
-- | Evaluation error from Plutus interpreter
P.EvaluationError
-- | Everything that is needed in order to run the script
(PlutusWithContext c)
-- | Expected execution budget. This value is Nothing when the supplied script can't
-- be executed within 5 second limit or there is a problem with decoding plutus script
-- itself.
(Maybe P.ExBudget)
deriving (Show)

debugPlutus :: Crypto c => String -> PlutusDebugInfo c
debugPlutus :: Crypto c => String -> IO (PlutusDebugInfo c)
debugPlutus db =
case B64.decode (BSU.fromString db) of
Left e -> DebugBadHex (show e)
Left e -> pure $ DebugBadHex (show e)
Right bs ->
case Plain.decodeFull' bs of
Left e -> DebugCannotDecode $ show e
Left e -> pure $ DebugCannotDecode $ show e
Right pwc@(PlutusWithContext {..}) ->
let cm = getEvaluationContext pwcCostModel
eu = transExUnits pwcExUnits
onDecoderError err = DebugFailure [] err pwc
toDebugInfo = \case
(logs, Left err) -> DebugFailure logs err pwc
(logs, Right ex) -> DebugSuccess logs ex
onDecoderError err = pure $ DebugFailure [] err pwc Nothing
in withRunnablePlutusWithContext pwc onDecoderError $ \plutusRunnable args ->
toDebugInfo $
evaluatePlutusRunnable pwcProtocolVersion P.Verbose cm eu plutusRunnable args
let toDebugInfo = \case
(logs, Left err@(P.CodecError {})) -> pure $ DebugFailure logs err pwc Nothing
(logs, Left err) -> do
mExpectedExUnits <-
timeout 5_000_000 $ do
let res =
evaluatePlutusRunnableBudget pwcProtocolVersion P.Verbose cm plutusRunnable args
case snd res of
Left {} -> pure Nothing
Right exUnits -> Just <$> evaluate (force exUnits)
pure $ DebugFailure logs err pwc (join mExpectedExUnits)
(logs, Right ex) -> pure $ DebugSuccess logs ex
in toDebugInfo $
evaluatePlutusRunnable pwcProtocolVersion P.Verbose cm eu plutusRunnable args

runPlutusScript :: PlutusWithContext c -> ScriptResult c
runPlutusScript = snd . runPlutusScriptWithLogs
Expand Down
13 changes: 0 additions & 13 deletions libs/plutus-preprocessor/plutus-preprocessor.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,3 @@ executable plutus-preprocessor

if (impl(ghc <9.6) || impl(ghc >=9.7))
buildable: False

executable plutus-debug
main-is: Debug.hs
hs-source-dirs: src
default-language: Haskell2010
ghc-options:
-Wall -Wcompat -Wincomplete-record-updates
-Wincomplete-uni-patterns -Wredundant-constraints -Wpartial-fields
-Wunused-packages -threaded -rtsopts -with-rtsopts=-N

build-depends:
base >=4.14 && <5,
cardano-ledger-core >=1.0