Skip to content

Commit

Permalink
Update getLocalChainTip to return a ChainTip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jan 29, 2021
1 parent 5f7ff36 commit d38e7a7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ library
--TODO: these should either be removed once the new IPC
-- is fully integrated, or re-exported via the export
-- modules above
Cardano.Api.Block
Cardano.Api.Crypto.Ed25519Bip32
Cardano.Api.Eras
Cardano.Api.LocalChainSync
Expand Down Expand Up @@ -49,7 +50,7 @@ library

-- Splitting up the big Typed module:
Cardano.Api.Address
Cardano.Api.Block
-- TODO: move here Cardano.Api.Block
Cardano.Api.Certificate
Cardano.Api.Error
Cardano.Api.Fees
Expand Down
15 changes: 8 additions & 7 deletions cardano-api/src/Cardano/Api/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,6 @@ data ChainPoint = ChainPointAtGenesis
| ChainPoint !SlotNo !(Hash BlockHeader)
deriving (Eq, Show)

instance ToJSON ChainPoint where
toJSON ChainPointAtGenesis = Aeson.String "Tip is currently at genesis block"
toJSON (ChainPoint slot headerHash) =
object [ "Slot Number" .= slot
, "Slot Header Hash" .= Text.decodeUtf8 (serialiseToRawBytesHex headerHash)
]


toConsensusPointInMode :: ConsensusMode mode
-> ChainPoint
Expand Down Expand Up @@ -336,6 +329,14 @@ data ChainTip = ChainTipAtGenesis
| ChainTip !SlotNo !(Hash BlockHeader) !BlockNo
deriving (Eq, Show)

instance ToJSON ChainTip where
toJSON ChainTipAtGenesis = Aeson.String "Tip is currently at genesis block"
toJSON (ChainTip slot headerHash (Consensus.BlockNo bNum)) =
object [ "slotNo" .= slot
, "headerHash" .= Text.decodeUtf8 (serialiseToRawBytesHex headerHash)
, "blockNo" .= bNum
]

chainTipToChainPoint :: ChainTip -> ChainPoint
chainTipToChainPoint ChainTipAtGenesis = ChainPointAtGenesis
chainTipToChainPoint (ChainTip s h _) = ChainPoint s h
Expand Down
4 changes: 2 additions & 2 deletions cardano-api/src/Cardano/Api/IPC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ submitTxToNodeLocal connctInfo tx = do
--


getLocalChainTip :: LocalNodeConnectInfo mode -> IO ChainPoint
getLocalChainTip :: LocalNodeConnectInfo mode -> IO ChainTip
getLocalChainTip localNodeConInfo = do
resultVar <- newEmptyTMVarIO
connectToLocalNode
Expand All @@ -497,7 +497,7 @@ getLocalChainTip localNodeConInfo = do
, localTxSubmissionClient = Nothing
, localStateQueryClient = Nothing
}
atomically $ chainTipToChainPoint <$> takeTMVar resultVar
atomically $ takeTMVar resultVar

chainSyncGetCurrentTip
:: forall mode. TMVar ChainTip
Expand Down
13 changes: 7 additions & 6 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT
import qualified Control.State.Transition as STS

import Cardano.Api
import Cardano.Api.Block
import Cardano.Api.Byron
import qualified Cardano.Api.IPC as NewIPC
import Cardano.Api.Modes (AnyConsensusMode (..), AnyConsensusModeParams (..), toEraInMode)
Expand Down Expand Up @@ -135,7 +136,7 @@ runQueryProtocolParameters anyEra@(AnyCardanoEra era) (AnyConsensusModeParams cM
$ NewIPC.QueryInShelleyBasedEra sbe NewIPC.QueryProtocolParameters

tip <- liftIO $ NewIPC.getLocalChainTip localNodeConnInfo
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo tip qInMode
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo (chainTipToChainPoint tip) qInMode
case res of
Left acqFailure -> left $ ShelleyQueryCmdAcquireFailure acqFailure
Right ePparams ->
Expand Down Expand Up @@ -198,7 +199,7 @@ runQueryUTxO anyEra@(AnyCardanoEra era) (AnyConsensusModeParams cModeParams)
qInMode <- createQuery sbe eraInMode

tip <- liftIO $ NewIPC.getLocalChainTip localNodeConnInfo
eUtxo <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo tip qInMode
eUtxo <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo (chainTipToChainPoint tip) qInMode
case eUtxo of
Left aF -> left $ ShelleyQueryCmdAcquireFailure aF
Right eU -> case eU of
Expand Down Expand Up @@ -243,7 +244,7 @@ runQueryLedgerState anyEra@(AnyCardanoEra era) (AnyConsensusModeParams cModePara
$ NewIPC.QueryLedgerState

tip <- liftIO $ NewIPC.getLocalChainTip localNodeConnInfo
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo tip qInMode
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo (chainTipToChainPoint tip) qInMode
case res of
Left acqFailure -> left $ ShelleyQueryCmdAcquireFailure acqFailure
Right eStakeDist ->
Expand Down Expand Up @@ -278,7 +279,7 @@ runQueryProtocolState anyEra@(AnyCardanoEra era) (AnyConsensusModeParams cModePa


tip <- liftIO $ NewIPC.getLocalChainTip localNodeConnInfo
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo tip qInMode
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo (chainTipToChainPoint tip) qInMode
case res of
Left acqFailure -> left $ ShelleyQueryCmdAcquireFailure acqFailure
Right eStakeDist ->
Expand Down Expand Up @@ -319,7 +320,7 @@ runQueryStakeAddressInfo anyEra@(AnyCardanoEra era) (AnyConsensusModeParams cMod


tip <- liftIO $ NewIPC.getLocalChainTip localNodeConnInfo
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo tip qInMode
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo (chainTipToChainPoint tip) qInMode
case res of
Left acqFailure -> left $ ShelleyQueryCmdAcquireFailure acqFailure
Right eDelegsAndRwds ->
Expand Down Expand Up @@ -509,7 +510,7 @@ runQueryStakeDistribution anyEra@(AnyCardanoEra era) (AnyConsensusModeParams cMo
in return $ NewIPC.QueryInEra eraInMode query

tip <- liftIO $ NewIPC.getLocalChainTip localNodeConnInfo
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo tip qInMode
res <- liftIO $ NewIPC.queryNodeLocalState localNodeConnInfo (chainTipToChainPoint tip) qInMode
case res of
Left acqFailure -> left $ ShelleyQueryCmdAcquireFailure acqFailure
Right eStakeDist ->
Expand Down

0 comments on commit d38e7a7

Please sign in to comment.