Skip to content

Commit 81627f9

Browse files
committed
tx-generator: type TxGenPlutusResolvedTo documenting origin of a script
1 parent fc5f95a commit 81627f9

File tree

7 files changed

+37
-18
lines changed

7 files changed

+37
-18
lines changed

bench/tx-generator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* A new cabal flag `withplutuslib` is added, enabling import and re-compilation of Plutus scripts from `plutus-scripts-bench` - default: false; use for dev/test of new benchmarks only.
66
* Port `ProtocolParameters` type and typeclass instances from `cardano-api` into new module `Cardano.Api.Internal`, removing dependency on the deprecated API type.
77
* A new executable `calibrate-script` which is a tool aimed at auto-calibrating Plutus benchmarking scripts to best fit scaled execution budgets and provides developer-friendly CSV reports of the process.
8+
* Better document how a Plutus script name was resolved by new type `TxGenPlutusResolvedTo`.
89
* Bump for Node 10.3
910

1011
## 2.14.2 -- Oct 2024

bench/tx-generator/app/calibrate-script.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,14 @@ runPlutus s@TargetBlockExpenditure{} _ _ _ _
155155
= putStrLn $ "--> fitting strategy currently unsupported: " ++ show s
156156
runPlutus strategy budgetType protoParamFile plutusDef@PlutusOn{..} scales
157157
= do
158-
script <- either (error . show) pure =<< readPlutusScript plutusScript
159-
putStrLn $ "--> read Plutus script: " ++ scriptNameExt
160158
protocolParameters <- readProtocolParametersOrDie protoParamFile
159+
(script, resolvedTo) <- either (error . show) pure =<< readPlutusScript plutusScript
161160

162-
let redeemerDef = Right plutusDef
161+
let
162+
redeemerDef = Right plutusDef
163+
scriptNameExt = show resolvedTo
164+
scriptName = takeWhile (/= '.') . drop 1 . dropWhile (not . isSpace) $ scriptNameExt
165+
putStrLn $ "--> got script " ++ scriptNameExt
163166

164167
redeemer :: ScriptData <-
165168
resolveRedeemer redeemerDef >>= either
@@ -186,13 +189,9 @@ runPlutus strategy budgetType protoParamFile plutusDef@PlutusOn{..} scales
186189
jsonName = "summaries_" ++ scriptName <.> "json"
187190
csvName = "scaling_" ++ scriptName <.> "csv"
188191

189-
190192
summariesWithApprox <- mapM (approximateTxProperties script protocolParameters) summaries
191193
writeResultsJSON jsonName summariesWithApprox
192194
writeResultsCSV csvName summariesWithApprox
193-
where
194-
scriptNameExt = either (++ " (known script)") (++ " (from file)") plutusScript
195-
scriptName = takeWhile (not . isSpace) scriptNameExt
196195

197196
runPlutus _ _ _ _ _ = error "calibrate-script: implementation error"
198197

bench/tx-generator/src/Cardano/Benchmarking/Script/Core.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ makePlutusContext :: forall era. IsShelleyBasedEra era
427427
-> ActionM (Witness WitCtxTxIn era, ScriptInAnyLang, ScriptData, L.Coin)
428428
makePlutusContext ScriptSpec{..} = do
429429
protocolParameters <- getProtocolParameters
430-
script <- liftIOSafe $ Plutus.readPlutusScript scriptSpecFile
430+
(script, resolvedTo) <- liftIOSafe $ Plutus.readPlutusScript scriptSpecFile
431431

432432
executionUnitPrices <- case protocolParamPrices protocolParameters of
433433
Just x -> return x
@@ -470,7 +470,7 @@ makePlutusContext ScriptSpec{..} = do
470470
, autoBudgetRedeemer = unsafeHashableScriptData $ scriptDataModifyNumber (const 1_000_000) (getScriptData redeemer)
471471
, autoBudgetUpperBoundHint = Nothing
472472
}
473-
scriptInfo = (either ("builtin: " ++) ("plutus file: " ++) scriptSpecFile, show strategy)
473+
scriptInfo = (show resolvedTo, show strategy)
474474
traceDebug $ "Plutus auto mode : Available budget per Tx: " ++ show perTxBudget
475475
++ " -- split between inputs per Tx: " ++ show txInputs
476476

bench/tx-generator/src/Cardano/TxGenerator/Setup/Plutus.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ import qualified PlutusLedgerApi.V3 as PlutusV3
3535
import qualified PlutusTx.AssocMap as AssocMap (empty)
3636

3737
import Cardano.TxGenerator.ProtocolParameters (ProtocolParameters(..))
38-
import Cardano.TxGenerator.Types (TxGenError (..))
38+
import Cardano.TxGenerator.Types (TxGenError (..), TxGenPlutusResolvedTo (..))
3939
#ifdef WITH_LIBRARY
4040
import Cardano.Benchmarking.PlutusScripts (findPlutusScript)
4141
#endif
4242
import Control.Exception (SomeException (..), try)
43+
import System.FilePath ((<.>), (</>))
44+
4345
import Paths_tx_generator
4446

4547
type ProtocolVersion = (Int, Int)
@@ -58,19 +60,22 @@ resolveFromLibrary = const Nothing
5860
-- What the @WITH_LIBRARY@ flag signifies is to use a set of statically-
5961
-- defined (via TH) scripts for the script name lookups instead of a
6062
-- set of library files.
61-
readPlutusScript :: Either String FilePath -> IO (Either TxGenError ScriptInAnyLang)
63+
readPlutusScript :: Either String FilePath -> IO (Either TxGenError (ScriptInAnyLang, TxGenPlutusResolvedTo))
6264
readPlutusScript (Left s)
6365
= case resolveFromLibrary s of
64-
Just s' -> pure $ Right s'
65-
Nothing -> try (getDataFileName $ "scripts-fallback/" ++ s ++ ".plutus") >>= either
66+
Just s' -> pure $ Right (s', ResolvedToLibrary s)
67+
Nothing -> try (getDataFileName $ "scripts-fallback" </> asFileName) >>= either
6668
(\(SomeException e) -> pure $ Left $ TxGenError $ show e)
67-
(readPlutusScript . Right)
69+
doLoad
70+
where
71+
asFileName = s <.> "plutus"
72+
doLoad fp = second (second (const $ ResolvedToFallback asFileName)) <$> readPlutusScript (Right fp)
6873
readPlutusScript (Right fp)
6974
= runExceptT $ do
7075
script <- firstExceptT ApiError $
7176
readFileScriptInAnyLang fp
7277
case script of
73-
ScriptInAnyLang (PlutusScriptLanguage _) _ -> pure script
78+
ScriptInAnyLang (PlutusScriptLanguage _) _ -> pure (script, ResolvedToFileName fp)
7479
ScriptInAnyLang lang _ -> throwE $ TxGenError $ "readPlutusScript: only PlutusScript supported, found: " ++ show lang
7580

7681
-- | 'preExecutePlutusScript' is a front end for the internal

bench/tx-generator/src/Cardano/TxGenerator/Types.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ data TxGenPlutusType
9393
data TxGenPlutusParams
9494
= PlutusOn -- ^ Generate Plutus Txs for given script
9595
{ plutusType :: !TxGenPlutusType
96-
, plutusScript :: !(Either String FilePath) -- ^ Path to the Plutus script
96+
, plutusScript :: !(Either String FilePath) -- ^ name or path of the Plutus script
9797
, plutusDatum :: !(Maybe FilePath) -- ^ Datum passed to the Plutus script (JSON file in ScriptData schema)
9898
, plutusRedeemer :: !(Maybe FilePath) -- ^ Redeemer passed to the Plutus script (JSON file in ScriptData schema)
9999
, plutusExecMemory :: !(Maybe Natural) -- ^ Max. memory for ExecutionUnits (overriding corresponding protocol parameter)
@@ -102,6 +102,19 @@ data TxGenPlutusParams
102102
| PlutusOff -- ^ Do not generate Plutus Txs
103103
deriving (Show, Eq)
104104

105+
-- | Documents how the `plutusScript` parameter above was eventually resolved
106+
data TxGenPlutusResolvedTo
107+
= ResolvedToLibrary String -- ^ source is the library from the plutus-scripts-bench package
108+
| ResolvedToFallback FilePath -- ^ source it the tx-generator's scripts-fallback data directory
109+
| ResolvedToFileName FilePath -- ^ source is a .plutus file
110+
deriving Eq
111+
112+
instance Show TxGenPlutusResolvedTo where
113+
show = \case
114+
ResolvedToLibrary n -> "builtin: " ++ n
115+
ResolvedToFallback f -> "fallback: " ++ f
116+
ResolvedToFileName f -> "file: " ++ f
117+
105118
isPlutusMode :: TxGenPlutusParams -> Bool
106119
isPlutusMode
107120
= (/= PlutusOff)

bench/tx-generator/test/ApiTest.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ checkPlutusLoop ::
185185
-> IO ()
186186
checkPlutusLoop protoParamFile (Just _plutusDef@PlutusOn{..})
187187
= do
188-
script <- either (die . show) pure =<< readPlutusScript plutusScript
189-
putStrLn $ "--> Read plutus script: " ++ scriptName
188+
(script, resolvedTo) <- either (die . show) pure =<< readPlutusScript plutusScript
189+
putStrLn $ "--> Got script " ++ show resolvedTo
190190
protocolParameters <- readProtocolParametersOrDie protoParamFile
191191

192192
let count = 1_792 -- arbitrary counter for a loop script; should respect mainnet limits

bench/tx-generator/tx-generator.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ library
129129
, constraints-extras
130130
, dlist
131131
, extra
132+
, filepath
132133
, formatting
133134
, generic-monoid
134135
, ghc-prim

0 commit comments

Comments
 (0)