Skip to content

Commit

Permalink
Add slotsInEpoch and slotsToEpochEnd to query tip command
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Mar 2, 2023
1 parent 78d1ed5 commit 2f964fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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
, mSlotInEpoch :: 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)
. ("slotInEpoch" ..=? mSlotInEpoch 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)
. ("slotInEpoch" ..=? mSlotInEpoch a)
. ("slotsToEpochEnd" ..=? mSlotsToEpochEnd a)
. ("syncProgress" ..=? mSyncProgress a)
) []
toEncoding a = case localStateChainTip a of
ChainTipAtGenesis ->
pairs $ mconcat $
( ("era" ..=? mEra a)
. ("epoch" ..=? mEpoch a)
. ("slotInEpoch" ..=? mSlotInEpoch 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)
. ("slotInEpoch" ..=? mSlotInEpoch 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"
mSlotInEpoch' <- o .:? "slotInEpoch"
mSlotsToEpochEnd' <- o .:? "slotsToEpochEnd"
case (mSlot, mHash, mBlock) of
(Nothing, Nothing, Nothing) ->
pure $ QueryTipLocalStateOutput
ChainTipAtGenesis
mEra'
mEpoch'
mSlotInEpoch'
mSlotsToEpochEnd'
mSyncProgress'
(Just slot, Just hash, Just block) ->
pure $ QueryTipLocalStateOutput
(ChainTip slot hash block)
mEra'
mEpoch'
mSlotInEpoch'
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.mSlotInEpoch = 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.mSlotInEpoch = Just slotsInEpoch
, O.mSlotsToEpochEnd = Just slotsToEpochEnd
, O.mSyncProgress = mSyncProgress
}

Expand Down

0 comments on commit 2f964fc

Please sign in to comment.