Skip to content

Commit

Permalink
Update to latest revision and patch JSON in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Feb 29, 2024
1 parent 3d889c5 commit f9d86e1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

env:
# Modify this value to "invalidate" the cabal cache.
CABAL_CACHE_VERSION: "2024-02-28-golden"
CABAL_CACHE_VERSION: "2024-02-29-golden"

concurrency:
group: >
Expand Down
20 changes: 10 additions & 10 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ package cardano-api-gen
source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-ledger.git
tag: b9fc6d5b4837e7923e2d8bdefb66deae6522404a
tag: 3975dc36034238677351958224535a0a992529e0
subdir: eras/alonzo/impl
--sha256: sha256-dp7rZ4n0NCoWO2UoGEwVbtjS6R4FusKjBYyhJR1qEn4=
--sha256: sha256-PUZzjs9GvIe2EkV9iGb+Lo+CCVA24x4mQ6oGkEsDPOc=

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-ledger.git
tag: b9fc6d5b4837e7923e2d8bdefb66deae6522404a
tag: 3975dc36034238677351958224535a0a992529e0
subdir: eras/babbage/impl
--sha256: sha256-dp7rZ4n0NCoWO2UoGEwVbtjS6R4FusKjBYyhJR1qEn4=
--sha256: sha256-PUZzjs9GvIe2EkV9iGb+Lo+CCVA24x4mQ6oGkEsDPOc=

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-ledger.git
tag: b9fc6d5b4837e7923e2d8bdefb66deae6522404a
tag: 3975dc36034238677351958224535a0a992529e0
subdir: eras/shelley/impl
--sha256: sha256-dp7rZ4n0NCoWO2UoGEwVbtjS6R4FusKjBYyhJR1qEn4=
--sha256: sha256-PUZzjs9GvIe2EkV9iGb+Lo+CCVA24x4mQ6oGkEsDPOc=

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-ledger.git
tag: b9fc6d5b4837e7923e2d8bdefb66deae6522404a
tag: 3975dc36034238677351958224535a0a992529e0
subdir: eras/conway/impl
--sha256: sha256-dp7rZ4n0NCoWO2UoGEwVbtjS6R4FusKjBYyhJR1qEn4=
--sha256: sha256-PUZzjs9GvIe2EkV9iGb+Lo+CCVA24x4mQ6oGkEsDPOc=

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-ledger.git
tag: b9fc6d5b4837e7923e2d8bdefb66deae6522404a
tag: 3975dc36034238677351958224535a0a992529e0
subdir: libs/cardano-ledger-core
--sha256: sha256-dp7rZ4n0NCoWO2UoGEwVbtjS6R4FusKjBYyhJR1qEn4=
--sha256: sha256-PUZzjs9GvIe2EkV9iGb+Lo+CCVA24x4mQ6oGkEsDPOc=


package cryptonite
Expand Down
24 changes: 13 additions & 11 deletions cardano-api/test/cardano-api-golden/files/golden/ShelleyGenesis
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{
"activeSlotsCoeff": 0.259,
"protocolParams": {
"stakePoolDeposit": 0,
"poolDeposit": 0,
"protocolVersion": {
"minor": 0,
"major": 2
},
"minUTxOValue": 0,
"decentralization": 1.9e-2,
"decentralisationParam": 1.9e-2,
"maxTxSize": 2048,
"minPoolCost": 0,
"txFeePerByte": 0,
"minFeeA": 0,
"maxBlockBodySize": 65535,
"txFeeFixed": 0,
"poolRetireMaxEpoch": 0,
"extraPraosEntropy": null,
"minFeeB": 0,
"eMax": 0,
"extraEntropy": {
"tag": "NeutralNonce"
},
"maxBlockHeaderSize": 65535,
"stakeAddressDeposit": 0,
"stakePoolTargetNum": 100,
"monetaryExpansion": 0,
"treasuryCut": 0,
"poolPledgeInfluence": 0
"keyDeposit": 0,
"nOpt": 100,
"rho": 0,
"tau": 0,
"a0": 0
},
"genDelegs": {
"23d51e91ae5adc7ae801e9de4cd54175fb7464ec2680b25686bbb194": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
{-# LANGUAGE ScopedTypeVariables #-}
module Test.Cardano.Api.ProtocolParameters(tests) where

import Cardano.Api (CardanoEra (..), FromJSON, ProtocolParametersConversionError, ToJSON,
inEonForEra, prettyPrintJSON)
import Cardano.Api (CardanoEra (..), ProtocolParametersConversionError, inEonForEra,
prettyPrintJSON)
import Cardano.Api.Eon.ShelleyBasedEra (ShelleyBasedEra (..), ShelleyLedgerEra)
import Cardano.Api.Ledger (PParams (..))
import Cardano.Api.ProtocolParameters (LedgerProtocolParameters (..),
convertToLedgerProtocolParameters, fromLedgerPParams)

import Data.Aeson (eitherDecode)
import Data.Aeson (FromJSON, Object, ToJSON, decode, eitherDecode)
import qualified Data.Aeson.Key as Aeson
import qualified Data.Aeson.KeyMap as Aeson
import qualified Data.ByteString.Lazy as LBS
import Data.Foldable (foldl')

import Test.Gen.Cardano.Api.Typed (genProtocolParameters)

Expand Down Expand Up @@ -47,7 +50,8 @@ protocolParametersSerializeTheSame era =
property $ do ValidatedSerializedPair { serializedProtocolParams
, serializedPParams
} <- forAll $ genValidSerializedPair era
serializedPParams === serializedProtocolParams
patchedSerializedProtocolParams <- patchProtocolParamsJSONOrFail era serializedProtocolParams
serializedPParams === patchedSerializedProtocolParams

-- | Ensure that ProtocolParameter serialization can be deserialized by PParams FromJSON instance
protocolParametersAreCompatible :: forall era. ( ToJSON (PParams (ShelleyLedgerEra era))
Expand All @@ -70,9 +74,10 @@ ppParamsRoundtrip era =
property $ do ValidatedSerializedPair { serializedProtocolParams
, serializedPParams = _
} <- forAll $ genValidSerializedPair era
patchedSerializedProtocolParams <- patchProtocolParamsJSONOrFail era serializedProtocolParams
case eitherDecode serializedProtocolParams :: Either String (PParams (ShelleyLedgerEra era)) of
Left err -> fail err
Right pParams -> prettyPrintJSON pParams === LBS.toStrict serializedProtocolParams
Right pParams -> prettyPrintJSON pParams === LBS.toStrict patchedSerializedProtocolParams

-------------------------
-- Auxiliary generator --
Expand Down Expand Up @@ -102,3 +107,38 @@ genValidSerializedPair era = do

toShelleyBased :: CardanoEra era -> ShelleyBasedEra era
toShelleyBased = inEonForEra (error "Not a Shelley-based era") id


patchProtocolParamsJSONOrFail :: MonadFail m => CardanoEra era -> LBS.ByteString -> m LBS.ByteString
patchProtocolParamsJSONOrFail era b = maybe (fail "Cannot fix JSON") return $ patchProtocolParamsJSON b
where
patchProtocolParamsJSON :: LBS.ByteString -> Maybe LBS.ByteString
patchProtocolParamsJSON s = LBS.fromStrict . prettyPrintJSON <$> (patchProtocolParamRepresentation =<< decode s)
where
patchProtocolParamRepresentation :: Object -> Maybe Object
patchProtocolParamRepresentation o = do filters <- filtersForEra era
replace "committeeTermLength" "committeeMaxTermLength"
=<< replace "minCommitteeSize" "committeeMinSize"
(applyFilters filters o)

filtersForEra :: CardanoEra era -> Maybe [String]
filtersForEra ShelleyEra = Just [ "collateralPercentage", "costModels", "executionUnitPrices"
, "maxBlockExecutionUnits", "maxCollateralInputs", "maxTxExecutionUnits"
, "maxValueSize", "utxoCostPerByte" ]
filtersForEra AlonzoEra = Just [ "minUTxOValue" ]
filtersForEra BabbageEra = Just [ "decentralization", "extraPraosEntropy", "minUTxOValue" ]
filtersForEra _ = Nothing

applyFilters :: [String] -> Object -> Object
applyFilters filters o = foldl' (flip Aeson.delete) o (map Aeson.fromString filters)

replace :: String -> String -> Object -> Maybe Object
replace src dest o =
let srcKey = Aeson.fromString src
destKey = Aeson.fromString dest in
case Aeson.lookup srcKey o of
Nothing -> Just o
Just v -> if Aeson.member destKey o
then Nothing
else Just $ Aeson.insert destKey v $ Aeson.delete srcKey o

0 comments on commit f9d86e1

Please sign in to comment.