Skip to content

tx-generator cleanups #4231

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

Merged
merged 3 commits into from
Aug 5, 2022
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
51 changes: 23 additions & 28 deletions bench/tx-generator/src/Cardano/Benchmarking/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ compileToScript = do
genesisWallet <- newWallet "genesis_wallet"
importGenesisFunds genesisWallet
splitWallet <- splittingPhase genesisWallet
addCollaterals genesisWallet splitWallet
benchmarkingPhase splitWallet
collateralWallet <- addCollaterals genesisWallet
benchmarkingPhase splitWallet collateralWallet

initConstants :: Compiler ()
initConstants = do
Expand All @@ -72,16 +72,18 @@ importGenesisFunds wallet = do
emit $ ImportGenesisFund era wallet LocalSocket (KeyName "pass-partout") (KeyName "pass-partout")
delay

addCollaterals :: SrcWallet -> DstWallet -> Compiler ()
addCollaterals src dest = do
addCollaterals :: SrcWallet -> Compiler (Maybe WalletName)
addCollaterals src = do
era <- askNixOption _nix_era
isAnyPlutusMode >>= \case
False -> return ()
True -> do
False -> return Nothing
True -> do
tx_fee <- askNixOption _nix_tx_fee
safeCollateral <- _safeCollateral <$> evilFeeMagic
collateralWallet <- newWallet "collaeral_wallet"
emit $ CreateChange era src src LocalSocket (PayToAddr $ KeyName "pass-partout") (safeCollateral + tx_fee) 1
emit $ CreateChange era src dest LocalSocket (PayToCollateral $ KeyName "pass-partout") safeCollateral 1
emit $ CreateChange era src collateralWallet LocalSocket (PayToCollateral $ KeyName "pass-partout") safeCollateral 1
return $ Just collateralWallet

splittingPhase :: SrcWallet -> Compiler DstWallet
splittingPhase srcWallet = do
Expand All @@ -104,10 +106,17 @@ splittingPhase srcWallet = do
createChangePlutus :: AnyCardanoEra -> SplitStep -> Compiler DstWallet
createChangePlutus era (src, dst, value, count) = do
autoMode <- isPlutusAutoMode
plutusTarget <- if autoMode
then PayToScript <$> askNixOption _nix_plutusLoopScript <*> pure (ScriptDataNumber 0)
else PayToScript <$> askNixOption _nix_plutusScript <*> (ScriptDataNumber <$> askNixOption _nix_plutusData)
emit $ CreateChange era src dst LocalSocket plutusTarget value count
scriptSpec <- if autoMode
then ScriptSpec <$> askNixOption _nix_plutusLoopScript <*> pure AutoScript
else do
executionUnits <- ExecutionUnits <$> askNixOption _nix_executionMemory <*> askNixOption _nix_executionSteps
debugMode <- askNixOption _nix_debugMode
budget <- (if debugMode then CheckScriptBudget else StaticScriptBudget)
<$> (ScriptDataNumber <$> askNixOption _nix_plutusData)
<*> (ScriptDataNumber <$> askNixOption _nix_plutusRedeemer)
<*> pure executionUnits
ScriptSpec <$> askNixOption _nix_plutusScript <*> pure budget
emit $ CreateChange era src dst LocalSocket (PayToScript scriptSpec) value count
delay
return dst

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

benchmarkingPhase :: WalletName -> Compiler ()
benchmarkingPhase wallet = do
benchmarkingPhase :: WalletName -> Maybe WalletName -> Compiler ()
benchmarkingPhase wallet collateralWallet = do
debugMode <- askNixOption _nix_debugMode
plutusMode <- askNixOption _nix_plutusMode
plutusAutoMode <- askNixOption _nix_plutusAutoMode
targetNodes <- askNixOption _nix_targetNodes
extraArgs <- evilValueMagic
tps <- askNixOption _nix_tps
era <- askNixOption _nix_era
let target = if debugMode then LocalSocket else NodeToNode targetNodes
spendMode <- case (plutusAutoMode, plutusMode) of
( True, _ ) -> SpendAutoScript <$> askNixOption _nix_plutusLoopScript
(False, True ) -> do
executionUnits <- ExecutionUnits <$> askNixOption _nix_executionMemory <*> askNixOption _nix_executionSteps
scriptBudget <- if debugMode
then return $ CheckScriptBudget executionUnits
else return $ StaticScriptBudget executionUnits
SpendScript <$> askNixOption _nix_plutusScript
<*> pure scriptBudget
<*> (ScriptDataNumber <$> askNixOption _nix_plutusData)
<*> (ScriptDataNumber <$> askNixOption _nix_plutusRedeemer)
(False,False) -> return SpendOutput
emit $ RunBenchmark era wallet target spendMode (ThreadName "tx-submit-benchmark") extraArgs tps
emit $ RunBenchmark era wallet target (ThreadName "tx-submit-benchmark") extraArgs collateralWallet tps
unless debugMode $ do
emit $ WaitBenchmark $ ThreadName "tx-submit-benchmark"

Expand Down
28 changes: 24 additions & 4 deletions bench/tx-generator/src/Cardano/Benchmarking/FundSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{-# Language GeneralizedNewtypeDeriving #-}
{-# Language MultiParamTypeClasses #-}
{-# Language RankNTypes #-}
{-# Language TypeApplications #-}
{-# Language ScopedTypeVariables #-}

module Cardano.Benchmarking.FundSet
where
Expand All @@ -21,6 +23,7 @@ import Cardano.Api as Api

data FundInEra era = FundInEra {
_fundTxIn :: !TxIn
, _fundWitness :: Witness WitCtxTxIn era
, _fundVal :: !(TxOutValue era)
, _fundSigningKey :: !(Maybe (SigningKey PaymentKey))
, _fundVariant :: !Variant
Expand All @@ -30,7 +33,7 @@ data FundInEra era = FundInEra {
data Variant
= PlainOldFund
-- maybe better use the script itself instead of the filePath
| PlutusScriptFund !FilePath !ScriptData
| PlutusScriptFund
-- A collateralFund is just a regular (PlainOldFund) on the chain,
-- but tagged in the wallet so that it is not selected for spending.
| CollateralFund
Expand Down Expand Up @@ -66,6 +69,22 @@ getFundLovelace (Fund (InAnyCardanoEra _ a)) = case _fundVal a of
TxOutAdaOnly _era l -> l
TxOutValue _era v -> selectLovelace v


Copy link
Contributor

@mgmeier mgmeier Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a dummy question... if we don't actually want it, what's the motivation for ruling out era transitions in this context? Would be great if the comment could provide that info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there are two 'era's in play: The era when the fund is created (this is wrapped an InAnyCardanoEra) and the era from getFundWitness :: forall era. IsShelleyBasedEra era => Fund -> Witness WitCtxTxIn era (That is basically when trying to spend the fund.)
getFundWitness must be partial (for example it makes no sense to spend funds in an earlier era that that not support a specific feature). But the way it was done here simply enforced that both eras are the same.
That was just lazyness on my side because writing the most general possible function is clunky.
So it does not intentionally rule out era transitions but it is a feature is just not implemented at the moment.

-- This effectively rules out era-transitions for transactions !
-- This is not what we want !!
getFundWitness :: forall era. IsShelleyBasedEra era => Fund -> Witness WitCtxTxIn era
getFundWitness fund = case (cardanoEra @ era, fund) of
(ByronEra , Fund (InAnyCardanoEra ByronEra a)) -> _fundWitness a
(ShelleyEra , Fund (InAnyCardanoEra ShelleyEra a)) -> _fundWitness a
(AllegraEra , Fund (InAnyCardanoEra AllegraEra a)) -> _fundWitness a
(MaryEra , Fund (InAnyCardanoEra MaryEra a)) -> _fundWitness a
(AlonzoEra , Fund (InAnyCardanoEra AlonzoEra a)) -> _fundWitness a
(BabbageEra , Fund (InAnyCardanoEra BabbageEra a)) -> _fundWitness a
-- This effectively rules out era-transitions for transactions !
-- This is not what we want !!
-- It should be possible to cast KeyWitnesses from one era to an other !
(_ , _) -> error "getFundWitness: era mismatch"

data IsConfirmed = IsConfirmed | IsNotConfirmed
deriving (Show, Eq, Ord)

Expand Down Expand Up @@ -227,7 +246,7 @@ selectInputs allowRecycle count minTotalValue variant targetNode fs
selectToBuffer ::
Int
-> Lovelace
-> Variant
-> Maybe Variant
-> FundSet
-> Either String [Fund]
selectToBuffer count minValue variant fs
Expand All @@ -239,8 +258,9 @@ selectToBuffer count minValue variant fs
]
else Right coins
where
coins = take count $ toAscList ( Proxy :: Proxy Lovelace) (fs @=variant @= IsConfirmed @>= minValue)

coins = case variant of
Just v -> take count $ toAscList ( Proxy :: Proxy Lovelace) (fs @=v @= IsConfirmed @>= minValue)
Nothing -> take count $ toAscList ( Proxy :: Proxy Lovelace) (fs @= IsConfirmed @>= minValue)
-- Todo: check sufficient funds and minimumValuePerUtxo
inputsToOutputsWithFee :: Lovelace -> Int -> [Lovelace] -> [Lovelace]
inputsToOutputsWithFee fee count inputs = map (quantityToLovelace . Quantity) outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mkBufferedSource ::
WalletRef
-> Int
-> Lovelace
-> Variant
-> Maybe Variant
-> Int
-> IO (Either String FundSource)
mkBufferedSource walletRef count minValue variant munch
Expand Down
36 changes: 1 addition & 35 deletions bench/tx-generator/src/Cardano/Benchmarking/PlutusExample.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,13 @@ import qualified Data.ByteString.Char8 as BSC
import Cardano.CLI.Shelley.Script (readFileScriptInAnyLang)

import Cardano.Api
import Cardano.Api.Shelley ( ProtocolParameters(..), PlutusScript(..), ReferenceScript(..)
import Cardano.Api.Shelley ( ProtocolParameters(..), PlutusScript(..)
, fromAlonzoExUnits, protocolParamCostModels, toPlutusData)
import Cardano.Ledger.Alonzo.TxInfo (exBudgetToExUnits)
import Cardano.Benchmarking.FundSet
import Cardano.Benchmarking.Wallet

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

mkUtxoScript ::
NetworkId
-> (FilePath, Script PlutusScriptV1, ScriptData)
-> Validity
-> ToUTxO AlonzoEra
mkUtxoScript networkId (scriptFile, script, txOutDatum) validity values
= ( map mkTxOut values
, newFunds
)
where
mkTxOut v = TxOut
plutusScriptAddr
(lovelaceToTxOutValue v)
(TxOutDatumHash ScriptDataInAlonzoEra $ hashScriptData txOutDatum)
ReferenceScriptNone

plutusScriptAddr = makeShelleyAddressInEra
networkId
(PaymentCredentialByScript $ hashScript script)
NoStakeAddress

newFunds txId = zipWith (mkNewFund txId) [TxIx 0 ..] values

mkNewFund :: TxId -> TxIx -> Lovelace -> Fund
mkNewFund txId txIx val = Fund $ InAnyCardanoEra AlonzoEra $ FundInEra {
_fundTxIn = TxIn txId txIx
, _fundVal = lovelaceToTxOutValue val
, _fundSigningKey = Nothing
, _fundValidity = validity
, _fundVariant = PlutusScriptFund scriptFile txOutDatum
}

readScript :: FilePath -> IO (Script PlutusScriptV1)
readScript fp = do
res <- runExceptT $ readFileScriptInAnyLang fp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ action a = case a of
Delay t -> delay t
ImportGenesisFund era wallet submitMode genesisKey fundKey -> importGenesisFund era wallet submitMode genesisKey fundKey
CreateChange era sourceWallet dstWallet payMode submitMode value count -> createChange era sourceWallet dstWallet payMode submitMode value count
RunBenchmark era sourceWallet submitMode spendMode thread auxArgs tps -> runBenchmark era sourceWallet submitMode spendMode thread auxArgs tps
RunBenchmark era sourceWallet submitMode thread auxArgs collateralWallet tps
-> runBenchmark era sourceWallet submitMode thread auxArgs collateralWallet tps
WaitBenchmark thread -> waitBenchmark thread
CancelBenchmark thread -> cancelBenchmark thread
WaitForEra era -> waitForEra era
Expand Down
8 changes: 4 additions & 4 deletions bench/tx-generator/src/Cardano/Benchmarking/Script/Aeson.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ instance ToJSON PayMode where
instance FromJSON PayMode where
parseJSON = genericParseJSON jsonOptionsUnTaggedSum

instance ToJSON SpendMode where
instance ToJSON ScriptBudget where
toJSON = genericToJSON jsonOptionsUnTaggedSum
toEncoding = genericToEncoding jsonOptionsUnTaggedSum
instance FromJSON SpendMode where
instance FromJSON ScriptBudget where
parseJSON = genericParseJSON jsonOptionsUnTaggedSum

instance ToJSON ScriptBudget where
instance ToJSON ScriptSpec where
toJSON = genericToJSON jsonOptionsUnTaggedSum
toEncoding = genericToEncoding jsonOptionsUnTaggedSum
instance FromJSON ScriptBudget where
instance FromJSON ScriptSpec where
parseJSON = genericParseJSON jsonOptionsUnTaggedSum

instance ToJSON (DSum Tag Identity) where
Expand Down
Loading