Skip to content

Commit 170158e

Browse files
authored
Merge pull request #1183 from IntersectMBO/newhoggy/more-accurate-format-flags
More accurate format flags
2 parents 3e78626 + 41d0d2e commit 170158e

File tree

17 files changed

+152
-61
lines changed

17 files changed

+152
-61
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,11 +1840,17 @@ flagFormatBech32
18401840
flagFormatBech32 =
18411841
mkFlag "output-bech32" "BECH32" FormatBech32
18421842

1843-
flagFormatCbor
1844-
:: FormatCbor :| fs
1843+
flagFormatCborHex
1844+
:: FormatCborHex :| fs
18451845
=> Flag (Vary fs)
1846-
flagFormatCbor =
1847-
mkFlag "output-cbor" "BASE16 CBOR" FormatCbor
1846+
flagFormatCborHex =
1847+
mkFlag "output-cbor-hex" "BASE16 CBOR" FormatCborHex
1848+
1849+
flagFormatCborBin
1850+
:: FormatCborBin :| fs
1851+
=> Flag (Vary fs)
1852+
flagFormatCborBin =
1853+
mkFlag "output-cbor-bin" "CBOR" FormatCborBin
18481854

18491855
flagFormatHex
18501856
:: FormatHex :| fs

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ data QueryStakeAddressInfoCmdArgs = QueryStakeAddressInfoCmdArgs
138138
data QueryUTxOCmdArgs = QueryUTxOCmdArgs
139139
{ commons :: !QueryCommons
140140
, queryFilter :: !QueryUTxOFilter
141-
, outputFormat :: !(Vary [FormatCbor, FormatJson, FormatText])
141+
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText])
142142
, mOutFile :: !(Maybe (File () Out))
143143
}
144144
deriving (Generic, Show)
@@ -159,6 +159,7 @@ data QueryLedgerPeerSnapshotCmdArgs = QueryLedgerPeerSnapshotCmdArgs
159159

160160
data QueryProtocolStateCmdArgs = QueryProtocolStateCmdArgs
161161
{ commons :: !QueryCommons
162+
, outputFormat :: !(Vary [FormatCborBin, FormatCborHex, FormatJson, FormatYaml])
162163
, mOutFile :: !(Maybe (File () Out))
163164
}
164165
deriving (Generic, Show)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ pQueryUTxOCmd era envCli =
382382
<*> pQueryUTxOFilter
383383
<*> pFormatFlags
384384
"utxo query output"
385-
[ flagFormatCbor
385+
[ flagFormatCborBin
386+
, flagFormatCborHex
386387
, flagFormatJson & setDefault
387388
, flagFormatText
388389
]
@@ -454,6 +455,13 @@ pQueryProtocolStateCmd era envCli =
454455
fmap QueryProtocolStateCmd $
455456
QueryProtocolStateCmdArgs
456457
<$> pQueryCommons era envCli
458+
<*> pFormatFlags
459+
"protocol-state query output"
460+
[ flagFormatCborBin
461+
, flagFormatCborHex
462+
, flagFormatJson & setDefault
463+
, flagFormatYaml
464+
]
457465
<*> pMaybeOutputFile
458466

459467
pAllStakePoolsOrSome :: Parser (AllOrOnly (Hash StakePoolKey))

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

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -936,24 +936,58 @@ runQueryProtocolStateCmd
936936
{ Cmd.nodeConnInfo
937937
, Cmd.target
938938
}
939+
, Cmd.outputFormat
939940
, Cmd.mOutFile
940941
}
941942
) = do
942-
join $
943-
lift
944-
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
945-
AnyCardanoEra era <- easyRunQueryCurrentEra
943+
() <-
944+
join $
945+
lift
946+
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
947+
AnyCardanoEra era <- easyRunQueryCurrentEra
946948

947-
sbe <-
948-
requireShelleyBasedEra era
949-
& onNothing (left QueryCmdByronEra)
949+
sbe <-
950+
requireShelleyBasedEra era
951+
& onNothing (left QueryCmdByronEra)
950952

951-
result <- easyRunQuery (queryProtocolState sbe)
953+
ps <- easyRunQuery (queryProtocolState sbe)
952954

953-
pure $ shelleyBasedEraConstraints sbe $ writeProtocolState sbe mOutFile result
954-
)
955-
& onLeft (left . QueryCmdAcquireFailure)
956-
& onLeft left
955+
pure $ do
956+
output <-
957+
shelleyBasedEraConstraints sbe
958+
$ outputFormat
959+
& ( id
960+
. Vary.on (\FormatCborBin -> protocolStateToCborBinary)
961+
. Vary.on (\FormatCborHex -> fmap Base16.encode . protocolStateToCborBinary)
962+
. Vary.on (\FormatJson -> fmap (Json.encodeJson . toJSON) . protocolStateToChainDepState sbe)
963+
. Vary.on (\FormatYaml -> fmap (Json.encodeYaml . toJSON) . protocolStateToChainDepState sbe)
964+
$ Vary.exhaustiveCase
965+
)
966+
$ ps
967+
968+
firstExceptT QueryCmdWriteFileError
969+
. newExceptT
970+
$ writeLazyByteStringOutput mOutFile output
971+
)
972+
& onLeft (left . QueryCmdAcquireFailure)
973+
& onLeft left
974+
975+
pure ()
976+
where
977+
protocolStateToChainDepState
978+
:: ShelleyBasedEra era
979+
-> ProtocolState era
980+
-> ExceptT QueryCmdError IO (Consensus.ChainDepState (ConsensusProtocol era))
981+
protocolStateToChainDepState sbe ps =
982+
shelleyBasedEraConstraints sbe $ do
983+
pure (decodeProtocolState ps)
984+
& onLeft (left . QueryCmdProtocolStateDecodeFailure)
985+
986+
protocolStateToCborBinary
987+
:: ProtocolState era
988+
-> ExceptT QueryCmdError IO LBS.ByteString
989+
protocolStateToCborBinary (ProtocolState pstate) =
990+
pure $ unSerialised pstate
957991

958992
-- | Query the current delegations and reward accounts, filtered by a given
959993
-- set of addresses, from a Shelley node via the local state query protocol.
@@ -1217,29 +1251,9 @@ writePoolState outputFormat mOutFile serialisedCurrentEpochState = do
12171251
. newExceptT
12181252
$ writeLazyByteStringOutput mOutFile output
12191253

1220-
writeProtocolState
1221-
:: ShelleyBasedEra era
1222-
-> Maybe (File () Out)
1223-
-> ProtocolState era
1224-
-> ExceptT QueryCmdError IO ()
1225-
writeProtocolState sbe mOutFile ps@(ProtocolState pstate) =
1226-
shelleyBasedEraConstraints sbe $
1227-
case mOutFile of
1228-
Nothing -> decodePState ps
1229-
Just (File fpath) -> writePState fpath pstate
1230-
where
1231-
writePState fpath pstate' =
1232-
handleIOExceptT (QueryCmdWriteFileError . FileIOError fpath)
1233-
. LBS.writeFile fpath
1234-
$ unSerialised pstate'
1235-
decodePState ps' =
1236-
case decodeProtocolState ps' of
1237-
Left (bs, _) -> firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
1238-
Right chainDepstate -> liftIO . LBS.putStrLn $ Aeson.encodePretty chainDepstate
1239-
12401254
writeFilteredUTxOs
12411255
:: Api.ShelleyBasedEra era
1242-
-> Vary [FormatCbor, FormatJson, FormatText]
1256+
-> Vary [FormatCborBin, FormatCborHex, FormatJson, FormatText]
12431257
-> Maybe (File () Out)
12441258
-> UTxO era
12451259
-> ExceptT QueryCmdError IO ()
@@ -1248,7 +1262,8 @@ writeFilteredUTxOs sbe format mOutFile utxo = do
12481262
shelleyBasedEraConstraints sbe $
12491263
format
12501264
& ( id
1251-
. Vary.on (\FormatCbor -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
1265+
. Vary.on (\FormatCborBin -> CBOR.serialize $ toLedgerUTxO sbe utxo)
1266+
. Vary.on (\FormatCborHex -> Base16.encode . CBOR.serialize $ toLedgerUTxO sbe utxo)
12521267
. Vary.on (\FormatJson -> Json.encodeJson utxo)
12531268
. Vary.on (\FormatText -> strictTextToLazyBytestring $ filteredUTxOsToText sbe utxo)
12541269
$ Vary.exhaustiveCase

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ where
1010

1111
import Cardano.Api.Shelley
1212

13-
import Cardano.CLI.Type.Common (FormatCbor, FormatText)
13+
import Cardano.CLI.Type.Common (FormatCborHex, FormatText)
1414

1515
import Data.Text (Text)
1616
import Vary
@@ -22,7 +22,7 @@ newtype TextViewCmds era
2222
data TextViewDecodeCborCmdArgs
2323
= TextViewDecodeCborCmdArgs
2424
{ inputFile :: !FilePath
25-
, outputFormat :: !(Vary [FormatCbor, FormatText])
25+
, outputFormat :: !(Vary [FormatCborHex, FormatText])
2626
, mOutFile :: Maybe (File () Out)
2727
}
2828
deriving Show

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pTextViewCmds =
4040
<$> pCBORInFile
4141
<*> pFormatFlags
4242
"text view info output format"
43-
[ flagFormatCbor
43+
[ flagFormatCborHex
4444
, flagFormatText & setDefault
4545
]
4646
<*> pMaybeOutputFile

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ runTextViewInfoCmd
4242
output <-
4343
outputFormat
4444
& ( id
45-
. Vary.on (\FormatCbor -> pure lbCBOR)
45+
. Vary.on (\FormatCborHex -> pure lbCBOR)
4646
. Vary.on (\FormatText -> LBS.fromStrict . Text.encodeUtf8 <$> cborToText lbCBOR)
4747
$ Vary.exhaustiveCase
4848
)

cardano-cli/src/Cardano/CLI/Type/Common.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module Cardano.CLI.Type.Common
2727
, File (..)
2828
, FileDirection (..)
2929
, FormatBech32 (..)
30-
, FormatCbor (..)
30+
, FormatCborBin (..)
31+
, FormatCborHex (..)
3132
, FormatHex (..)
3233
, FormatJson (..)
3334
, FormatText (..)
@@ -479,7 +480,10 @@ data FormatBech32 = FormatBech32
479480
data FormatHex = FormatHex
480481
deriving (Enum, Eq, Ord, Show)
481482

482-
data FormatCbor = FormatCbor
483+
data FormatCborBin = FormatCborBin
484+
deriving (Enum, Eq, Ord, Show)
485+
486+
data FormatCborHex = FormatCborHex
483487
deriving (Enum, Eq, Ord, Show)
484488

485489
data FormatJson = FormatJson

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ Usage: cardano-cli query utxo [--cardano-mode [--epoch-slots SLOTS]]
365365
| (--address ADDRESS)
366366
| (--tx-in TX_IN)
367367
)
368-
[--output-cbor | --output-json | --output-text]
368+
[ --output-cbor-bin
369+
| --output-cbor-hex
370+
| --output-json
371+
| --output-text
372+
]
369373
[--out-file FILEPATH]
370374

371375
Get a portion of the current UTxO: by tx in, by address or the whole.
@@ -384,6 +388,11 @@ Usage: cardano-cli query protocol-state [--cardano-mode [--epoch-slots SLOTS]]
384388
(--mainnet | --testnet-magic NATURAL)
385389
--socket-path SOCKET_PATH
386390
[--volatile-tip | --immutable-tip]
391+
[ --output-cbor-bin
392+
| --output-cbor-hex
393+
| --output-json
394+
| --output-yaml
395+
]
387396
[--out-file FILEPATH]
388397

389398
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -2162,6 +2171,11 @@ Usage: cardano-cli conway query protocol-state
21622171
[ --volatile-tip
21632172
| --immutable-tip
21642173
]
2174+
[ --output-cbor-bin
2175+
| --output-cbor-hex
2176+
| --output-json
2177+
| --output-yaml
2178+
]
21652179
[--out-file FILEPATH]
21662180

21672181
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -2374,7 +2388,8 @@ Usage: cardano-cli conway query utxo [--cardano-mode [--epoch-slots SLOTS]]
23742388
| (--address ADDRESS)
23752389
| (--tx-in TX_IN)
23762390
)
2377-
[ --output-cbor
2391+
[ --output-cbor-bin
2392+
| --output-cbor-hex
23782393
| --output-json
23792394
| --output-text
23802395
]
@@ -2660,7 +2675,7 @@ Usage: cardano-cli conway text-view decode-cbor
26602675
are stored on disk as TextView files.
26612676

26622677
Usage: cardano-cli conway text-view decode-cbor --in-file FILEPATH
2663-
[ --output-cbor
2678+
[ --output-cbor-hex
26642679
| --output-text
26652680
]
26662681
[--out-file FILEPATH]
@@ -4423,6 +4438,11 @@ Usage: cardano-cli latest query protocol-state
44234438
[ --volatile-tip
44244439
| --immutable-tip
44254440
]
4441+
[ --output-cbor-bin
4442+
| --output-cbor-hex
4443+
| --output-json
4444+
| --output-yaml
4445+
]
44264446
[--out-file FILEPATH]
44274447

44284448
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -4635,7 +4655,8 @@ Usage: cardano-cli latest query utxo [--cardano-mode [--epoch-slots SLOTS]]
46354655
| (--address ADDRESS)
46364656
| (--tx-in TX_IN)
46374657
)
4638-
[ --output-cbor
4658+
[ --output-cbor-bin
4659+
| --output-cbor-hex
46394660
| --output-json
46404661
| --output-text
46414662
]
@@ -4921,7 +4942,7 @@ Usage: cardano-cli latest text-view decode-cbor
49214942
are stored on disk as TextView files.
49224943

49234944
Usage: cardano-cli latest text-view decode-cbor --in-file FILEPATH
4924-
[ --output-cbor
4945+
[ --output-cbor-hex
49254946
| --output-text
49264947
]
49274948
[--out-file FILEPATH]

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Usage: cardano-cli conway query protocol-state
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[ --output-cbor-bin
12+
| --output-cbor-hex
13+
| --output-json
14+
| --output-yaml
15+
]
1116
[--out-file FILEPATH]
1217

1318
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -30,5 +35,9 @@ Available options:
3035
--volatile-tip Use the volatile tip as a target. (This is the
3136
default)
3237
--immutable-tip Use the immutable tip as a target.
38+
--output-cbor-bin Format protocol-state query output to CBOR.
39+
--output-cbor-hex Format protocol-state query output to BASE16 CBOR.
40+
--output-json Format protocol-state query output to JSON (default).
41+
--output-yaml Format protocol-state query output to YAML.
3342
--out-file FILEPATH Optional output file. Default is to write to stdout.
3443
-h,--help Show this help text

0 commit comments

Comments
 (0)