Skip to content

Commit

Permalink
Add utxoCostPerByte parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jul 7, 2022
1 parent 0cd6878 commit 75bb759
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-ledger
tag: 0913292b13963ae4b60136eddb8d18b137f96a21
--sha256: 19rrnvvplvg8v989bcv6vpjwvblfa0m65izxkcp8dclf0a914qq3
tag: 65292694de72f137e6b90c5f361ae7646b48775f
--sha256: 05m1c7v8a2797675gkagpzl6bcjnj7w6lnx5x7hf90847ap88b05
subdir:
eras/alonzo/impl
eras/alonzo/test-suite
Expand Down
2 changes: 2 additions & 0 deletions cardano-api/gen/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ genProtocolParameters =
<*> Gen.maybe genNat
<*> Gen.maybe genNat
<*> Gen.maybe genNat
<*> Gen.maybe genLovelace

genProtocolParametersUpdate :: Gen ProtocolParametersUpdate
genProtocolParametersUpdate = do
Expand Down Expand Up @@ -795,6 +796,7 @@ genProtocolParametersUpdate = do
protocolUpdateMaxValueSize <- Gen.maybe genNat
protocolUpdateCollateralPercent <- Gen.maybe genNat
protocolUpdateMaxCollateralInputs <- Gen.maybe genNat
protocolUpdateUTxOCostPerByte <- Gen.maybe genLovelace
pure ProtocolParametersUpdate{..}


Expand Down
11 changes: 11 additions & 0 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,10 @@ data TxBodyErrorAutoBalance =
-- word parameter, for eras that use this parameter.
| TxBodyErrorMissingParamCostPerWord

-- | The 'ProtocolParameters' must provide the value for the cost per
-- byte parameter, for eras that use this parameter.
| TxBodyErrorMissingParamCostPerByte

-- | The transaction validity interval is too far into the future.
-- See 'TransactionValidityIntervalError' for details.
| TxBodyErrorValidityInterval TransactionValidityError
Expand Down Expand Up @@ -859,6 +863,9 @@ instance Error TxBodyErrorAutoBalance where
displayError TxBodyErrorMissingParamCostPerWord =
"The utxoCostPerWord protocol parameter is required but missing"

displayError TxBodyErrorMissingParamCostPerByte =
"The utxoCostPerByte protocol parameter is required but missing"

displayError (TxBodyErrorValidityInterval err) =
displayError err

Expand Down Expand Up @@ -1260,6 +1267,7 @@ calculateMinimumUTxO era txout@(TxOut _ v _ _) pparams' =
data MinimumUTxOError =
PParamsMinUTxOMissing
| PParamsUTxOCostPerWordMissing
| PParamsUTxOCostPerByteMissing
deriving Show

instance Error MinimumUTxOError where
Expand All @@ -1269,3 +1277,6 @@ instance Error MinimumUTxOError where
displayError PParamsUTxOCostPerWordMissing =
"\"utxoCostPerWord\" field not present in protocol parameters when \
\trying to calculate minimum UTxO value."
displayError PParamsUTxOCostPerByteMissing =
"\"utxoCostPerByte\" field not present in protocol parameters when \
\trying to calculate minimum UTxO value."
85 changes: 58 additions & 27 deletions cardano-api/src/Cardano/Api/ProtocolParameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo

import qualified Cardano.Ledger.Babbage.PParams as Babbage
import Cardano.Ledger.Babbage.Translation (coinsPerUTxOWordToCoinsPerUTxOByte)

import Text.PrettyBy.Default (display)

Expand Down Expand Up @@ -286,7 +285,13 @@ data ProtocolParameters =
-- | The maximum number of collateral inputs allowed in a transaction.
--
-- /Introduced in Alonzo/
protocolParamMaxCollateralInputs :: Maybe Natural
protocolParamMaxCollateralInputs :: Maybe Natural,

-- | Cost in ada per byte of UTxO storage.
--
-- /Introduced in Babbage/
protocolParamUTxOCostPerByte :: Maybe Lovelace

}
deriving (Eq, Generic, Show)

Expand Down Expand Up @@ -320,6 +325,7 @@ instance FromJSON ProtocolParameters where
<*> o .:? "maxValueSize"
<*> o .:? "collateralPercentage"
<*> o .:? "maxCollateralInputs"
<*> o .:? "utxoCostPerByte"

instance ToJSON ProtocolParameters where
toJSON ProtocolParameters{..} =
Expand Down Expand Up @@ -351,6 +357,8 @@ instance ToJSON ProtocolParameters where
, "maxValueSize" .= protocolParamMaxValueSize
, "collateralPercentage" .= protocolParamCollateralPercent
, "maxCollateralInputs" .= protocolParamMaxCollateralInputs
-- Babbage era:
, "utxoCostPerByte" .= protocolParamUTxOCostPerByte
]


Expand Down Expand Up @@ -473,13 +481,15 @@ data ProtocolParametersUpdate =
--
protocolUpdateTreasuryCut :: Maybe Rational,

-- Introduced in Alonzo
-- Introduced in Alonzo,

-- | Cost in ada per word of UTxO storage.
--
-- /Introduced in Alonzo/
-- /Introduced in Alonzo, obsoleted in Babbage by 'protocolUpdateUTxOCostPerByte'/
protocolUpdateUTxOCostPerWord :: Maybe Lovelace,

-- Introduced in Alonzo,

-- | Cost models for script languages that use them.
--
-- /Introduced in Alonzo/
Expand Down Expand Up @@ -514,7 +524,12 @@ data ProtocolParametersUpdate =
-- | The maximum number of collateral inputs allowed in a transaction.
--
-- /Introduced in Alonzo/
protocolUpdateMaxCollateralInputs :: Maybe Natural
protocolUpdateMaxCollateralInputs :: Maybe Natural,

-- | Cost in ada per byte of UTxO storage.
--
-- /Introduced in Babbage. Supercedes 'protocolUpdateUTxOCostPerWord'/
protocolUpdateUTxOCostPerByte :: Maybe Lovelace
}
deriving (Eq, Show)

Expand Down Expand Up @@ -547,6 +562,8 @@ instance Semigroup ProtocolParametersUpdate where
, protocolUpdateMaxValueSize = merge protocolUpdateMaxValueSize
, protocolUpdateCollateralPercent = merge protocolUpdateCollateralPercent
, protocolUpdateMaxCollateralInputs = merge protocolUpdateMaxCollateralInputs
-- Introduced in Babbage below.
, protocolUpdateUTxOCostPerByte = merge protocolUpdateUTxOCostPerByte
}
where
-- prefer the right hand side:
Expand Down Expand Up @@ -585,11 +602,12 @@ instance Monoid ProtocolParametersUpdate where
, protocolUpdateMaxValueSize = Nothing
, protocolUpdateCollateralPercent = Nothing
, protocolUpdateMaxCollateralInputs = Nothing
, protocolUpdateUTxOCostPerByte = Nothing
}

instance ToCBOR ProtocolParametersUpdate where
toCBOR ProtocolParametersUpdate{..} =
CBOR.encodeListLen 25
CBOR.encodeListLen 26
<> toCBOR protocolUpdateProtocolVersion
<> toCBOR protocolUpdateDecentralization
<> toCBOR protocolUpdateExtraPraosEntropy
Expand All @@ -615,10 +633,11 @@ instance ToCBOR ProtocolParametersUpdate where
<> toCBOR protocolUpdateMaxValueSize
<> toCBOR protocolUpdateCollateralPercent
<> toCBOR protocolUpdateMaxCollateralInputs
<> toCBOR protocolUpdateUTxOCostPerByte

instance FromCBOR ProtocolParametersUpdate where
fromCBOR = do
CBOR.enforceSize "ProtocolParametersUpdate" 25
CBOR.enforceSize "ProtocolParametersUpdate" 26
ProtocolParametersUpdate
<$> fromCBOR
<*> fromCBOR
Expand All @@ -645,6 +664,7 @@ instance FromCBOR ProtocolParametersUpdate where
<*> fromCBOR
<*> fromCBOR
<*> fromCBOR
<*> fromCBOR


-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -1033,14 +1053,14 @@ toBabbagePParamsUpdate
, protocolUpdatePoolPledgeInfluence
, protocolUpdateMonetaryExpansion
, protocolUpdateTreasuryCut
, protocolUpdateUTxOCostPerWord
, protocolUpdateCostModels
, protocolUpdatePrices
, protocolUpdateMaxTxExUnits
, protocolUpdateMaxBlockExUnits
, protocolUpdateMaxValueSize
, protocolUpdateCollateralPercent
, protocolUpdateMaxCollateralInputs
, protocolUpdateUTxOCostPerByte
} =
Babbage.PParams {
Babbage._minfeeA = noInlineMaybeToStrictMaybe protocolUpdateTxFeePerByte
Expand All @@ -1064,8 +1084,6 @@ toBabbagePParamsUpdate
noInlineMaybeToStrictMaybe protocolUpdateProtocolVersion
, Babbage._minPoolCost = toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateMinPoolCost
, Babbage._coinsPerUTxOByte = coinsPerUTxOWordToCoinsPerUTxOByte . toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateUTxOCostPerWord
, Babbage._costmdls = if Map.null protocolUpdateCostModels
then Ledger.SNothing
else either (const Ledger.SNothing) Ledger.SJust
Expand All @@ -1079,6 +1097,8 @@ toBabbagePParamsUpdate
, Babbage._maxValSize = noInlineMaybeToStrictMaybe protocolUpdateMaxValueSize
, Babbage._collateralPercentage = noInlineMaybeToStrictMaybe protocolUpdateCollateralPercent
, Babbage._maxCollateralInputs = noInlineMaybeToStrictMaybe protocolUpdateMaxCollateralInputs
, Babbage._coinsPerUTxOByte = toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateUTxOCostPerByte
}

-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -1175,6 +1195,8 @@ fromShelleyPParamsUpdate
, protocolUpdateMaxValueSize = Nothing
, protocolUpdateCollateralPercent = Nothing
, protocolUpdateMaxCollateralInputs = Nothing

, protocolUpdateUTxOCostPerByte = Nothing
}

fromAlonzoPParamsUpdate :: Alonzo.PParamsUpdate ledgerera
Expand Down Expand Up @@ -1246,6 +1268,7 @@ fromAlonzoPParamsUpdate
, protocolUpdateMaxValueSize = strictMaybeToMaybe _maxValSize
, protocolUpdateCollateralPercent = strictMaybeToMaybe _collateralPercentage
, protocolUpdateMaxCollateralInputs = strictMaybeToMaybe _maxCollateralInputs
, protocolUpdateUTxOCostPerByte = Nothing
}


Expand Down Expand Up @@ -1301,8 +1324,7 @@ fromBabbagePParamsUpdate
strictMaybeToMaybe _rho
, protocolUpdateTreasuryCut = Ledger.unboundRational <$>
strictMaybeToMaybe _tau
, protocolUpdateUTxOCostPerWord = (*8) . fromShelleyLovelace <$>
strictMaybeToMaybe _coinsPerUTxOByte
, protocolUpdateUTxOCostPerWord = Nothing
, protocolUpdateCostModels = maybe mempty fromAlonzoCostModels
(strictMaybeToMaybe _costmdls)
, protocolUpdatePrices = fromAlonzoPrices <$>
Expand All @@ -1314,6 +1336,8 @@ fromBabbagePParamsUpdate
, protocolUpdateMaxValueSize = strictMaybeToMaybe _maxValSize
, protocolUpdateCollateralPercent = strictMaybeToMaybe _collateralPercentage
, protocolUpdateMaxCollateralInputs = strictMaybeToMaybe _maxCollateralInputs
, protocolUpdateUTxOCostPerByte = fromShelleyLovelace <$>
strictMaybeToMaybe _coinsPerUTxOByte
}


Expand Down Expand Up @@ -1413,7 +1437,7 @@ toAlonzoPParams ProtocolParameters {
protocolParamPoolPledgeInfluence,
protocolParamMonetaryExpansion,
protocolParamTreasuryCut,
protocolParamUTxOCostPerWord = Just utxoCostPerWord,
protocolParamUTxOCostPerByte = Just utxoCostPerByte,
protocolParamCostModels,
protocolParamPrices = Just prices,
protocolParamMaxTxExUnits = Just maxTxExUnits,
Expand Down Expand Up @@ -1460,7 +1484,8 @@ toAlonzoPParams ProtocolParameters {
(Ledger.boundRational protocolParamTreasuryCut)

-- New params in Alonzo:
, Alonzo._coinsPerUTxOWord = toShelleyLovelace utxoCostPerWord
, Alonzo._coinsPerUTxOWord = -- adapted from babbage protocol parameter field
toShelleyLovelace (utxoCostPerByte * 8)
, Alonzo._costmdls = either
(\e -> error $ "toAlonzoPParams: invalid cost models, error: " <> e)
id
Expand All @@ -1474,8 +1499,8 @@ toAlonzoPParams ProtocolParameters {
, Alonzo._collateralPercentage = collateralPercentage
, Alonzo._maxCollateralInputs = maxCollateralInputs
}
toAlonzoPParams ProtocolParameters { protocolParamUTxOCostPerWord = Nothing } =
error "toAlonzoPParams: must specify protocolParamUTxOCostPerWord"
toAlonzoPParams ProtocolParameters { protocolParamUTxOCostPerByte = Nothing } =
error "toAlonzoPParams: must specify protocolParamUTxOCostPerBytes"
toAlonzoPParams ProtocolParameters { protocolParamPrices = Nothing } =
error "toAlonzoPParams: must specify protocolParamPrices"
toAlonzoPParams ProtocolParameters { protocolParamMaxTxExUnits = Nothing } =
Expand Down Expand Up @@ -1506,7 +1531,7 @@ toBabbagePParams ProtocolParameters {
protocolParamPoolPledgeInfluence,
protocolParamMonetaryExpansion,
protocolParamTreasuryCut,
protocolParamUTxOCostPerWord = Just utxoCostPerWord,
protocolParamUTxOCostPerByte = Just utxoCostPerByte,
protocolParamCostModels,
protocolParamPrices = Just prices,
protocolParamMaxTxExUnits = Just maxTxExUnits,
Expand Down Expand Up @@ -1540,8 +1565,7 @@ toBabbagePParams ProtocolParameters {
(Ledger.boundRational protocolParamTreasuryCut)

-- New params in Babbage.
, Babbage._coinsPerUTxOByte = coinsPerUTxOWordToCoinsPerUTxOByte
(toShelleyLovelace utxoCostPerWord)
, Babbage._coinsPerUTxOByte = toShelleyLovelace utxoCostPerByte

, Babbage._costmdls = either
(\e -> error $ "toAlonzoPParams: invalid cost models, error: " <> e)
Expand All @@ -1556,8 +1580,8 @@ toBabbagePParams ProtocolParameters {
, Babbage._collateralPercentage = collateralPercentage
, Babbage._maxCollateralInputs = maxCollateralInputs
}
toBabbagePParams ProtocolParameters { protocolParamUTxOCostPerWord = Nothing } =
error "toBabbagePParams: must specify protocolParamUTxOCostPerWord"
toBabbagePParams ProtocolParameters { protocolParamUTxOCostPerByte = Nothing } =
error "toBabbagePParams: must specify protocolParamUTxOCostPerByte"
toBabbagePParams ProtocolParameters { protocolParamPrices = Nothing } =
error "toBabbagePParams: must specify protocolParamPrices"
toBabbagePParams ProtocolParameters { protocolParamMaxTxExUnits = Nothing } =
Expand Down Expand Up @@ -1635,6 +1659,7 @@ fromShelleyPParams
, protocolParamMaxValueSize = Nothing
, protocolParamCollateralPercent = Nothing
, protocolParamMaxCollateralInputs = Nothing
, protocolParamUTxOCostPerByte = Nothing
}


Expand Down Expand Up @@ -1693,6 +1718,7 @@ fromAlonzoPParams
, protocolParamMaxValueSize = Just _maxValSize
, protocolParamCollateralPercent = Just _collateralPercentage
, protocolParamMaxCollateralInputs = Just _maxCollateralInputs
, protocolParamUTxOCostPerByte = Nothing
}

fromBabbagePParams :: Babbage.PParams ledgerera -> ProtocolParameters
Expand Down Expand Up @@ -1740,14 +1766,15 @@ fromBabbagePParams
, protocolParamPoolPledgeInfluence = Ledger.unboundRational _a0
, protocolParamMonetaryExpansion = Ledger.unboundRational _rho
, protocolParamTreasuryCut = Ledger.unboundRational _tau
, protocolParamUTxOCostPerWord = Just (8 * fromShelleyLovelace _coinsPerUTxOByte)
, protocolParamUTxOCostPerWord = Nothing
, protocolParamCostModels = fromAlonzoCostModels _costmdls
, protocolParamPrices = Just (fromAlonzoPrices _prices)
, protocolParamMaxTxExUnits = Just (fromAlonzoExUnits _maxTxExUnits)
, protocolParamMaxBlockExUnits = Just (fromAlonzoExUnits _maxBlockExUnits)
, protocolParamMaxValueSize = Just _maxValSize
, protocolParamCollateralPercent = Just _collateralPercentage
, protocolParamMaxCollateralInputs = Just _maxCollateralInputs
, protocolParamUTxOCostPerByte = Just (fromShelleyLovelace _coinsPerUTxOByte)
}

data ProtocolParametersError =
Expand All @@ -1762,7 +1789,7 @@ instance Error ProtocolParametersError where
\ parameters value?"
displayError PParamsErrorMissingAlonzoProtocolParameter =
"The Alonzo era protocol parameters in use is missing one or more of the \
\following fields: UTxOCostPerWord, CostModels, Prices, MaxTxExUnits, \
\following fields: UTxOCostPerByte, CostModels, Prices, MaxTxExUnits, \
\MaxBlockExUnits, MaxValueSize, CollateralPercent, MaxCollateralInputs. Did \
\you intend to use an Alonzo era protocol parameters value?"

Expand All @@ -1782,7 +1809,7 @@ checkProtocolParameters sbe ProtocolParameters{..} =
era :: CardanoEra era
era = shelleyBasedToCardanoEra sbe

costPerWord = isJust protocolParamUTxOCostPerWord
costPerByte = isJust protocolParamUTxOCostPerByte
cModel = not $ Map.null protocolParamCostModels
prices = isJust protocolParamPrices
maxTxUnits = isJust protocolParamMaxTxExUnits
Expand All @@ -1793,8 +1820,7 @@ checkProtocolParameters sbe ProtocolParameters{..} =

alonzoRequiredPParamFields :: [Bool]
alonzoRequiredPParamFields =
[ costPerWord
, cModel
[ cModel
, prices
, maxTxUnits
, maxBlockExUnits
Expand All @@ -1803,6 +1829,11 @@ checkProtocolParameters sbe ProtocolParameters{..} =
, maxCollateralInputs
]

babbageRequiredPParamFields :: [Bool]
babbageRequiredPParamFields =
[ costPerByte
]

checkAlonzoParams :: Either ProtocolParametersError ()
checkAlonzoParams = do
if all (== True) alonzoRequiredPParamFields
Expand All @@ -1816,7 +1847,7 @@ checkProtocolParameters sbe ProtocolParameters{..} =

checkBabbageParams :: Either ProtocolParametersError ()
checkBabbageParams =
if all (== True) $ alonzoRequiredPParamFields ++ babbageDeprecatedFields
if all (== True) $ alonzoRequiredPParamFields ++ babbageRequiredPParamFields ++ babbageDeprecatedFields
then return ()
else Left PParamsErrorMissingAlonzoProtocolParameter

Expand Down
Loading

0 comments on commit 75bb759

Please sign in to comment.