|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE OverloadedLists #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | +{-# LANGUAGE TypeApplications #-} |
| 7 | + |
| 8 | +module Cardano.Testnet.Test.Rpc.Query |
| 9 | + ( hprop_rpc_query_pparams |
| 10 | + ) |
| 11 | +where |
| 12 | + |
| 13 | +import Cardano.Api |
| 14 | +import qualified Cardano.Api.Ledger as L |
| 15 | + |
| 16 | +import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..)) |
| 17 | +import qualified Cardano.Ledger.Api as L |
| 18 | +import qualified Cardano.Ledger.Binary.Version as L |
| 19 | +import qualified Cardano.Ledger.Conway.Core as L |
| 20 | +import qualified Cardano.Ledger.Conway.PParams as L |
| 21 | +import qualified Cardano.Ledger.Plutus as L |
| 22 | +import qualified Cardano.Rpc.Client as Rpc |
| 23 | +import qualified Cardano.Rpc.Proto.Api.UtxoRpc.Query as U5c |
| 24 | +import Cardano.Rpc.Server.Internal.UtxoRpc.Query () |
| 25 | +import Cardano.Rpc.Server.Internal.UtxoRpc.Type (anyUtxoDataUtxoRpcToUtxo, |
| 26 | + utxoRpcBigIntToInteger) |
| 27 | +import Cardano.Testnet |
| 28 | + |
| 29 | +import Prelude |
| 30 | + |
| 31 | +import Control.Exception |
| 32 | +import qualified Data.ByteString.Short as SBS |
| 33 | +import Data.Default.Class |
| 34 | +import qualified Data.Map.Strict as M |
| 35 | +import Lens.Micro |
| 36 | + |
| 37 | +import Testnet.Components.Query |
| 38 | +import Testnet.Process.Run |
| 39 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 40 | +import Testnet.Start.Types |
| 41 | + |
| 42 | +import Hedgehog |
| 43 | +import qualified Hedgehog as H |
| 44 | +import qualified Hedgehog.Extras.Test.Base as H |
| 45 | +import qualified Hedgehog.Extras.Test.TestWatchdog as H |
| 46 | + |
| 47 | +hprop_rpc_query_pparams :: Property |
| 48 | +hprop_rpc_query_pparams = integrationRetryWorkspace 2 "rpc-query-pparams" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do |
| 49 | + conf@Conf{tempAbsPath} <- mkConf tempAbsBasePath' |
| 50 | + let tempAbsPath' = unTmpAbsPath tempAbsPath |
| 51 | + |
| 52 | + let ceo = ConwayEraOnwardsConway |
| 53 | + sbe = convert ceo |
| 54 | + eraName = eraToString sbe |
| 55 | + options = def{cardanoNodeEra = AnyShelleyBasedEra sbe, cardanoEnableRpc = True} |
| 56 | + |
| 57 | + TestnetRuntime |
| 58 | + { testnetMagic |
| 59 | + , configurationFile |
| 60 | + , testnetNodes = node0@TestnetNode{nodeSprocket} : _ |
| 61 | + } <- |
| 62 | + createAndRunTestnet options def conf |
| 63 | + |
| 64 | + execConfig <- mkExecConfig tempAbsPath' nodeSprocket testnetMagic |
| 65 | + epochStateView <- getEpochStateView configurationFile (nodeSocketPath node0) |
| 66 | + pparams <- unLedgerProtocolParameters <$> getProtocolParams epochStateView ceo |
| 67 | + -- H.noteShowPretty_ pparams |
| 68 | + utxos <- findAllUtxos epochStateView sbe |
| 69 | + H.noteShowPretty_ utxos |
| 70 | + rpcSocket <- H.note . unFile $ nodeRpcSocketPath node0 |
| 71 | + |
| 72 | + ---------- |
| 73 | + -- Get tip |
| 74 | + ---------- |
| 75 | + QueryTipLocalStateOutput{localStateChainTip} <- |
| 76 | + H.noteShowM $ execCliStdoutToJson execConfig [eraName, "query", "tip"] |
| 77 | + (slot, blockHash, blockNo) <- case localStateChainTip of |
| 78 | + ChainTipAtGenesis -> H.failure |
| 79 | + ChainTip (SlotNo slot) (HeaderHash hash) (BlockNo blockNo) -> pure (slot, SBS.fromShort hash, blockNo) |
| 80 | + |
| 81 | + -------------- |
| 82 | + -- RPC queries |
| 83 | + -------------- |
| 84 | + let rpcServer = Rpc.ServerUnix rpcSocket |
| 85 | + (pparamsResponse, utxosResponse) <- H.noteShowM . H.evalIO . Rpc.withConnection def rpcServer $ \conn -> do |
| 86 | + pparams' <- do |
| 87 | + let req = Rpc.defMessage |
| 88 | + Rpc.nonStreaming conn (Rpc.rpc @(Rpc.Protobuf U5c.QueryService "readParams")) req |
| 89 | + |
| 90 | + utxos' <- do |
| 91 | + let req = Rpc.defMessage |
| 92 | + Rpc.nonStreaming conn (Rpc.rpc @(Rpc.Protobuf U5c.QueryService "readUtxos")) req |
| 93 | + pure (pparams', utxos') |
| 94 | + |
| 95 | + --------------------------- |
| 96 | + -- Test readParams response |
| 97 | + --------------------------- |
| 98 | + pparamsResponse ^. U5c.ledgerTip . U5c.slot === slot |
| 99 | + pparamsResponse ^. U5c.ledgerTip . U5c.hash === blockHash |
| 100 | + pparamsResponse ^. U5c.ledgerTip . U5c.height === blockNo |
| 101 | + pparamsResponse ^. U5c.ledgerTip . U5c.timestamp === 0 -- not possible to implement at this moment |
| 102 | + |
| 103 | + -- https://docs.cardano.org/about-cardano/explore-more/parameter-guide |
| 104 | + let chainParams = pparamsResponse ^. U5c.values . U5c.cardano |
| 105 | + babbageEraOnwardsConstraints (convert ceo) $ do |
| 106 | + pparams ^. L.ppCoinsPerUTxOByteL . to L.unCoinPerByte . to L.unCoin |
| 107 | + ===^ chainParams ^. U5c.coinsPerUtxoByte . to utxoRpcBigIntToInteger |
| 108 | + pparams ^. L.ppMaxTxSizeL === chainParams ^. U5c.maxTxSize . to fromIntegral |
| 109 | + pparams ^. L.ppMinFeeBL ===^ chainParams ^. U5c.minFeeCoefficient . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 110 | + pparams ^. L.ppMinFeeAL ===^ chainParams ^. U5c.minFeeConstant . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 111 | + pparams ^. L.ppMaxBBSizeL === chainParams ^. U5c.maxBlockBodySize . to fromIntegral |
| 112 | + pparams ^. L.ppMaxBHSizeL === chainParams ^. U5c.maxBlockHeaderSize . to fromIntegral |
| 113 | + pparams ^. L.ppKeyDepositL ===^ chainParams ^. U5c.stakeKeyDeposit . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 114 | + pparams ^. L.ppPoolDepositL ===^ chainParams ^. U5c.poolDeposit . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 115 | + pparams ^. L.ppEMaxL . to L.unEpochInterval === chainParams ^. U5c.poolRetirementEpochBound . to fromIntegral |
| 116 | + pparams ^. L.ppNOptL === chainParams ^. U5c.desiredNumberOfPools . to fromIntegral |
| 117 | + pparams ^. L.ppA0L . to L.unboundRational === chainParams ^. U5c.poolInfluence . to inject |
| 118 | + pparams ^. L.ppNOptL === chainParams ^. U5c.desiredNumberOfPools . to fromIntegral |
| 119 | + pparams ^. L.ppRhoL . to L.unboundRational === chainParams ^. U5c.monetaryExpansion . to inject |
| 120 | + pparams ^. L.ppMinPoolCostL ===^ chainParams ^. U5c.minPoolCost . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 121 | + ( pparams ^. L.ppProtocolVersionL . to L.pvMajor . to L.getVersion |
| 122 | + , pparams ^. L.ppProtocolVersionL . to L.pvMinor |
| 123 | + ) |
| 124 | + === ( chainParams ^. U5c.protocolVersion . U5c.major |
| 125 | + , chainParams ^. U5c.protocolVersion . U5c.minor . to fromIntegral |
| 126 | + ) |
| 127 | + pparams ^. L.ppMaxValSizeL === chainParams ^. U5c.maxValueSize . to fromIntegral |
| 128 | + pparams ^. L.ppCollateralPercentageL === chainParams ^. U5c.collateralPercentage . to fromIntegral |
| 129 | + pparams ^. L.ppMaxCollateralInputsL === chainParams ^. U5c.maxCollateralInputs . to fromIntegral |
| 130 | + let pparamsCostModels = L.getCostModelParams <$> pparams ^. L.ppCostModelsL . to L.costModelsValid |
| 131 | + wrapInMaybe v = if v == mempty then Nothing else Just v |
| 132 | + M.lookup L.PlutusV1 pparamsCostModels === chainParams ^. U5c.costModels . U5c.plutusV1 . U5c.values . to wrapInMaybe |
| 133 | + M.lookup L.PlutusV2 pparamsCostModels === chainParams ^. U5c.costModels . U5c.plutusV2 . U5c.values . to wrapInMaybe |
| 134 | + M.lookup L.PlutusV3 pparamsCostModels === chainParams ^. U5c.costModels . U5c.plutusV3 . U5c.values . to wrapInMaybe |
| 135 | + M.lookup L.PlutusV4 pparamsCostModels === chainParams ^. U5c.costModels . U5c.plutusV4 . U5c.values . to wrapInMaybe |
| 136 | + pparams ^. L.ppPricesL . to L.prSteps . to L.unboundRational === chainParams ^. U5c.prices . U5c.steps . to inject |
| 137 | + pparams ^. L.ppPricesL . to L.prMem . to L.unboundRational === chainParams ^. U5c.prices . U5c.memory . to inject |
| 138 | + pparams ^. L.ppMaxTxExUnitsL === chainParams ^. U5c.maxExecutionUnitsPerTransaction . to inject |
| 139 | + pparams ^. L.ppMaxBlockExUnitsL === chainParams ^. U5c.maxExecutionUnitsPerBlock . to inject |
| 140 | + pparams ^. L.ppMinFeeRefScriptCostPerByteL . to L.unboundRational |
| 141 | + === chainParams ^. U5c.minFeeScriptRefCostPerByte . to inject |
| 142 | + let poolVotingThresholds :: L.PoolVotingThresholds = |
| 143 | + conwayEraOnwardsConstraints ceo $ |
| 144 | + pparams ^. L.ppPoolVotingThresholdsL |
| 145 | + ( L.unboundRational |
| 146 | + <$> [ poolVotingThresholds ^. L.pvtMotionNoConfidenceL |
| 147 | + , poolVotingThresholds ^. L.pvtCommitteeNormalL |
| 148 | + , poolVotingThresholds ^. L.pvtCommitteeNoConfidenceL |
| 149 | + , poolVotingThresholds ^. L.pvtHardForkInitiationL |
| 150 | + , poolVotingThresholds ^. L.pvtPPSecurityGroupL |
| 151 | + ] |
| 152 | + ) |
| 153 | + === chainParams ^. U5c.poolVotingThresholds . U5c.thresholds . to (map inject) |
| 154 | + let drepVotingThresholds :: L.DRepVotingThresholds = |
| 155 | + conwayEraOnwardsConstraints ceo $ |
| 156 | + pparams ^. L.ppDRepVotingThresholdsL |
| 157 | + ( L.unboundRational |
| 158 | + <$> [ drepVotingThresholds ^. L.dvtMotionNoConfidenceL |
| 159 | + , drepVotingThresholds ^. L.dvtCommitteeNormalL |
| 160 | + , drepVotingThresholds ^. L.dvtCommitteeNoConfidenceL |
| 161 | + , drepVotingThresholds ^. L.dvtUpdateToConstitutionL |
| 162 | + , drepVotingThresholds ^. L.dvtHardForkInitiationL |
| 163 | + , drepVotingThresholds ^. L.dvtPPNetworkGroupL |
| 164 | + , drepVotingThresholds ^. L.dvtPPEconomicGroupL |
| 165 | + , drepVotingThresholds ^. L.dvtPPTechnicalGroupL |
| 166 | + , drepVotingThresholds ^. L.dvtPPGovGroupL |
| 167 | + , drepVotingThresholds ^. L.dvtTreasuryWithdrawalL |
| 168 | + ] |
| 169 | + ) |
| 170 | + === chainParams ^. U5c.drepVotingThresholds . U5c.thresholds . to (map inject) |
| 171 | + pparams ^. L.ppCommitteeMinSizeL === chainParams ^. U5c.minCommitteeSize . to fromIntegral |
| 172 | + pparams ^. L.ppCommitteeMaxTermLengthL . to L.unEpochInterval |
| 173 | + === chainParams ^. U5c.committeeTermLimit . to fromIntegral |
| 174 | + pparams ^. L.ppGovActionLifetimeL . to L.unEpochInterval |
| 175 | + === chainParams ^. U5c.governanceActionValidityPeriod . to fromIntegral |
| 176 | + pparams ^. L.ppGovActionDepositL ===^ chainParams ^. U5c.governanceActionDeposit . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 177 | + pparams ^. L.ppDRepDepositL ===^ chainParams ^. U5c.drepDeposit . to (fmap L.Coin . utxoRpcBigIntToInteger) |
| 178 | + pparams ^. L.ppDRepActivityL . to L.unEpochInterval === chainParams ^. U5c.drepInactivityPeriod . to fromIntegral |
| 179 | + |
| 180 | + -------------------------- |
| 181 | + -- Test readUtxos response |
| 182 | + -------------------------- |
| 183 | + |
| 184 | + utxoFromUtxoRpc <- H.leftFail $ utxosResponse ^. U5c.items . to (anyUtxoDataUtxoRpcToUtxo $ convert ceo) |
| 185 | + utxos === utxoFromUtxoRpc |
| 186 | + |
| 187 | +(===^) :: (Eq a, Show a, H.MonadTest m) => a -> Either SomeException a -> m () |
| 188 | +expected ===^ actual = do |
| 189 | + v <- H.leftFail actual |
| 190 | + expected === v |
| 191 | + |
| 192 | +infix 4 ===^ |
0 commit comments