Skip to content

Consistent output for query ledger-state cmd #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ data QueryUTxOCmdArgs = QueryUTxOCmdArgs

data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
{ commons :: !QueryCommons
, outputFormat :: !(Vary [FormatJson, FormatText])
, mOutFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)
Expand Down
5 changes: 5 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Option.hs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ pQueryLedgerStateCmd era envCli =
fmap QueryLedgerStateCmd $
QueryLedgerStateCmdArgs
<$> pQueryCommons era envCli
<*> pFormatFlags
"ledger-state query output"
[ flagFormatJson & setDefault
, flagFormatText
]
<*> pMaybeOutputFile

pQueryLedgerPeerSnapshotCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
Expand Down
80 changes: 46 additions & 34 deletions cardano-cli/src/Cardano/CLI/EraBased/Query/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -791,24 +791,55 @@ runQueryLedgerStateCmd
{ Cmd.nodeConnInfo
, Cmd.target
}
, Cmd.outputFormat
, Cmd.mOutFile
}
) = do
join $
lift
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- easyRunQueryCurrentEra

sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)

result <- easyRunQuery (queryDebugLedgerState sbe)

pure $ shelleyBasedEraConstraints sbe (writeLedgerState mOutFile) result
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left
writeOutputContents <- case mOutFile of
Nothing -> pure LBS.putStr
Just (File fpath) -> pure $ LBS.writeFile fpath

contents <-
join $
lift
( executeLocalStateQueryExpr nodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- easyRunQueryCurrentEra

sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)

serialisedDebugLedgerState <- easyRunQuery (queryDebugLedgerState sbe)

pure $
shelleyBasedEraConstraints sbe $
outputFormat
& ( id
. Vary.on (\FormatJson -> ledgerStateAsJsonByteString serialisedDebugLedgerState)
. Vary.on (\FormatText -> ledgerStateAsTextByteString serialisedDebugLedgerState)
$ Vary.exhaustiveCase
)
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

liftIO $ writeOutputContents contents

ledgerStateAsJsonByteString
:: IsShelleyBasedEra era
=> SerialisedDebugLedgerState era
-> ExceptT QueryCmdError IO LBS.ByteString
ledgerStateAsJsonByteString serialisedDebugLedgerState =
case decodeDebugLedgerState serialisedDebugLedgerState of
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ cborToTextByteString bs
Right decodededgerState -> pure $ Aeson.encode decodededgerState <> "\n"

ledgerStateAsTextByteString
:: Applicative f
=> SerialisedDebugLedgerState era -> f LBS.ByteString
ledgerStateAsTextByteString serialisedDebugLedgerState =
let SerialisedDebugLedgerState serLedgerState = serialisedDebugLedgerState
in pure $ unSerialised serLedgerState

runQueryLedgerPeerSnapshot
:: ()
Expand Down Expand Up @@ -1046,25 +1077,6 @@ writeStakeAddressInfo
mDRep = Map.lookup addr voteDelegatees
]

writeLedgerState
:: forall era ledgerera
. ShelleyLedgerEra era ~ ledgerera
=> ToJSON (DebugLedgerState era)
=> FromCBOR (DebugLedgerState era)
=> Maybe (File () Out)
-> SerialisedDebugLedgerState era
-> ExceptT QueryCmdError IO ()
writeLedgerState mOutFile qState@(SerialisedDebugLedgerState serLedgerState) =
case mOutFile of
Nothing ->
case decodeDebugLedgerState qState of
Left (bs, _decoderError) -> firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
Right ledgerState -> liftIO . LBS.putStrLn $ Aeson.encode ledgerState
Just (File fpath) ->
handleIOExceptT (QueryCmdWriteFileError . FileIOError fpath) $
LBS.writeFile fpath $
unSerialised serLedgerState

-- | Writes JSON-encoded big ledger peer snapshot
writeLedgerPeerSnapshot
:: Maybe (File () Out)
Expand Down
7 changes: 7 additions & 0 deletions cardano-cli/src/Cardano/CLI/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module Cardano.CLI.Helper
( HelpersError (..)
, cborToText
, cborToTextByteString
, printWarning
, deprecationWarning
, ensureNewFile
Expand Down Expand Up @@ -37,6 +38,7 @@ import Data.ByteString.Lazy qualified as LB
import Data.Functor (void)
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import Data.Text.IO qualified as Text
import Data.Typeable (Typeable)
import System.Console.ANSI
Expand Down Expand Up @@ -107,6 +109,11 @@ pPrintCBOR bs = do
unless (LB.null remaining) $
pPrintCBOR remaining

cborToTextByteString :: LB.ByteString -> ExceptT HelpersError IO LB.ByteString
cborToTextByteString bs = do
text <- cborToText bs
pure $ LB.fromStrict $ Text.encodeUtf8 text

cborToText :: LB.ByteString -> ExceptT HelpersError IO Text
cborToText bs = do
as <- cborToTextList bs
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ Usage: cardano-cli query ledger-state [--cardano-mode [--epoch-slots SLOTS]]
(--mainnet | --testnet-magic NATURAL)
--socket-path SOCKET_PATH
[--volatile-tip | --immutable-tip]
[--output-json | --output-text]
[--out-file FILEPATH]

Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
Expand Down Expand Up @@ -2062,6 +2063,7 @@ Usage: cardano-cli conway query ledger-state
[ --volatile-tip
| --immutable-tip
]
[--output-json | --output-text]
[--out-file FILEPATH]

Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
Expand Down Expand Up @@ -4298,6 +4300,7 @@ Usage: cardano-cli latest query ledger-state
[ --volatile-tip
| --immutable-tip
]
[--output-json | --output-text]
[--out-file FILEPATH]

Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Usage: cardano-cli conway query ledger-state
[ --volatile-tip
| --immutable-tip
]
[--output-json | --output-text]
[--out-file FILEPATH]

Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
Expand All @@ -30,5 +31,7 @@ Available options:
--volatile-tip Use the volatile tip as a target. (This is the
default)
--immutable-tip Use the immutable tip as a target.
--output-json Format ledger-state query output to JSON (default).
--output-text Format ledger-state query output to TEXT.
--out-file FILEPATH Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Usage: cardano-cli latest query ledger-state
[ --volatile-tip
| --immutable-tip
]
[--output-json | --output-text]
[--out-file FILEPATH]

Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
Expand All @@ -30,5 +31,7 @@ Available options:
--volatile-tip Use the volatile tip as a target. (This is the
default)
--immutable-tip Use the immutable tip as a target.
--output-json Format ledger-state query output to JSON (default).
--output-text Format ledger-state query output to TEXT.
--out-file FILEPATH Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Usage: cardano-cli query ledger-state [--cardano-mode [--epoch-slots SLOTS]]
(--mainnet | --testnet-magic NATURAL)
--socket-path SOCKET_PATH
[--volatile-tip | --immutable-tip]
[--output-json | --output-text]
[--out-file FILEPATH]

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