Skip to content

Commit 6e22c35

Browse files
committed
Improvements to design and code style
- Split online and offline functionality into subcommands - Use point free style to improve code layout - Replace `evalEitherM` and `readJsonFile` with `readJsonFileOk` - Remove parametrisation on era Co-authored-by: Mateusz Galazyn <[email protected]>
1 parent 0a2b340 commit 6e22c35

11 files changed

+285
-189
lines changed

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Command.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Cardano.CLI.Type.Common
4444
import Cardano.CLI.Type.Governance
4545

4646
import Data.Text (Text)
47+
import Data.Universe (Some)
4748

4849
import Vary (Vary)
4950

@@ -280,11 +281,10 @@ data NodeContextInfoSource era
280281
-- | Transaction context, required to evaluate the execution
281282
-- costs of the plutus scripts in the transaction.
282283
data TransactionContext era = TransactionContext
283-
{ shelleyBasedEra :: ShelleyBasedEra era
284-
, systemStartSource :: SystemStartOrGenesisFileSource
284+
{ systemStartSource :: SystemStartOrGenesisFileSource
285285
, mustExtendSafeZone :: MustExtendSafeZone
286286
, eraHistoryFile :: File EraHistory In
287-
, utxoFile :: File (UTxO era) In
287+
, utxoFile :: File (Some UTxO) In
288288
, protocolParamsFile :: ProtocolParamsFile
289289
}
290290

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Option.hs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.Foldable
2525
import Data.Function ((&))
2626
import Data.Functor
2727
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
28+
import Data.Universe (Some)
2829
import Options.Applicative hiding (help, str)
2930
import Options.Applicative qualified as Opt
3031
import Options.Applicative.Help qualified as H
@@ -109,7 +110,7 @@ pTransactionCmds era' envCli =
109110
, Just $
110111
Opt.hsubparser $
111112
commandWithMetavar "calculate-plutus-script-cost" $
112-
Opt.info (pTransactionCalculatePlutusScriptCost era' envCli) $
113+
Opt.info (pTransactionCalculatePlutusScriptCost envCli) $
113114
Opt.progDesc "Calculate the costs of the Plutus scripts of a given transaction."
114115
, Just $ pCalculateMinRequiredUtxoBackwardCompatible era'
115116
, Just $
@@ -402,25 +403,37 @@ pTransactionCalculateMinReqUTxO era' =
402403
<*> pTxOutShelleyBased
403404

404405
pTransactionCalculatePlutusScriptCost
405-
:: ShelleyBasedEra era -> EnvCli -> Parser (TransactionCmds era)
406-
pTransactionCalculatePlutusScriptCost sbe envCli =
407-
fmap TransactionCalculatePlutusScriptCostCmd $
408-
TransactionCalculatePlutusScriptCostCmdArgs
409-
<$> pNodeContext sbe envCli
410-
<*> pTxInputFile
411-
<*> optional pOutputFile
406+
:: EnvCli -> Parser (TransactionCmds era)
407+
pTransactionCalculatePlutusScriptCost envCli =
408+
( Opt.hsubparser
409+
. commandWithMetavar "online"
410+
. Opt.info (pTransactionCalculatePlutusScriptCostParams (pNodeConnectionInfo envCli))
411+
$ Opt.progDesc
412+
"Connect to a running node to get context info and calculate the costs of the Plutus scripts of a given transaction."
413+
)
414+
<|> ( Opt.hsubparser
415+
. commandWithMetavar "offline"
416+
. Opt.info (pTransactionCalculatePlutusScriptCostParams pLocalContext)
417+
$ Opt.progDesc
418+
"Manually provide get context info and calculate the costs of the Plutus scripts of a given transaction."
419+
)
412420
where
421+
pTransactionCalculatePlutusScriptCostParams nodeContext =
422+
TransactionCalculatePlutusScriptCostCmd
423+
<$> ( TransactionCalculatePlutusScriptCostCmdArgs
424+
<$> nodeContext
425+
<*> pTxInputFile
426+
<*> optional pOutputFile
427+
)
428+
413429
pTxInputFile :: Parser FilePath
414430
pTxInputFile = parseFilePath "tx-file" "Filepath of the transaction whose Plutus scripts to calculate the cost."
415431

416-
pNodeContext :: ShelleyBasedEra era -> EnvCli -> Parser (NodeContextInfoSource era)
417-
pNodeContext sbe envCli = pNodeConnectionInfo sbe <|> pLocalContext envCli
418-
419-
pNodeConnectionInfo :: ShelleyBasedEra era -> Parser (NodeContextInfoSource era)
420-
pNodeConnectionInfo sbe =
432+
pLocalContext :: Parser (NodeContextInfoSource era)
433+
pLocalContext =
421434
ProvidedTransactionContextInfo
422-
<$> ( pure (TransactionContext sbe)
423-
<*> pSystemStart
435+
<$> ( TransactionContext
436+
<$> pSystemStart
424437
<*> pMustExtendEraHistorySafeZone
425438
<*> pEraHistoryFile
426439
<*> pUtxoFile
@@ -496,7 +509,7 @@ pEraHistoryFile =
496509
]
497510
)
498511

499-
pUtxoFile :: Parser (File (UTxO era) In)
512+
pUtxoFile :: Parser (File (Some UTxO) In)
500513
pUtxoFile =
501514
File
502515
<$> ( parseFilePath "utxo-file" $
@@ -507,8 +520,8 @@ pUtxoFile =
507520
]
508521
)
509522

510-
pLocalContext :: EnvCli -> Parser (NodeContextInfoSource era)
511-
pLocalContext envCli =
523+
pNodeConnectionInfo :: EnvCli -> Parser (NodeContextInfoSource era)
524+
pNodeConnectionInfo envCli =
512525
NodeConnectionInfo
513526
<$> ( LocalNodeConnectInfo
514527
<$> pConsensusModeParams

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Run.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ import Data.Set qualified as Set
113113
import Data.Text qualified as Text
114114
import Data.Text.IO qualified as Text
115115
import Data.Type.Equality (TestEquality (..))
116+
import Data.Universe (Some)
116117
import GHC.Exts (IsList (..))
117118
import Lens.Micro ((^.))
118119
import System.IO qualified as IO
@@ -1723,7 +1724,7 @@ runTransactionCalculatePlutusScriptCostCmd
17231724
systemStartSource
17241725
mustExtendSafeZone
17251726
eraHistoryFile
1726-
(castUtxoFileEra utxoFile)
1727+
utxoFile
17271728
protocolParamsFile
17281729

17291730
Refl <-
@@ -1775,15 +1776,12 @@ runTransactionCalculatePlutusScriptCostCmd
17751776
)
17761777
$ encodePretty scriptCostOutput
17771778

1778-
castUtxoFileEra :: File (UTxO era1) In -> File (UTxO era2) In
1779-
castUtxoFileEra (File x) = File x
1780-
17811779
buildTransactionContext
17821780
:: ShelleyBasedEra era
17831781
-> SystemStartOrGenesisFileSource
17841782
-> MustExtendSafeZone
17851783
-> File EraHistory In
1786-
-> File (UTxO era) In
1784+
-> File (Some UTxO) In
17871785
-> ProtocolParamsFile
17881786
-> ExceptT
17891787
TxCmdError

cardano-cli/test/cardano-cli-golden/files/golden/help.cli

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,26 +3144,40 @@ Usage: cardano-cli conway transaction calculate-min-required-utxo --protocol-par
31443144
Calculate the minimum required UTxO for a transaction output.
31453145

31463146
Usage: cardano-cli conway transaction calculate-plutus-script-cost
3147-
( ( --start-time-utc UTC_TIME
3148-
| --start-time-posix POSIX_TIME
3149-
| --genesis-file FILEPATH
3150-
)
3151-
[--unsafe-extend-safe-zone]
3152-
--era-history-file FILEPATH
3153-
--utxo-file FILEPATH
3154-
--protocol-params-file FILEPATH
3155-
| [--cardano-mode
3156-
[--epoch-slots SLOTS]]
3157-
( --mainnet
3158-
| --testnet-magic NATURAL
3159-
)
3160-
--socket-path SOCKET_PATH
3147+
( online
3148+
| offline
31613149
)
3162-
--tx-file FILEPATH
3163-
[--out-file FILEPATH]
31643150

31653151
Calculate the costs of the Plutus scripts of a given transaction.
31663152

3153+
Usage: cardano-cli conway transaction calculate-plutus-script-cost online
3154+
[--cardano-mode
3155+
[--epoch-slots SLOTS]]
3156+
( --mainnet
3157+
| --testnet-magic NATURAL
3158+
)
3159+
--socket-path SOCKET_PATH
3160+
--tx-file FILEPATH
3161+
[--out-file FILEPATH]
3162+
3163+
Connect to a running node to get context info and calculate the costs of the
3164+
Plutus scripts of a given transaction.
3165+
3166+
Usage: cardano-cli conway transaction calculate-plutus-script-cost offline
3167+
( --start-time-utc UTC_TIME
3168+
| --start-time-posix POSIX_TIME
3169+
| --genesis-file FILEPATH
3170+
)
3171+
[--unsafe-extend-safe-zone]
3172+
--era-history-file FILEPATH
3173+
--utxo-file FILEPATH
3174+
--protocol-params-file FILEPATH
3175+
--tx-file FILEPATH
3176+
[--out-file FILEPATH]
3177+
3178+
Manually provide get context info and calculate the costs of the Plutus
3179+
scripts of a given transaction.
3180+
31673181
Usage: cardano-cli conway transaction calculate-min-value --protocol-params-file FILEPATH
31683182
--tx-out ADDRESS VALUE
31693183
[ --tx-out-datum-hash HASH
@@ -5321,26 +5335,40 @@ Usage: cardano-cli latest transaction calculate-min-required-utxo --protocol-par
53215335
Calculate the minimum required UTxO for a transaction output.
53225336

53235337
Usage: cardano-cli latest transaction calculate-plutus-script-cost
5324-
( ( --start-time-utc UTC_TIME
5325-
| --start-time-posix POSIX_TIME
5326-
| --genesis-file FILEPATH
5327-
)
5328-
[--unsafe-extend-safe-zone]
5329-
--era-history-file FILEPATH
5330-
--utxo-file FILEPATH
5331-
--protocol-params-file FILEPATH
5332-
| [--cardano-mode
5333-
[--epoch-slots SLOTS]]
5334-
( --mainnet
5335-
| --testnet-magic NATURAL
5336-
)
5337-
--socket-path SOCKET_PATH
5338+
( online
5339+
| offline
53385340
)
5339-
--tx-file FILEPATH
5340-
[--out-file FILEPATH]
53415341

53425342
Calculate the costs of the Plutus scripts of a given transaction.
53435343

5344+
Usage: cardano-cli latest transaction calculate-plutus-script-cost online
5345+
[--cardano-mode
5346+
[--epoch-slots SLOTS]]
5347+
( --mainnet
5348+
| --testnet-magic NATURAL
5349+
)
5350+
--socket-path SOCKET_PATH
5351+
--tx-file FILEPATH
5352+
[--out-file FILEPATH]
5353+
5354+
Connect to a running node to get context info and calculate the costs of the
5355+
Plutus scripts of a given transaction.
5356+
5357+
Usage: cardano-cli latest transaction calculate-plutus-script-cost offline
5358+
( --start-time-utc UTC_TIME
5359+
| --start-time-posix POSIX_TIME
5360+
| --genesis-file FILEPATH
5361+
)
5362+
[--unsafe-extend-safe-zone]
5363+
--era-history-file FILEPATH
5364+
--utxo-file FILEPATH
5365+
--protocol-params-file FILEPATH
5366+
--tx-file FILEPATH
5367+
[--out-file FILEPATH]
5368+
5369+
Manually provide get context info and calculate the costs of the Plutus
5370+
scripts of a given transaction.
5371+
53445372
Usage: cardano-cli latest transaction calculate-min-value --protocol-params-file FILEPATH
53455373
--tx-out ADDRESS VALUE
53465374
[ --tx-out-datum-hash HASH
Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,16 @@
11
Usage: cardano-cli conway transaction calculate-plutus-script-cost
2-
( ( --start-time-utc UTC_TIME
3-
| --start-time-posix POSIX_TIME
4-
| --genesis-file FILEPATH
5-
)
6-
[--unsafe-extend-safe-zone]
7-
--era-history-file FILEPATH
8-
--utxo-file FILEPATH
9-
--protocol-params-file FILEPATH
10-
| [--cardano-mode
11-
[--epoch-slots SLOTS]]
12-
( --mainnet
13-
| --testnet-magic NATURAL
14-
)
15-
--socket-path SOCKET_PATH
2+
( online
3+
| offline
164
)
17-
--tx-file FILEPATH
18-
[--out-file FILEPATH]
195

206
Calculate the costs of the Plutus scripts of a given transaction.
217

228
Available options:
23-
--start-time-utc UTC_TIME
24-
The genesis start time in YYYY-MM-DDThh:mm:ssZ
25-
format.
26-
--start-time-posix POSIX_TIME
27-
The genesis start time as POSIX seconds.
28-
--genesis-file FILEPATH Path to the Byron genesis file from which to get the
29-
start time.
30-
--unsafe-extend-safe-zone
31-
Allow overriding the validity of the era history past
32-
the safe zone. The safe zone is a period of time
33-
during which we are sure there won't be any era
34-
transition (hard fork), and we are confident that the
35-
slot duration will not change, thus the conversion
36-
from slot numbers to POSIX times using the era
37-
history will be correct. This safe zone is
38-
conservative. Even if we are past the safe zone, if
39-
there hasn't been any era transition (hard fork)
40-
since we obtained it, we can continue safely using
41-
the era history. This flag essentially disables the
42-
safe zone check. This allows the user to use the era
43-
history past the safe zone, at the user's discretion.
44-
--era-history-file FILEPATH
45-
Filepath of the era history file as produced by the
46-
'query era-history' command. The era history contains
47-
information about when era transitions happened and
48-
can be used together with the start time to convert
49-
slot numbers to POSIX times.
50-
--utxo-file FILEPATH Filepath to a JSON-encoded UTxO file as produced by
51-
the 'query utxo' command. Only UTxOs referenced by
52-
the transaction are needed, not the whole UTxO, but
53-
unnecessary info will be ignored.
54-
--protocol-params-file FILEPATH
55-
Filepath of the JSON-encoded protocol parameters file
56-
--cardano-mode For talking to a node running in full Cardano mode
57-
(default).
58-
--epoch-slots SLOTS The number of slots per epoch for the Byron era.
59-
(default: 21600)
60-
--mainnet Use the mainnet magic id. This overrides the
61-
CARDANO_NODE_NETWORK_ID environment variable
62-
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
63-
CARDANO_NODE_NETWORK_ID environment variable
64-
--socket-path SOCKET_PATH
65-
Path to the node socket. This overrides the
66-
CARDANO_NODE_SOCKET_PATH environment variable. The
67-
argument is optional if CARDANO_NODE_SOCKET_PATH is
68-
defined and mandatory otherwise.
69-
--tx-file FILEPATH Filepath of the transaction whose Plutus scripts to
70-
calculate the cost.
71-
--out-file FILEPATH The output file.
729
-h,--help Show this help text
10+
11+
Available commands:
12+
online Connect to a running node to get context info and
13+
calculate the costs of the Plutus scripts of a given
14+
transaction.
15+
offline Manually provide get context info and calculate the
16+
costs of the Plutus scripts of a given transaction.

0 commit comments

Comments
 (0)