@@ -13,53 +13,45 @@ import Control.Concurrent.MVar
13
13
import Cardano.Api
14
14
15
15
import Cardano.Benchmarking.FundSet as FundSet
16
+ import Cardano.Benchmarking.Fifo as Fifo
16
17
import Cardano.Benchmarking.Types (NumberOfTxs (.. ))
17
18
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
19
26
20
27
type TxGenerator era = [Fund ] -> [TxOut CtxTx era ] -> Either String (Tx era , TxId )
21
28
22
29
type ToUTxO era = Lovelace -> (TxOut CtxTx era , TxIx -> TxId -> Fund )
23
30
31
+ -- todo: inline inToOut :: [Lovelace] -> [Lovelace] and FundToStore
24
32
type ToUTxOList era = [Lovelace ] -> ([TxOut CtxTx era ], TxId -> [Fund ])
25
33
-- 'ToUTxOList era' is more powerful than '[ ToUTxO era ]' but
26
34
-- '[ ToUTxO era ]` is easier to construct.
27
35
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
38
38
39
- askWalletRef :: WalletRef -> (Wallet -> a ) -> IO a
39
+ askWalletRef :: WalletRef -> (FundSet -> a ) -> IO a
40
40
askWalletRef r f = do
41
41
w <- readMVar r
42
42
return $ f w
43
43
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
-
53
44
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
59
46
60
47
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)
63
55
64
56
makeToUTxOList :: [ ToUTxO era ] -> ToUTxOList era
65
57
makeToUTxOList fkts values
@@ -71,12 +63,13 @@ makeToUTxOList fkts values
71
63
in (o, f idx)
72
64
73
65
-- TODO use Error monad
66
+ -- TODO need to break this up
74
67
sourceToStoreTransaction ::
75
68
TxGenerator era
76
- -> FundSource IO
77
- -> ([Lovelace ] -> [Lovelace ])
69
+ -> FundSource IO
70
+ -> ([Lovelace ] -> [Lovelace ]) -- inline to ToUTxOList
78
71
-> ToUTxOList era
79
- -> FundToStore IO
72
+ -> FundToStore IO -- inline to ToUTxOList
80
73
-> IO (Either String (Tx era ))
81
74
sourceToStoreTransaction txGenerator fundSource inToOut mkTxOut fundToStore = do
82
75
fundSource >>= \ case
0 commit comments