-
Notifications
You must be signed in to change notification settings - Fork 156
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
Create CLI for plutus-debug
#4776
Draft
Lucsanszky
wants to merge
1
commit into
master
Choose a base branch
from
ldan/plutus-debug-cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module CLI where | ||
|
||
import Cardano.Ledger.Binary (Version, mkVersion64) | ||
import Cardano.Ledger.Plutus.Language (Language) | ||
import Data.ByteString (ByteString) | ||
import Data.Int (Int64) | ||
import Numeric.Natural (Natural) | ||
import Options.Applicative | ||
|
||
data Opts = Opts | ||
{ optsScriptWithContext :: !String | ||
, optsScript :: !(Maybe ByteString) | ||
, optsProtocolVersion :: !(Maybe Version) | ||
, optsLanguage :: !(Maybe Language) | ||
, optsCostModelValues :: !(Maybe [Int64]) | ||
, optsExUnitsMem :: !(Maybe Natural) | ||
, optsExUnitsSteps :: !(Maybe Natural) | ||
} | ||
deriving (Show) | ||
|
||
optsParser :: Parser Opts | ||
optsParser = | ||
Opts | ||
<$> strArgument | ||
(metavar "SCRIPT_WITH_CONTEXT(BASE64)") | ||
<*> option | ||
(Just <$> str) | ||
( long "script" | ||
<> value Nothing | ||
<> help "Plutus script" | ||
) | ||
<*> option | ||
(mkVersion64 <$> auto) | ||
( long "protocol-version" | ||
<> short 'v' | ||
<> value Nothing | ||
<> help "Major protocol version" | ||
) | ||
<*> option | ||
(Just <$> auto) | ||
( long "language" | ||
<> value Nothing | ||
<> help "Plutus language version" | ||
) | ||
<*> option | ||
(str >>= pure . Just . map read . words) | ||
( long "cost-model-values" | ||
<> value Nothing | ||
<> help "" | ||
) | ||
<*> option | ||
(Just <$> auto) | ||
( long "execution-units-memory" | ||
<> value Nothing | ||
<> help "" | ||
) | ||
<*> option | ||
(Just <$> auto) | ||
( long "execution-units-steps" | ||
<> value Nothing | ||
<> help "" | ||
) |
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 |
---|---|---|
@@ -1,11 +1,114 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE NumericUnderscores #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Main where | ||
|
||
import Cardano.Ledger.Crypto (StandardCrypto) | ||
import Cardano.Ledger.Plutus.Evaluate (debugPlutus) | ||
import Control.Monad ((<=<)) | ||
import System.Environment (getArgs) | ||
import CLI | ||
import qualified Cardano.Ledger.Binary.Plain as Plain | ||
import Cardano.Ledger.Crypto (Crypto, StandardCrypto) | ||
import Cardano.Ledger.Plutus.CostModels ( | ||
getCostModelLanguage, | ||
getCostModelParams, | ||
getEvaluationContext, | ||
mkCostModel, | ||
) | ||
import Cardano.Ledger.Plutus.Evaluate | ||
import Cardano.Ledger.Plutus.ExUnits (ExUnits (..)) | ||
import Cardano.Ledger.Plutus.Language ( | ||
Plutus (..), | ||
PlutusBinary (..), | ||
PlutusLanguage (..), | ||
) | ||
import Cardano.Ledger.Plutus.TxInfo | ||
import Control.DeepSeq (force) | ||
import Control.Exception (evaluate) | ||
import Control.Monad (join) | ||
import qualified Data.ByteString.Base16 as B16 | ||
import qualified Data.ByteString.Base64 as B64 | ||
import qualified Data.ByteString.Char8 as BSC | ||
import qualified Data.ByteString.Short as SBS | ||
import qualified Data.ByteString.UTF8 as BSU | ||
import Data.Either (fromRight) | ||
import Data.Maybe (fromMaybe) | ||
import Options.Applicative | ||
import PlutusLedgerApi.Common as P | ||
import System.Timeout (timeout) | ||
|
||
overrideContext :: PlutusWithContext c -> Opts -> PlutusWithContext c | ||
overrideContext pwc@(PlutusWithContext {..}) Opts {..} = | ||
pwc | ||
{ pwcProtocolVersion = fromMaybe pwcProtocolVersion optsProtocolVersion | ||
, pwcScript = overrideScript | ||
, pwcExUnits = overrideExUnits | ||
, pwcCostModel = overrideCostModel | ||
, -- TODO: Add support for overriding arguments. | ||
-- Also note that this is needed in order to make GHC happy. | ||
-- Due to the `PlutusLanguage l` constraint in `PlutusWithContext` | ||
-- where `l` is an existential, without the line below GHC won't be able to | ||
-- tell that the `l` before the record update is the same as the `l` after | ||
-- the record update. | ||
pwcArgs | ||
} | ||
where | ||
overrideExUnits = | ||
ExUnits | ||
(fromMaybe (exUnitsMem pwcExUnits) optsExUnitsMem) | ||
(fromMaybe (exUnitsSteps pwcExUnits) optsExUnitsSteps) | ||
overrideCostModel = | ||
fromRight pwcCostModel $ | ||
mkCostModel | ||
(fromMaybe (getCostModelLanguage pwcCostModel) optsLanguage) | ||
(fromMaybe (getCostModelParams pwcCostModel) optsCostModelValues) | ||
overrideScript = | ||
case optsScript of | ||
Nothing -> pwcScript | ||
Just script -> | ||
either error (Left . Plutus . PlutusBinary . SBS.toShort) . B16.decode $ BSC.filter (/= '\n') script | ||
|
||
debugPlutus :: Crypto c => Opts -> IO (PlutusDebugInfo c) | ||
debugPlutus opts@Opts {..} = | ||
case B64.decode (BSU.fromString optsScriptWithContext) of | ||
Left e -> pure $ DebugBadHex (show e) | ||
Right bs -> | ||
case Plain.decodeFull' bs of | ||
Left e -> pure $ DebugCannotDecode $ show e | ||
Right pwcOriginal -> | ||
let pwc = overrideContext pwcOriginal opts | ||
cm = getEvaluationContext $ pwcCostModel pwc | ||
eu = transExUnits $ pwcExUnits pwc | ||
onDecoderError err = pure $ DebugFailure [] err pwc Nothing | ||
in withRunnablePlutusWithContext pwc onDecoderError $ \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 pwc) 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 pwc) P.Verbose cm eu plutusRunnable args | ||
|
||
main :: IO () | ||
main = mapM_ (print <=< debugPlutus @StandardCrypto) =<< getArgs | ||
main = do | ||
opts <- | ||
execParser $ | ||
info | ||
(optsParser <* abortOption (ShowHelpText Nothing) (long "help")) | ||
( header "plutus-debug - A Plutus script debugger" | ||
<> progDesc | ||
( "The purpose of this tool is to troubleshoot failing Plutus scripts. " | ||
<> "When you encounter a `PlutusFailure`, you can pass the `Base64-encoded script bytes` " | ||
<> "to `plutus-debug` for debugging purposes and override various parts of the failed script " | ||
<> "with the available command line options." | ||
) | ||
<> footer "" | ||
) | ||
debugPlutus @StandardCrypto opts >>= print |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we cannot remove this function from the library, because it looks like there are already downstream users of it. In particular I found that hydra uses this functionality.
What I suggest in order to resolve it is to create a type in this module:
then the
Opts
in CLI become:Then the only breaking change to this function would be addition of a new argument: