Skip to content

Commit b301852

Browse files
Merge #4231
4231: tx-generator cleanups r=MarcFontaine a=MarcFontaine * Merge `createChangeInEra` with `createScriptChange`. * Make `mkUTxOScript` polymorphic on the era. * Merge `runBenchmarkInEra` with `runPlutusBenchmark`. `runBenchmark` can now create transactions that contain any mix of normal and plutus inputs. * Change `ToUTxO era = [Lovelace] -> ([TxOut CtxTx era], TxId -> [Fund])` to `ToUTxO era = Lovelace -> (TxOut CtxTx era, TxIx -> TxId -> Fund)` (Deal with every output of a TX individually. This makes it possible to create TXs with a mix of regular and Plutus outputs). Co-authored-by: MarcFontaine <[email protected]>
2 parents 13a50c1 + aa5d5bb commit b301852

File tree

10 files changed

+266
-309
lines changed

10 files changed

+266
-309
lines changed

bench/tx-generator/src/Cardano/Benchmarking/Compiler.hs

+23-28
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ compileToScript = do
4949
genesisWallet <- newWallet "genesis_wallet"
5050
importGenesisFunds genesisWallet
5151
splitWallet <- splittingPhase genesisWallet
52-
addCollaterals genesisWallet splitWallet
53-
benchmarkingPhase splitWallet
52+
collateralWallet <- addCollaterals genesisWallet
53+
benchmarkingPhase splitWallet collateralWallet
5454

5555
initConstants :: Compiler ()
5656
initConstants = do
@@ -72,16 +72,18 @@ importGenesisFunds wallet = do
7272
emit $ ImportGenesisFund era wallet LocalSocket (KeyName "pass-partout") (KeyName "pass-partout")
7373
delay
7474

75-
addCollaterals :: SrcWallet -> DstWallet -> Compiler ()
76-
addCollaterals src dest = do
75+
addCollaterals :: SrcWallet -> Compiler (Maybe WalletName)
76+
addCollaterals src = do
7777
era <- askNixOption _nix_era
7878
isAnyPlutusMode >>= \case
79-
False -> return ()
80-
True -> do
79+
False -> return Nothing
80+
True -> do
8181
tx_fee <- askNixOption _nix_tx_fee
8282
safeCollateral <- _safeCollateral <$> evilFeeMagic
83+
collateralWallet <- newWallet "collaeral_wallet"
8384
emit $ CreateChange era src src LocalSocket (PayToAddr $ KeyName "pass-partout") (safeCollateral + tx_fee) 1
84-
emit $ CreateChange era src dest LocalSocket (PayToCollateral $ KeyName "pass-partout") safeCollateral 1
85+
emit $ CreateChange era src collateralWallet LocalSocket (PayToCollateral $ KeyName "pass-partout") safeCollateral 1
86+
return $ Just collateralWallet
8587

8688
splittingPhase :: SrcWallet -> Compiler DstWallet
8789
splittingPhase srcWallet = do
@@ -104,10 +106,17 @@ splittingPhase srcWallet = do
104106
createChangePlutus :: AnyCardanoEra -> SplitStep -> Compiler DstWallet
105107
createChangePlutus era (src, dst, value, count) = do
106108
autoMode <- isPlutusAutoMode
107-
plutusTarget <- if autoMode
108-
then PayToScript <$> askNixOption _nix_plutusLoopScript <*> pure (ScriptDataNumber 0)
109-
else PayToScript <$> askNixOption _nix_plutusScript <*> (ScriptDataNumber <$> askNixOption _nix_plutusData)
110-
emit $ CreateChange era src dst LocalSocket plutusTarget value count
109+
scriptSpec <- if autoMode
110+
then ScriptSpec <$> askNixOption _nix_plutusLoopScript <*> pure AutoScript
111+
else do
112+
executionUnits <- ExecutionUnits <$> askNixOption _nix_executionMemory <*> askNixOption _nix_executionSteps
113+
debugMode <- askNixOption _nix_debugMode
114+
budget <- (if debugMode then CheckScriptBudget else StaticScriptBudget)
115+
<$> (ScriptDataNumber <$> askNixOption _nix_plutusData)
116+
<*> (ScriptDataNumber <$> askNixOption _nix_plutusRedeemer)
117+
<*> pure executionUnits
118+
ScriptSpec <$> askNixOption _nix_plutusScript <*> pure budget
119+
emit $ CreateChange era src dst LocalSocket (PayToScript scriptSpec) value count
111120
delay
112121
return dst
113122

@@ -138,29 +147,15 @@ unfoldSplitSequence fee value count
138147
-- todo: this must be in sync with Scipt/Core.hs
139148
maxOutputs = 30
140149

141-
benchmarkingPhase :: WalletName -> Compiler ()
142-
benchmarkingPhase wallet = do
150+
benchmarkingPhase :: WalletName -> Maybe WalletName -> Compiler ()
151+
benchmarkingPhase wallet collateralWallet = do
143152
debugMode <- askNixOption _nix_debugMode
144-
plutusMode <- askNixOption _nix_plutusMode
145-
plutusAutoMode <- askNixOption _nix_plutusAutoMode
146153
targetNodes <- askNixOption _nix_targetNodes
147154
extraArgs <- evilValueMagic
148155
tps <- askNixOption _nix_tps
149156
era <- askNixOption _nix_era
150157
let target = if debugMode then LocalSocket else NodeToNode targetNodes
151-
spendMode <- case (plutusAutoMode, plutusMode) of
152-
( True, _ ) -> SpendAutoScript <$> askNixOption _nix_plutusLoopScript
153-
(False, True ) -> do
154-
executionUnits <- ExecutionUnits <$> askNixOption _nix_executionMemory <*> askNixOption _nix_executionSteps
155-
scriptBudget <- if debugMode
156-
then return $ CheckScriptBudget executionUnits
157-
else return $ StaticScriptBudget executionUnits
158-
SpendScript <$> askNixOption _nix_plutusScript
159-
<*> pure scriptBudget
160-
<*> (ScriptDataNumber <$> askNixOption _nix_plutusData)
161-
<*> (ScriptDataNumber <$> askNixOption _nix_plutusRedeemer)
162-
(False,False) -> return SpendOutput
163-
emit $ RunBenchmark era wallet target spendMode (ThreadName "tx-submit-benchmark") extraArgs tps
158+
emit $ RunBenchmark era wallet target (ThreadName "tx-submit-benchmark") extraArgs collateralWallet tps
164159
unless debugMode $ do
165160
emit $ WaitBenchmark $ ThreadName "tx-submit-benchmark"
166161

bench/tx-generator/src/Cardano/Benchmarking/FundSet.hs

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
{-# Language GeneralizedNewtypeDeriving #-}
66
{-# Language MultiParamTypeClasses #-}
77
{-# Language RankNTypes #-}
8+
{-# Language TypeApplications #-}
9+
{-# Language ScopedTypeVariables #-}
810

911
module Cardano.Benchmarking.FundSet
1012
where
@@ -21,6 +23,7 @@ import Cardano.Api as Api
2123

2224
data FundInEra era = FundInEra {
2325
_fundTxIn :: !TxIn
26+
, _fundWitness :: Witness WitCtxTxIn era
2427
, _fundVal :: !(TxOutValue era)
2528
, _fundSigningKey :: !(Maybe (SigningKey PaymentKey))
2629
, _fundVariant :: !Variant
@@ -30,7 +33,7 @@ data FundInEra era = FundInEra {
3033
data Variant
3134
= PlainOldFund
3235
-- maybe better use the script itself instead of the filePath
33-
| PlutusScriptFund !FilePath !ScriptData
36+
| PlutusScriptFund
3437
-- A collateralFund is just a regular (PlainOldFund) on the chain,
3538
-- but tagged in the wallet so that it is not selected for spending.
3639
| CollateralFund
@@ -66,6 +69,22 @@ getFundLovelace (Fund (InAnyCardanoEra _ a)) = case _fundVal a of
6669
TxOutAdaOnly _era l -> l
6770
TxOutValue _era v -> selectLovelace v
6871

72+
73+
-- This effectively rules out era-transitions for transactions !
74+
-- This is not what we want !!
75+
getFundWitness :: forall era. IsShelleyBasedEra era => Fund -> Witness WitCtxTxIn era
76+
getFundWitness fund = case (cardanoEra @ era, fund) of
77+
(ByronEra , Fund (InAnyCardanoEra ByronEra a)) -> _fundWitness a
78+
(ShelleyEra , Fund (InAnyCardanoEra ShelleyEra a)) -> _fundWitness a
79+
(AllegraEra , Fund (InAnyCardanoEra AllegraEra a)) -> _fundWitness a
80+
(MaryEra , Fund (InAnyCardanoEra MaryEra a)) -> _fundWitness a
81+
(AlonzoEra , Fund (InAnyCardanoEra AlonzoEra a)) -> _fundWitness a
82+
(BabbageEra , Fund (InAnyCardanoEra BabbageEra a)) -> _fundWitness a
83+
-- This effectively rules out era-transitions for transactions !
84+
-- This is not what we want !!
85+
-- It should be possible to cast KeyWitnesses from one era to an other !
86+
(_ , _) -> error "getFundWitness: era mismatch"
87+
6988
data IsConfirmed = IsConfirmed | IsNotConfirmed
7089
deriving (Show, Eq, Ord)
7190

@@ -227,7 +246,7 @@ selectInputs allowRecycle count minTotalValue variant targetNode fs
227246
selectToBuffer ::
228247
Int
229248
-> Lovelace
230-
-> Variant
249+
-> Maybe Variant
231250
-> FundSet
232251
-> Either String [Fund]
233252
selectToBuffer count minValue variant fs
@@ -239,8 +258,9 @@ selectToBuffer count minValue variant fs
239258
]
240259
else Right coins
241260
where
242-
coins = take count $ toAscList ( Proxy :: Proxy Lovelace) (fs @=variant @= IsConfirmed @>= minValue)
243-
261+
coins = case variant of
262+
Just v -> take count $ toAscList ( Proxy :: Proxy Lovelace) (fs @=v @= IsConfirmed @>= minValue)
263+
Nothing -> take count $ toAscList ( Proxy :: Proxy Lovelace) (fs @= IsConfirmed @>= minValue)
244264
-- Todo: check sufficient funds and minimumValuePerUtxo
245265
inputsToOutputsWithFee :: Lovelace -> Int -> [Lovelace] -> [Lovelace]
246266
inputsToOutputsWithFee fee count inputs = map (quantityToLovelace . Quantity) outputs

bench/tx-generator/src/Cardano/Benchmarking/ListBufferedSelector.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mkBufferedSource ::
1616
WalletRef
1717
-> Int
1818
-> Lovelace
19-
-> Variant
19+
-> Maybe Variant
2020
-> Int
2121
-> IO (Either String FundSource)
2222
mkBufferedSource walletRef count minValue variant munch

bench/tx-generator/src/Cardano/Benchmarking/PlutusExample.hs

+1-35
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,13 @@ import qualified Data.ByteString.Char8 as BSC
1212
import Cardano.CLI.Shelley.Script (readFileScriptInAnyLang)
1313

1414
import Cardano.Api
15-
import Cardano.Api.Shelley ( ProtocolParameters(..), PlutusScript(..), ReferenceScript(..)
15+
import Cardano.Api.Shelley ( ProtocolParameters(..), PlutusScript(..)
1616
, fromAlonzoExUnits, protocolParamCostModels, toPlutusData)
1717
import Cardano.Ledger.Alonzo.TxInfo (exBudgetToExUnits)
18-
import Cardano.Benchmarking.FundSet
19-
import Cardano.Benchmarking.Wallet
2018

2119
import qualified Plutus.V1.Ledger.Api as Plutus
2220
import Plutus.V1.Ledger.Contexts (ScriptContext(..), ScriptPurpose(..), TxInfo(..), TxOutRef(..))
2321

24-
mkUtxoScript ::
25-
NetworkId
26-
-> (FilePath, Script PlutusScriptV1, ScriptData)
27-
-> Validity
28-
-> ToUTxO AlonzoEra
29-
mkUtxoScript networkId (scriptFile, script, txOutDatum) validity values
30-
= ( map mkTxOut values
31-
, newFunds
32-
)
33-
where
34-
mkTxOut v = TxOut
35-
plutusScriptAddr
36-
(lovelaceToTxOutValue v)
37-
(TxOutDatumHash ScriptDataInAlonzoEra $ hashScriptData txOutDatum)
38-
ReferenceScriptNone
39-
40-
plutusScriptAddr = makeShelleyAddressInEra
41-
networkId
42-
(PaymentCredentialByScript $ hashScript script)
43-
NoStakeAddress
44-
45-
newFunds txId = zipWith (mkNewFund txId) [TxIx 0 ..] values
46-
47-
mkNewFund :: TxId -> TxIx -> Lovelace -> Fund
48-
mkNewFund txId txIx val = Fund $ InAnyCardanoEra AlonzoEra $ FundInEra {
49-
_fundTxIn = TxIn txId txIx
50-
, _fundVal = lovelaceToTxOutValue val
51-
, _fundSigningKey = Nothing
52-
, _fundValidity = validity
53-
, _fundVariant = PlutusScriptFund scriptFile txOutDatum
54-
}
55-
5622
readScript :: FilePath -> IO (Script PlutusScriptV1)
5723
readScript fp = do
5824
res <- runExceptT $ readFileScriptInAnyLang fp

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ action a = case a of
2222
Delay t -> delay t
2323
ImportGenesisFund era wallet submitMode genesisKey fundKey -> importGenesisFund era wallet submitMode genesisKey fundKey
2424
CreateChange era sourceWallet dstWallet payMode submitMode value count -> createChange era sourceWallet dstWallet payMode submitMode value count
25-
RunBenchmark era sourceWallet submitMode spendMode thread auxArgs tps -> runBenchmark era sourceWallet submitMode spendMode thread auxArgs tps
25+
RunBenchmark era sourceWallet submitMode thread auxArgs collateralWallet tps
26+
-> runBenchmark era sourceWallet submitMode thread auxArgs collateralWallet tps
2627
WaitBenchmark thread -> waitBenchmark thread
2728
CancelBenchmark thread -> cancelBenchmark thread
2829
WaitForEra era -> waitForEra era

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ instance ToJSON PayMode where
7373
instance FromJSON PayMode where
7474
parseJSON = genericParseJSON jsonOptionsUnTaggedSum
7575

76-
instance ToJSON SpendMode where
76+
instance ToJSON ScriptBudget where
7777
toJSON = genericToJSON jsonOptionsUnTaggedSum
7878
toEncoding = genericToEncoding jsonOptionsUnTaggedSum
79-
instance FromJSON SpendMode where
79+
instance FromJSON ScriptBudget where
8080
parseJSON = genericParseJSON jsonOptionsUnTaggedSum
8181

82-
instance ToJSON ScriptBudget where
82+
instance ToJSON ScriptSpec where
8383
toJSON = genericToJSON jsonOptionsUnTaggedSum
8484
toEncoding = genericToEncoding jsonOptionsUnTaggedSum
85-
instance FromJSON ScriptBudget where
85+
instance FromJSON ScriptSpec where
8686
parseJSON = genericParseJSON jsonOptionsUnTaggedSum
8787

8888
instance ToJSON (DSum Tag Identity) where

0 commit comments

Comments
 (0)