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 Apr 22, 2021
1 parent a593cec commit d46dc08
Show file tree
Hide file tree
Showing 14 changed files with 1,028 additions and 233 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: 0730828363ad6d0669b7a5a12635e22944b32880
--sha256: 099j8xcmhlfqz5p8qpxsxixzlb06zmxxl7yqd06dkr96gbjd2npz
subdir:
alonzo/impl
byron/chain/executable-spec
byron/crypto
byron/crypto/test
Expand All @@ -133,6 +134,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

source-repository-package
Expand Down Expand Up @@ -189,6 +191,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 @@ -200,3 +213,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 @@ -98,6 +98,7 @@ library
, cardano-crypto
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-ledger-alonzo
, cardano-ledger-byron
, cardano-ledger-shelley-ma
, cardano-prelude
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 @@ -283,7 +284,7 @@ module Cardano.Api (
SimpleScriptV2,
ScriptLanguage(..),
SimpleScriptVersion(..),
PlutusScriptVersion,
PlutusScriptVersion(..),
AnyScriptLanguage(..),
IsScriptLanguage(..),
IsSimpleScriptLanguage(..),
Expand Down Expand Up @@ -466,6 +467,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
72 changes: 37 additions & 35 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ module Cardano.Api.Fees (

import Prelude

import Numeric.Natural

import qualified Data.ByteString as BS
--import GHC.Records (HasField (..))
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
Expand All @@ -36,25 +38,31 @@ 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 = Shelley.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 +74,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 +84,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 +129,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 d46dc08

Please sign in to comment.