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
cli: Add 'shelley query ledger-state' command #1016
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,19 +20,22 @@ import Numeric (showEFloat) | |
import Control.Monad.Trans.Except (ExceptT) | ||
import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT) | ||
|
||
import Cardano.Crypto.Hash.Class (getHashBytesAsHex) | ||
|
||
import Cardano.Api | ||
(Address, LocalStateQueryError, Network(..), getLocalTip, | ||
queryFilteredUTxOFromLocalState, queryPParamsFromLocalState, | ||
queryStakeDistributionFromLocalState) | ||
queryFilteredUTxOFromLocalState, queryLocalLedgerState, | ||
queryPParamsFromLocalState, queryStakeDistributionFromLocalState) | ||
|
||
import Cardano.CLI.Environment (EnvSocketError, readEnvSocketPath) | ||
import Cardano.CLI.Helpers | ||
import Cardano.CLI.Shelley.Parsers (OutputFile (..), QueryCmd (..)) | ||
|
||
import Cardano.Config.Shelley.Orphans () | ||
import Cardano.Config.Shelley.Protocol (mkNodeClientProtocolTPraos) | ||
|
||
import Cardano.Crypto.Hash.Class (getHashBytesAsHex) | ||
|
||
import Data.Aeson (ToJSON) | ||
|
||
import Ouroboros.Consensus.Cardano (protocolClientInfo) | ||
import Ouroboros.Consensus.Node.ProtocolInfo (pClientInfoCodecConfig) | ||
import Ouroboros.Consensus.Shelley.Protocol.Crypto (TPraosStandardCrypto) | ||
|
@@ -45,8 +48,7 @@ import Shelley.Spec.Ledger.TxData (TxId (..), TxIn (..), TxOut (..)) | |
import Shelley.Spec.Ledger.UTxO (UTxO (..)) | ||
import Shelley.Spec.Ledger.PParams (PParams) | ||
import Shelley.Spec.Ledger.Delegation.Certificates (PoolDistr(..)) | ||
import Shelley.Spec.Ledger.Keys | ||
(Hash, KeyHash(..), KeyRole (..), VerKeyVRF) | ||
import Shelley.Spec.Ledger.Keys (Hash, KeyHash(..), KeyRole (..), VerKeyVRF) | ||
|
||
|
||
data ShelleyQueryCmdError | ||
|
@@ -67,6 +69,8 @@ runQueryCmd cmd = | |
runQueryFilteredUTxO addr network mOutFile | ||
QueryStakeDistribution network mOutFile -> | ||
runQueryStakeDistribution network mOutFile | ||
QueryLedgerState network mOutFile -> | ||
runQueryLedgerState network mOutFile | ||
_ -> liftIO $ putStrLn $ "runQueryCmd: " ++ show cmd | ||
|
||
runQueryProtocolParameters | ||
|
@@ -109,11 +113,40 @@ runQueryFilteredUTxO addr network mOutFile = do | |
sockPath <- firstExceptT ShelleyQueryEnvVarSocketErr readEnvSocketPath | ||
let ptclClientInfo = pClientInfoCodecConfig . protocolClientInfo $ mkNodeClientProtocolTPraos | ||
tip <- liftIO $ withIOManager $ \iomgr -> | ||
getLocalTip iomgr ptclClientInfo network sockPath | ||
getLocalTip iomgr ptclClientInfo network sockPath | ||
filteredUtxo <- firstExceptT NodeLocalStateQueryError $ | ||
queryFilteredUTxOFromLocalState network sockPath (Set.singleton addr) (getTipPoint tip) | ||
queryFilteredUTxOFromLocalState network sockPath (Set.singleton addr) (getTipPoint tip) | ||
writeFilteredUTxOs mOutFile filteredUtxo | ||
|
||
runQueryLedgerState | ||
:: Network | ||
-> Maybe OutputFile | ||
-> ExceptT ShelleyQueryCmdError IO () | ||
runQueryLedgerState network mOutFile = do | ||
sockPath <- firstExceptT ShelleyQueryEnvVarSocketErr readEnvSocketPath | ||
let ptclClientInfo = pClientInfoCodecConfig . protocolClientInfo $ mkNodeClientProtocolTPraos | ||
tip <- liftIO $ withIOManager $ \iomgr -> | ||
getLocalTip iomgr ptclClientInfo network sockPath | ||
els <- firstExceptT NodeLocalStateQueryError $ | ||
queryLocalLedgerState network sockPath (getTipPoint tip) | ||
case els of | ||
Right lstate -> maybePrintFileJson mOutFile lstate | ||
Left lbs -> do | ||
liftIO $ putTextLn "Verion mismatch beteen node and consensus, so dumping this as generic CBOR." | ||
firstExceptT ShelleyHelperError $ pPrintCBOR lbs | ||
|
||
-- ------------------------------------------------------------------------------------------------- | ||
|
||
maybePrintFileJson :: ToJSON a => Maybe OutputFile -> a -> ExceptT ShelleyQueryCmdError IO () | ||
maybePrintFileJson mOutputFile x = do | ||
let jsonX = encodePretty x | ||
case mOutputFile of | ||
Just (OutputFile fpath) -> | ||
handleIOExceptT (ShelleyHelperError . IOError' fpath) | ||
$ LBS.writeFile fpath jsonX | ||
Nothing -> liftIO $ LBS.putStrLn jsonX | ||
|
||
|
||
writeFilteredUTxOs :: Maybe OutputFile -> UTxO TPraosStandardCrypto -> ExceptT ShelleyQueryCmdError IO () | ||
writeFilteredUTxOs mOutFile utxo = | ||
case mOutFile of | ||
|
@@ -155,27 +188,27 @@ runQueryStakeDistribution network mOutFile = do | |
let ptclClientInfo = pClientInfoCodecConfig . protocolClientInfo $ mkNodeClientProtocolTPraos | ||
tip <- liftIO $ withIOManager $ \iomgr -> | ||
getLocalTip iomgr ptclClientInfo network sockPath | ||
stakeDistr <- firstExceptT NodeLocalStateQueryError $ | ||
stakeDist <- firstExceptT NodeLocalStateQueryError $ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes were required to avoid name shadowing warnings. |
||
queryStakeDistributionFromLocalState network sockPath (getTipPoint tip) | ||
writeStakeDistribution mOutFile stakeDistr | ||
writeStakeDistribution mOutFile stakeDist | ||
|
||
writeStakeDistribution :: Maybe OutputFile | ||
-> PoolDistr TPraosStandardCrypto | ||
-> ExceptT ShelleyQueryCmdError IO () | ||
writeStakeDistribution (Just (OutputFile outFile)) (PoolDistr stakeDistr) = | ||
writeStakeDistribution (Just (OutputFile outFile)) (PoolDistr stakeDist) = | ||
handleIOExceptT (ShelleyHelperError . IOError' outFile) $ | ||
LBS.writeFile outFile (encodePretty stakeDistr) | ||
LBS.writeFile outFile (encodePretty stakeDist) | ||
|
||
writeStakeDistribution Nothing stakeDistr = | ||
liftIO $ printStakeDistribution stakeDistr | ||
writeStakeDistribution Nothing stakeDist = | ||
liftIO $ printStakeDistribution stakeDist | ||
|
||
printStakeDistribution :: PoolDistr TPraosStandardCrypto -> IO () | ||
printStakeDistribution (PoolDistr stakeDistr) = do | ||
printStakeDistribution (PoolDistr stakeDist) = do | ||
Text.putStrLn title | ||
putStrLn $ replicate (Text.length title + 2) '-' | ||
sequence_ | ||
[ putStrLn $ showStakeDistr poolId stakeFraction vrfKeyId | ||
| (poolId, (stakeFraction, vrfKeyId)) <- Map.toList stakeDistr ] | ||
| (poolId, (stakeFraction, vrfKeyId)) <- Map.toList stakeDist ] | ||
where | ||
title :: Text | ||
title = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we move that
LByteString
error intoLocalStateQueryError
as a new constructor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, these really are two different things. If CBOR decoding is successful, the return the
LedgerState
. If decoding fails, return the ByteString.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood now: https://github.com/input-output-hk/cardano-node/pull/1016/files/ba755abfd4b9f4f467629aba4a577dcd59574e08#diff-b548d89cce82358cb320307aed52a300R135