Skip to content

Commit c6d5dd3

Browse files
committed
Output flags for 'query protocol-state' command
1 parent 8a64332 commit c6d5dd3

File tree

7 files changed

+73
-23
lines changed

7 files changed

+73
-23
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ pQueryProtocolStateCmd era envCli =
455455
fmap QueryProtocolStateCmd $
456456
QueryProtocolStateCmdArgs
457457
<$> pQueryCommons era envCli
458+
<*> pFormatFlags
459+
"protocol-state query output"
460+
[ flagFormatCborBin
461+
, flagFormatCborHex
462+
, flagFormatJson & setDefault
463+
, flagFormatYaml
464+
]
458465
<*> pMaybeOutputFile
459466

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

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ runQueryProtocolStateCmd
936936
{ Cmd.nodeConnInfo
937937
, Cmd.target
938938
}
939+
, Cmd.outputFormat
939940
, Cmd.mOutFile
940941
}
941942
) = do
@@ -949,46 +950,45 @@ runQueryProtocolStateCmd
949950
requireShelleyBasedEra era
950951
& onNothing (left QueryCmdByronEra)
951952

952-
result <- easyRunQuery (queryProtocolState sbe)
953+
ps <- easyRunQuery (queryProtocolState sbe)
953954

954-
pure $ writeProtocolState sbe result
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
955971
)
956972
& onLeft (left . QueryCmdAcquireFailure)
957973
& onLeft left
958974

959975
pure ()
960976
where
961-
protocolStateToJsonLazyByteString
977+
protocolStateToChainDepState
962978
:: ShelleyBasedEra era
963979
-> ProtocolState era
964-
-> ExceptT QueryCmdError IO LBS.ByteString
965-
protocolStateToJsonLazyByteString sbe ps =
980+
-> ExceptT QueryCmdError IO (Consensus.ChainDepState (ConsensusProtocol era))
981+
protocolStateToChainDepState sbe ps =
966982
shelleyBasedEraConstraints sbe $ do
967-
chainDepState <-
968-
pure (decodeProtocolState ps)
969-
& onLeft (left . QueryCmdProtocolStateDecodeFailure)
970-
pure $ Aeson.encodePretty chainDepState
983+
pure (decodeProtocolState ps)
984+
& onLeft (left . QueryCmdProtocolStateDecodeFailure)
971985

972986
protocolStateToCborBinary
973987
:: ProtocolState era
974988
-> ExceptT QueryCmdError IO LBS.ByteString
975989
protocolStateToCborBinary (ProtocolState pstate) =
976990
pure $ unSerialised pstate
977991

978-
writeProtocolState
979-
:: forall era
980-
. ShelleyBasedEra era
981-
-> ProtocolState era
982-
-> ExceptT QueryCmdError IO ()
983-
writeProtocolState sbe ps = do
984-
output <- case mOutFile of
985-
Nothing -> protocolStateToJsonLazyByteString sbe ps
986-
Just _ -> protocolStateToCborBinary ps
987-
988-
firstExceptT QueryCmdWriteFileError
989-
. newExceptT
990-
$ writeLazyByteStringOutput mOutFile output
991-
992992
-- | Query the current delegations and reward accounts, filtered by a given
993993
-- set of addresses, from a Shelley node via the local state query protocol.
994994
runQueryStakeAddressInfoCmd

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ Usage: cardano-cli query protocol-state [--cardano-mode [--epoch-slots SLOTS]]
388388
(--mainnet | --testnet-magic NATURAL)
389389
--socket-path SOCKET_PATH
390390
[--volatile-tip | --immutable-tip]
391+
[ --output-cbor-bin
392+
| --output-cbor-hex
393+
| --output-json
394+
| --output-yaml
395+
]
391396
[--out-file FILEPATH]
392397

393398
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -2166,6 +2171,11 @@ Usage: cardano-cli conway query protocol-state
21662171
[ --volatile-tip
21672172
| --immutable-tip
21682173
]
2174+
[ --output-cbor-bin
2175+
| --output-cbor-hex
2176+
| --output-json
2177+
| --output-yaml
2178+
]
21692179
[--out-file FILEPATH]
21702180

21712181
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -4428,6 +4438,11 @@ Usage: cardano-cli latest query protocol-state
44284438
[ --volatile-tip
44294439
| --immutable-tip
44304440
]
4441+
[ --output-cbor-bin
4442+
| --output-cbor-hex
4443+
| --output-json
4444+
| --output-yaml
4445+
]
44314446
[--out-file FILEPATH]
44324447

44334448
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced

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

cardano-cli/test/cardano-cli-golden/files/golden/help/latest_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 latest 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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ Usage: cardano-cli query protocol-state [--cardano-mode [--epoch-slots SLOTS]]
22
(--mainnet | --testnet-magic NATURAL)
33
--socket-path SOCKET_PATH
44
[--volatile-tip | --immutable-tip]
5+
[ --output-cbor-bin
6+
| --output-cbor-hex
7+
| --output-json
8+
| --output-yaml
9+
]
510
[--out-file FILEPATH]
611

712
Dump the current protocol state of the node (Ledger.ChainDepState -- advanced
@@ -24,5 +29,9 @@ Available options:
2429
--volatile-tip Use the volatile tip as a target. (This is the
2530
default)
2631
--immutable-tip Use the immutable tip as a target.
32+
--output-cbor-bin Format protocol-state query output to CBOR.
33+
--output-cbor-hex Format protocol-state query output to BASE16 CBOR.
34+
--output-json Format protocol-state query output to JSON (default).
35+
--output-yaml Format protocol-state query output to YAML.
2736
--out-file FILEPATH Optional output file. Default is to write to stdout.
2837
-h,--help Show this help text

0 commit comments

Comments
 (0)