Skip to content

Commit

Permalink
tx-generator: final move away from IsShelleyBasedEra to ShelleyBasedEra
Browse files Browse the repository at this point in the history
These were not strictly necessary, as opposed to the previous commit.
  • Loading branch information
smelc committed Jan 27, 2025
1 parent 221a805 commit af43a21
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 46 deletions.
29 changes: 12 additions & 17 deletions bench/tx-generator/src/Cardano/Benchmarking/Script/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Benchmarking.Script.Core
Expand Down Expand Up @@ -217,15 +216,12 @@ toMetadata sbe (Just payloadSize) = case mkMetadata sbe payloadSize of
Left err -> error err

submitAction :: ShelleyBasedEra sbe -> SubmitMode -> Generator -> TxGenTxParams -> ActionM ()
submitAction sbe submitMode generator txParams = submitInEra sbe submitMode generator txParams

submitInEra :: ShelleyBasedEra era -> SubmitMode -> Generator -> TxGenTxParams -> ActionM ()
submitInEra sbe submitMode generator txParams = do
submitAction sbe submitMode generator txParams = do
txStream <- evalGenerator sbe generator txParams
case submitMode of
NodeToNode _ -> error "NodeToNode deprecated: ToDo: remove"
Benchmark nodes tpsRate txCount -> benchmarkTxStream sbe txStream nodes tpsRate txCount
LocalSocket -> shelleyBasedEraConstraints sbe $ submitAll (void . localSubmitTx . Utils.mkTxInModeCardano) txStream
LocalSocket -> shelleyBasedEraConstraints sbe $ submitAll (void . localSubmitTx . TxInMode sbe) txStream
DumpToFile filePath -> liftIO $ Streaming.writeFile filePath $ Streaming.map showTx txStream
DiscardTX -> liftIO $ Streaming.mapM_ forceTx txStream
where
Expand Down Expand Up @@ -375,10 +371,10 @@ selectCollateralFunds sbe (Just walletName) = do
cw <- getEnvWallets walletName
collateralFunds <- liftIO ( askWalletRef cw FundQueue.toList ) >>= \case
[] -> throwE $ WalletError "selectCollateralFunds: emptylist"
l -> return l
l -> pure l
case forShelleyBasedEraMaybeEon sbe of
Nothing -> throwE $ WalletError $ "selectCollateralFunds: collateral: era not supported :" ++ show sbe
Just p -> return (TxInsCollateral p $ map getFundTxIn collateralFunds, collateralFunds)
Just p -> pure (TxInsCollateral p $ map getFundTxIn collateralFunds, collateralFunds)

dumpToFile :: FilePath -> TxInMode -> ActionM ()
dumpToFile filePath tx = liftIO $ dumpToFileIO filePath tx
Expand All @@ -396,20 +392,20 @@ interpretPayMode sbe payMode = do
PayToAddr keyName destWallet -> do
fundKey <- getEnvKeys keyName
walletRef <- getEnvWallets destWallet
return ( createAndStore (mkUTxOVariant sbe networkId fundKey) (mkWalletFundStore walletRef)
pure ( createAndStore (mkUTxOVariant sbe networkId fundKey) (mkWalletFundStore walletRef)
, Text.unpack $ serialiseAddress $ Utils.keyAddress sbe networkId fundKey)
PayToScript scriptSpec destWallet -> do
walletRef <- getEnvWallets destWallet
(witness, script, scriptData, _scriptFee) <- makePlutusContext scriptSpec
(witness, script, scriptData, _scriptFee) <- makePlutusContext sbe scriptSpec
case script of
ScriptInAnyLang _ script' ->
return ( createAndStore (mkUTxOScript networkId (script, scriptData) witness) (mkWalletFundStore walletRef)
pure ( createAndStore (mkUTxOScript sbe networkId (script, scriptData) witness) (mkWalletFundStore walletRef)
, Text.unpack $ serialiseAddress $ makeShelleyAddress networkId (PaymentCredentialByScript $ hashScript script') NoStakeAddress )

makePlutusContext :: forall era. IsShelleyBasedEra era
=> ScriptSpec
makePlutusContext :: ShelleyBasedEra era
-> ScriptSpec
-> ActionM (Witness WitCtxTxIn era, ScriptInAnyLang, ScriptData, L.Coin)
makePlutusContext ScriptSpec{..} = do
makePlutusContext sbe ScriptSpec{..} = do
protocolParameters <- getProtocolParameters
script <- liftIOSafe $ Plutus.readPlutusScript scriptSpecFile

Expand Down Expand Up @@ -483,9 +479,8 @@ makePlutusContext ScriptSpec{..} = do
case script of
ScriptInAnyLang lang (PlutusScript version script') ->
let
scriptWitness :: ScriptWitness WitCtxTxIn era
scriptWitness = case scriptLanguageSupportedInEra (shelleyBasedEra @era) lang of
Nothing -> error $ "runPlutusBenchmark: " ++ show version ++ " not supported in era: " ++ show (cardanoEra @era)
scriptWitness = case scriptLanguageSupportedInEra sbe lang of
Nothing -> error $ "runPlutusBenchmark: " ++ show version ++ " not supported in era: " ++ show sbe
Just scriptLang -> PlutusScriptWitness
scriptLang
version
Expand Down
18 changes: 8 additions & 10 deletions bench/tx-generator/src/Cardano/TxGenerator/Fund.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-|
Module : Cardano.TxGenerator.Fund
Description : A type for funds to build transactions with.
Expand Down Expand Up @@ -76,15 +75,14 @@ getFundCoin (Fund (InAnyCardanoEra _ a)) = case _fundVal a of
-- TODO: facilitate casting KeyWitnesses between eras -- Note [Era transitions]
-- | The `Fund` alternative is checked against `cardanoEra`, but
-- `getFundWitness` otherwise wraps `_fundWitness`.
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
(ConwayEra , Fund (InAnyCardanoEra ConwayEra a)) -> _fundWitness a
getFundWitness :: ShelleyBasedEra era -> Fund -> Witness WitCtxTxIn era
getFundWitness sbe fund = case (sbe, fund) of
(ShelleyBasedEraShelley , Fund (InAnyCardanoEra ShelleyEra a)) -> _fundWitness a
(ShelleyBasedEraAllegra , Fund (InAnyCardanoEra AllegraEra a)) -> _fundWitness a
(ShelleyBasedEraMary , Fund (InAnyCardanoEra MaryEra a)) -> _fundWitness a
(ShelleyBasedEraAlonzo , Fund (InAnyCardanoEra AlonzoEra a)) -> _fundWitness a
(ShelleyBasedEraBabbage , Fund (InAnyCardanoEra BabbageEra a)) -> _fundWitness a
(ShelleyBasedEraConway , Fund (InAnyCardanoEra ConwayEra a)) -> _fundWitness a
_ -> error "getFundWitness: era mismatch"

{-
Expand Down
2 changes: 1 addition & 1 deletion bench/tx-generator/src/Cardano/TxGenerator/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ genTx sbe ledgerParameters (collateral, collFunds) fee metadata inFunds outputs
where
allKeys = mapMaybe getFundKey $ inFunds ++ collFunds
txBodyContent = shelleyBasedEraConstraints sbe $ defaultTxBodyContent sbe
& setTxIns (map (\f -> (getFundTxIn f, BuildTxWith $ getFundWitness f)) inFunds)
& setTxIns (map (\f -> (getFundTxIn f, BuildTxWith $ getFundWitness sbe f)) inFunds)
& setTxInsCollateral collateral
& setTxOuts outputs
& setTxFee fee
Expand Down
21 changes: 10 additions & 11 deletions bench/tx-generator/src/Cardano/TxGenerator/UTxO.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.TxGenerator.UTxO
(module Cardano.TxGenerator.UTxO)
Expand Down Expand Up @@ -47,39 +46,39 @@ mkUTxOVariant sbe networkId key value
}

-- to be merged with mkUTxOVariant
mkUTxOScript :: forall era.
IsShelleyBasedEra era
=> NetworkId
mkUTxOScript ::
ShelleyBasedEra era
-> NetworkId
-> (ScriptInAnyLang, ScriptData)
-> Witness WitCtxTxIn era
-> ToUTxO era
mkUTxOScript networkId (script, txOutDatum) witness value
mkUTxOScript sbe networkId (script, txOutDatum) witness value
= ( mkTxOut value
, mkNewFund value
)
where
plutusScriptAddr = case script of
ScriptInAnyLang lang script' ->
case scriptLanguageSupportedInEra (shelleyBasedEra @era) lang of
case scriptLanguageSupportedInEra sbe lang of
Nothing -> error "mkUtxOScript: scriptLanguageSupportedInEra==Nothing"
Just{} -> makeShelleyAddressInEra
(shelleyBasedEra @era)
sbe
networkId
(PaymentCredentialByScript $ hashScript script')
NoStakeAddress

mkTxOut v = case forEraMaybeEon (cardanoEra @era) of
mkTxOut v = case forShelleyBasedEraMaybeEon sbe of
Nothing -> error "mkUtxOScript: scriptDataSupportedInEra==Nothing"
Just tag -> TxOut
plutusScriptAddr
(lovelaceToTxOutValue (shelleyBasedEra @era) v)
(lovelaceToTxOutValue sbe v)
(TxOutDatumHash tag $ hashScriptDataBytes $ unsafeHashableScriptData txOutDatum)
ReferenceScriptNone

mkNewFund :: L.Coin -> TxIx -> TxId -> Fund
mkNewFund val txIx txId = Fund $ InAnyCardanoEra (cardanoEra @era) $ FundInEra {
mkNewFund val txIx txId = shelleyBasedEraConstraints sbe $ Fund $ InAnyCardanoEra (toCardanoEra sbe) $ FundInEra {
_fundTxIn = TxIn txId txIx
, _fundWitness = witness
, _fundVal = lovelaceToTxOutValue (shelleyBasedEra @era) val
, _fundVal = lovelaceToTxOutValue sbe val
, _fundSigningKey = Nothing
}
7 changes: 0 additions & 7 deletions bench/tx-generator/src/Cardano/TxGenerator/Utils.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

{-
Module : Cardano.TxGenerator.Utils
Expand Down Expand Up @@ -74,8 +72,3 @@ mkTxFee = TxFeeExplicit
mkTxValidityUpperBound :: ShelleyBasedEra era -> SlotNo -> TxValidityUpperBound era
mkTxValidityUpperBound sbe slotNo =
TxValidityUpperBound (fromJust $ forShelleyBasedEraMaybeEon sbe) (Just slotNo)

-- | `mkTxInModeCardano` never uses the `TxInByronSpecial` constructor
-- because its type enforces it being a Shelley-based era.
mkTxInModeCardano :: IsShelleyBasedEra era => Tx era -> TxInMode
mkTxInModeCardano = TxInMode shelleyBasedEra

0 comments on commit af43a21

Please sign in to comment.