Skip to content

Commit

Permalink
Modify PParams to use similar naming to ProtocolParams in `cardano-ap…
Browse files Browse the repository at this point in the history
…i` (#4129)

* Modify PParams JSON instances to match cardano-api

* Protect ShelleyGenesis JSON instances by creating a wrapper type

Co-authored-by: Alexey Kuleshevich <[email protected]>
  • Loading branch information
palas and lehins authored Mar 5, 2024
1 parent 829ab73 commit 17c101b
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 98 deletions.
1 change: 1 addition & 0 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Add `SpendingPurpose`, `MintingPurpose`, `CertifyingPurpose`, `RewardingPurpose` pattern synonyms.
* Add `getSpendingScriptsNeeded`, `getRewardingScriptsNeeded`, `getMintingScriptsNeeded`
* Add `zipAsIxItem`
* Modify `PParams` JSON instances to match `cardano-api`

### `testlib`

Expand Down
48 changes: 24 additions & 24 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/PParams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import Data.Aeson as Aeson (
(.!=),
(.:),
)
import qualified Data.Aeson as Aeson (Value)
import qualified Data.Aeson.Types as Aeson
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -474,28 +474,28 @@ instance FromJSON (AlonzoPParams Identity era) where
parseJSON =
Aeson.withObject "PParams" $ \obj ->
AlonzoPParams
<$> obj .: "minFeeA"
<*> obj .: "minFeeB"
<$> obj .: "txFeePerByte"
<*> obj .: "txFeeFixed"
<*> obj .: "maxBlockBodySize"
<*> obj .: "maxTxSize"
<*> obj .: "maxBlockHeaderSize"
<*> obj .: "keyDeposit"
<*> obj .: "poolDeposit"
<*> obj .: "eMax"
<*> obj .: "nOpt"
<*> obj .: "a0"
<*> obj .: "rho"
<*> obj .: "tau"
<*> obj .: "decentralisationParam"
<*> obj .: "extraEntropy"
<*> obj .: "stakeAddressDeposit"
<*> obj .: "stakePoolDeposit"
<*> obj .: "poolRetireMaxEpoch"
<*> obj .: "stakePoolTargetNum"
<*> obj .: "poolPledgeInfluence"
<*> obj .: "monetaryExpansion"
<*> obj .: "treasuryCut"
<*> obj .: "decentralization"
<*> obj .: "extraPraosEntropy"
<*> obj .: "protocolVersion"
<*> obj .: "minPoolCost" .!= mempty
<*> obj .: "lovelacePerUTxOWord"
<*> obj .: "costmdls"
<*> obj .: "prices"
<*> obj .: "maxTxExUnits"
<*> obj .: "maxBlockExUnits"
<*> obj .: "maxValSize"
<*> obj .: "utxoCostPerByte"
<*> obj .: "costModels"
<*> obj .: "executionUnitPrices"
<*> obj .: "maxTxExecutionUnits"
<*> obj .: "maxBlockExecutionUnits"
<*> obj .: "maxValueSize"
<*> obj .: "collateralPercentage"
<*> obj .: "maxCollateralInputs"

Expand Down Expand Up @@ -730,7 +730,7 @@ alonzoPParamsHKDPairs px pp =
alonzoCommonPParamsHKDPairs px pp
++ shelleyCommonPParamsHKDPairsV8 px pp
++ shelleyCommonPParamsHKDPairsV6 px pp
++ [("lovelacePerUTxOWord", hkdMap px (toJSON @CoinPerWord) (pp ^. hkdCoinsPerUTxOWordL @_ @f))]
++ [("utxoCostPerByte", hkdMap px (toJSON @CoinPerWord) (pp ^. hkdCoinsPerUTxOWordL @_ @f))]

-- | These are the fields that are common across all eras starting with Alonzo.
alonzoCommonPParamsHKDPairs ::
Expand All @@ -741,11 +741,11 @@ alonzoCommonPParamsHKDPairs ::
[(Key, HKD f Aeson.Value)]
alonzoCommonPParamsHKDPairs px pp =
shelleyCommonPParamsHKDPairs px pp
++ [ ("costmdls", hkdMap px (toJSON @CostModels) (pp ^. hkdCostModelsL @era @f))
, ("prices", hkdMap px (toJSON @Prices) (pp ^. hkdPricesL @era @f))
, ("maxTxExUnits", hkdMap px (toJSON @ExUnits) (pp ^. hkdMaxTxExUnitsL @era @f))
, ("maxBlockExUnits", hkdMap px (toJSON @ExUnits) (pp ^. hkdMaxBlockExUnitsL @era @f))
, ("maxValSize", hkdMap px (toJSON @Natural) (pp ^. hkdMaxValSizeL @era @f))
++ [ ("costModels", hkdMap px (toJSON @CostModels) (pp ^. hkdCostModelsL @era @f))
, ("executionUnitPrices", hkdMap px (toJSON @Prices) (pp ^. hkdPricesL @era @f))
, ("maxTxExecutionUnits", hkdMap px (toJSON @ExUnits) (pp ^. hkdMaxTxExUnitsL @era @f))
, ("maxBlockExecutionUnits", hkdMap px (toJSON @ExUnits) (pp ^. hkdMaxBlockExUnitsL @era @f))
, ("maxValueSize", hkdMap px (toJSON @Natural) (pp ^. hkdMaxValSizeL @era @f))
, ("collateralPercentage", hkdMap px (toJSON @Natural) (pp ^. hkdCollateralPercentageL @era @f))
, ("maxCollateralInputs", hkdMap px (toJSON @Natural) (pp ^. hkdMaxCollateralInputsL @era @f))
]
Expand Down
1 change: 1 addition & 0 deletions eras/babbage/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Add `getReferenceScriptsNonDistinct`
* Add the constructor `BabbageNonDisjointRefInputs` to `BabbageUtxoPredFailure`
* Utxo rule raises that `PredicateFailure` in Conway and future Eras when they are not disjoint.
* Modify `PParams` JSON instances to match `cardano-api`

## 1.6.0.0

Expand Down
34 changes: 17 additions & 17 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/PParams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ import Data.Aeson as Aeson (
Key,
KeyValue ((.=)),
ToJSON (..),
Value,
object,
pairs,
withObject,
(.!=),
(.:),
)
import qualified Data.Aeson as Aeson (Value)
import Data.Functor.Identity (Identity (..))
import Data.Proxy (Proxy (Proxy))
import Data.Word (Word16, Word32)
Expand Down Expand Up @@ -356,26 +356,26 @@ instance FromJSON (BabbagePParams Identity era) where
parseJSON =
withObject "PParams" $ \obj ->
BabbagePParams
<$> obj .: "minFeeA"
<*> obj .: "minFeeB"
<$> obj .: "txFeePerByte"
<*> obj .: "txFeeFixed"
<*> obj .: "maxBlockBodySize"
<*> obj .: "maxTxSize"
<*> obj .: "maxBlockHeaderSize"
<*> obj .: "keyDeposit"
<*> obj .: "poolDeposit"
<*> obj .: "eMax"
<*> obj .: "nOpt"
<*> obj .: "a0"
<*> obj .: "rho"
<*> obj .: "tau"
<*> obj .: "stakeAddressDeposit"
<*> obj .: "stakePoolDeposit"
<*> obj .: "poolRetireMaxEpoch"
<*> obj .: "stakePoolTargetNum"
<*> obj .: "poolPledgeInfluence"
<*> obj .: "monetaryExpansion"
<*> obj .: "treasuryCut"
<*> obj .: "protocolVersion"
<*> obj .: "minPoolCost" .!= mempty
<*> obj .: "coinsPerUTxOByte"
<*> obj .: "costmdls"
<*> obj .: "prices"
<*> obj .: "maxTxExUnits"
<*> obj .: "maxBlockExUnits"
<*> obj .: "maxValSize"
<*> obj .: "utxoCostPerByte"
<*> obj .: "costModels"
<*> obj .: "executionUnitPrices"
<*> obj .: "maxTxExecutionUnits"
<*> obj .: "maxBlockExecutionUnits"
<*> obj .: "maxValueSize"
<*> obj .: "collateralPercentage"
<*> obj .: "maxCollateralInputs"

Expand Down Expand Up @@ -550,7 +550,7 @@ babbageCommonPParamsHKDPairs ::
[(Key, HKD f Aeson.Value)]
babbageCommonPParamsHKDPairs px pp =
alonzoCommonPParamsHKDPairs px pp
<> [("coinsPerUTxOByte", hkdMap px (toJSON @CoinPerByte) (pp ^. hkdCoinsPerUTxOByteL @_ @f))]
<> [("utxoCostPerByte", hkdMap px (toJSON @CoinPerByte) (pp ^. hkdCoinsPerUTxOByteL @_ @f))]

upgradeBabbagePParams ::
forall f c.
Expand Down
1 change: 1 addition & 0 deletions eras/conway/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* Add `TreeMaybe`, `toGovRelationTree` and `toGovRelationTreeEither`
* Remove `proposalsAreConsistent`
* Remove `registerDelegs` and `registerInitialDReps`
* Modify `PParams` JSON instances to match `cardano-api`

### `testlib`

Expand Down
32 changes: 16 additions & 16 deletions eras/conway/impl/src/Cardano/Ledger/Conway/PParams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -840,28 +840,28 @@ conwayPParamsPairs pp =

instance Era era => FromJSON (ConwayPParams Identity era) where
parseJSON =
withObject "PParams" $ \obj ->
withObject "ProtocolParameters" $ \obj ->
ConwayPParams
<$> obj .: "minFeeA"
<*> obj .: "minFeeB"
<$> obj .: "txFeePerByte"
<*> obj .: "txFeeFixed"
<*> obj .: "maxBlockBodySize"
<*> obj .: "maxTxSize"
<*> obj .: "maxBlockHeaderSize"
<*> obj .: "keyDeposit"
<*> obj .: "poolDeposit"
<*> obj .: "eMax"
<*> obj .: "nOpt"
<*> obj .: "a0"
<*> obj .: "rho"
<*> obj .: "tau"
<*> obj .: "stakeAddressDeposit"
<*> obj .: "stakePoolDeposit"
<*> obj .: "poolRetireMaxEpoch"
<*> obj .: "stakePoolTargetNum"
<*> obj .: "poolPledgeInfluence"
<*> obj .: "monetaryExpansion"
<*> obj .: "treasuryCut"
<*> obj .: "protocolVersion"
<*> obj .: "minPoolCost" .!= mempty
<*> obj .: "coinsPerUTxOByte"
<*> obj .: "costmdls"
<*> obj .: "prices"
<*> obj .: "maxTxExUnits"
<*> obj .: "maxBlockExUnits"
<*> obj .: "maxValSize"
<*> obj .: "utxoCostPerByte"
<*> obj .: "costModels"
<*> obj .: "executionUnitPrices"
<*> obj .: "maxTxExecutionUnits"
<*> obj .: "maxBlockExecutionUnits"
<*> obj .: "maxValueSize"
<*> obj .: "collateralPercentage"
<*> obj .: "maxCollateralInputs"
<*> obj .: "poolVotingThresholds"
Expand Down
2 changes: 2 additions & 0 deletions eras/shelley/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* Deprecate `RewardAcnt` in favor of `RewardAccount`
* Remove `registerInitialFunds` and `registerInitialStaking`
* Add `registerInitialFundsThenStaking`
* Modify `PParams` JSON instances to match `cardano-api`
* Add wrapper to `PParams` in `ShelleyGenesis` to preserve the legacy behaviour of JSON instances

### `testlib`

Expand Down
110 changes: 106 additions & 4 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import Cardano.Ledger.BaseTypes (
EpochSize (..),
Globals (..),
Network,
Nonce (..),
PositiveUnitInterval,
Version,
mkActiveSlotCoeff,
Expand Down Expand Up @@ -79,15 +80,17 @@ import Cardano.Ledger.Keys
import Cardano.Ledger.PoolParams (PoolParams (..))
import Cardano.Ledger.SafeHash (unsafeMakeSafeHash)
import Cardano.Ledger.Shelley.Era (ShelleyEra)
import Cardano.Ledger.Shelley.PParams ()
import Cardano.Ledger.Shelley.PParams (ShelleyPParams (..))
import Cardano.Ledger.Shelley.StabilityWindow
import Cardano.Ledger.TxIn (TxId (..), TxIn (..))
import Cardano.Ledger.UTxO (UTxO (UTxO))
import qualified Cardano.Ledger.Val as Val
import Cardano.Slotting.EpochInfo (EpochInfo)
import Cardano.Slotting.Time (SystemStart (SystemStart))
import Data.Aeson (FromJSON (..), ToJSON (..), (.!=), (.:), (.:?), (.=))
import Control.Monad.Identity (Identity)
import Data.Aeson (FromJSON (..), ToJSON (..), object, (.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (Parser, Value (..), typeMismatch)
import Data.Fixed (Fixed (..), Micro, Pico)
import qualified Data.ListMap as LM
import Data.Map.Strict (Map)
Expand Down Expand Up @@ -247,6 +250,105 @@ instance Crypto c => ToJSON (ShelleyGenesis c) where
toJSON = Aeson.object . toShelleyGenesisPairs
toEncoding = Aeson.pairs . mconcat . toShelleyGenesisPairs

--------------------------------------------------
-- Legacy JSON representation of ShelleyGenesis --
--------------------------------------------------
newtype LegacyJSONPParams c = LegacyJSONPParams (PParamsHKD Identity (ShelleyEra c))

legacyFromJSONPParams :: LegacyJSONPParams c -> PParams (ShelleyEra c)
legacyFromJSONPParams (LegacyJSONPParams x) = PParams x

instance FromJSON (LegacyJSONPParams c) where
parseJSON =
Aeson.withObject "ShelleyPParams" $ \obj -> do
LegacyJSONPParams
<$> ( ShelleyPParams
<$> obj .: "minFeeA"
<*> obj .: "minFeeB"
<*> obj .: "maxBlockBodySize"
<*> obj .: "maxTxSize"
<*> obj .: "maxBlockHeaderSize"
<*> obj .: "keyDeposit"
<*> obj .: "poolDeposit"
<*> obj .: "eMax"
<*> obj .: "nOpt"
<*> obj .: "a0"
<*> obj .: "rho"
<*> obj .: "tau"
<*> obj .: "decentralisationParam"
<*> (parseNonce =<< (obj .: "extraEntropy"))
<*> obj .: "protocolVersion"
<*> obj .:? "minUTxOValue" .!= mempty
<*> obj .:? "minPoolCost" .!= mempty
)
where
parseNonce :: Aeson.Value -> Parser Nonce
parseNonce =
Aeson.withObject
"Nonce"
( \obj -> do
tag <- (obj .: "tag" :: Parser Text)
case tag of
"Nonce" -> Nonce <$> obj .: "contents"
"NeutralNonce" -> return NeutralNonce
_ -> typeMismatch "Nonce" (Object obj)
)

legacyToJSONPParams :: PParams (ShelleyEra c) -> LegacyJSONPParams c
legacyToJSONPParams (PParams x) = LegacyJSONPParams x

instance ToJSON (LegacyJSONPParams c) where
toJSON
( LegacyJSONPParams
( ShelleyPParams
{ sppMinFeeA
, sppMinFeeB
, sppMaxBBSize
, sppMaxTxSize
, sppMaxBHSize
, sppKeyDeposit
, sppPoolDeposit
, sppEMax
, sppNOpt
, sppA0
, sppRho
, sppTau
, sppD
, sppExtraEntropy
, sppProtocolVersion
, sppMinUTxOValue
, sppMinPoolCost
}
)
) =
Aeson.object
[ "minFeeA" .= sppMinFeeA
, "minFeeB" .= sppMinFeeB
, "maxBlockBodySize" .= sppMaxBBSize
, "maxTxSize" .= sppMaxTxSize
, "maxBlockHeaderSize" .= sppMaxBHSize
, "keyDeposit" .= sppKeyDeposit
, "poolDeposit" .= sppPoolDeposit
, "eMax" .= sppEMax
, "nOpt" .= sppNOpt
, "a0" .= sppA0
, "rho" .= sppRho
, "tau" .= sppTau
, "decentralisationParam" .= sppD
, "extraEntropy"
.= object
( case sppExtraEntropy of
Nonce hash ->
[ "tag" .= ("Nonce" :: Text)
, "contents" .= hash
]
NeutralNonce -> ["tag" .= ("NeutralNonce" :: Text)]
)
, "protocolVersion" .= sppProtocolVersion
, "minUTxOValue" .= sppMinUTxOValue
, "minPoolCost" .= sppMinPoolCost
]

toShelleyGenesisPairs :: (Aeson.KeyValue e a, Crypto c) => ShelleyGenesis c -> [a]
toShelleyGenesisPairs
ShelleyGenesis
Expand Down Expand Up @@ -279,7 +381,7 @@ toShelleyGenesisPairs
, "slotLength" .= sgSlotLength
, "updateQuorum" .= sgUpdateQuorum
, "maxLovelaceSupply" .= sgMaxLovelaceSupply
, "protocolParams" .= sgProtocolParams
, "protocolParams" .= legacyToJSONPParams sgProtocolParams
, "genDelegs" .= sgGenDelegs
, "initialFunds" .= strictSgInitialFunds
, "staking" .= strictSgStaking
Expand All @@ -300,7 +402,7 @@ instance Crypto c => FromJSON (ShelleyGenesis c) where
<*> obj .: "slotLength"
<*> obj .: "updateQuorum"
<*> obj .: "maxLovelaceSupply"
<*> obj .: "protocolParams"
<*> (legacyFromJSONPParams <$> obj .: "protocolParams")
<*> (forceElemsToWHNF <$> obj .: "genDelegs")
<*> (forceElemsToWHNF <$> obj .: "initialFunds") -- TODO: disable. Move to EraTransition
<*> obj .:? "staking" .!= emptyGenesisStaking -- TODO: remove. Move to EraTransition
Expand Down
Loading

0 comments on commit 17c101b

Please sign in to comment.