CardanoSolutions / ogmios

❇️ A WebSocket JSON/RPC bridge for Cardano
https://ogmios.dev
Mozilla Public License 2.0
304 stars 90 forks source link

How is the correspondence between currentProtocolParameters and cardano parameters? #213

Closed Luis-omega closed 2 years ago

Luis-omega commented 2 years ago

Hi, I'm trying to get the Cardano parameters using Ogmios but I found that the names in the response to currentProtocolParameters are different from the ones used by Cardano in its fromJSON instance for Aeson.

This is the current Cardano fromJSON instance :

instance FromJSON ProtocolParameters here
  parseJSON =
    withObject "ProtocolParameters" $ \o -> do
      v <- o .: "protocolVersion"
      ProtocolParameters
        <$> ((,) <$> v .: "major" <*> v .: "minor")
        <*> o .: "decentralization"
        <*> o .: "extraPraosEntropy"
        <*> o .: "maxBlockHeaderSize"
        <*> o .: "maxBlockBodySize"
        <*> o .: "maxTxSize"
        <*> o .: "txFeeFixed"
        <*> o .: "txFeePerByte"
        <*> o .: "minUTxOValue"
        <*> o .: "stakeAddressDeposit"
        <*> o .: "stakePoolDeposit"
        <*> o .: "minPoolCost"
        <*> o .: "poolRetireMaxEpoch"
        <*> o .: "stakePoolTargetNum"
        <*> o .: "poolPledgeInfluence"
        <*> o .: "monetaryExpansion"
        <*> o .: "treasuryCut"
        <*> o .:? "utxoCostPerWord"
        <*> o .:? "costModels" .!= Map.empty
        <*> o .:? "executionUnitPrices"
        <*> o .:? "maxTxExecutionUnits"
        <*> o .:? "maxBlockExecutionUnits"
        <*> o .:? "maxValueSize"
        <*> o .:? "collateralPercentage"
        <*> o .:? "maxCollateralInputs"

and this is the current local server response :

{
  "type": "jsonwsp/response",
  "version": "1.0",
  "servicename": "ogmios",
  "methodname": "Query",
  "result": {
    "minFeeCoefficient": 44,
    "minFeeConstant": 155381,
    "maxBlockBodySize": 98304,
    "maxBlockHeaderSize": 1100,
    "maxTxSize": 16384,
    "stakeKeyDeposit": 2000000,
    "poolDeposit": 500000000,
    "poolRetirementEpochBound": 18,
    "desiredNumberOfPools": 500,
    "poolInfluence": "3/10",
    "monetaryExpansion": "3/1000",
    "treasuryExpansion": "1/5",
    "decentralizationParameter": "0/1",
    "extraEntropy": "neutral",
    "protocolVersion": {
      "major": 6,
      "minor": 0
    },
    "minPoolCost": 340000000,
    "coinsPerUtxoWord": 34482,
    "costModels": {
           Lots of things here 
    },
    "prices": {
      "memory": "577/10000",
      "steps": "721/10000000"
    },
    "maxExecutionUnitsPerTransaction": {
      "memory": 16000000,
      "steps": 10000000000
    },
    "maxExecutionUnitsPerBlock": {
      "memory": 80000000,
      "steps": 40000000000
    },
    "maxValueSize": 5000,
    "collateralPercentage": 150,
    "maxCollateralInputs": 3
  },
  "reflection": null
}

Some of the parameters I don't know how to map are: minUTxOValue and stakePoolTargetNum

Thanks for your time.

Luis-omega commented 2 years ago

After a process of elimination, if someone wants to use Aeson to parse the Ogmios JSON, can almost do it with the following instance

instance FromJSON ProtocolParametersWrapper where
  parseJSON =
    Aeson.withObject "ProtocolParametersWrapper" $ \top -> do
      o :: Aeson.Object <- top .: "result"
      v <- o .: "protocolVersion"
      params <-
        Shelley.ProtocolParameters
          <$> ((,) <$> v .: "major" <*> v .: "minor")
          <*> o .: "decentralizationParameter"
          <*> o .: "extraEntropy"
          <*> o .: "maxBlockHeaderSize"
          <*> o .: "maxBlockBodySize"
          <*> o .: "maxTxSize"
          <*> o .: "minFeeConstant" 
          <*> o .: "minFeeCoefficient"
          <*> o .: "minUTxOValue" -- Don't know the corresponding value
          <*> o .: "stakeKeyDeposit"
          <*> o .: "poolDeposit"
          <*> o .: "minPoolCost"
          <*> o .: "poolRetirementEpochBound"
          <*> o .: "desiredNumberOfPools"
          <*> o .: "poolInfluence"
          <*> o .: "monetaryExpansion"
          <*> o .: "treasuryExpansion"
          <*> o .:? "coinsPerUTxOWord"
          <*> o .:? "costModels" .!= Map.empty -- Ogmios use `plutus:v1` instead of the cardanos `PlutusScriptV1`
                                                                      -- so, this must need further work.
          <*> o .:? "prices"
          <*> o .:? "maxExecutionUnitsPerTransaction"
          <*> o .:? "maxExecutionUnitsPerBlock"
          <*> o .:? "maxValueSize"
          <*> o .:? "collateralPercentage"
          <*> o .:? "maxCollateralInputs"
      return $ ProtocolParametersWrapper params

I still don't know the corresponding minUTxOValue parameter.

KtorZ commented 2 years ago

Hey! Thanks for reaching out and let me clarify a few things. The protocol parameters regards the Cardano network. The cardano-cli / cardano-api are respectively a command-line interface and a Haskell library that are using some (JSON) representation of those parameters. Ogmios offers a different representation of the same network parameters. Why is that? Mainly because the pieces of software were developed in isolation with slightly different design decisions; they now offer two different flavors. Yet, the same information is available on both side because they ultimately stem from the on-chain binary representation of protocol parameters.

Having said that, know that there are actually 2 versions of the protocol parameters; one introduced with the Shelley era, and a revised one introduced with the Alonzo era. Depending on which era your node is when making the query, you'll get one or the other. In all likelihood, on a synchronized node on Mainnet, you'll get the Alonzo's version. In Alonzo, the minUTxOValue is gone and was replaced by a coinsPerUTxOWord (or utxoCostPerWord in cardano-api / cardano-cli). In addition, stakePoolTargetNum is called desiredNumberOfPools in Ogmios.

I hope this helps a bit.

Luis-omega commented 2 years ago

Yes, this has helpme a lot, thanks!

Also, pretty much thanks for the work in Ogmios.