Skip to content

Commit

Permalink
#667 Add ref-script-size query command
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Mar 26, 2024
1 parent 644294c commit f582c37
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ library
, binary
, bytestring
, canonical-json
, cardano-api ^>= 8.42.0.0
, cardano-api ^>= 8.43.0.0
, cardano-binary
, cardano-crypto
, cardano-crypto-class ^>= 2.1.2
Expand Down
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Cardano.CLI.EraBased.Commands.Query
, QueryPoolStateCmdArgs(..)
, QueryTxMempoolCmdArgs(..)
, QuerySlotNumberCmdArgs(..)
, QueryRefScriptSizeCmdArgs(..)
, QueryNoArgCmdArgs(..)
, QueryDRepStateCmdArgs(..)
, QueryDRepStakeDistributionCmdArgs(..)
Expand All @@ -34,6 +35,7 @@ import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Key
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus

import Data.Set (Set)
import Data.Text (Text)
import Data.Time.Clock
import GHC.Generics
Expand All @@ -54,6 +56,7 @@ data QueryCmds era
| QueryPoolStateCmd !QueryPoolStateCmdArgs
| QueryTxMempoolCmd !QueryTxMempoolCmdArgs
| QuerySlotNumberCmd !QuerySlotNumberCmdArgs
| QueryRefScriptSizeCmd !QueryRefScriptSizeCmdArgs
| QueryConstitutionCmd !(QueryNoArgCmdArgs era)
| QueryGovStateCmd !(QueryNoArgCmdArgs era)
| QueryDRepStateCmd !(QueryDRepStateCmdArgs era)
Expand Down Expand Up @@ -193,6 +196,16 @@ data QuerySlotNumberCmdArgs = QuerySlotNumberCmdArgs
, utcTime :: !UTCTime
} deriving (Generic, Show)

data QueryRefScriptSizeCmdArgs = QueryRefScriptSizeCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, transactionInputs :: !(Set TxIn)
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, format :: Maybe QueryOutputFormat
, mOutFile :: !(Maybe (File () Out))
} deriving (Generic, Show)

data QueryNoArgCmdArgs era = QueryNoArgCmdArgs
{ eon :: !(ConwayEraOnwards era)
, nodeSocketPath :: !SocketPath
Expand Down Expand Up @@ -272,6 +285,8 @@ renderQueryCmds = \case
"query tx-mempool" <> renderTxMempoolQuery q
QuerySlotNumberCmd {} ->
"query slot-number"
QueryRefScriptSizeCmd {} ->
"query ref-script-size"
QueryConstitutionCmd {} ->
"constitution"
QueryGovStateCmd {} ->
Expand Down
25 changes: 25 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Key

import Data.Foldable
import GHC.Exts (IsList (..))
import Options.Applicative hiding (help, str)
import qualified Options.Applicative as Opt

Expand Down Expand Up @@ -109,6 +110,10 @@ pQueryCmds era envCli =
$ subParser "slot-number"
$ Opt.info (pQuerySlotNumberCmd era envCli)
$ Opt.progDesc "Query slot number for UTC timestamp"
, Just
. subParser "ref-script-size"
. Opt.info (pQueryRefScriptSizeCmd era envCli)
$ Opt.progDesc "Calculate the reference input scripts size in bytes for provided transaction ids"
, pQueryGetConstitutionCmd era envCli
, pQueryGetGovStateCmd era envCli
, pQueryDRepStateCmd era envCli
Expand Down Expand Up @@ -295,6 +300,26 @@ pQuerySlotNumberCmd era envCli =
, Opt.help "UTC timestamp in YYYY-MM-DDThh:mm:ssZ format"
]

pQueryRefScriptSizeCmd :: CardanoEra era -> EnvCli -> Parser (QueryCmds era)
pQueryRefScriptSizeCmd era envCli =
fmap QueryRefScriptSizeCmd $
QueryRefScriptSizeCmdArgs
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> (fromList <$> some pByTxIn)
<*> pNetworkId envCli
<*> pTarget era
<*> (optional $ pQueryOutputFormat "reference inputs")
<*> pMaybeOutputFile
where
pByTxIn :: Parser TxIn
pByTxIn =
Opt.option (readerFromParsecParser parseTxIn) $ mconcat
[ Opt.long "tx-in"
, Opt.metavar "TX-IN"
, Opt.help "Transaction input (TxId#TxIx)."
]

pQueryGetConstitutionCmd :: ()
=> CardanoEra era
-> EnvCli
Expand Down
68 changes: 68 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -82,13 +84,15 @@ import qualified Data.Map.Strict as Map
import Data.Proxy (Proxy (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.String
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Text.IO as T
import qualified Data.Text.IO as Text
import qualified Data.Text.Lazy.IO as LT
import Data.Time.Clock
import GHC.Generics
import Lens.Micro ((^.))
import Numeric (showEFloat)
import Prettyprinter
Expand Down Expand Up @@ -116,6 +120,7 @@ runQueryCmds = \case
Cmd.QueryPoolStateCmd args -> runQueryPoolStateCmd args
Cmd.QueryTxMempoolCmd args -> runQueryTxMempoolCmd args
Cmd.QuerySlotNumberCmd args -> runQuerySlotNumberCmd args
Cmd.QueryRefScriptSizeCmd args -> runQueryRefScriptSizeCmd args
Cmd.QueryConstitutionCmd args -> runQueryConstitution args
Cmd.QueryGovStateCmd args -> runQueryGovState args
Cmd.QueryDRepStateCmd args -> runQueryDRepState args
Expand Down Expand Up @@ -673,6 +678,51 @@ runQuerySlotNumberCmd
SlotNo slotNo <- utcTimeToSlotNo nodeSocketPath consensusModeParams networkId target utcTime
liftIO . putStr $ show slotNo

runQueryRefScriptSizeCmd
:: ()
=> Cmd.QueryRefScriptSizeCmdArgs
-> ExceptT QueryCmdError IO ()
runQueryRefScriptSizeCmd
Cmd.QueryRefScriptSizeCmdArgs
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.transactionInputs
, Cmd.networkId
, Cmd.target
, Cmd.format
, Cmd.mOutFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

join $ lift
( executeLocalStateQueryExpr localNodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <- lift queryCurrentEra
& onLeft (left . QueryCmdUnsupportedNtcVersion)

sbe <- requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)

beo <- requireEon BabbageEra era

utxo <- lift (queryUtxo sbe $ QueryUTxOByTxIn transactionInputs)
& onLeft (left . QueryCmdUnsupportedNtcVersion)
& onLeft (left . QueryCmdLocalStateQueryError . EraMismatchError)

pure $
writeFormattedOutput format mOutFile $
RefInputScriptSize $
getReferenceInputsSizeForTxIds beo (toLedgerUTxO sbe utxo) transactionInputs
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

newtype RefInputScriptSize = RefInputScriptSize { refInputScriptSize :: Int }
deriving (Generic)
deriving anyclass (ToJSON)

instance Pretty RefInputScriptSize where
pretty (RefInputScriptSize s) = "Reference inputs scripts size is" <+> pretty s <+> "bytes."

-- | Obtain stake snapshot information for a pool, plus information about the total active stake.
-- This information can be used for leader slot calculation, for example, and has been requested by SPOs.
-- Obtaining the information directly is significantly more time and memory efficient than using a full ledger state dump.
Expand Down Expand Up @@ -1121,6 +1171,7 @@ runQueryStakePoolsCmd
) & onLeft (left . QueryCmdAcquireFailure)
& onLeft left

-- TODO: replace with writeFormattedOutput
writeStakePools
:: QueryOutputFormat
-> Maybe (File () Out)
Expand All @@ -1139,6 +1190,23 @@ writeStakePools format mOutFile stakePools =
QueryOutputFormatJson ->
encodePretty stakePools

writeFormattedOutput
:: MonadIOTransError QueryCmdError t m
=> ToJSON a
=> Pretty a
=> Maybe QueryOutputFormat
-> Maybe (File b Out)
-> a
-> t m ()
writeFormattedOutput mFormat mOutFile value =
modifyError QueryCmdWriteFileError . hoistIOEither $
writeLazyByteStringOutput mOutFile toWrite
where
toWrite :: LBS.ByteString =
case newOutputFormat mFormat mOutFile of
QueryOutputFormatText -> fromString . docToString $ pretty value
QueryOutputFormatJson -> encodePretty value

runQueryStakeDistributionCmd :: ()
=> Cmd.QueryStakeDistributionCmdArgs
-> ExceptT QueryCmdError IO ()
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Help.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ hprop_golden_HelpCmds =
let usages = List.filter (not . null) $ fmap extractCmd $ maybeToList . selectCmd =<< lines

forM_ usages $ \usage -> do
-- FIXME: this is sloow
H.noteShow_ usage
let expectedCmdHelpFp = "test/cardano-cli-golden/files/golden/help" </> Text.unpack (Text.intercalate "_" usage) <> ".cli"

Expand Down
Loading

0 comments on commit f582c37

Please sign in to comment.