Skip to content

Commit fc61ef4

Browse files
committed
Consistent output for the query ledger-state command
1 parent 629d085 commit fc61ef4

File tree

7 files changed

+55
-33
lines changed

7 files changed

+55
-33
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
@@ -142,6 +142,7 @@ data QueryUTxOCmdArgs = QueryUTxOCmdArgs
142142

143143
data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
144144
{ commons :: !QueryCommons
145+
, outputFormat :: !(Vary [FormatJson, FormatText])
145146
, mOutFile :: !(Maybe (File () Out))
146147
}
147148
deriving (Generic, Show)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ pQueryLedgerStateCmd era envCli =
415415
fmap QueryLedgerStateCmd $
416416
QueryLedgerStateCmdArgs
417417
<$> pQueryCommons era envCli
418+
<*> pFormatFlags
419+
"ledger-state query output"
420+
[ flagFormatJson & setDefault
421+
, flagFormatText
422+
]
418423
<*> pMaybeOutputFile
419424

420425
pQueryLedgerPeerSnapshotCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)

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

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -791,24 +791,47 @@ runQueryLedgerStateCmd
791791
{ Cmd.nodeConnInfo
792792
, Cmd.target
793793
}
794+
, Cmd.outputFormat
794795
, Cmd.mOutFile
795796
}
796797
) = do
797-
join $
798-
lift
799-
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
800-
AnyCardanoEra era <- easyRunQueryCurrentEra
801-
802-
sbe <-
803-
requireShelleyBasedEra era
804-
& onNothing (left QueryCmdByronEra)
805-
806-
result <- easyRunQuery (queryDebugLedgerState sbe)
798+
writeOutputContents <- case mOutFile of
799+
Nothing -> pure LBS.putStr
800+
Just (File fpath) -> pure $ LBS.writeFile fpath
801+
802+
contents <-
803+
join $
804+
lift
805+
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
806+
AnyCardanoEra era <- easyRunQueryCurrentEra
807+
808+
sbe <-
809+
requireShelleyBasedEra era
810+
& onNothing (left QueryCmdByronEra)
811+
812+
result <- easyRunQuery (queryDebugLedgerState sbe)
813+
814+
pure $
815+
shelleyBasedEraConstraints sbe $
816+
outputFormat
817+
& ( id
818+
. Vary.on
819+
( \FormatJson -> case decodeDebugLedgerState result of
820+
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
821+
Right ledgerState -> pure $ Aeson.encode ledgerState <> "\n"
822+
)
823+
. Vary.on
824+
( \FormatText ->
825+
let SerialisedDebugLedgerState serLedgerState = result
826+
in pure $ unSerialised serLedgerState
827+
)
828+
$ Vary.exhaustiveCase
829+
)
830+
)
831+
& onLeft (left . QueryCmdAcquireFailure)
832+
& onLeft left
807833

808-
pure $ shelleyBasedEraConstraints sbe (writeLedgerState mOutFile) result
809-
)
810-
& onLeft (left . QueryCmdAcquireFailure)
811-
& onLeft left
834+
liftIO $ writeOutputContents contents
812835

813836
runQueryLedgerPeerSnapshot
814837
:: ()
@@ -1046,25 +1069,6 @@ writeStakeAddressInfo
10461069
mDRep = Map.lookup addr voteDelegatees
10471070
]
10481071

1049-
writeLedgerState
1050-
:: forall era ledgerera
1051-
. ShelleyLedgerEra era ~ ledgerera
1052-
=> ToJSON (DebugLedgerState era)
1053-
=> FromCBOR (DebugLedgerState era)
1054-
=> Maybe (File () Out)
1055-
-> SerialisedDebugLedgerState era
1056-
-> ExceptT QueryCmdError IO ()
1057-
writeLedgerState mOutFile qState@(SerialisedDebugLedgerState serLedgerState) =
1058-
case mOutFile of
1059-
Nothing ->
1060-
case decodeDebugLedgerState qState of
1061-
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
1062-
Right ledgerState -> liftIO . LBS.putStrLn $ Aeson.encode ledgerState
1063-
Just (File fpath) ->
1064-
handleIOExceptT (QueryCmdWriteFileError . FileIOError fpath) $
1065-
LBS.writeFile fpath $
1066-
unSerialised serLedgerState
1067-
10681072
-- | Writes JSON-encoded big ledger peer snapshot
10691073
writeLedgerPeerSnapshot
10701074
:: Maybe (File () Out)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Usage: cardano-cli query ledger-state [--cardano-mode [--epoch-slots SLOTS]]
371371
(--mainnet | --testnet-magic NATURAL)
372372
--socket-path SOCKET_PATH
373373
[--volatile-tip | --immutable-tip]
374+
[--output-json | --output-text]
374375
[--out-file FILEPATH]
375376

376377
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -2062,6 +2063,7 @@ Usage: cardano-cli conway query ledger-state
20622063
[ --volatile-tip
20632064
| --immutable-tip
20642065
]
2066+
[--output-json | --output-text]
20652067
[--out-file FILEPATH]
20662068

20672069
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -4298,6 +4300,7 @@ Usage: cardano-cli latest query ledger-state
42984300
[ --volatile-tip
42994301
| --immutable-tip
43004302
]
4303+
[--output-json | --output-text]
43014304
[--out-file FILEPATH]
43024305

43034306
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Usage: cardano-cli conway query ledger-state
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[--output-json | --output-text]
1112
[--out-file FILEPATH]
1213

1314
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -30,5 +31,7 @@ Available options:
3031
--volatile-tip Use the volatile tip as a target. (This is the
3132
default)
3233
--immutable-tip Use the immutable tip as a target.
34+
--output-json Format ledger-state query output to JSON (default).
35+
--output-text Format ledger-state query output to TEXT.
3336
--out-file FILEPATH Optional output file. Default is to write to stdout.
3437
-h,--help Show this help text

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Usage: cardano-cli latest query ledger-state
88
[ --volatile-tip
99
| --immutable-tip
1010
]
11+
[--output-json | --output-text]
1112
[--out-file FILEPATH]
1213

1314
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -30,5 +31,7 @@ Available options:
3031
--volatile-tip Use the volatile tip as a target. (This is the
3132
default)
3233
--immutable-tip Use the immutable tip as a target.
34+
--output-json Format ledger-state query output to JSON (default).
35+
--output-text Format ledger-state query output to TEXT.
3336
--out-file FILEPATH Optional output file. Default is to write to stdout.
3437
-h,--help Show this help text

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Usage: cardano-cli query ledger-state [--cardano-mode [--epoch-slots SLOTS]]
22
(--mainnet | --testnet-magic NATURAL)
33
--socket-path SOCKET_PATH
44
[--volatile-tip | --immutable-tip]
5+
[--output-json | --output-text]
56
[--out-file FILEPATH]
67

78
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
@@ -24,5 +25,7 @@ Available options:
2425
--volatile-tip Use the volatile tip as a target. (This is the
2526
default)
2627
--immutable-tip Use the immutable tip as a target.
28+
--output-json Format ledger-state query output to JSON (default).
29+
--output-text Format ledger-state query output to TEXT.
2730
--out-file FILEPATH Optional output file. Default is to write to stdout.
2831
-h,--help Show this help text

0 commit comments

Comments
 (0)