Skip to content

Commit

Permalink
Merge #4912
Browse files Browse the repository at this point in the history
4912: Add slotsInEpoch and slotsToEpochEnd to query tip command r=newhoggy a=newhoggy

Example output:

```
CARDANO_NODE_SOCKET_PATH=example/node-pool1/node.sock cardano-cli query tip --testnet-magic 42
{
    "block": 768,
    "epoch": 33,
    "era": "Alonzo",
    "hash": "4ca7fd3a9827e74f98e779a8ae333dc6fa3b69832cb11fdbab7af215eb68b1ee",
    "slot": 16814,
    "slotsInEpoch": 314,
    "slotsToEpochEnd": 186,
    "syncProgress": "100.00"
}
```

Resolves #3878


Co-authored-by: John Ky <[email protected]>
  • Loading branch information
iohk-bors[bot] and newhoggy authored Mar 1, 2023
2 parents 3b21b2d + fbb1f0d commit aa5a410
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cardano-cli/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- Allow assembling transactions with no witnesses ([PR 4408](https://github.com/input-output-hk/cardano-node/pull/4408))

- Add `slotsInEpoch` and `slotsToEpochEnd` to output of `query tip` command ([PR 4912](https://github.com/input-output-hk/cardano-node/pull/4912))

### Bugs

- Allow reading signing keys from a pipe ([PR 4342](https://github.com/input-output-hk/cardano-node/pull/4342))
Expand Down
16 changes: 16 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ data QueryTipLocalStateOutput = QueryTipLocalStateOutput
{ localStateChainTip :: ChainTip
, mEra :: Maybe AnyCardanoEra
, mEpoch :: Maybe EpochNo
, mSlotsInEpoch :: Maybe Word64
, mSlotsToEpochEnd :: Maybe Word64
, mSyncProgress :: Maybe Text
} deriving Show

Expand All @@ -169,6 +171,8 @@ instance ToJSON QueryTipLocalStateOutput where
object $
( ("era" ..=? mEra a)
. ("epoch" ..=? mEpoch a)
. ("slotsInEpoch" ..=? mSlotsInEpoch a)
. ("slotsToEpochEnd" ..=? mSlotsToEpochEnd a)
. ("syncProgress" ..=? mSyncProgress a)
) []
ChainTip slotNo blockHeader blockNo ->
Expand All @@ -178,13 +182,17 @@ instance ToJSON QueryTipLocalStateOutput where
. ("block" ..= blockNo)
. ("era" ..=? mEra a)
. ("epoch" ..=? mEpoch a)
. ("slotsInEpoch" ..=? mSlotsInEpoch a)
. ("slotsToEpochEnd" ..=? mSlotsToEpochEnd a)
. ("syncProgress" ..=? mSyncProgress a)
) []
toEncoding a = case localStateChainTip a of
ChainTipAtGenesis ->
pairs $ mconcat $
( ("era" ..=? mEra a)
. ("epoch" ..=? mEpoch a)
. ("slotsInEpoch" ..=? mSlotsInEpoch a)
. ("slotsToEpochEnd" ..=? mSlotsToEpochEnd a)
. ("syncProgress" ..=? mSyncProgress a)
) []
ChainTip slotNo blockHeader blockNo ->
Expand All @@ -194,6 +202,8 @@ instance ToJSON QueryTipLocalStateOutput where
. ("block" ..= blockNo)
. ("era" ..=? mEra a)
. ("epoch" ..=? mEpoch a)
. ("slotsInEpoch" ..=? mSlotsInEpoch a)
. ("slotsToEpochEnd" ..=? mSlotsToEpochEnd a)
. ("syncProgress" ..=? mSyncProgress a)
) []

Expand All @@ -206,18 +216,24 @@ instance FromJSON QueryTipLocalStateOutput where
mSlot <- o .:? "slot"
mHash <- o .:? "hash"
mBlock <- o .:? "block"
mSlotsInEpoch' <- o .:? "slotsInEpoch"
mSlotsToEpochEnd' <- o .:? "slotsToEpochEnd"
case (mSlot, mHash, mBlock) of
(Nothing, Nothing, Nothing) ->
pure $ QueryTipLocalStateOutput
ChainTipAtGenesis
mEra'
mEpoch'
mSlotsInEpoch'
mSlotsToEpochEnd'
mSyncProgress'
(Just slot, Just hash, Just block) ->
pure $ QueryTipLocalStateOutput
(ChainTip slot hash block)
mEra'
mEpoch'
mSlotsInEpoch'
mSlotsToEpochEnd'
mSyncProgress'
(_,_,_) ->
fail $ mconcat
Expand Down
6 changes: 5 additions & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,11 @@ runQueryTip (AnyConsensusModeParams cModeParams) network mOutFile = do
, O.mEra = Nothing
, O.mEpoch = Nothing
, O.mSyncProgress = Nothing
, O.mSlotsInEpoch = Nothing
, O.mSlotsToEpochEnd = Nothing
}

Right (epochNo, _, _) -> do
Right (epochNo, SlotsInEpoch slotsInEpoch, SlotsToEpochEnd slotsToEpochEnd) -> do
syncProgressResult <- runExceptT $ do
systemStart <- fmap getSystemStart (O.mSystemStart localState) & hoistMaybe ShelleyQueryCmdSystemStartUnavailable
nowSeconds <- toRelativeTime (SystemStart systemStart) <$> liftIO getCurrentTime
Expand All @@ -359,6 +361,8 @@ runQueryTip (AnyConsensusModeParams cModeParams) network mOutFile = do
{ O.localStateChainTip = chainTip
, O.mEra = Just (O.era localState)
, O.mEpoch = Just epochNo
, O.mSlotsInEpoch = Just slotsInEpoch
, O.mSlotsToEpochEnd = Just slotsToEpochEnd
, O.mSyncProgress = mSyncProgress
}

Expand Down

0 comments on commit aa5a410

Please sign in to comment.