Skip to content
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

Query for stakepools in run tx build #3140

Merged
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
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ renderPoolCmd cmd =
data QueryCmd =
QueryProtocolParameters' AnyConsensusModeParams NetworkId (Maybe OutputFile)
| QueryTip AnyConsensusModeParams NetworkId (Maybe OutputFile)
| QueryStakePools' AnyConsensusModeParams NetworkId (Maybe OutputFile)
| QueryStakeDistribution' AnyConsensusModeParams NetworkId (Maybe OutputFile)
| QueryStakeAddressInfo AnyConsensusModeParams StakeAddress NetworkId (Maybe OutputFile)
| QueryUTxO' AnyConsensusModeParams QueryUTxOFilter NetworkId (Maybe OutputFile)
Expand All @@ -354,6 +355,7 @@ renderQueryCmd cmd =
case cmd of
QueryProtocolParameters' {} -> "query protocol-parameters "
QueryTip {} -> "query tip"
QueryStakePools' {} -> "query stake-pools"
QueryStakeDistribution' {} -> "query stake-distribution"
QueryStakeAddressInfo {} -> "query stake-address-info"
QueryUTxO' {} -> "query utxo"
Expand Down
9 changes: 9 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,8 @@ pQueryCmd =
(Opt.info pQueryProtocolParameters $ Opt.progDesc "Get the node's current protocol parameters")
, subParser "tip"
(Opt.info pQueryTip $ Opt.progDesc "Get the node's current tip (slot no, hash, block no)")
, subParser "stake-pools"
(Opt.info pQueryStakePools $ Opt.progDesc "Get the node's current set of stake pool ids")
, subParser "stake-distribution"
(Opt.info pQueryStakeDistribution $ Opt.progDesc "Get the node's current aggregated stake distribution")
, subParser "stake-address-info"
Expand Down Expand Up @@ -889,6 +891,13 @@ pQueryCmd =
<*> pNetworkId
<*> pMaybeOutputFile

pQueryStakePools :: Parser QueryCmd
pQueryStakePools =
QueryStakePools'
<$> pConsensusModeParams
<*> pNetworkId
<*> pMaybeOutputFile

pQueryStakeDistribution :: Parser QueryCmd
pQueryStakeDistribution =
QueryStakeDistribution'
Expand Down
52 changes: 51 additions & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ runQueryCmd cmd =
runQueryProtocolParameters consensusModeParams network mOutFile
QueryTip consensusModeParams network mOutFile ->
runQueryTip consensusModeParams network mOutFile
QueryStakePools' consensusModeParams network mOutFile ->
runQueryStakePools consensusModeParams network mOutFile
QueryStakeDistribution' consensusModeParams network mOutFile ->
runQueryStakeDistribution consensusModeParams network mOutFile
QueryStakeAddressInfo consensusModeParams addr network mOutFile ->
Expand Down Expand Up @@ -664,6 +666,54 @@ printUtxo shelleyBasedEra' txInOutTuple =
printableValue (TxOutValue _ val) = renderValue val
printableValue (TxOutAdaOnly _ (Lovelace i)) = Text.pack $ show i

joinEither :: (x -> z) -> (y -> z) -> Either x (Either y a) -> Either z a
joinEither f g = join . bimap f (first g)

joinEitherM :: Functor m => (x -> z) -> (y -> z) -> m (Either x (Either y a)) -> m (Either z a)
joinEitherM f g = fmap (joinEither f g)

runQueryStakePools
:: AnyConsensusModeParams
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyQueryCmdError IO ()
runQueryStakePools (AnyConsensusModeParams cModeParams)
network mOutFile = do
SocketPath sockPath <- firstExceptT ShelleyQueryCmdEnvVarSocketErr readEnvSocketPath

let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath

result <- ExceptT . joinEitherM ShelleyQueryCmdAcquireFailure id $
executeLocalStateQueryExpr localNodeConnInfo Nothing $ \_ntcVersion -> runExceptT @ShelleyQueryCmdError $ do
anyE@(AnyCardanoEra era) <- case consensusModeOnly cModeParams of
ByronMode -> return $ AnyCardanoEra ByronEra
ShelleyMode -> return $ AnyCardanoEra ShelleyEra
CardanoMode -> lift . queryExpr $ QueryCurrentEra CardanoModeIsMultiEra

let cMode = consensusModeOnly cModeParams

case toEraInMode era cMode of
Just eInMode -> do
sbe <- getSbe $ cardanoEraStyle era

firstExceptT ShelleyQueryCmdEraMismatch . ExceptT $
queryExpr . QueryInEra eInMode . QueryInShelleyBasedEra sbe $ QueryStakePools

Nothing -> left $ ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE

writeStakePools mOutFile result

writeStakePools
:: Maybe OutputFile
-> Set PoolId
-> ExceptT ShelleyQueryCmdError IO ()
writeStakePools (Just (OutputFile outFile)) stakePools =
handleIOExceptT (ShelleyQueryCmdWriteFileError . FileIOError outFile) $
LBS.writeFile outFile (encodePretty stakePools)

writeStakePools Nothing stakePools =
forM_ (Set.toList stakePools) $ \poolId ->
liftIO . putStrLn $ Text.unpack (serialiseToBech32 poolId)

runQueryStakeDistribution
:: AnyConsensusModeParams
Expand Down Expand Up @@ -794,7 +844,7 @@ executeQuery era cModeP localNodeConnInfo q = do
execQuery :: IO (Either AcquireFailure (Either EraMismatch result))
execQuery = queryNodeLocalState localNodeConnInfo Nothing q

getSbe :: CardanoEraStyle era -> ExceptT ShelleyQueryCmdError IO (ShelleyBasedEra era)
getSbe :: Monad m => CardanoEraStyle era -> ExceptT ShelleyQueryCmdError m (ShelleyBasedEra era)
getSbe LegacyByronEra = left ShelleyQueryCmdByronEra
getSbe (ShelleyBasedEra sbe) = return sbe

Expand Down
122 changes: 26 additions & 96 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Cardano.CLI.Shelley.Run.Transaction
) where

import Cardano.Prelude hiding (All, Any)
import Prelude (String, error)
import Prelude (String, error, id)

import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Char8 as BS
Expand All @@ -24,7 +24,6 @@ import qualified Data.Set as Set
import qualified Data.Text as Text
import Data.Type.Equality (TestEquality (..))

import Control.Concurrent.STM
import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT, hoistEither,
hoistMaybe, left, newExceptT)

Expand All @@ -50,14 +49,11 @@ import Cardano.CLI.Shelley.Run.Query (ShelleyQueryCmdLocalStateQueryEr
renderLocalStateQueryError)
import Cardano.CLI.Shelley.Script
import Cardano.CLI.Types
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (..))
import Ouroboros.Consensus.Byron.Ledger (ByronBlock)
import Ouroboros.Consensus.Cardano.Block (EraMismatch (..))
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr)
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Client as Net.Query
import Ouroboros.Network.Protocol.LocalStateQuery.Type (AcquireFailure (..))
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Net.Query
import qualified Ouroboros.Network.Protocol.LocalTxSubmission.Client as Net.Tx

import qualified System.IO as IO
Expand Down Expand Up @@ -363,6 +359,11 @@ runTxBuildRaw (AnyCardanoEra era)
firstExceptT ShelleyTxCmdWriteFileError . newExceptT $
writeFileTextEnvelope fpath Nothing txBody

joinEither :: (x -> z) -> (y -> z) -> Either x (Either y a) -> Either z a
joinEither f g = join . bimap f (first g)

joinEitherM :: Functor m => (x -> z) -> (y -> z) -> m (Either x (Either y a)) -> m (Either z a)
joinEitherM f g = fmap (joinEither f g)

runTxBuild
:: AnyCardanoEra
Expand Down Expand Up @@ -407,6 +408,7 @@ runTxBuild (AnyCardanoEra era) (AnyConsensusModeParams cModeParams) networkId mS
consensusMode = consensusModeOnly cModeParams
dummyFee = Just $ Lovelace 0
onlyInputs = [input | (input,_) <- txins]

case (consensusMode, cardanoEraStyle era) of
(CardanoMode, ShelleyBasedEra sbe) -> do
txBodyContent <-
Expand All @@ -428,26 +430,30 @@ runTxBuild (AnyCardanoEra era) (AnyConsensusModeParams cModeParams) networkId mS
<*> validateTxMintValue era mValue
<*> validateTxScriptValidity era mScriptValidity

-- TODO: Combine queries
let localConnInfo = LocalNodeConnectInfo
{ localConsensusModeParams = CardanoModeParams (EpochSlots 21600)
, localNodeNetworkId = networkId
, localNodeSocketPath = sockPath
}

eInMode <- case toEraInMode era CardanoMode of
Just result -> return result
Nothing ->
left (ShelleyTxCmdEraConsensusModeMismatchTxBalance outBody
(AnyConsensusMode CardanoMode) (AnyCardanoEra era))

let utxoQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe
(QueryUTxO . QueryUTxOByTxIn $ Set.fromList onlyInputs)
let pParamsQuery = QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryProtocolParameters
utxo <- executeQuery era cModeParams localConnInfo utxoQuery
pparams <- executeQuery era cModeParams localConnInfo pParamsQuery
(eraHistory, systemStart) <- firstExceptT ShelleyTxCmdAcquireFailure
$ newExceptT $ queryEraHistoryAndSystemStart localNodeConnInfo Nothing
(utxo, pparams, eraHistory, systemStart, stakePools) <-
newExceptT . joinEitherM ShelleyTxCmdAcquireFailure id $
executeLocalStateQueryExpr localNodeConnInfo Nothing $ \_ntcVersion -> runExceptT $ do
utxo <- firstExceptT ShelleyTxCmdTxSubmitErrorEraMismatch . newExceptT . queryExpr
$ QueryInEra eInMode $ QueryInShelleyBasedEra sbe
$ QueryUTxO (QueryUTxOByTxIn (Set.fromList onlyInputs))

pparams <- firstExceptT ShelleyTxCmdTxSubmitErrorEraMismatch . newExceptT . queryExpr
$ QueryInEra eInMode $ QueryInShelleyBasedEra sbe QueryProtocolParameters

eraHistory <- lift . queryExpr $ QueryEraHistory CardanoModeIsMultiEra

systemStart <- lift $ queryExpr QuerySystemStart

stakePools <- firstExceptT ShelleyTxCmdTxSubmitErrorEraMismatch . ExceptT $
queryExpr . QueryInEra eInMode . QueryInShelleyBasedEra sbe $ QueryStakePools

return (utxo, pparams, eraHistory, systemStart, stakePools)

let cAddr = case anyAddressInEra era changeAddr of
Just addr -> addr
Expand All @@ -457,7 +463,7 @@ runTxBuild (AnyCardanoEra era) (AnyConsensusModeParams cModeParams) networkId mS
firstExceptT ShelleyTxCmdBalanceTxBody
. hoistEither
$ makeTransactionBodyAutoBalance eInMode systemStart eraHistory
pparams Set.empty utxo txBodyContent
pparams stakePools utxo txBodyContent
cAddr mOverrideWits

putStrLn $ "Estimated transaction fee: " <> (show fee :: String)
Expand All @@ -469,47 +475,6 @@ runTxBuild (AnyCardanoEra era) (AnyConsensusModeParams cModeParams) networkId mS

(wrongMode, _) -> left (ShelleyTxCmdUnsupportedMode (AnyConsensusMode wrongMode))

queryEraHistoryAndSystemStart
:: LocalNodeConnectInfo CardanoMode
-> Maybe ChainPoint
-> IO (Either Net.Query.AcquireFailure (EraHistory CardanoMode, SystemStart))
queryEraHistoryAndSystemStart connctInfo mpoint = do
resultVar <- newEmptyTMVarIO
connectToLocalNode
connctInfo
LocalNodeClientProtocols
{ localChainSyncClient = NoLocalChainSyncClient
, localStateQueryClient = Just (singleQuery mpoint resultVar)
, localTxSubmissionClient = Nothing
}
atomically (takeTMVar resultVar)
where
singleQuery
:: Maybe ChainPoint
-> TMVar (Either Net.Query.AcquireFailure (EraHistory CardanoMode, SystemStart))
-> Net.Query.LocalStateQueryClient (BlockInMode CardanoMode) ChainPoint
(QueryInMode CardanoMode) IO ()
singleQuery mPointVar' resultVar' =
LocalStateQueryClient $ do
pure . Net.Query.SendMsgAcquire mPointVar' $
Net.Query.ClientStAcquiring
{ Net.Query.recvMsgAcquired =
pure $ Net.Query.SendMsgQuery (QueryEraHistory CardanoModeIsMultiEra) $
Net.Query.ClientStQuerying
{ Net.Query.recvMsgResult = \result1 -> do
pure $ Net.Query.SendMsgQuery QuerySystemStart $
Net.Query.ClientStQuerying
{ Net.Query.recvMsgResult = \result2 -> do
atomically $ putTMVar resultVar' (Right (result1, result2))

pure $ Net.Query.SendMsgRelease $ pure $ Net.Query.SendMsgDone ()
}
}
, Net.Query.recvMsgFailure = \failure -> do
atomically $ putTMVar resultVar' (Left failure)
pure $ Net.Query.SendMsgDone ()
}


-- ----------------------------------------------------------------------------
-- Transaction body validation and conversion
Expand Down Expand Up @@ -1460,38 +1425,3 @@ readFileTxMetadata _ (MetadataFileCBOR fp) = do
firstExceptT (ShelleyTxCmdMetaValidationError fp) $ hoistEither $ do
validateTxMetadata txMetadata
return txMetadata

executeQuery
:: forall result era mode. CardanoEra era
-> ConsensusModeParams mode
-> LocalNodeConnectInfo mode
-> QueryInMode mode (Either EraMismatch result)
-> ExceptT ShelleyTxCmdError IO result
executeQuery era cModeP localNodeConnInfo q = do
eraInMode <- calcEraInMode era $ consensusModeOnly cModeP
case eraInMode of
ByronEraInByronMode -> left ShelleyTxCmdByronEraQuery
_ -> liftIO execQuery >>= queryResult
where
execQuery :: IO (Either AcquireFailure (Either EraMismatch result))
execQuery = queryNodeLocalState localNodeConnInfo Nothing q


queryResult
:: Either AcquireFailure (Either EraMismatch a)
-> ExceptT ShelleyTxCmdError IO a
queryResult eAcq =
case eAcq of
Left acqFailure -> left $ ShelleyTxCmdAcquireFailure acqFailure
Right eResult ->
case eResult of
Left err -> left . ShelleyTxCmdLocalStateQueryError $ EraMismatchError err
Right result -> return result

calcEraInMode
:: CardanoEra era
-> ConsensusMode mode
-> ExceptT ShelleyTxCmdError IO (EraInMode era mode)
calcEraInMode era mode=
hoistMaybe (ShelleyTxCmdEraConsensusModeMismatchQuery (AnyConsensusMode mode) (anyCardanoEra era))
$ toEraInMode era mode