Skip to content

Commit

Permalink
Merge pull request #649 from IntersectMBO/smelc/query-leadership-cont…
Browse files Browse the repository at this point in the history
…rol-output-format

query leadership-schedule: add --output-[json,text] flag to control format of the output
  • Loading branch information
smelc authored Mar 15, 2024
2 parents 4ff26c0 + 2ddb708 commit bd22b2e
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 46 deletions.
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ data QueryLeadershipScheduleCmdArgs = QueryLeadershipScheduleCmdArgs
, vrkSkeyFp :: !(SigningKeyFile In)
, whichSchedule :: !EpochLeadershipSchedule
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pLeadershipScheduleCmd era envCli =
<*> pVrfSigningKeyFile
<*> pWhichLeadershipSchedule
<*> pTarget era
<*> (optional $ pQueryOutputFormat "leadership-schedule")
<*> pMaybeOutputFile

pKesPeriodInfoCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand Down
85 changes: 39 additions & 46 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,8 @@ runQueryConstitutionHashCmd
-> Maybe (L.SafeHash L.StandardCrypto L.AnchorData)
-> ExceptT QueryCmdError IO ()
writeConstitutionHash mOutFile' cHash =
case mOutFile' of
Nothing -> liftIO $ LBS.putStrLn (encodePretty cHash)
Just (File fpath) ->
handleIOExceptT (QueryCmdWriteFileError . FileIOError fpath) $
LBS.writeFile fpath (encodePretty cHash)
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile' $ encodePretty cHash

runQueryProtocolParametersCmd :: ()
=> Cmd.QueryProtocolParametersCmdArgs
Expand All @@ -183,12 +180,8 @@ runQueryProtocolParametersCmd
-> L.PParams (ShelleyLedgerEra era)
-> ExceptT QueryCmdError IO ()
writeProtocolParameters sbe mOutFile' pparams =
let apiPParamsJSON = (encodePretty $ fromLedgerPParams sbe pparams)
in case mOutFile' of
Nothing -> liftIO $ LBS.putStrLn apiPParamsJSON
Just (File fpath) ->
handleIOExceptT (QueryCmdWriteFileError . FileIOError fpath) $
LBS.writeFile fpath apiPParamsJSON
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile' $ encodePretty $ fromLedgerPParams sbe pparams

-- | Calculate the percentage sync rendered as text.
percentage
Expand Down Expand Up @@ -302,9 +295,8 @@ runQueryTipCmd
, O.mSyncProgress = mSyncProgress
}

case mOutFile of
Just (File fpath) -> liftIO $ LBS.writeFile fpath $ encodePretty localStateOutput
Nothing -> liftIO $ LBS.putStrLn $ encodePretty localStateOutput
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile $ encodePretty localStateOutput

-- | Query the UTxO, filtered by a given set of addresses, from a Shelley node
-- via the local state query protocol.
Expand Down Expand Up @@ -664,11 +656,8 @@ runQueryTxMempoolCmd
TxMempoolQueryInfo -> pure LocalTxMonitoringMempoolInformation

result <- liftIO $ queryTxMonitoringLocal localNodeConnInfo localQuery
let renderedResult = encodePretty result
case mOutFile of
Nothing -> liftIO $ LBS.putStrLn renderedResult
Just (File oFp) -> handleIOExceptT (QueryCmdWriteFileError . FileIOError oFp)
$ LBS.writeFile oFp renderedResult
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile $ encodePretty result

runQuerySlotNumberCmd :: ()
=> Cmd.QuerySlotNumberCmdArgs
Expand Down Expand Up @@ -1128,8 +1117,7 @@ runQueryStakePoolsCmd
& onLeft (left . QueryCmdUnsupportedNtcVersion)
& onLeft (left . QueryCmdEraMismatch)

pure $ do
writeStakePools (newOutputFormat format mOutFile) mOutFile poolIds
pure $ writeStakePools (newOutputFormat format mOutFile) mOutFile poolIds
) & onLeft (left . QueryCmdAcquireFailure)
& onLeft left

Expand Down Expand Up @@ -1230,6 +1218,7 @@ runQueryLeadershipScheduleCmd
, Cmd.vrkSkeyFp
, Cmd.whichSchedule
, Cmd.target
, Cmd.format
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath
Expand Down Expand Up @@ -1312,23 +1301,27 @@ runQueryLeadershipScheduleCmd
& onLeft left
where
writeSchedule mOutFile' eInfo shelleyGenesis schedule =
case mOutFile' of
Nothing -> liftIO $ printLeadershipScheduleAsText schedule eInfo (SystemStart $ sgSystemStart shelleyGenesis)
Just (File jsonOutputFile) ->
liftIO $ LBS.writeFile jsonOutputFile $
printLeadershipScheduleAsJson schedule eInfo (SystemStart $ sgSystemStart shelleyGenesis)

printLeadershipScheduleAsText
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile' toWrite
where
start = SystemStart $ sgSystemStart shelleyGenesis
toWrite =
case newOutputFormat format mOutFile' of
QueryOutputFormatJson ->
encodePretty $ leadershipScheduleToJson schedule eInfo start
QueryOutputFormatText ->
strictTextToLazyBytestring $ leadershipScheduleToText schedule eInfo start

leadershipScheduleToText
:: Set SlotNo
-> EpochInfo (Either Text)
-> SystemStart
-> IO ()
printLeadershipScheduleAsText leadershipSlots eInfo sStart = do
Text.putStrLn title
putStrLn $ replicate (Text.length title + 2) '-'
sequence_
[ putStrLn $ showLeadershipSlot slot eInfo sStart
| slot <- Set.toList leadershipSlots ]
-> Text
leadershipScheduleToText leadershipSlots eInfo sStart = do
Text.unlines $
title
: Text.replicate (Text.length title + 2) "-"
: [ showLeadershipSlot slot eInfo sStart | slot <- Set.toList leadershipSlots ]
where
title :: Text
title =
Expand All @@ -1338,30 +1331,30 @@ runQueryLeadershipScheduleCmd
:: SlotNo
-> EpochInfo (Either Text)
-> SystemStart
-> String
-> Text
showLeadershipSlot lSlot@(SlotNo sn) eInfo' sStart' =
case epochInfoSlotToUTCTime eInfo' sStart' lSlot of
Right slotTime ->
concat
mconcat
[ " "
, show sn
, Text.pack $ show sn
, " "
, show slotTime
, Text.pack $ show slotTime
]
Left err ->
concat
mconcat
[ " "
, show sn
, Text.pack $ show sn
, " "
, Text.unpack err
, err
]
printLeadershipScheduleAsJson
leadershipScheduleToJson
:: Set SlotNo
-> EpochInfo (Either Text)
-> SystemStart
-> LBS.ByteString
printLeadershipScheduleAsJson leadershipSlots eInfo sStart =
encodePretty $ showLeadershipSlot <$> List.sort (Set.toList leadershipSlots)
-> [Aeson.Value]
leadershipScheduleToJson leadershipSlots eInfo sStart =
showLeadershipSlot <$> List.sort (Set.toList leadershipSlots)
where
showLeadershipSlot :: SlotNo -> Aeson.Value
showLeadershipSlot lSlot@(SlotNo sn) =
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Legacy/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ data LegacyQueryLeadershipScheduleCmdArgs = LegacyQueryLeadershipScheduleCmdArgs
, poolColdVerKeyFile :: !(VerificationKeyOrHashOrFile StakePoolKey)
, vrkSkeyFp :: !(SigningKeyFile In)
, whichSchedule :: !EpochLeadershipSchedule
, format :: Maybe QueryOutputFormat
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Legacy/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ pQueryCmds envCli =
<*> pStakePoolVerificationKeyOrHashOrFile Nothing
<*> pVrfSigningKeyFile
<*> pWhichLeadershipSchedule
<*> (optional $ pQueryOutputFormat "leadership-schedule")
<*> pMaybeOutputFile

pKesPeriodInfo :: Parser LegacyQueryCmds
Expand Down
25 changes: 25 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 @@ -578,6 +578,9 @@ Usage: cardano-cli shelley query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -1743,6 +1746,9 @@ Usage: cardano-cli allegra query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -2906,6 +2912,9 @@ Usage: cardano-cli mary query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -4061,6 +4070,9 @@ Usage: cardano-cli alonzo query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -5251,6 +5263,9 @@ Usage: cardano-cli babbage query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -6711,6 +6726,9 @@ Usage: cardano-cli conway query leadership-schedule --socket-path SOCKET_PATH
[ --volatile-tip
| --immutable-tip
]
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -8065,6 +8083,9 @@ Usage: cardano-cli latest query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -9084,6 +9105,9 @@ Usage: cardano-cli legacy query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -10327,6 +10351,7 @@ Usage: cardano-cli query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[--output-json | --output-text]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Usage: cardano-cli allegra query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -41,5 +44,9 @@ Available options:
Input filepath of the VRF signing key.
--current Get the leadership schedule for the current epoch.
--next Get the leadership schedule for the following epoch.
--output-json Format leadership-schedule query output to JSON.
Default format when writing to a file
--output-text Format leadership-schedule query output to TEXT.
Default format when writing to stdout
--out-file FILE 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 @@ -11,6 +11,9 @@ Usage: cardano-cli alonzo query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -41,5 +44,9 @@ Available options:
Input filepath of the VRF signing key.
--current Get the leadership schedule for the current epoch.
--next Get the leadership schedule for the following epoch.
--output-json Format leadership-schedule query output to JSON.
Default format when writing to a file
--output-text Format leadership-schedule query output to TEXT.
Default format when writing to stdout
--out-file FILE 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 @@ -11,6 +11,9 @@ Usage: cardano-cli babbage query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -41,5 +44,9 @@ Available options:
Input filepath of the VRF signing key.
--current Get the leadership schedule for the current epoch.
--next Get the leadership schedule for the following epoch.
--output-json Format leadership-schedule query output to JSON.
Default format when writing to a file
--output-text Format leadership-schedule query output to TEXT.
Default format when writing to stdout
--out-file FILE 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 @@ -14,6 +14,9 @@ Usage: cardano-cli conway query leadership-schedule --socket-path SOCKET_PATH
[ --volatile-tip
| --immutable-tip
]
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -47,5 +50,9 @@ 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 leadership-schedule query output to JSON.
Default format when writing to a file
--output-text Format leadership-schedule query output to TEXT.
Default format when writing to stdout
--out-file FILE 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 @@ -11,6 +11,9 @@ Usage: cardano-cli latest query leadership-schedule --socket-path SOCKET_PATH
)
--vrf-signing-key-file FILE
(--current | --next)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the slots the node is expected to mint a block in (advanced command)
Expand Down Expand Up @@ -41,5 +44,9 @@ Available options:
Input filepath of the VRF signing key.
--current Get the leadership schedule for the current epoch.
--next Get the leadership schedule for the following epoch.
--output-json Format leadership-schedule query output to JSON.
Default format when writing to a file
--output-text Format leadership-schedule query output to TEXT.
Default format when writing to stdout
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Loading

0 comments on commit bd22b2e

Please sign in to comment.