Skip to content

Fix query protocol-state #4102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions cardano-cli/src/Cardano/CLI/Shelley/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -13,38 +14,37 @@

module Cardano.CLI.Shelley.Orphans () where

import Cardano.Prelude

import Control.SetAlgebra as SetAlgebra
import Data.Aeson
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Short as SBS
import qualified Data.Text.Encoding as Text

import Cardano.Api.Orphans ()

import Ouroboros.Consensus.Byron.Ledger.Block (ByronHash (..))
import Ouroboros.Consensus.HardFork.Combinator (OneEraHash (..))
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
import Ouroboros.Consensus.Shelley.Ledger.Block (ShelleyHash (..))
import Ouroboros.Network.Block (BlockNo (..), HeaderHash, Tip (..))

import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash (..))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you undo the import changes to follow the style in other modules?

Copy link
Contributor Author

@newhoggy newhoggy Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-organised the imports, but any chance we could use ImportQualifiedPost language extension like we do in plutus-apps so I could just do a line sort to keep my imports tidy?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to make a decision to use ImportQualifiedPost in the repo. Having a mix of some modules using it and others not is not ideal. I personally don't see justification for this aesthetic change.

import qualified Cardano.Ledger.Credential as Ledger
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import qualified Cardano.Ledger.Mary.Value as Ledger.Mary
import Cardano.Ledger.PoolDistr (PoolDistr (..))
import Cardano.Protocol.TPraos.BHeader (HashHeader (..))

import qualified Cardano.Ledger.Credential as Ledger
import qualified Cardano.Protocol.TPraos.API as Ledger
import qualified Cardano.Ledger.Shelley.EpochBoundary as Ledger
import qualified Cardano.Ledger.Shelley.PoolRank as Ledger
import Cardano.Ledger.TxIn (TxId (..))
import Cardano.Prelude (Bool(True), Category((.)))
import qualified Cardano.Protocol.TPraos.API as Ledger
import Cardano.Protocol.TPraos.BHeader (HashHeader (..))
import qualified Cardano.Protocol.TPraos.Rules.Prtcl as Ledger
import qualified Cardano.Protocol.TPraos.Rules.Tickn as Ledger

import qualified Cardano.Ledger.Mary.Value as Ledger.Mary

import qualified Cardano.Slotting.Slot as Cardano
import qualified Control.SetAlgebra as SetAlgebra (BiMap, forwards)
import Data.Aeson (FromJSON(..), KeyValue((.=)), ToJSON(..), ToJSONKey)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Short as SBS
import qualified Data.Text.Encoding as Text
import qualified Data.VMap as VMap
import qualified Cardano.Ledger.Shelley.PoolRank as Ledger
import Ouroboros.Consensus.Byron.Ledger.Block (ByronHash (..))
import Ouroboros.Consensus.HardFork.Combinator (OneEraHash (..))
import Ouroboros.Consensus.Protocol.Praos (PraosState)
import qualified Ouroboros.Consensus.Protocol.Praos as Consensus
import Ouroboros.Consensus.Protocol.TPraos (TPraosState)
import qualified Ouroboros.Consensus.Protocol.TPraos as Consensus
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
import Ouroboros.Consensus.Shelley.Ledger.Block (ShelleyHash (..))
import Ouroboros.Network.Block (BlockNo (..), HeaderHash, Tip (..))

instance ToJSON (OneEraHash xs) where
toJSON = toJSON
Expand All @@ -58,9 +58,9 @@ deriving newtype instance ToJSON ByronHash
-- This instance is temporarily duplicated in cardano-config

instance ToJSON (HeaderHash blk) => ToJSON (Tip blk) where
toJSON TipGenesis = object [ "genesis" .= True ]
toJSON TipGenesis = Aeson.object [ "genesis" .= True ]
toJSON (Tip slotNo headerHash blockNo) =
object
Aeson.object
[ "slotNo" .= slotNo
, "headerHash" .= headerHash
, "blockNo" .= blockNo
Expand Down Expand Up @@ -94,3 +94,26 @@ deriving newtype instance ToJSON (Ledger.Mary.PolicyID StandardCrypto)

instance (ToJSONKey k, ToJSON v) => ToJSON (SetAlgebra.BiMap v k v) where
toJSON = toJSON . SetAlgebra.forwards -- to normal Map

instance ToJSON (TPraosState StandardCrypto) where
toJSON s = Aeson.object
[ "lastSlot" .= Consensus.tpraosStateLastSlot s
, "chainDepState" .= Consensus.tpraosStateChainDepState s
]

instance ToJSON (PraosState StandardCrypto) where
toJSON s = Aeson.object
[ "lastSlot" .= Consensus.praosStateLastSlot s
, "oCertCounters" .= Consensus.praosStateOCertCounters s
, "evolvingNonce" .= Consensus.praosStateEvolvingNonce s
, "candidateNonce" .= Consensus.praosStateCandidateNonce s
, "epochNonce" .= Consensus.praosStateEpochNonce s
, "labNonce" .= Consensus.praosStateLabNonce s
, "lastEpochBlockNonce" .= Consensus.praosStateLastEpochBlockNonce s
]


instance ToJSON (Cardano.WithOrigin Cardano.SlotNo) where
toJSON = \case
Cardano.Origin -> Aeson.String "origin"
Cardano.At (Cardano.SlotNo n) -> toJSON n
41 changes: 30 additions & 11 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ runQueryProtocolState
-> Maybe OutputFile
-> ExceptT ShelleyQueryCmdError IO ()
runQueryProtocolState (AnyConsensusModeParams cModeParams)
network _mOutFile = do
network mOutFile = do
SocketPath sockPath <- firstExceptT ShelleyQueryCmdEnvVarSocketErr readEnvSocketPath
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath

Expand All @@ -689,12 +689,16 @@ runQueryProtocolState (AnyConsensusModeParams cModeParams)
let qInMode = QueryInEra eInMode
. QueryInShelleyBasedEra sbe
$ QueryProtocolState
_result <- executeQuery
result <- executeQuery
era
cModeParams
localNodeConnInfo
qInMode
panic "currentlyBroken: runQueryProtocolState writeProtocolState mOutFile result"

case cMode of
CardanoMode -> eligibleWriteProtocolStateConstaints sbe $ writeProtocolState mOutFile result
mode -> left . ShelleyQueryCmdUnsupportedMode $ AnyConsensusMode mode

Nothing -> left $ ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE

-- | Query the current delegations and reward accounts, filtered by a given
Expand Down Expand Up @@ -853,16 +857,18 @@ writePoolParams (StakePoolKeyHash hk) qState =

liftIO . LBS.putStrLn $ encodePretty $ Params poolParams fPoolParams retiring

_writeProtocolState :: FromCBOR (Consensus.ChainDepState (ConsensusProtocol era))
=> ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
=> Maybe OutputFile
-> ProtocolState era
-> ExceptT ShelleyQueryCmdError IO ()
_writeProtocolState mOutFile ps@(ProtocolState pstate) =
writeProtocolState ::
( FromCBOR (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
)
=> Maybe OutputFile
-> ProtocolState era
-> ExceptT ShelleyQueryCmdError IO ()
writeProtocolState mOutFile ps@(ProtocolState pstate) =
case mOutFile of
Nothing -> case decodeProtocolState ps of
Left (bs, _) -> firstExceptT ShelleyQueryCmdHelpersError $ pPrintCBOR bs
Right chainDepstate -> liftIO . LBS.putStrLn $ encodePretty chainDepstate
Left (bs, _) -> firstExceptT ShelleyQueryCmdHelpersError $ pPrintCBOR bs
Right chainDepstate -> liftIO . LBS.putStrLn $ encodePretty chainDepstate
Just (OutputFile fpath) ->
handleIOExceptT (ShelleyQueryCmdWriteFileError . FileIOError fpath)
. LBS.writeFile fpath $ unSerialised pstate
Expand Down Expand Up @@ -1365,6 +1371,19 @@ eligibleLeaderSlotsConstaints ShelleyBasedEraMary f = f
eligibleLeaderSlotsConstaints ShelleyBasedEraAlonzo f = f
eligibleLeaderSlotsConstaints ShelleyBasedEraBabbage f = f

eligibleWriteProtocolStateConstaints
:: ShelleyBasedEra era
-> (( FromCBOR (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
) => a
)
-> a
eligibleWriteProtocolStateConstaints ShelleyBasedEraShelley f = f
eligibleWriteProtocolStateConstaints ShelleyBasedEraAllegra f = f
eligibleWriteProtocolStateConstaints ShelleyBasedEraMary f = f
eligibleWriteProtocolStateConstaints ShelleyBasedEraAlonzo f = f
eligibleWriteProtocolStateConstaints ShelleyBasedEraBabbage f = f

-- Required instances
-- instance FromCBOR (TPraosState StandardCrypto) where
-- instance FromCBOR (Praos.PraosState StandardCrypto) where