Skip to content

Commit

Permalink
Rename utxoCostPerWord to utxoCostPerByte and adjust the value approp…
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jul 6, 2022
1 parent 0cd6878 commit d1eb9ea
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 54 deletions.
2 changes: 1 addition & 1 deletion cardano-api/gen/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ genProtocolParametersUpdate = do
protocolUpdatePoolPledgeInfluence <- Gen.maybe genRationalInt64
protocolUpdateMonetaryExpansion <- Gen.maybe genRational
protocolUpdateTreasuryCut <- Gen.maybe genRational
protocolUpdateUTxOCostPerWord <- Gen.maybe genLovelace
protocolUpdateUTxOCostPerByte <- Gen.maybe genLovelace
let protocolUpdateCostModels = mempty -- genCostModels
--TODO: Babbage figure out how to deal with
-- asymmetric cost model JSON instances
Expand Down
12 changes: 6 additions & 6 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ data TxBodyErrorAutoBalance =

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

-- | The transaction validity interval is too far into the future.
-- See 'TransactionValidityIntervalError' for details.
Expand Down Expand Up @@ -856,8 +856,8 @@ instance Error TxBodyErrorAutoBalance where
displayError TxBodyErrorMissingParamMinUTxO =
"The minUTxOValue protocol parameter is required but missing"

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 @@ -1259,13 +1259,13 @@ calculateMinimumUTxO era txout@(TxOut _ v _ _) pparams' =

data MinimumUTxOError =
PParamsMinUTxOMissing
| PParamsUTxOCostPerWordMissing
| PParamsUTxOCostPerByteMissing
deriving Show

instance Error MinimumUTxOError where
displayError PParamsMinUTxOMissing =
"\"minUtxoValue\" field not present in protocol parameters when \
\trying to calculate minimum UTxO value."
displayError PParamsUTxOCostPerWordMissing =
"\"utxoCostPerWord\" field not present in protocol parameters when \
displayError PParamsUTxOCostPerByteMissing =
"\"utxoCostPerByte\" field not present in protocol parameters when \
\trying to calculate minimum UTxO value."
85 changes: 47 additions & 38 deletions cardano-api/src/Cardano/Api/ProtocolParameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ data ProtocolParameters =
--
protocolParamTreasuryCut :: Rational,

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

-- | Cost models for script languages that use them.
--
Expand Down Expand Up @@ -312,7 +312,7 @@ instance FromJSON ProtocolParameters where
<*> o .: "poolPledgeInfluence"
<*> o .: "monetaryExpansion"
<*> o .: "treasuryCut"
<*> o .:? "utxoCostPerWord"
<*> o .:? "utxoCostPerBytes"
<*> o .:? "costModels" .!= Map.empty
<*> o .:? "executionUnitPrices"
<*> o .:? "maxTxExecutionUnits"
Expand Down Expand Up @@ -343,14 +343,15 @@ instance ToJSON ProtocolParameters where
, "txFeeFixed" .= protocolParamTxFeeFixed
, "txFeePerByte" .= protocolParamTxFeePerByte
-- Alonzo era:
, "utxoCostPerWord" .= protocolParamUTxOCostPerWord
, "costModels" .= protocolParamCostModels
, "executionUnitPrices" .= protocolParamPrices
, "maxTxExecutionUnits" .= protocolParamMaxTxExUnits
, "maxBlockExecutionUnits" .= protocolParamMaxBlockExUnits
, "maxValueSize" .= protocolParamMaxValueSize
, "collateralPercentage" .= protocolParamCollateralPercent
, "maxCollateralInputs" .= protocolParamMaxCollateralInputs
-- Babbage era:
, "utxoCostPerByte" .= protocolParamUTxOCostPerByte
]


Expand Down Expand Up @@ -473,12 +474,14 @@ data ProtocolParametersUpdate =
--
protocolUpdateTreasuryCut :: Maybe Rational,

-- Introduced in Alonzo
-- Introduced in Babbage,

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

-- Introduced in Alonzo,

-- | Cost models for script languages that use them.
--
Expand Down Expand Up @@ -538,8 +541,9 @@ instance Semigroup ProtocolParametersUpdate where
, protocolUpdatePoolPledgeInfluence = merge protocolUpdatePoolPledgeInfluence
, protocolUpdateMonetaryExpansion = merge protocolUpdateMonetaryExpansion
, protocolUpdateTreasuryCut = merge protocolUpdateTreasuryCut
-- Introduced in Babbage below.
, protocolUpdateUTxOCostPerByte = merge protocolUpdateUTxOCostPerByte
-- Introduced in Alonzo below.
, protocolUpdateUTxOCostPerWord = merge protocolUpdateUTxOCostPerWord
, protocolUpdateCostModels = mergeMap protocolUpdateCostModels
, protocolUpdatePrices = merge protocolUpdatePrices
, protocolUpdateMaxTxExUnits = merge protocolUpdateMaxTxExUnits
Expand Down Expand Up @@ -577,7 +581,7 @@ instance Monoid ProtocolParametersUpdate where
, protocolUpdatePoolPledgeInfluence = Nothing
, protocolUpdateMonetaryExpansion = Nothing
, protocolUpdateTreasuryCut = Nothing
, protocolUpdateUTxOCostPerWord = Nothing
, protocolUpdateUTxOCostPerByte = Nothing
, protocolUpdateCostModels = mempty
, protocolUpdatePrices = Nothing
, protocolUpdateMaxTxExUnits = Nothing
Expand Down Expand Up @@ -607,7 +611,7 @@ instance ToCBOR ProtocolParametersUpdate where
<> toCBOR protocolUpdatePoolPledgeInfluence
<> toCBOR protocolUpdateMonetaryExpansion
<> toCBOR protocolUpdateTreasuryCut
<> toCBOR protocolUpdateUTxOCostPerWord
<> toCBOR protocolUpdateUTxOCostPerByte
<> toCBOR protocolUpdateCostModels
<> toCBOR protocolUpdatePrices
<> toCBOR protocolUpdateMaxTxExUnits
Expand Down Expand Up @@ -962,7 +966,7 @@ toAlonzoPParamsUpdate
, protocolUpdatePoolPledgeInfluence
, protocolUpdateMonetaryExpansion
, protocolUpdateTreasuryCut
, protocolUpdateUTxOCostPerWord
, protocolUpdateUTxOCostPerByte -- New in babbage
, protocolUpdateCostModels
, protocolUpdatePrices
, protocolUpdateMaxTxExUnits
Expand Down Expand Up @@ -997,8 +1001,8 @@ toAlonzoPParamsUpdate
noInlineMaybeToStrictMaybe protocolUpdateProtocolVersion
, Alonzo._minPoolCost = toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateMinPoolCost
, Alonzo._coinsPerUTxOWord = toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateUTxOCostPerWord
, Alonzo._coinsPerUTxOWord = toShelleyLovelace . (* 8) <$>
noInlineMaybeToStrictMaybe protocolUpdateUTxOCostPerByte
, Alonzo._costmdls = if Map.null protocolUpdateCostModels
then Ledger.SNothing
else either (const Ledger.SNothing) Ledger.SJust
Expand Down Expand Up @@ -1033,7 +1037,7 @@ toBabbagePParamsUpdate
, protocolUpdatePoolPledgeInfluence
, protocolUpdateMonetaryExpansion
, protocolUpdateTreasuryCut
, protocolUpdateUTxOCostPerWord
, protocolUpdateUTxOCostPerByte -- Babbage
, protocolUpdateCostModels
, protocolUpdatePrices
, protocolUpdateMaxTxExUnits
Expand Down Expand Up @@ -1064,8 +1068,8 @@ toBabbagePParamsUpdate
noInlineMaybeToStrictMaybe protocolUpdateProtocolVersion
, Babbage._minPoolCost = toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateMinPoolCost
, Babbage._coinsPerUTxOByte = coinsPerUTxOWordToCoinsPerUTxOByte . toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateUTxOCostPerWord
, Babbage._coinsPerUTxOByte = toShelleyLovelace <$>
noInlineMaybeToStrictMaybe protocolUpdateUTxOCostPerByte
, Babbage._costmdls = if Map.null protocolUpdateCostModels
then Ledger.SNothing
else either (const Ledger.SNothing) Ledger.SJust
Expand Down Expand Up @@ -1167,7 +1171,7 @@ fromShelleyPParamsUpdate
strictMaybeToMaybe _rho
, protocolUpdateTreasuryCut = Ledger.unboundRational <$>
strictMaybeToMaybe _tau
, protocolUpdateUTxOCostPerWord = Nothing
, protocolUpdateUTxOCostPerByte = Nothing -- Babbage
, protocolUpdateCostModels = mempty
, protocolUpdatePrices = Nothing
, protocolUpdateMaxTxExUnits = Nothing
Expand Down Expand Up @@ -1233,7 +1237,7 @@ fromAlonzoPParamsUpdate
strictMaybeToMaybe _rho
, protocolUpdateTreasuryCut = Ledger.unboundRational <$>
strictMaybeToMaybe _tau
, protocolUpdateUTxOCostPerWord = fromShelleyLovelace <$>
, protocolUpdateUTxOCostPerByte = fromShelleyLovelace . coinsPerUTxOWordToCoinsPerUTxOByte <$>
strictMaybeToMaybe _coinsPerUTxOWord
, protocolUpdateCostModels = maybe mempty fromAlonzoCostModels
(strictMaybeToMaybe _costmdls)
Expand Down Expand Up @@ -1301,7 +1305,7 @@ fromBabbagePParamsUpdate
strictMaybeToMaybe _rho
, protocolUpdateTreasuryCut = Ledger.unboundRational <$>
strictMaybeToMaybe _tau
, protocolUpdateUTxOCostPerWord = (*8) . fromShelleyLovelace <$>
, protocolUpdateUTxOCostPerByte = fromShelleyLovelace <$>
strictMaybeToMaybe _coinsPerUTxOByte
, protocolUpdateCostModels = maybe mempty fromAlonzoCostModels
(strictMaybeToMaybe _costmdls)
Expand Down Expand Up @@ -1413,7 +1417,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 +1464,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 +1479,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 +1511,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 +1545,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 +1560,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 @@ -1627,7 +1631,7 @@ fromShelleyPParams
, protocolParamPoolPledgeInfluence = Ledger.unboundRational _a0
, protocolParamMonetaryExpansion = Ledger.unboundRational _rho
, protocolParamTreasuryCut = Ledger.unboundRational _tau
, protocolParamUTxOCostPerWord = Nothing
, protocolParamUTxOCostPerByte = Nothing
, protocolParamCostModels = Map.empty
, protocolParamPrices = Nothing
, protocolParamMaxTxExUnits = Nothing
Expand Down Expand Up @@ -1685,7 +1689,8 @@ fromAlonzoPParams
, protocolParamPoolPledgeInfluence = Ledger.unboundRational _a0
, protocolParamMonetaryExpansion = Ledger.unboundRational _rho
, protocolParamTreasuryCut = Ledger.unboundRational _tau
, protocolParamUTxOCostPerWord = Just (fromShelleyLovelace _coinsPerUTxOWord)
, protocolParamUTxOCostPerByte = -- adapted from alonzo protocol parameter field
Just (fromShelleyLovelace (coinsPerUTxOWordToCoinsPerUTxOByte _coinsPerUTxOWord))
, protocolParamCostModels = fromAlonzoCostModels _costmdls
, protocolParamPrices = Just (fromAlonzoPrices _prices)
, protocolParamMaxTxExUnits = Just (fromAlonzoExUnits _maxTxExUnits)
Expand Down Expand Up @@ -1740,7 +1745,7 @@ fromBabbagePParams
, protocolParamPoolPledgeInfluence = Ledger.unboundRational _a0
, protocolParamMonetaryExpansion = Ledger.unboundRational _rho
, protocolParamTreasuryCut = Ledger.unboundRational _tau
, protocolParamUTxOCostPerWord = Just (8 * fromShelleyLovelace _coinsPerUTxOByte)
, protocolParamUTxOCostPerByte = Just (fromShelleyLovelace _coinsPerUTxOByte)
, protocolParamCostModels = fromAlonzoCostModels _costmdls
, protocolParamPrices = Just (fromAlonzoPrices _prices)
, protocolParamMaxTxExUnits = Just (fromAlonzoExUnits _maxTxExUnits)
Expand All @@ -1762,7 +1767,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 +1787,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 +1798,7 @@ checkProtocolParameters sbe ProtocolParameters{..} =

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

babbageRequiredPParamFields :: [Bool]
babbageRequiredPParamFields =
[ costPerByte
]

checkAlonzoParams :: Either ProtocolParametersError ()
checkAlonzoParams = do
if all (== True) alonzoRequiredPParamFields
Expand All @@ -1816,7 +1825,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
3 changes: 1 addition & 2 deletions cardano-api/src/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseUsing
import Cardano.Api.Utils (failEitherWith)


-- ----------------------------------------------------------------------------
-- Lovelace
--

newtype Lovelace = Lovelace Integer
deriving stock (Eq, Ord, Show)
deriving newtype (Enum, Num, ToJSON, FromJSON, ToCBOR, FromCBOR)
deriving newtype (Enum, Real, Integral, Num, ToJSON, FromJSON, ToCBOR, FromCBOR)

instance Semigroup Lovelace where
Lovelace a <> Lovelace b = Lovelace (a + b)
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ library
, cardano-crypto-wrapper
, cardano-data
, cardano-ledger-alonzo
-- , cardano-ledger-babbage
, cardano-ledger-byron
, cardano-ledger-core
, cardano-ledger-shelley-ma
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/Run/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ friendlyProtocolParametersUpdate
, protocolUpdatePoolPledgeInfluence
, protocolUpdateMonetaryExpansion
, protocolUpdateTreasuryCut
, protocolUpdateUTxOCostPerWord
, protocolUpdateUTxOCostPerByte
, protocolUpdateCollateralPercent
, protocolUpdateMaxBlockExUnits
, protocolUpdateMaxCollateralInputs
Expand Down Expand Up @@ -261,7 +261,7 @@ friendlyProtocolParametersUpdate
, protocolUpdateMonetaryExpansion <&>
("monetary expansion" .=) . friendlyRational
, protocolUpdateTreasuryCut <&> ("treasury expansion" .=) . friendlyRational
, protocolUpdateUTxOCostPerWord <&>
, protocolUpdateUTxOCostPerByte <&>
("UTxO storage cost per unit" .=) . friendlyLovelace
, protocolUpdateCollateralPercent <&>
("collateral inputs share" .=) . (<> "%") . textShow
Expand Down
Loading

0 comments on commit d1eb9ea

Please sign in to comment.