Skip to content

Commit 56f5a34

Browse files
committed
WIP use fifo
1 parent b6cdd56 commit 56f5a34

File tree

7 files changed

+78
-79
lines changed

7 files changed

+78
-79
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ addCollaterals src = do
8080
True -> do
8181
tx_fee <- askNixOption _nix_tx_fee
8282
safeCollateral <- _safeCollateral <$> evilFeeMagic
83-
collateralWallet <- newWallet "collaeral_wallet"
83+
collateralWallet <- newWallet "collateral_wallet"
8484
emit $ CreateChange era src src LocalSocket (PayToAddr $ KeyName "pass-partout") (safeCollateral + tx_fee) 1
8585
emit $ CreateChange era src collateralWallet LocalSocket (PayToCollateral $ KeyName "pass-partout") safeCollateral 1
8686
return $ Just collateralWallet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Cardano.Benchmarking.Fifo
2+
where
3+
import Prelude
4+
5+
-- This is to be used single threaded behind an MVar.
6+
7+
data Fifo a = Fifo ![a] ![a]
8+
9+
emptyFifo :: Fifo a
10+
emptyFifo = Fifo [] []
11+
12+
toList :: Fifo a -> [a]
13+
toList (Fifo x y) = x ++ reverse y
14+
15+
insert :: Fifo a -> a -> Fifo a
16+
insert (Fifo x y) e = Fifo x $ e:y
17+
18+
remove :: Fifo a -> Maybe (Fifo a, a)
19+
remove fifo = case fifo of
20+
Fifo [] [] -> Nothing
21+
Fifo (h:t) y -> Just (Fifo t y, h)
22+
Fifo [] y -> case reverse y of
23+
(h:t) -> Just (Fifo t [], h)
24+
[] -> error "unreachable"
25+
26+
removeN :: Int -> Fifo a -> Maybe (Fifo a, [a])
27+
removeN 0 f = return (f, [])
28+
removeN n f = do
29+
(a, h) <- remove f
30+
(r, t) <- removeN (pred n) a
31+
return (r, h:t)

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import Prelude
1414

1515
import Cardano.Api as Api
1616

17+
import Cardano.Benchmarking.Fifo as Fifo
18+
1719
-- Outputs that are available for spending.
1820
-- When building a new TX they provide the TxIn parts.
1921

@@ -26,14 +28,11 @@ data FundInEra era = FundInEra {
2628

2729
newtype Fund = Fund {unFund :: InAnyCardanoEra FundInEra}
2830

29-
type FundSet = [ Fund ]
31+
type FundSet = Fifo Fund
3032

3133
type FundSource m = m (Either String [Fund])
3234
type FundToStore m = [Fund] -> m ()
3335

34-
newtype SeqNumber = SeqNumber Int
35-
deriving (Show, Eq, Ord, Enum)
36-
3736
getFundTxIn :: Fund -> TxIn
3837
getFundTxIn (Fund (InAnyCardanoEra _ a)) = _fundTxIn a
3938

@@ -73,11 +72,11 @@ instance Ord Fund where
7372
compare a b = compare (getFundTxIn a) (getFundTxIn b)
7473

7574

76-
emptyFunds :: FundSet
77-
emptyFunds = [ ]
75+
emptyFundSet :: FundSet
76+
emptyFundSet = Fifo.emptyFifo
7877

7978
insertFund :: FundSet -> Fund -> FundSet
80-
insertFund s f = f : s
79+
insertFund = Fifo.insert
8180

8281
liftAnyEra :: ( forall era. IsCardanoEra era => f1 era -> f2 era ) -> InAnyCardanoEra f1 -> InAnyCardanoEra f2
8382
liftAnyEra f x = case x of

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

-30
This file was deleted.

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import Ouroboros.Network.Protocol.LocalTxSubmission.Type (SubmitResult
3333
import Cardano.Benchmarking.FundSet (FundInEra (..),
3434
liftAnyEra)
3535

36+
import qualified Cardano.Benchmarking.Fifo as Fifo
3637
import qualified Cardano.Benchmarking.FundSet as FundSet
3738
import Cardano.Benchmarking.FundSet as FundSet (getFundTxIn)
3839
import Cardano.Benchmarking.GeneratorTx as GeneratorTx (AsyncBenchmarkControl, TxGenError)
@@ -44,7 +45,6 @@ import Cardano.Benchmarking.GeneratorTx.NodeToNode (ConnectClient,
4445
import Cardano.Benchmarking.GeneratorTx.SizedMetadata (mkMetadata)
4546
import Cardano.Benchmarking.GeneratorTx.Tx as Core (keyAddress, mkFee, txInModeCardano)
4647

47-
import Cardano.Benchmarking.ListBufferedSelector
4848
import Cardano.Benchmarking.OuroborosImports as Core (LocalSubmitTx, SigningKeyFile,
4949
makeLocalConnectInfo, protocolToCodecConfig)
5050
import Cardano.Benchmarking.PlutusExample as PlutusExample
@@ -231,11 +231,17 @@ localSubmitTx tx = do
231231
submit <- getLocalSubmitTx
232232
ret <- liftIO $ submit tx
233233
case ret of
234-
SubmitSuccess -> return ()
235-
SubmitFail e -> traceDebug $ concat
236-
[ "local submit failed: " , show e , " (" , show tx , ")"]
237-
return ret
238-
234+
SubmitSuccess -> return ret
235+
SubmitFail e -> do
236+
let msg = concat [ "local submit failed: " , show e , " (" , show tx , ")" ]
237+
traceDebug msg
238+
return ret
239+
-- throwE $ ApiError msg
240+
241+
-- TODO:
242+
-- It should be possible to exit the tx-generator with an exception and also get the log messages.
243+
-- Problem 1: When doing throwE $ ApiError msg logmessages get lost !
244+
-- Problem 2: Workbench restarts the tx-generator -> this may be the reason for loss of messages
239245
makeMetadata :: forall era. IsShelleyBasedEra era => ActionM (TxMetadataInEra era)
240246
makeMetadata = do
241247
payloadSize <- getUser TTxAdditionalSize
@@ -273,7 +279,7 @@ runBenchmarkInEra sourceWallet submitMode (ThreadName threadName) shape collater
273279
let walletRefDst = walletRefSrc
274280
metadata <- makeMetadata
275281

276-
fundSource <- liftIO (mkBufferedSource walletRefSrc (auxInputsPerTx shape))
282+
let fundSource = walletSource walletRefSrc (auxInputsPerTx shape)
277283

278284
collaterals <- selectCollateralFunds collateralWallet
279285

@@ -311,7 +317,7 @@ selectCollateralFunds :: forall era. IsShelleyBasedEra era
311317
selectCollateralFunds Nothing = return (TxInsCollateralNone, [])
312318
selectCollateralFunds (Just walletName) = do
313319
cw <- getName walletName
314-
collateralFunds <- liftIO ( askWalletRef cw walletFunds) >>= \case
320+
collateralFunds <- liftIO ( askWalletRef cw Fifo.toList ) >>= \case
315321
[] -> throwE $ WalletError "selectCollateralFunds: emptylist"
316322
l -> return l
317323
case collateralSupportedInEra (cardanoEra @ era) of
@@ -428,7 +434,7 @@ createChangeGeneric sourceWallet submitMode createCoins addressMsg value count =
428434
, " address: ", addressMsg
429435
]
430436
traceDebug msg
431-
fundSource <- liftIO (mkBufferedSource walletRef 1)
437+
let fundSource = walletSource walletRef 1
432438

433439
forM_ chunks $ \coins -> do
434440
gen <- createCoins fundSource coins

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

+24-31
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,45 @@ import Control.Concurrent.MVar
1313
import Cardano.Api
1414

1515
import Cardano.Benchmarking.FundSet as FundSet
16+
import Cardano.Benchmarking.Fifo as Fifo
1617
import Cardano.Benchmarking.Types (NumberOfTxs (..))
1718
import Cardano.Api.Shelley (ProtocolParameters, ReferenceScript(..))
18-
type WalletRef = MVar Wallet
19+
20+
-- All the actual functionality of Wallet / WalletRef has been removed
21+
-- and WalletRef has been stripped down to MVar FundSet.
22+
-- The implementation of Wallet has become trivial.
23+
-- Todo: Remove trivial wrapper functions.
24+
25+
type WalletRef = MVar FundSet
1926

2027
type TxGenerator era = [Fund] -> [TxOut CtxTx era] -> Either String (Tx era, TxId)
2128

2229
type ToUTxO era = Lovelace -> (TxOut CtxTx era, TxIx -> TxId -> Fund)
2330

31+
--todo: inline inToOut :: [Lovelace] -> [Lovelace] and FundToStore
2432
type ToUTxOList era = [Lovelace] -> ([TxOut CtxTx era], TxId -> [Fund])
2533
-- 'ToUTxOList era' is more powerful than '[ ToUTxO era ]' but
2634
-- '[ ToUTxO era ]` is easier to construct.
2735

28-
data Wallet = Wallet {
29-
walletSeqNumber :: !SeqNumber
30-
, walletFunds :: !FundSet
31-
}
32-
33-
initWallet :: IO (MVar Wallet)
34-
initWallet = newMVar $ Wallet {
35-
walletSeqNumber = SeqNumber 1
36-
, walletFunds = emptyFunds
37-
}
36+
initWallet :: IO WalletRef
37+
initWallet = newMVar emptyFundSet
3838

39-
askWalletRef :: WalletRef -> (Wallet -> a) -> IO a
39+
askWalletRef :: WalletRef -> (FundSet -> a) -> IO a
4040
askWalletRef r f = do
4141
w <- readMVar r
4242
return $ f w
4343

44-
modifyWalletRef :: WalletRef -> (Wallet -> IO (Wallet, a)) -> IO a
45-
modifyWalletRef = modifyMVar
46-
47-
modifyWalletRefEither :: WalletRef -> (Wallet -> IO (Either err (Wallet,a))) -> IO (Either err a)
48-
modifyWalletRefEither ref action
49-
= modifyMVar ref $ \w -> action w >>= \case
50-
Right (newWallet, res) -> return (newWallet, Right res)
51-
Left err -> return (w, Left err)
52-
5344
walletRefInsertFund :: WalletRef -> Fund -> IO ()
54-
walletRefInsertFund ref fund = modifyMVar_ ref $ \w -> return $ walletInsertFund fund w
55-
56-
walletInsertFund :: Fund -> Wallet -> Wallet
57-
walletInsertFund f w
58-
= w { walletFunds = FundSet.insertFund (walletFunds w) f }
45+
walletRefInsertFund ref fund = modifyMVar_ ref $ \w -> return $ FundSet.insertFund w fund
5946

6047
mkWalletFundStore :: WalletRef -> FundToStore IO
61-
mkWalletFundStore walletRef funds = modifyWalletRef walletRef
62-
$ \wallet -> return (foldl (flip walletInsertFund) wallet funds, ())
48+
mkWalletFundStore walletRef funds = modifyMVar_ walletRef
49+
$ \wallet -> return (foldl FundSet.insertFund wallet funds)
50+
51+
walletSource :: WalletRef -> Int -> FundSource IO
52+
walletSource ref munch = modifyMVar ref $ \fifo -> return $ case Fifo.removeN munch fifo of
53+
Nothing -> (fifo, Left "WalletSource: out of funds")
54+
Just (newFifo, funds) -> (newFifo, Right funds)
6355

6456
makeToUTxOList :: [ ToUTxO era ] -> ToUTxOList era
6557
makeToUTxOList fkts values
@@ -71,12 +63,13 @@ makeToUTxOList fkts values
7163
in (o, f idx)
7264

7365
--TODO use Error monad
66+
--TODO need to break this up
7467
sourceToStoreTransaction ::
7568
TxGenerator era
76-
-> FundSource IO
77-
-> ([Lovelace] -> [Lovelace])
69+
-> FundSource IO
70+
-> ([Lovelace] -> [Lovelace]) --inline to ToUTxOList
7871
-> ToUTxOList era
79-
-> FundToStore IO
72+
-> FundToStore IO --inline to ToUTxOList
8073
-> IO (Either String (Tx era))
8174
sourceToStoreTransaction txGenerator fundSource inToOut mkTxOut fundToStore = do
8275
fundSource >>= \case

bench/tx-generator/tx-generator.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ library
2727
exposed-modules:
2828
Cardano.Benchmarking.Command
2929
Cardano.Benchmarking.Compiler
30+
Cardano.Benchmarking.Fifo
3031
Cardano.Benchmarking.FundSet
3132
Cardano.Benchmarking.GeneratorTx
3233
Cardano.Benchmarking.GeneratorTx.Error
@@ -55,7 +56,6 @@ library
5556
Cardano.Benchmarking.Types
5657
Cardano.Benchmarking.Version
5758
Cardano.Benchmarking.Wallet
58-
Cardano.Benchmarking.ListBufferedSelector
5959
Cardano.Benchmarking.PlutusExample
6060

6161
other-modules: Paths_tx_generator

0 commit comments

Comments
 (0)