Skip to content

Commit f90ce2f

Browse files
committed
tx-generator: reintroduce ToUTxOList and makeToUTxOList.
1 parent 43fe509 commit f90ce2f

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,14 @@ runBenchmarkInEra sourceWallet submitMode (ThreadName threadName) shape collater
292292

293293
txGenerator = genTx protocolParameters collaterals (mkFee (auxFee shape)) metadata
294294

295-
toUTxO :: FundSet.Target -> FundSet.SeqNumber -> [ToUTxO era]
295+
toUTxO :: FundSet.Target -> FundSet.SeqNumber -> [ ToUTxO era ]
296296
toUTxO target seqNumber = repeat $ Wallet.mkUTxOVariant PlainOldFund networkId fundKey (InFlight target seqNumber)
297297

298298
fundToStore = mkWalletFundStore walletRefDst
299299

300300
walletScript :: FundSet.Target -> WalletScript era
301-
walletScript = benchmarkWalletScript walletRefSrc txGenerator (NumberOfTxs $ auxTxCount shape) (const fundSource) inToOut toUTxO fundToStore
301+
walletScript = benchmarkWalletScript walletRefSrc txGenerator (NumberOfTxs $ auxTxCount shape)
302+
(const fundSource) inToOut toUTxO fundToStore
302303

303304
case submitMode of
304305
NodeToNode targetNodes -> do
@@ -390,7 +391,9 @@ createChangeInEra sourceWallet dstWallet submitMode payMode value count _era = d
390391
(tx :: Either String (Tx era)) <- liftIO $ sourceToStoreTransaction
391392
(genTx protocolParameters (TxInsCollateralNone, [])
392393
(mkFee fee) TxMetadataNone )
393-
fundSource (Wallet.includeChange fee coins) (repeat toUTxO) (mkWalletFundStore walletRef)
394+
fundSource (Wallet.includeChange fee coins)
395+
(makeToUTxOList $ repeat toUTxO)
396+
(mkWalletFundStore walletRef)
394397
return $ fmap txInModeCardano tx
395398
createChangeGeneric sourceWallet submitMode createCoins addressMsg value count
396399

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

+23-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type TxGenerator era = [Fund] -> [TxOut CtxTx era] -> Either String (Tx era, TxI
2121

2222
type ToUTxO era = Lovelace -> (TxOut CtxTx era, TxIx -> TxId -> Fund)
2323

24+
type ToUTxOList era = [Lovelace] -> ([TxOut CtxTx era], TxId -> [Fund])
25+
-- 'ToUTxOList era' is more powerful than '[ ToUTxO era ]' but
26+
-- '[ ToUTxO era ]` is easier to construct.
27+
2428
data Wallet = Wallet {
2529
walletSeqNumber :: !SeqNumber
2630
, walletFunds :: !FundSet
@@ -74,15 +78,23 @@ mkWalletFundStore :: WalletRef -> FundToStore IO
7478
mkWalletFundStore walletRef funds = modifyWalletRef walletRef
7579
$ \wallet -> return (foldl (flip walletInsertFund) wallet funds, ())
7680

81+
makeToUTxOList :: [ ToUTxO era ] -> ToUTxOList era
82+
makeToUTxOList fkts values
83+
= (outs, \txId -> map (\f -> f txId) fs)
84+
where
85+
(outs, fs) =unzip $ map worker $ zip3 fkts values [TxIx 0 ..]
86+
worker (toUTxO, value, idx)
87+
= let (o, f ) = toUTxO value
88+
in (o, f idx)
89+
7790
--TODO use Error monad
7891
sourceToStoreTransaction ::
79-
Monad m
80-
=> TxGenerator era
81-
-> FundSource m
92+
TxGenerator era
93+
-> FundSource IO
8294
-> ([Lovelace] -> [Lovelace])
83-
-> [ToUTxO era]
84-
-> FundToStore m
85-
-> m (Either String (Tx era))
95+
-> ToUTxOList era
96+
-> FundToStore IO
97+
-> IO (Either String (Tx era))
8698
sourceToStoreTransaction txGenerator fundSource inToOut mkTxOut fundToStore = do
8799
fundSource >>= \case
88100
Left err -> return $ Left err
@@ -91,14 +103,11 @@ sourceToStoreTransaction txGenerator fundSource inToOut mkTxOut fundToStore = do
91103
work inputFunds = do
92104
let
93105
outValues = inToOut $ map getFundLovelace inputFunds
94-
outs = zipWith ($) mkTxOut outValues
95-
case txGenerator inputFunds $ map fst outs of
106+
(outputs, toFunds) = mkTxOut outValues
107+
case txGenerator inputFunds outputs of
96108
Left err -> return $ Left err
97109
Right (tx, txId) -> do
98-
let
99-
fkt :: (a, TxIx -> TxId -> Fund) -> TxIx -> Fund
100-
fkt a txIx = snd a txIx txId
101-
fundToStore $ zipWith fkt outs [TxIx 0 ..]
110+
fundToStore $ toFunds txId
102111
return $ Right tx
103112

104113
includeChange :: Lovelace -> [Lovelace] -> [Lovelace] -> [Lovelace]
@@ -226,7 +235,7 @@ benchmarkWalletScript :: forall era .
226235
-> NumberOfTxs
227236
-> (Target -> FundSource IO)
228237
-> ([Lovelace] -> [Lovelace])
229-
-> ( Target -> SeqNumber -> [ToUTxO era])
238+
-> ( Target -> SeqNumber -> [ToUTxO era] )
230239
-> FundToStore IO
231240
-> Target
232241
-> WalletScript era
@@ -239,7 +248,7 @@ benchmarkWalletScript wRef txGenerator (NumberOfTxs maxCount) fundSource inOut t
239248
walletStep = modifyMVarMasked wRef nextSeqNumber >>= \case
240249
Nothing -> return Done
241250
Just seqNumber -> do
242-
sourceToStoreTransaction txGenerator (fundSource targetNode) inOut (toUTxO targetNode seqNumber) fundToStore >>= \case
251+
sourceToStoreTransaction txGenerator (fundSource targetNode) inOut (makeToUTxOList $ toUTxO targetNode seqNumber) fundToStore >>= \case
243252
Left err -> return $ Error err
244253
Right tx -> return $ NextTx nextCall tx
245254

0 commit comments

Comments
 (0)