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,88 @@ 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+ case sbeToEra shelleyBasedEra of
492+ Left _ -> do
493+ putStrLn " approximation failed: unsupported era\n --> using unmodified summary"
494+ pure summary
495+ Right expEra ->
496+ obtainCommonConstraints expEra $
497+ let
498+ ledgerPParams =
499+ either (error . docToString . prettyError) id
500+ $ toLedgerPParams shelleyBasedEra protocolParameters
501+
502+ ledgerProtocolParameters :: LedgerProtocolParameters ConwayEra
503+ ledgerProtocolParameters =
504+ either (error . docToString . prettyError) id
505+ $ convertToLedgerProtocolParameters shelleyBasedEra protocolParameters
506+
507+ witness :: Witness WitCtxTxIn ConwayEra
508+ witness =
509+ fromMaybe (error " could not get PlutusScriptWitness" )
510+ $ case script of
511+ ScriptInAnyLang lang (PlutusScript version script') -> do
512+ scriptLang <- scriptLanguageSupportedInEra shelleyBasedEra lang
513+ pure
514+ $ ScriptWitness ScriptWitnessForSpending
515+ $ PlutusScriptWitness
516+ scriptLang
517+ version
518+ (PScript script')
519+ (ScriptDatumForTxIn $ Just $ unsafeHashableScriptData $ ScriptDataNumber 0 )
520+ redeemer
521+ (budgetUsedPerTxInput summary)
522+ _ -> Nothing
523+
524+ anyWitness =
525+ case legacyWitnessConversion AlonzoEraOnwardsConway [(WitTxIn (dummyTxIn 0 ), BuildTxWith witness)] of
526+ Right [(_, w)] -> w
527+ Right _ -> error " approximateTxProperties: unexpected witness conversion result"
528+ Left err -> error $ " approximateTxProperties: witness conversion failed: " ++ show err
529+
530+ expMetadata = case dummyMetadata of
531+ TxMetadataNone -> mempty
532+ TxMetadataInEra _ m -> m
533+
534+ -- build a dummy tx akin to what we'd get in the tx-generator's benchmarking workload;
535+ -- it just needs to be sufficient to get our approximations.
536+ txBodyContent =
537+ Exp. defaultTxBodyContent
538+ & Exp. setTxIns [(dummyTxIn 0 , anyWitness)]
539+ & Exp. setTxInsCollateral [dummyTxIn 1 ]
540+ & Exp. setTxOuts [Exp. TxOut $ toShelleyTxOutAny shelleyBasedEra dummyTxOut]
541+ & Exp. setTxFee (Coin 1_000_000 )
542+ & Exp. setTxMetadata expMetadata
543+ & Exp. setTxProtocolParams (unLedgerProtocolParameters ledgerProtocolParameters)
544+
545+ unsignedTx =
546+ either (\ err -> error $ " approximateTxProperties: " ++ show err) id
547+ $ makeUnsignedTx expEra txBodyContent
548+
549+ witVKey = makeKeyWitness expEra unsignedTx (WitnessPaymentKey keyBenchmarkInputs)
550+
551+ dummyTx :: Tx ConwayEra
552+ dummyTx =
553+ case signTx expEra [] [witVKey] unsignedTx of
554+ SignedTx signedLedgerTx -> ShelleyTx shelleyBasedEra signedLedgerTx
555+
556+ -- Corresponds to the metadata inserted in benchmarking workloads, which is why it's needed for the estimate.
557+ -- default value taken from: `add_tx_size` in nix/nixos/tx-generator-service.nix
558+ dummyMetadata :: TxMetadataInEra ConwayEra
559+ dummyMetadata = either error id $ mkMetadata 100
560+
561+ -- just placeholders
562+ dummyTxIn ix = mkTxIn $ " 900fc5da77a0747da53f7675cbb7d149d46779346dea2f879ab811ccc72a2162#" <> textShow @ Int ix
563+ dummyTxOut = TxOut (keyAddress (Testnet (NetworkMagic 42 )) keyBenchmarkInputs) (lovelaceToTxOutValue shelleyBasedEra 1_000_000 ) TxOutDatumNone ReferenceScriptNone
564+ in
565+ evaluate
566+ ( summary
567+ { projectedTxSize = Just $ txSizeInBytes dummyTx
568+ , projectedTxFee = Just $ Exp. evaluateTransactionFee ledgerPParams unsignedTx 2 0 0 -- 1 (script witness) + 1 (collateral) = 2
569+ }
570+ )
571+ `catch` \ (SomeException e) -> do
572+ putStrLn $ " approximation failed: " ++ show e
573+ ++ " \n --> using unmodified summary"
574+ pure summary
555575
0 commit comments