Skip to content

Commit

Permalink
Implement JSON output support for Shelley tip query command
Browse files Browse the repository at this point in the history
  • Loading branch information
intricate committed Jul 3, 2020
1 parent 588f13a commit 226d47e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Expand Up @@ -133,8 +133,8 @@ runQueryTip network mOutFile = do
tip <- liftIO $ withIOManager $ \iomgr ->
getLocalTip iomgr ptclClientInfo network sockPath
case mOutFile of
Just (OutputFile fpath) -> liftIO . writeFile fpath $ show tip
Nothing -> liftIO $ putTextLn (show tip)
Just (OutputFile fpath) -> liftIO . LBS.writeFile fpath $ encodePretty tip
Nothing -> liftIO $ LBS.putStrLn (encodePretty tip)


runQueryUTxO
Expand Down
1 change: 1 addition & 0 deletions cardano-config/cardano-config.cabal
Expand Up @@ -86,6 +86,7 @@ library
, iproute
, safe-exceptions
, scientific
, serialise
, shelley-spec-ledger
, string-conv
, stm
Expand Down
19 changes: 18 additions & 1 deletion cardano-config/src/Cardano/Config/Orphanage.hs
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -11,14 +12,20 @@ module Cardano.Config.Orphanage () where
import Cardano.Prelude
import qualified Prelude

import Codec.Serialise (Serialise, serialise)
import Data.Aeson
import Network.Socket (PortNumber)
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as LBS
import Data.Scientific (coefficient)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Network.Socket (PortNumber)

import Cardano.BM.Data.Tracer (TracingVerbosity(..))
import qualified Cardano.Chain.Update as Update
import Cardano.Slotting.Block (BlockNo (..))
import Ouroboros.Consensus.NodeId (CoreNodeId (..))
import Ouroboros.Network.Block (HeaderHash, Tip (..))


deriving instance Show TracingVerbosity
Expand Down Expand Up @@ -50,3 +57,13 @@ instance FromJSON Update.ApplicationName where
parseJSON invalid =
panic $ "Parsing of application name failed due to type mismatch. "
<> "Encountered: " <> (Text.pack $ Prelude.show invalid)

instance Serialise (HeaderHash blk) => ToJSON (Tip blk) where
toJSON TipGenesis = object [ "genesis" .= True ]
toJSON (Tip slotNo headerHash (BlockNo bn)) =
object
[ "slotNo" .= slotNo
, "headerHash" .=
(Text.decodeLatin1 . Base16.encode . LBS.toStrict . serialise) headerHash
, "blockNo" .= bn
]

0 comments on commit 226d47e

Please sign in to comment.