-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
397bf8f
commit 159a8dd
Showing
5 changed files
with
187 additions
and
45 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
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