Skip to content

Commit

Permalink
Merge pull request #743 from IntersectMBO/smelc/query-stake-distribut…
Browse files Browse the repository at this point in the history
…ion-control-output-format

query stake-distribution: add --output-{json,text} flags
  • Loading branch information
smelc committed Apr 29, 2024
2 parents d63cfe5 + fecb4cc commit 8d97919
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 33 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 @@ -114,6 +114,7 @@ data QueryStakeDistributionCmdArgs = QueryStakeDistributionCmdArgs
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, 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 @@ -171,6 +171,7 @@ pQueryStakeDistributionCmd era envCli =
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "stake-distribution")
<*> pMaybeOutputFile

pQueryStakeAddressInfoCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
Expand Down
62 changes: 29 additions & 33 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Text.IO as T
import qualified Data.Text.IO as Text
import qualified Data.Text.Lazy.IO as LT
import Data.Time.Clock
import GHC.Generics
Expand Down Expand Up @@ -1215,6 +1214,7 @@ runQueryStakeDistributionCmd
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.format
, Cmd.target
, Cmd.mOutFile
} = do
Expand All @@ -1233,45 +1233,41 @@ runQueryStakeDistributionCmd
& onLeft (left . QueryCmdLocalStateQueryError . EraMismatchError)

pure $ do
writeStakeDistribution mOutFile result
writeStakeDistribution (newOutputFormat format mOutFile) mOutFile result
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

writeStakeDistribution
:: Maybe (File () Out)
:: QueryOutputFormat
-> Maybe (File () Out)
-> Map PoolId Rational
-> ExceptT QueryCmdError IO ()
writeStakeDistribution (Just (File outFile)) stakeDistrib =
handleIOExceptT (QueryCmdWriteFileError . FileIOError outFile) $
LBS.writeFile outFile (encodePretty stakeDistrib)

writeStakeDistribution Nothing stakeDistrib =
liftIO $ printStakeDistribution stakeDistrib


printStakeDistribution :: Map PoolId Rational -> IO ()
printStakeDistribution stakeDistrib = do
Text.putStrLn title
putStrLn $ replicate (Text.length title + 2) '-'
sequence_
[ putStrLn $ showStakeDistr poolId stakeFraction
| (poolId, stakeFraction) <- Map.toList stakeDistrib ]
where
title :: Text
title =
" PoolId Stake frac"

showStakeDistr :: PoolId
-> Rational
-- ^ Stake fraction
-> String
showStakeDistr poolId stakeFraction =
concat
[ Text.unpack (serialiseToBech32 poolId)
, " "
, showEFloat (Just 3) (fromRational stakeFraction :: Double) ""
]
writeStakeDistribution format mOutFile stakeDistrib =
firstExceptT QueryCmdWriteFileError . newExceptT
$ writeLazyByteStringOutput mOutFile toWrite
where
toWrite :: LBS.ByteString =
case format of
QueryOutputFormatJson -> encodePretty stakeDistrib
QueryOutputFormatText -> strictTextToLazyBytestring stakeDistributionText
stakeDistributionText =
Text.unlines $
[ title
, Text.replicate (Text.length title + 2) "-"
] ++
[ showStakeDistr poolId stakeFraction | (poolId, stakeFraction) <- Map.toList stakeDistrib ]
where
title :: Text
title =
" PoolId Stake frac"
showStakeDistr :: PoolId -> Rational -> Text
showStakeDistr poolId stakeFraction =
mconcat
[ serialiseToBech32 poolId
, " "
, Text.pack $ showEFloat (Just 3) (fromRational stakeFraction :: Double) ""
]

runQueryLeadershipScheduleCmd
:: Cmd.QueryLeadershipScheduleCmdArgs
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 @@ -94,6 +94,7 @@ data LegacyQueryStakeDistributionCmdArgs = LegacyQueryStakeDistributionCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, 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 @@ -671,6 +671,7 @@ pQueryCmds envCli =
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> (optional $ pQueryOutputFormat "stake-distribution")
<*> pMaybeOutputFile

pQueryStakeAddressInfo :: 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 @@ -495,6 +495,9 @@ Usage: cardano-cli shelley query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -1687,6 +1690,9 @@ Usage: cardano-cli allegra query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -2877,6 +2883,9 @@ Usage: cardano-cli mary query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -4190,6 +4199,9 @@ Usage: cardano-cli alonzo query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -5540,6 +5552,9 @@ Usage: cardano-cli babbage query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -7154,6 +7169,9 @@ Usage: cardano-cli conway query stake-distribution --socket-path SOCKET_PATH
[ --volatile-tip
| --immutable-tip
]
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -8703,6 +8721,9 @@ Usage: cardano-cli latest query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -9874,6 +9895,9 @@ Usage: cardano-cli legacy query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down Expand Up @@ -11136,6 +11160,7 @@ Usage: cardano-cli query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Usage: cardano-cli allegra query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +25,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -4,6 +4,9 @@ Usage: cardano-cli alonzo query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +25,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -4,6 +4,9 @@ Usage: cardano-cli babbage query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +25,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -7,6 +7,9 @@ Usage: cardano-cli conway query stake-distribution --socket-path SOCKET_PATH
[ --volatile-tip
| --immutable-tip
]
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -28,5 +31,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 stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -4,6 +4,9 @@ Usage: cardano-cli latest query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +25,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -4,6 +4,9 @@ Usage: cardano-cli legacy query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +25,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -4,6 +4,9 @@ Usage: cardano-cli mary query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[ --output-json
| --output-text
]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +25,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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 @@ -4,6 +4,7 @@ Usage: cardano-cli query stake-distribution --socket-path SOCKET_PATH
( --mainnet
| --testnet-magic NATURAL
)
[--output-json | --output-text]
[--out-file FILE]

Get the node's current aggregated stake distribution
Expand All @@ -22,5 +23,9 @@ Available options:
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--output-json Format stake-distribution query output to JSON.
Default format when writing to a file
--output-text Format stake-distribution 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

0 comments on commit 8d97919

Please sign in to comment.