Skip to content

Commit

Permalink
#4928 Add CLI command for printing slot number for UTC Time
Browse files Browse the repository at this point in the history
adf
  • Loading branch information
carbolymer committed Apr 26, 2023
1 parent 4a455c0 commit 39dc687
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import Prelude
import Cardano.Api.Shelley

import Data.Text (Text)
import Data.Time.Clock

import Cardano.CLI.Shelley.Key (DelegationTarget, PaymentVerifier, StakeIdentifier,
StakeVerifier, VerificationKeyOrFile, VerificationKeyOrHashOrFile,
Expand Down Expand Up @@ -368,6 +369,7 @@ data QueryCmd =
(Maybe (File () Out))
| QueryPoolState' (Maybe SocketPath) AnyConsensusModeParams NetworkId [Hash StakePoolKey]
| QueryTxMempool (Maybe SocketPath) AnyConsensusModeParams NetworkId TxMempoolQuery (Maybe (File () Out))
| QuerySlotNumber (Maybe SocketPath) AnyConsensusModeParams NetworkId UTCTime
deriving Show

renderQueryCmd :: QueryCmd -> Text
Expand All @@ -386,6 +388,7 @@ renderQueryCmd cmd =
QueryKesPeriodInfo {} -> "query kes-period-info"
QueryPoolState' {} -> "query pool-state"
QueryTxMempool _ _ _ query _ -> "query tx-mempool" <> renderTxMempoolQuery query
QuerySlotNumber {} -> "query slot-number"
where
renderTxMempoolQuery query =
case query of
Expand Down
24 changes: 20 additions & 4 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ pQueryCmd envCli =
(Opt.info pQueryPoolState $ Opt.progDesc "Dump the pool state")
, subParser "tx-mempool"
(Opt.info pQueryTxMempool $ Opt.progDesc "Local Mempool info")
, subParser "slot-number"
(Opt.info pQuerySlotNumber $ Opt.progDesc "Query slot number for UTC timestamp")
]
where
pQueryProtocolParameters :: Parser QueryCmd
Expand Down Expand Up @@ -1092,6 +1094,20 @@ pQueryCmd envCli =
<*> pOperationalCertificateFile
<*> pMaybeOutputFile

pQuerySlotNumber :: Parser QueryCmd
pQuerySlotNumber =
QuerySlotNumber
<$> pSocketPath
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pUtcTimestamp
where
pUtcTimestamp =
convertTime <$> (Opt.strArgument . mconcat)
[ Opt.metavar "TIMESTAMP"
, Opt.help "UTC timestamp in YYYY-MM-DDThh:mm:ssZ format"
]

pGovernanceCmd :: Parser GovernanceCmd
pGovernanceCmd =
asum
Expand Down Expand Up @@ -1475,10 +1491,6 @@ pGenesisCmd envCli =
<> Opt.completer (Opt.bashCompleter "file")
)

convertTime :: String -> UTCTime
convertTime =
parseTimeOrError False defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ"

pInitialSupplyNonDelegated :: Parser (Maybe Lovelace)
pInitialSupplyNonDelegated =
Opt.optional $
Expand Down Expand Up @@ -1655,6 +1667,10 @@ pTxMetadataJsonSchema =
-- Default to the no-schema conversion.
pure TxMetadataJsonNoSchema

convertTime :: String -> UTCTime
convertTime =
parseTimeOrError False defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ"

pMetadataFile :: Parser MetadataFile
pMetadataFile =
MetadataFileJSON <$>
Expand Down
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ runQueryCmd cmd =
runQueryPoolState mNodeSocketPath consensusModeParams network poolid
QueryTxMempool mNodeSocketPath consensusModeParams network op mOutFile ->
runQueryTxMempool mNodeSocketPath consensusModeParams network op mOutFile
QuerySlotNumber mNodeSocketPath consensusModeParams network utcTime ->
runQuerySlotNumber mNodeSocketPath consensusModeParams network utcTime

runQueryProtocolParameters
:: Maybe SocketPath
Expand Down Expand Up @@ -718,6 +720,17 @@ runQueryTxMempool mNodeSocketPath (AnyConsensusModeParams cModeParams) network q
Just (File oFp) -> handleIOExceptT (ShelleyQueryCmdWriteFileError . FileIOError oFp)
$ LBS.writeFile oFp renderedResult

runQuerySlotNumber
:: Maybe SocketPath
-> AnyConsensusModeParams
-> NetworkId
-> UTCTime
-> ExceptT ShelleyQueryCmdError IO ()
runQuerySlotNumber mNodeSocketPath aCmp network utcTime = do
sockPath <- maybe (lift readEnvSocketPath) (pure . Right) mNodeSocketPath
& onLeft (left . ShelleyQueryCmdEnvVarSocketErr)
SlotNo slotNo <- utcTimeToSlotNo sockPath aCmp network utcTime
liftIO $ print slotNo

-- | Obtain stake snapshot information for a pool, plus information about the total active stake.
-- This information can be used for leader slot calculation, for example, and has been requested by SPOs.
Expand Down

0 comments on commit 39dc687

Please sign in to comment.