1111{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
1212
1313import Cardano.Api
14+ import Cardano.Api.Experimental (SignedTx (.. ), Witnessable (.. ), legacyWitnessConversion ,
15+ makeKeyWitness , makeUnsignedTx , obtainCommonConstraints , sbeToEra , signTx )
16+ import qualified Cardano.Api.Experimental as Exp (evaluateTransactionFee )
17+ import qualified Cardano.Api.Experimental.Tx as Exp
1418
1519import Cardano.Benchmarking.Compiler (keyBenchmarkInputs )
1620import Cardano.Benchmarking.GeneratorTx.SizedMetadata (mkMetadata )
@@ -19,8 +23,8 @@ import Cardano.Benchmarking.PlutusScripts (listPlutusScripts)
1923#endif
2024import Cardano.TxGenerator.Calibrate.Utils
2125import Cardano.TxGenerator.PlutusContext
22- import Cardano.TxGenerator.ProtocolParameters ( ProtocolParameters (.. ), convertToLedgerProtocolParameters ,
23- toLedgerPParams )
26+ import Cardano.TxGenerator.ProtocolParameters (ProtocolParameters (.. ),
27+ convertToLedgerProtocolParameters , toLedgerPParams )
2428import Cardano.TxGenerator.Setup.Plutus
2529import Cardano.TxGenerator.Tx (txSizeInBytes )
2630import Cardano.TxGenerator.Types
@@ -33,7 +37,6 @@ import Data.Char
3337import Data.Function (on , (&) )
3438import Data.List (nub , sort , transpose )
3539import Data.List.Extra (split )
36-
3740import Data.Map.Strict as Map (Map , empty , fromList , union )
3841import Data.Maybe
3942import qualified Data.Text as T
@@ -485,71 +488,86 @@ writeResultsJSON jsonName summaries = do
485488approximateTxProperties :: ScriptInAnyLang -> ProtocolParameters -> (PlutusBudgetSummary , ScriptRedeemer ) -> IO PlutusBudgetSummary
486489approximateTxProperties script protocolParameters (summary, redeemer) = do
487490 putStrLn $ " --> approximating txn size and fee for: " ++ messageId summary
488- evaluate $ summary
489- { projectedTxSize = Just $ txSizeInBytes dummyTx
490- , projectedTxFee = Just $ evaluateTransactionFee era ledgerPParams2 (getTxBody dummyTx) 2 0 0 -- 1 (script witness) + 1 (collateral) = 2
491- }
492-
493- `catch` \ (SomeException e) -> do
494- putStrLn $ " approximation failed: " ++ show e
495- ++ " \n --> using unmodified summary"
496- pure summary
497- where
498- era = ShelleyBasedEraConway
499-
500- ledgerPParams1 :: LedgerProtocolParameters ConwayEra
501- ledgerPParams1 =
502- either (error . docToString . prettyError) id
503- $ convertToLedgerProtocolParameters era protocolParameters
504-
505- ledgerPParams2 =
506- either (error . docToString . prettyError) id
507- $ toLedgerPParams era protocolParameters
508-
509- witness :: Witness WitCtxTxIn ConwayEra
510- witness =
511- fromMaybe (error " could not get PlutusScriptWitness" )
512- $ case script of
513- ScriptInAnyLang lang (PlutusScript version script') -> do
514- scriptLang <- scriptLanguageSupportedInEra era lang
515- pure
516- $ ScriptWitness ScriptWitnessForSpending
517- $ PlutusScriptWitness
518- scriptLang
519- version
520- (PScript script')
521- (ScriptDatumForTxIn $ Just $ unsafeHashableScriptData $ ScriptDataNumber 0 )
522- redeemer
523- (budgetUsedPerTxInput summary)
524- _ -> Nothing
525-
526- -- build a dummy tx akin to what we'd get in the tx-generator's benchmarking workload;
527- -- it just needs to be sufficient to get our approximations.
528- dummyTx :: Tx ConwayEra
529- dummyTx
530- = signShelleyTransaction era txbody [WitnessPaymentKey keyBenchmarkInputs]
531- where
532- txbody =
533- either (error . docToString . prettyError) id
534- $ createTransactionBody era content
535-
536- content =
537- defaultTxBodyContent era
538- & setTxIns [(dummyTxIn 0 , BuildTxWith witness)]
539- & setTxInsCollateral (TxInsCollateral AlonzoEraOnwardsConway [dummyTxIn 1 ])
540- & setTxOuts [dummyTxOut]
541- & setTxValidityLowerBound TxValidityNoLowerBound
542- & setTxValidityUpperBound (defaultTxValidityUpperBound era)
543- & setTxMetadata dummyMetadata
544- & setTxFee (TxFeeExplicit era 1_000_000 )
545- & setTxProtocolParams (BuildTxWith (Just ledgerPParams1))
546-
547- -- Corresponds to the metadata inserted in benchmarking workloads, which is why it's needed for the estimate.
548- -- default value taken from: `add_tx_size` in nix/nixos/tx-generator-service.nix
549- dummyMetadata :: TxMetadataInEra ConwayEra
550- dummyMetadata = either error id $ mkMetadata 100
551-
552- -- just placeholders
553- dummyTxIn ix = mkTxIn $ " 900fc5da77a0747da53f7675cbb7d149d46779346dea2f879ab811ccc72a2162#" <> textShow @ Int ix
554- dummyTxOut = TxOut (keyAddress (Testnet (NetworkMagic 42 )) keyBenchmarkInputs) (lovelaceToTxOutValue era 1_000_000 ) TxOutDatumNone ReferenceScriptNone
491+ let
492+ era = either (\ deprecated -> error $ " unsupported era: " ++ show deprecated) id
493+ $ sbeToEra shelleyBasedEra
494+ evaluate
495+ ( obtainCommonConstraints era $
496+ let
497+ ledgerPParams =
498+ either (error . docToString . prettyError) id
499+ $ toLedgerPParams shelleyBasedEra protocolParameters
500+
501+ ledgerProtocolParameters :: LedgerProtocolParameters ConwayEra
502+ ledgerProtocolParameters =
503+ either (error . docToString . prettyError) id
504+ $ convertToLedgerProtocolParameters shelleyBasedEra protocolParameters
505+
506+ witness :: Witness WitCtxTxIn ConwayEra
507+ witness =
508+ fromMaybe (error " could not get PlutusScriptWitness" )
509+ $ case script of
510+ ScriptInAnyLang lang (PlutusScript version script') -> do
511+ scriptLang <- scriptLanguageSupportedInEra shelleyBasedEra lang
512+ pure
513+ $ ScriptWitness ScriptWitnessForSpending
514+ $ PlutusScriptWitness
515+ scriptLang
516+ version
517+ (PScript script')
518+ (ScriptDatumForTxIn $ Just $ unsafeHashableScriptData $ ScriptDataNumber 0 )
519+ redeemer
520+ (budgetUsedPerTxInput summary)
521+ _ -> Nothing
522+
523+ anyWitness =
524+ case legacyWitnessConversion AlonzoEraOnwardsConway [(WitTxIn (dummyTxIn 0 ), BuildTxWith witness)] of
525+ Right [(_, w)] -> w
526+ Right _ -> error " approximateTxProperties: unexpected witness conversion result"
527+ Left err -> error $ " approximateTxProperties: witness conversion failed: " ++ show err
528+
529+ expMetadata = case dummyMetadata of
530+ TxMetadataNone -> mempty
531+ TxMetadataInEra _ m -> m
532+
533+ -- build a dummy tx akin to what we'd get in the tx-generator's benchmarking workload;
534+ -- it just needs to be sufficient to get our approximations.
535+ txBodyContent =
536+ Exp. defaultTxBodyContent
537+ & Exp. setTxIns [(dummyTxIn 0 , anyWitness)]
538+ & Exp. setTxInsCollateral [dummyTxIn 1 ]
539+ & Exp. setTxOuts [Exp. TxOut $ toShelleyTxOutAny shelleyBasedEra dummyTxOut]
540+ & Exp. setTxFee (Coin 1_000_000 )
541+ & Exp. setTxMetadata expMetadata
542+ & Exp. setTxProtocolParams (unLedgerProtocolParameters ledgerProtocolParameters)
543+
544+ unsignedTx =
545+ either (\ err -> error $ " approximateTxProperties: " ++ show err) id
546+ $ makeUnsignedTx era txBodyContent
547+
548+ witVKey = makeKeyWitness era unsignedTx (WitnessPaymentKey keyBenchmarkInputs)
549+
550+ dummyTx :: Tx ConwayEra
551+ dummyTx =
552+ case signTx era [] [witVKey] unsignedTx of
553+ SignedTx signedLedgerTx -> ShelleyTx shelleyBasedEra signedLedgerTx
554+
555+ -- Corresponds to the metadata inserted in benchmarking workloads, which is why it's needed for the estimate.
556+ -- default value taken from: `add_tx_size` in nix/nixos/tx-generator-service.nix
557+ dummyMetadata :: TxMetadataInEra ConwayEra
558+ dummyMetadata = either error id $ mkMetadata 100
559+
560+ -- just placeholders
561+ dummyTxIn ix = mkTxIn $ " 900fc5da77a0747da53f7675cbb7d149d46779346dea2f879ab811ccc72a2162#" <> textShow @ Int ix
562+ dummyTxOut = TxOut (keyAddress (Testnet (NetworkMagic 42 )) keyBenchmarkInputs) (lovelaceToTxOutValue shelleyBasedEra 1_000_000 ) TxOutDatumNone ReferenceScriptNone
563+ in
564+ summary
565+ { projectedTxSize = Just $ txSizeInBytes dummyTx
566+ , projectedTxFee = Just $ Exp. evaluateTransactionFee ledgerPParams unsignedTx 2 0 0 -- 1 (script witness) + 1 (collateral) = 2
567+ }
568+ )
569+ `catch` \ (SomeException e) -> do
570+ putStrLn $ " approximation failed: " ++ show e
571+ ++ " \n --> using unmodified summary"
572+ pure summary
555573
0 commit comments