Skip to content

Commit

Permalink
Prepare Cardano.Api.ProtocolParameters for Alonzo era.
Browse files Browse the repository at this point in the history
In the Alonzo era we introduce 6 new protocol parameters and remove 1
protocol parameter. In anticipation of the Alonzo era, we make some
modifications to the ProtocolParameters type.

minUTxOValue becomes a Maybe (This parameter gets deprecated in Alonzo)

We introduce the following but note they are currently ignored:
- adaPerUTxoByte - Cost in ada per byte of UTxO storage (replaces
minUTxOValue)
- costmdls - The cost models for plutus scripts
- prices - The prices of execution units for plutus scripts
- maxTxExUnits - Max total script execution resource  units allowed per tx
- maxBlockExUnits - Max total script execution resource units allowed per block
- maxValSize - Max size of a Value in a tx output

We introduce an era parameter in the ProtocolParameters type in order to
reduce boilerplate. We have avoided the typical GADT pattern in
cardano-api to prevent an explosion of types.
  • Loading branch information
Jimbo4350 committed May 12, 2021
1 parent 695441f commit 1f4ba55
Show file tree
Hide file tree
Showing 14 changed files with 1,021 additions and 232 deletions.
17 changes: 17 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ source-repository-package
tag: e8f19bcc9c8f405131cb95ca6ada26b2b4eac638
--sha256: 1v36d3lyhmadzj0abdfsppjna7n7llzqzp9ikx5yq28l2kda2f1p
subdir:
alonzo/impl
byron/chain/executable-spec
byron/crypto
byron/crypto/test
Expand All @@ -134,6 +135,7 @@ source-repository-package
shelley/chain-and-ledger/dependencies/non-integer
shelley/chain-and-ledger/executable-spec
shelley/chain-and-ledger/shelley-spec-ledger-test
shelley-ma/shelley-ma-test
shelley-ma/impl
shelley-ma/shelley-ma-test

Expand Down Expand Up @@ -192,6 +194,17 @@ source-repository-package
typed-protocols
typed-protocols-examples

source-repository-package
type: git
location: https://github.com/input-output-hk/plutus
tag: ffc1768f5be7af66d8d745a31c9c6a77c5c55104
--sha256: 0qwj0xnvkrxsmfw883w3idfnhj073d5swcxl54zs7ay4fgwrm4c2
subdir:
plutus-core
plutus-ledger-api
plutus-tx
prettyprinter-configurable

constraints:
hedgehog >= 1.0
, bimap >= 0.4.0
Expand All @@ -203,3 +216,7 @@ constraints:

package comonad
flags: -test-doctests

allow-newer:
monoidal-containers:aeson,
size-based:template-haskell
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ library
, cardano-crypto
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-ledger-alonzo
, cardano-ledger-byron
, cardano-ledger-core
, cardano-ledger-shelley-ma
Expand Down
5 changes: 4 additions & 1 deletion cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Cardano.Api (

-- ** Shelley-based eras
ShelleyBasedEra(..),
anyCardanoEraShelleyBased,
IsShelleyBasedEra(..),
InAnyShelleyBasedEra(..),
CardanoEraStyle(..),
Expand Down Expand Up @@ -285,7 +286,7 @@ module Cardano.Api (
SimpleScriptV2,
ScriptLanguage(..),
SimpleScriptVersion(..),
PlutusScriptVersion,
PlutusScriptVersion(..),
AnyScriptLanguage(..),
IsScriptLanguage(..),
IsSimpleScriptLanguage(..),
Expand Down Expand Up @@ -488,6 +489,8 @@ module Cardano.Api (
-- * Protocol parameter updates
UpdateProposal(..),
ProtocolParametersUpdate(..),
ProtocolParametersError(..),
renderProtocolParamsErr,
makeShelleyUpdateProposal,
PraosNonce,
makePraosNonce,
Expand Down
5 changes: 5 additions & 0 deletions cardano-api/src/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Cardano.Api.Eras
, IsCardanoEra(..)
, AnyCardanoEra(..)
, anyCardanoEra
, anyCardanoEraShelleyBased
, InAnyCardanoEra(..)

-- * Deprecated aliases
Expand Down Expand Up @@ -197,6 +198,10 @@ anyCardanoEra ShelleyEra = AnyCardanoEra ShelleyEra
anyCardanoEra AllegraEra = AnyCardanoEra AllegraEra
anyCardanoEra MaryEra = AnyCardanoEra MaryEra

anyCardanoEraShelleyBased :: ShelleyBasedEra era -> AnyCardanoEra
anyCardanoEraShelleyBased ShelleyBasedEraShelley = AnyCardanoEra ShelleyEra
anyCardanoEraShelleyBased ShelleyBasedEraAllegra = AnyCardanoEra AllegraEra
anyCardanoEraShelleyBased ShelleyBasedEraMary = AnyCardanoEra MaryEra

-- | This pairs up some era-dependent type with a 'CardanoEra' value that tells
-- us what era it is, but hides the era type. This is useful when the era is
Expand Down
69 changes: 37 additions & 32 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import Numeric.Natural
import qualified Cardano.Binary as CBOR
import qualified Cardano.Chain.Common as Byron

--import qualified Cardano.Ledger.Core as Core
import qualified Shelley.Spec.Ledger.LedgerState as Shelley
import qualified Shelley.Spec.Ledger.Tx as Shelley

import Cardano.Api.Eras
import Cardano.Api.NetworkId
import Cardano.Api.Tx
Expand All @@ -36,25 +40,32 @@ import Cardano.Api.Value
-- This function is simple, but if you are doing input selection then you
-- probably want to consider estimateTransactionFee.
--
transactionFee :: forall era.
IsShelleyBasedEra era
=> Natural -- ^ The fixed tx fee
transactionFee :: ShelleyBasedEra era
-> Natural -- ^ The fixed tx fee
-> Natural -- ^ The tx fee per byte
-> Tx era
-> Lovelace
transactionFee txFeeFixed txFeePerByte (ShelleyTx _ tx) =
Lovelace (a * x + b)
transactionFee sbe txFeeFixed txFeePerByte tx =
getFee
where
getFee :: Lovelace
getFee =
case tx of
ShelleyTx _ tx' -> let x = getTxSize sbe tx'
in Lovelace (a * x + b)
ByronTx _ -> case sbe :: ShelleyBasedEra ByronEra of {}

getTxSize :: ShelleyBasedEra era -> Shelley.Tx (ShelleyLedgerEra era) -> Integer
getTxSize ShelleyBasedEraShelley = Shelley.txsize
getTxSize ShelleyBasedEraAllegra = Shelley.txsize
getTxSize ShelleyBasedEraMary = Shelley.txsize
-- TODO: Change Shelley.Tx to Tx type family
-- getTxSize ShelleyBasedEraAlonzo = getField @"txsize"

a = toInteger txFeePerByte
x = getField @"txsize" tx
b = toInteger txFeeFixed

--TODO: This can be made to work for Byron txs too. Do that: fill in this case
-- and remove the IsShelleyBasedEra constraint.
transactionFee _ _ (ByronTx _) =
case shelleyBasedEra :: ShelleyBasedEra era of {}


--TODO: in the Byron case the per-byte is non-integral, would need different
-- parameters. e.g. a new data type for fee params, Byron vs Shelley

Expand All @@ -66,9 +77,8 @@ transactionFee _ _ (ByronTx _) =
-- contain all the things not subject to coin selection (such as script inputs,
-- metadata, withdrawals, certs etc)
--
estimateTransactionFee :: forall era.
IsShelleyBasedEra era
=> NetworkId
estimateTransactionFee :: ShelleyBasedEra era
-> NetworkId
-> Natural -- ^ The fixed tx fee
-> Natural -- ^ The tx fee per byte
-> Tx era
Expand All @@ -77,19 +87,19 @@ estimateTransactionFee :: forall era.
-> Int -- ^ The number of extra Shelley key witnesses
-> Int -- ^ The number of extra Byron key witnesses
-> Lovelace
estimateTransactionFee nw txFeeFixed txFeePerByte (ShelleyTx era tx) =
let Lovelace baseFee = transactionFee txFeeFixed txFeePerByte (ShelleyTx era tx)
in \nInputs nOutputs nShelleyKeyWitnesses nByronKeyWitnesses ->

--TODO: this is fragile. Move something like this to the ledger and
-- make it robust, based on the txsize calculation.
let extraBytes :: Int
extraBytes = nInputs * sizeInput
+ nOutputs * sizeOutput
+ nByronKeyWitnesses * sizeByronKeyWitnesses
+ nShelleyKeyWitnesses * sizeShelleyKeyWitnesses

in Lovelace (baseFee + toInteger txFeePerByte * toInteger extraBytes)
estimateTransactionFee sbe nw txFeeFixed txFeePerByte (ShelleyTx era tx) =
let Lovelace baseFee = transactionFee sbe txFeeFixed txFeePerByte (ShelleyTx era tx)
in \nInputs nOutputs nShelleyKeyWitnesses nByronKeyWitnesses ->

--TODO: this is fragile. Move something like this to the ledger and
-- make it robust, based on the txsize calculation.
let extraBytes :: Int
extraBytes = nInputs * sizeInput
+ nOutputs * sizeOutput
+ nByronKeyWitnesses * sizeByronKeyWitnesses
+ nShelleyKeyWitnesses * sizeShelleyKeyWitnesses

in Lovelace (baseFee + toInteger txFeePerByte * toInteger extraBytes)
where
sizeInput = smallArray + uint + hashObj
sizeOutput = smallArray + uint + address
Expand Down Expand Up @@ -122,8 +132,3 @@ estimateTransactionFee nw txFeeFixed txFeePerByte (ShelleyTx era tx) =
Byron.aaNetworkMagic = toByronNetworkMagic nw
}

--TODO: This can be made to work for Byron txs too. Do that: fill in this case
-- and remove the IsShelleyBasedEra constraint.
estimateTransactionFee _ _ _ (ByronTx _) =
case shelleyBasedEra :: ShelleyBasedEra era of {}

Loading

0 comments on commit 1f4ba55

Please sign in to comment.