1
1
{-# LANGUAGE GADTs #-}
2
2
{-# LANGUAGE LambdaCase #-}
3
+ {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
3
4
module Cardano.Benchmarking.Compiler
4
5
where
5
6
6
7
import Prelude
7
8
8
9
import Control.Applicative (liftA2 )
9
10
import Control.Monad
11
+ import Control.Monad.Trans.Class (lift )
10
12
import Control.Monad.Trans.Except
11
13
import Control.Monad.Trans.RWS.CPS
12
- import Data.ByteString.Base16 as Base16
13
14
import Data.ByteString as BS (ByteString )
14
- import Data.Dependent.Sum ( (==>) )
15
15
import Data.DList (DList )
16
16
import qualified Data.DList as DL
17
17
import Data.Text (Text )
18
18
import qualified Data.Text as Text
19
19
20
20
import Cardano.Api
21
-
22
- import Cardano.Benchmarking.NixOptions
23
- import Cardano.Benchmarking.Script.Setters
24
- import Cardano.Benchmarking.Script.Store (KeyName , Name (.. ), WalletName )
25
21
import Cardano.Benchmarking.Script.Types
22
+ import Cardano.TxGenerator.Setup.NixService
23
+ import Cardano.TxGenerator.Setup.SigningKey
24
+ import Cardano.TxGenerator.Types (TxGenTxParams (.. ))
26
25
27
26
data CompileError where
28
- SomeCompilerError :: CompileError
27
+ SomeCompilerError :: String -> CompileError
29
28
deriving (Show )
30
29
type Compiler a = RWST NixServiceOptions (DList Action ) Int (Except CompileError ) a
31
30
31
+ throwCompileError :: CompileError -> Compiler a
32
+ throwCompileError = lift . throwE
33
+
32
34
maxOutputsPerTx :: Int
33
35
maxOutputsPerTx = 30
34
36
35
- type SrcWallet = WalletName
36
- type DstWallet = WalletName
37
+ type SrcWallet = String
38
+ type DstWallet = String
37
39
38
40
compileOptions :: NixServiceOptions -> Either CompileError [Action ]
39
41
compileOptions opts = runCompiler opts compileToScript
@@ -51,84 +53,82 @@ testCompiler o c = case runExcept $ runRWST c o 0 of
51
53
compileToScript :: Compiler ()
52
54
compileToScript = do
53
55
initConstants
54
- StartProtocol <$> askNixOption getNodeConfigFile <*> askNixOption _nix_cardanoTracerSocket >>= emit
56
+ nc <- askNixOption getNodeConfigFile >>= maybe
57
+ (throwCompileError $ SomeCompilerError " nodeConfigFile not set in Nix options" )
58
+ pure
59
+ tc <- askNixOption _nix_cardanoTracerSocket
60
+ emit $ StartProtocol nc tc
55
61
genesisWallet <- importGenesisFunds
56
62
collateralWallet <- addCollaterals genesisWallet
57
63
splitWallet <- splittingPhase genesisWallet
58
64
void $ benchmarkingPhase splitWallet collateralWallet
59
65
60
66
initConstants :: Compiler ()
61
67
initConstants = do
62
- setN TLocalSocket _nix_localNodeSocketPath
63
- setConst TTTL 1000000
68
+ p <- askNixOption _nix_localNodeSocketPath
69
+ emit $ SetSocketPath p
64
70
emit $ DefineSigningKey keyNameTxGenFunds keyTxGenFunds
65
71
emit $ DefineSigningKey keyNameCollaterals keyCollaterals
66
72
emit $ DefineSigningKey keyNameSplitPhase keySplitPhase
67
73
emit $ DefineSigningKey keyNameBenchmarkInputs keyBenchmarkInputs
68
74
emit $ DefineSigningKey keyNameBenchmarkDone keyBenchmarkDone
69
- where
70
- setConst :: Tag v -> v -> Compiler ()
71
- setConst key val = emit $ Set $ key ==> val
72
75
73
- setN :: Tag v -> (NixServiceOptions -> v ) -> Compiler ()
74
- setN key s = askNixOption s >>= setConst key
75
-
76
- importGenesisFunds :: Compiler WalletName
76
+ importGenesisFunds :: Compiler String
77
77
importGenesisFunds = do
78
78
logMsg " Importing Genesis Fund."
79
79
wallet <- newWallet " genesis_wallet"
80
80
era <- askNixOption _nix_era
81
- fee <- askNixOption _nix_tx_fee
81
+ txParams <- askNixOption txGenTxParams
82
82
cmd1 (ReadSigningKey keyNameGenesisInputFund) _nix_sigKey
83
- emit $ Submit era LocalSocket $ SecureGenesis fee wallet keyNameGenesisInputFund keyNameTxGenFunds
83
+ emit $ Submit era LocalSocket txParams $ SecureGenesis wallet keyNameGenesisInputFund keyNameTxGenFunds
84
84
delay
85
85
logMsg " Importing Genesis Fund. Done."
86
86
return wallet
87
87
88
- addCollaterals :: SrcWallet -> Compiler (Maybe WalletName )
88
+ addCollaterals :: SrcWallet -> Compiler (Maybe String )
89
89
addCollaterals src = do
90
90
era <- askNixOption _nix_era
91
+ txParams <- askNixOption txGenTxParams
91
92
isAnyPlutusMode >>= \ case
92
93
False -> return Nothing
93
94
True -> do
94
95
logMsg " Create collaterals."
95
96
safeCollateral <- _safeCollateral <$> evilFeeMagic
96
97
collateralWallet <- newWallet " collateral_wallet"
97
- fee <- askNixOption _nix_tx_fee
98
- let generator = Split fee src
98
+ let generator = Split src
99
99
(PayToAddr keyNameCollaterals collateralWallet)
100
100
(PayToAddr keyNameTxGenFunds src)
101
101
[ safeCollateral ]
102
- emit $ Submit era LocalSocket generator
102
+ emit $ Submit era LocalSocket txParams generator
103
103
logMsg " Create collaterals. Done."
104
104
return $ Just collateralWallet
105
105
106
106
splittingPhase :: SrcWallet -> Compiler DstWallet
107
107
splittingPhase srcWallet = do
108
108
tx_count <- askNixOption _nix_tx_count
109
109
inputs_per_tx <- askNixOption _nix_inputs_per_tx
110
- tx_fee <- askNixOption _nix_tx_fee
111
- era <- askNixOption _nix_era
110
+ era <- askNixOption _nix_era
111
+ txParams <- askNixOption txGenTxParams
112
112
minValuePerInput <- _minValuePerInput <$> evilFeeMagic
113
113
finalDest <- newWallet " final_split_wallet"
114
- splitSteps <- splitSequenceWalletNames srcWallet finalDest $ unfoldSplitSequence tx_fee minValuePerInput (tx_count * inputs_per_tx)
114
+ splitSteps <- splitSequenceWalletNames srcWallet finalDest $
115
+ unfoldSplitSequence (txParamFee txParams) minValuePerInput (tx_count * inputs_per_tx)
115
116
isPlutus <- isAnyPlutusMode
116
- forM_ (init splitSteps) $ createChange False False era
117
- createChange True isPlutus era $ last splitSteps
117
+ forM_ (init splitSteps) $ createChange txParams False False era
118
+ createChange txParams True isPlutus era $ last splitSteps
118
119
return finalDest
119
120
where
120
- createChange :: Bool -> Bool -> AnyCardanoEra -> (SrcWallet , DstWallet , Split ) -> Compiler ()
121
- createChange isLastStep isPlutus era (src, dst, split) = do
121
+ createChange :: TxGenTxParams -> Bool -> Bool -> AnyCardanoEra -> (SrcWallet , DstWallet , Split ) -> Compiler ()
122
+ createChange txParams isLastStep isPlutus era (src, dst, split) = do
122
123
logMsg $ Text. pack $ " Splitting step: " ++ show split
123
- tx_fee <- askNixOption _nix_tx_fee
124
124
let valuePayMode = PayToAddr (if isLastStep then keyNameSplitPhase else keyNameBenchmarkInputs) dst
125
125
payMode <- if isPlutus then plutusPayMode dst else return valuePayMode
126
126
let generator = case split of
127
- SplitWithChange lovelace count -> Split tx_fee src payMode (PayToAddr keyNameTxGenFunds src) $ replicate count lovelace
128
- FullSplits txCount -> Take txCount $ Cycle $ SplitN tx_fee src payMode maxOutputsPerTx
129
- emit $ Submit era LocalSocket generator
127
+ SplitWithChange lovelace count -> Split src payMode (PayToAddr keyNameTxGenFunds src) $ replicate count lovelace
128
+ FullSplits txCount -> Take txCount $ Cycle $ SplitN src payMode maxOutputsPerTx
129
+ emit $ Submit era LocalSocket txParams generator
130
130
delay
131
- logMsg " Splitting step: Done"
131
+ logMsg " Splitting step: Done"
132
132
133
133
plutusPayMode :: DstWallet -> Compiler PayMode
134
134
plutusPayMode dst = do
@@ -175,44 +175,43 @@ unfoldSplitSequence fee value outputs
175
175
(x, 0 ) -> x
176
176
(x, _rest) -> x+ 1
177
177
178
- benchmarkingPhase :: WalletName -> Maybe WalletName -> Compiler WalletName
178
+ benchmarkingPhase :: String -> Maybe String -> Compiler String
179
179
benchmarkingPhase wallet collateralWallet = do
180
180
debugMode <- askNixOption _nix_debugMode
181
181
targetNodes <- askNixOption _nix_targetNodes
182
182
tps <- askNixOption _nix_tps
183
183
era <- askNixOption _nix_era
184
184
txCount <- askNixOption _nix_tx_count
185
- fee <- askNixOption _nix_tx_fee
186
185
inputs <- askNixOption _nix_inputs_per_tx
187
186
outputs <- askNixOption _nix_outputs_per_tx
188
- metadataSize <- askNixOption _nix_add_tx_size
187
+ txParams <- askNixOption txGenTxParams
189
188
doneWallet <- newWallet " done_wallet"
190
189
let
191
190
payMode = PayToAddr keyNameBenchmarkDone doneWallet
192
191
submitMode = if debugMode
193
192
then LocalSocket
194
- else Benchmark targetNodes ( ThreadName " tx-submit-benchmark" ) tps txCount
195
- generator = Take txCount $ Cycle $ NtoM fee wallet payMode inputs outputs (Just metadataSize ) collateralWallet
196
- emit $ Submit era submitMode generator
193
+ else Benchmark targetNodes " tx-submit-benchmark" tps txCount
194
+ generator = Take txCount $ Cycle $ NtoM wallet payMode inputs outputs (Just $ txParamAddTxSize txParams ) collateralWallet
195
+ emit $ Submit era submitMode txParams generator
197
196
unless debugMode $ do
198
- emit $ WaitBenchmark $ ThreadName " tx-submit-benchmark"
197
+ emit $ WaitBenchmark " tx-submit-benchmark"
199
198
return doneWallet
200
199
201
200
data Fees = Fees {
202
201
_safeCollateral :: Lovelace
203
202
, _minValuePerInput :: Lovelace
204
203
}
205
-
204
+
206
205
evilFeeMagic :: Compiler Fees
207
206
evilFeeMagic = do
208
207
(Quantity tx_fee) <- lovelaceToQuantity <$> askNixOption _nix_tx_fee
209
- plutusMode <- askNixOption _nix_plutusMode
208
+ plutusMode <- askNixOption _nix_plutusMode
210
209
inputs_per_tx <- askNixOption _nix_inputs_per_tx
211
- outputs_per_tx <- askNixOption _nix_outputs_per_tx
210
+ outputs_per_tx <- askNixOption _nix_outputs_per_tx
212
211
(Quantity min_utxo_value) <- lovelaceToQuantity <$> askNixOption _nix_min_utxo_value
213
212
let
214
- scriptFees = 5000000 ;
215
- collateralPercentage = 200 ;
213
+ scriptFees = 5000000 ; -- FIXME: should be taken from ProtocolParameters
214
+ collateralPercentage = 200 ; -- FIXME: should be taken from ProtocolParameters
216
215
217
216
totalFee = if plutusMode
218
217
then tx_fee + scriptFees * fromIntegral inputs_per_tx
@@ -233,7 +232,7 @@ logMsg = emit . LogMsg
233
232
234
233
cmd1 :: (v -> Action ) -> (NixServiceOptions -> v ) -> Compiler ()
235
234
cmd1 cmd arg = emit . cmd =<< askNixOption arg
236
-
235
+
237
236
askNixOption :: (NixServiceOptions -> v ) -> Compiler v
238
237
askNixOption = asks
239
238
@@ -255,60 +254,55 @@ newIdentifier prefix = do
255
254
put $ succ n
256
255
return $ prefix ++ " _" ++ show n
257
256
258
- newWallet :: String -> Compiler WalletName
257
+ newWallet :: String -> Compiler String
259
258
newWallet n = do
260
- name <- WalletName <$> newIdentifier n
259
+ name <- newIdentifier n
261
260
emit $ InitWallet name
262
261
return name
263
262
264
- parseKey :: BS. ByteString -> TextEnvelope
265
- parseKey x = case Base16. decode x of
266
- Left err -> error $ " parsing of key failed : " ++ show err
267
- Right addr -> TextEnvelope
268
- { teType = TextEnvelopeType " PaymentSigningKeyShelley_ed25519"
269
- , teDescription = " Payment Signing Key"
270
- , teRawCBOR = addr
271
- }
272
-
263
+ -- we assume the hardcoded base16 keys to successfully evaluate to a SigningKey PaymentKey
264
+ parseKey :: BS. ByteString -> SigningKey PaymentKey
265
+ parseKey k
266
+ = let ~ (Right k') = parseSigningKeyBase16 k in k'
273
267
274
- keyNameGenesisInputFund :: KeyName
275
- keyNameGenesisInputFund = KeyName " GenesisInputFund"
268
+ keyNameGenesisInputFund :: String
269
+ keyNameGenesisInputFund = " GenesisInputFund"
276
270
277
- keyNameTxGenFunds :: KeyName
278
- keyNameTxGenFunds = KeyName " TxGenFunds"
271
+ keyNameTxGenFunds :: String
272
+ keyNameTxGenFunds = " TxGenFunds"
279
273
280
274
{-|
281
275
The key that is used for the very first transaction, i.e. the secure Genesis transaction.
282
276
addr_test1vzd3muund27y5nw83vymqj3a83pcuzkkejej6s75e5lfjcc85nc3p is the actual address (in Testnet 42).
283
277
It is also used as change addresse in the first splitting-step.
284
278
-}
285
- keyTxGenFunds :: TextEnvelope
279
+ keyTxGenFunds :: SigningKey PaymentKey
286
280
keyTxGenFunds = parseKey " 5820617f846fc8b0e753bd51790de5f5a916de500175c6f5a0e27dde9da7879e1d35"
287
281
288
- keyNameSplitPhase :: KeyName
289
- keyNameSplitPhase = KeyName " SplitPhase"
282
+ keyNameSplitPhase :: String
283
+ keyNameSplitPhase = " SplitPhase"
290
284
291
285
{-|
292
286
UTxOs that are generated in intermediate splitting steps use:
293
287
addr_test1vz45dtkyzk6s3245qw8hmaddaatcx8td3pvmntl8ty7q99c22eahm
294
288
-}
295
289
296
- keySplitPhase :: TextEnvelope
290
+ keySplitPhase :: SigningKey PaymentKey
297
291
keySplitPhase = parseKey " 5820cf0083c2a5d4c90ab255bc8e68f407d52eebd9408de60a0b9e4c468f9714f076"
298
292
299
293
{-|
300
294
UTxOs of the final splitting steps, i.e. the inputs of the benchmarking phase, use:
301
295
addr_test1vzj7zv9msmdasvy5nc9jhnn2gqvrvu33v5rlg332zdfrkugklxkau
302
296
(Plutus script addresses are ofc different.)
303
297
-}
304
- keyNameBenchmarkInputs :: KeyName
305
- keyNameBenchmarkInputs = KeyName " BenchmarkInputs"
298
+ keyNameBenchmarkInputs :: String
299
+ keyNameBenchmarkInputs = " BenchmarkInputs"
306
300
307
- keyBenchmarkInputs :: TextEnvelope
301
+ keyBenchmarkInputs :: SigningKey PaymentKey
308
302
keyBenchmarkInputs = parseKey " 58205b7f272602661d4ad3d9a4081f25fdcdcdf64fdc4892107de50e50937b77ea42"
309
303
310
- keyNameBenchmarkDone :: KeyName
311
- keyNameBenchmarkDone = KeyName " BenchmarkingDone"
304
+ keyNameBenchmarkDone :: String
305
+ keyNameBenchmarkDone = " BenchmarkingDone"
312
306
313
307
{-|
314
308
The output of the actual benchmarking transactions use:
@@ -317,15 +311,15 @@ Query the progress of the benchmarking phase:
317
311
`cardano-node query utxo --testnet-magic 42 --address addr_test1vz4qz2ayucp7xvnthrx93uhha7e04gvxttpnuq4e6mx2n5gzfw23z`
318
312
-}
319
313
320
- keyBenchmarkDone :: TextEnvelope
314
+ keyBenchmarkDone :: SigningKey PaymentKey
321
315
keyBenchmarkDone = parseKey " 582016ca4f13fa17557e56a7d0dd3397d747db8e1e22fdb5b9df638abdb680650d50"
322
316
323
- keyNameCollaterals :: KeyName
324
- keyNameCollaterals = KeyName " Collaterals"
317
+ keyNameCollaterals :: String
318
+ keyNameCollaterals = " Collaterals"
325
319
326
320
{-|
327
321
Collateral inputs for Plutus transactions:
328
322
addr_test1vpckd9muw3l4f8ne4uzumy28p0k84rvx48q46kssjkta5ng4v6sfs
329
323
-}
330
- keyCollaterals :: TextEnvelope
324
+ keyCollaterals :: SigningKey PaymentKey
331
325
keyCollaterals = parseKey " 58204babdb63537ccdac393ea23d042af3b7c3587d7dc88ed3b66c959f198ad358fa"
0 commit comments