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 26176ec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
43 changes: 25 additions & 18 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 @@ -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 @@ -1413,7 +1414,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 +1461,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,7 +1476,7 @@ toAlonzoPParams ProtocolParameters {
, Alonzo._collateralPercentage = collateralPercentage
, Alonzo._maxCollateralInputs = maxCollateralInputs
}
toAlonzoPParams ProtocolParameters { protocolParamUTxOCostPerWord = Nothing } =
toAlonzoPParams ProtocolParameters { protocolParamUTxOCostPerByte = Nothing } =
error "toAlonzoPParams: must specify protocolParamUTxOCostPerWord"
toAlonzoPParams ProtocolParameters { protocolParamPrices = Nothing } =
error "toAlonzoPParams: must specify protocolParamPrices"
Expand Down Expand Up @@ -1506,7 +1508,7 @@ toBabbagePParams ProtocolParameters {
protocolParamPoolPledgeInfluence,
protocolParamMonetaryExpansion,
protocolParamTreasuryCut,
protocolParamUTxOCostPerWord = Just utxoCostPerWord,
protocolParamUTxOCostPerByte = Just utxoCostPerByte,
protocolParamCostModels,
protocolParamPrices = Just prices,
protocolParamMaxTxExUnits = Just maxTxExUnits,
Expand Down Expand Up @@ -1541,7 +1543,7 @@ toBabbagePParams ProtocolParameters {

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

, Babbage._costmdls = either
(\e -> error $ "toAlonzoPParams: invalid cost models, error: " <> e)
Expand All @@ -1556,8 +1558,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 +1629,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 +1687,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 _coinsPerUTxOWord `div` 8)
, protocolParamCostModels = fromAlonzoCostModels _costmdls
, protocolParamPrices = Just (fromAlonzoPrices _prices)
, protocolParamMaxTxExUnits = Just (fromAlonzoExUnits _maxTxExUnits)
Expand Down Expand Up @@ -1740,7 +1743,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 Down Expand Up @@ -1782,7 +1785,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 +1796,7 @@ checkProtocolParameters sbe ProtocolParameters{..} =

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

babbageRequiredPParamFields :: [Bool]
babbageRequiredPParamFields =
[ costPerByte
]

checkAlonzoParams :: Either ProtocolParametersError ()
checkAlonzoParams = do
if all (== True) alonzoRequiredPParamFields
Expand All @@ -1816,7 +1823,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

0 comments on commit 26176ec

Please sign in to comment.