Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Cardano.Api.ProtocolParameters to ease integration with Alonzo #2634

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-ledger-specs
tag: e8f19bcc9c8f405131cb95ca6ada26b2b4eac638
--sha256: 1v36d3lyhmadzj0abdfsppjna7n7llzqzp9ikx5yq28l2kda2f1p
tag: 46577026a5f3972258d675b193e0afc929d3905a
--sha256: 1daapjqkdmgmh0k61amim8k94vfca4z0pzbm1iv1y9ycyk0jbbxa
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 @@ -176,8 +178,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/input-output-hk/ouroboros-network
tag: 8d44955afdebcf9cc521f60e3a9f31e853ace2c1
--sha256: 1vv40h3ljnyi67jy8mjj1bd97pjlb5lsc2praf92sdc132bqyzz7
tag: aad909cfba4e23c623121457f5ef84e5c41a9b62
--sha256: 0657a5wlszpkpql7yskw09gpwqwqvw3izx4an03l2yd608g169si
subdir:
io-sim
io-sim-classes
Expand All @@ -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: 1578ab92f9ac389c68aa915370bf38ad7b4a75e9
--sha256: 1fnpv2l7zs1in1hgdpb85zv8l37kjcclz0zjzqmll391gbdsb99f
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,
Copy link
Contributor

@newhoggy newhoggy Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may not need monoidal-containers:aeson anymore as of now.

See IntersectMBO/cardano-ledger#2245

size-based:template-haskell
3 changes: 3 additions & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ library
Cardano.Api.NetworkId
Cardano.Api.OperationalCertificate
Cardano.Api.ProtocolParameters
Cardano.Api.ProtocolParametersUpdate
Cardano.Api.Query
Cardano.Api.Script
Cardano.Api.SerialiseBech32
Expand Down Expand Up @@ -99,6 +100,7 @@ library
, cardano-crypto
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-ledger-alonzo
, cardano-ledger-byron
, cardano-ledger-core
, cardano-ledger-shelley-ma
Expand All @@ -122,6 +124,7 @@ library
, ouroboros-consensus-shelley
, ouroboros-network
, ouroboros-network-framework
, plutus-core
, scientific
, shelley-spec-ledger
, small-steps
Expand Down
8 changes: 6 additions & 2 deletions 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 Expand Up @@ -523,7 +526,8 @@ import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.OperationalCertificate
import Cardano.Api.ProtocolParameters
import Cardano.Api.Query (SlotsInEpoch(..), SlotsToEpochEnd(..), slotToEpoch)
import Cardano.Api.ProtocolParametersUpdate
import Cardano.Api.Query (SlotsInEpoch (..), SlotsToEpochEnd (..), slotToEpoch)
import Cardano.Api.Script
import Cardano.Api.SerialiseBech32
import Cardano.Api.SerialiseCBOR
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fromConsensusBlock CardanoMode =
Consensus.BlockMary b' ->
BlockInMode (ShelleyBlock ShelleyBasedEraMary b')
MaryEraInCardanoMode

Consensus.BlockAlonzo _ -> error "fromConsensusBlock: Alonzo not implemented yet"

-- ----------------------------------------------------------------------------
-- Block headers
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 -> CardanoEra era
anyCardanoEraShelleyBased ShelleyBasedEraShelley = ShelleyEra
anyCardanoEraShelleyBased ShelleyBasedEraAllegra = AllegraEra
anyCardanoEraShelleyBased ShelleyBasedEraMary = 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
63 changes: 30 additions & 33 deletions cardano-api/src/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Numeric.Natural
import qualified Cardano.Binary as CBOR
import qualified Cardano.Chain.Common as Byron


import Cardano.Api.Eras
import Cardano.Api.NetworkId
import Cardano.Api.Tx
Expand All @@ -36,24 +37,26 @@ 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
a = toInteger txFeePerByte
x = getField @"txsize" tx
b = toInteger txFeeFixed
getFee :: Lovelace
getFee =
case tx of
ShelleyTx _ tx' -> let x = getField @"txsize" tx'
in Lovelace (a * x + b)
--TODO: This can be made to work for Byron txs too. Do that: fill in this case
-- and remove the IsShelleyBasedEra constraint.
ByronTx _ -> case sbe :: ShelleyBasedEra ByronEra of {}

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

a = toInteger txFeePerByte
b = toInteger txFeeFixed

--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 +69,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 +79,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 +124,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 {}

5 changes: 5 additions & 0 deletions cardano-api/src/Cardano/Api/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,14 @@ mkProtocolInfoCardano (GenesisCardano dnc byronGenesis shelleyGenesis)
Consensus.ProtocolParamsMary
{ Consensus.maryProtVer = shelleyProtVer dnc
}
Consensus.ProtocolParamsAlonzo
{ Consensus.alonzoGenesis = error "mkProtocolInfoCardano: Alonzo era not implemented yet"
, Consensus.alonzoProtVer = error "mkProtocolInfoCardano: Alonzo era not implemented yet"
}
(ncByronToShelley dnc)
(ncShelleyToAllegra dnc)
(ncAllegraToMary dnc)
(error "mkProtocolInfoCardano: Alonzo era not implemented yet")

shelleyPraosNonce :: ShelleyConfig -> Shelley.Spec.Nonce
shelleyPraosNonce sCfg = Shelley.Spec.Nonce (Cardano.Crypto.Hash.Class.castHash . unGenesisHashShelley $ scGenesisHash sCfg)
Expand Down
5 changes: 4 additions & 1 deletion cardano-api/src/Cardano/Api/Modes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ import qualified Ouroboros.Consensus.Cardano.ByronHFC as Consensus (ByronBlockHF
import Ouroboros.Consensus.HardFork.Combinator as Consensus (EraIndex (..), eraIndexSucc,
eraIndexZero)
import Ouroboros.Consensus.Shelley.Eras (StandardAllegra, StandardMary, StandardShelley)
import qualified Ouroboros.Consensus.Shelley.ShelleyHFC as Consensus (ShelleyBlockHFC)
import qualified Ouroboros.Consensus.Shelley.Ledger as Consensus
import Ouroboros.Consensus.Shelley.Protocol (StandardCrypto)
import qualified Ouroboros.Consensus.Shelley.ShelleyHFC as Consensus (ShelleyBlockHFC)

import qualified Cardano.Chain.Slotting as Byron (EpochSlots (..))

Expand Down Expand Up @@ -284,4 +284,7 @@ fromConsensusEraIndex CardanoMode = fromShelleyEraIndex

fromShelleyEraIndex (Consensus.EraIndex (S (S (S (Z (K ())))))) =
AnyEraInMode MaryEraInCardanoMode
fromShelleyEraIndex (Consensus.EraIndex (S (S (S (S (Z (K ()))))))) =
error "fromShelleyEraIndex: Alonzo not implemented yet"


9 changes: 7 additions & 2 deletions cardano-api/src/Cardano/Api/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Cardano.Api.Orphans () where
import Prelude

import Control.Iterate.SetAlgebra (BiMap (..), Bimap)
import Data.Aeson (ToJSON (..), object, (.=))
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject, (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (ToJSONKey (..), toJSONKeyText)
import qualified Data.ByteString.Base16 as B16
Expand All @@ -23,6 +23,7 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text

import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Coin as Shelley
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Crypto as Crypto
Expand All @@ -37,8 +38,8 @@ import qualified Shelley.Spec.Ledger.Delegation.Certificates as Shelley
import qualified Shelley.Spec.Ledger.EpochBoundary as ShelleyEpoch
import qualified Shelley.Spec.Ledger.LedgerState as ShelleyLedger
import Shelley.Spec.Ledger.PParams (PParamsUpdate)
import qualified Shelley.Spec.Ledger.Rewards as Shelley
import qualified Shelley.Spec.Ledger.RewardUpdate as Shelley
import qualified Shelley.Spec.Ledger.Rewards as Shelley

-- Orphan instances involved in the JSON output of the API queries.
-- We will remove/replace these as we provide more API wrapper types
Expand Down Expand Up @@ -278,3 +279,7 @@ instance ToJSON Shelley.RewardType where
instance ToJSON (SafeHash.SafeHash c a) where
toJSON = toJSON . SafeHash.extractHash

instance FromJSON Alonzo.Prices where
parseJSON = withObject "Prices" $ \o -> do
obj <- o .: "executionUnitPrices"
Alonzo.Prices <$> obj .: "unitSpace" <*> obj .: "unitTime"
12 changes: 9 additions & 3 deletions cardano-api/src/Cardano/Api/Protocol/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import Cardano.Chain.Slotting (EpochSlots)

import Ouroboros.Consensus.Byron.Ledger (ByronBlock)
import Ouroboros.Consensus.Cardano
import Ouroboros.Consensus.Cardano.Node
import Ouroboros.Consensus.Cardano.Block
import Ouroboros.Consensus.Cardano.ByronHFC (ByronBlockHFC)
import Ouroboros.Consensus.Cardano.Node
import Ouroboros.Consensus.HardFork.Combinator.Embed.Unary
import Ouroboros.Consensus.Node.ProtocolInfo (ProtocolClientInfo(..), ProtocolInfo(..))
import Ouroboros.Consensus.Node.ProtocolInfo (ProtocolClientInfo (..), ProtocolInfo (..))
import Ouroboros.Consensus.Node.Run (RunNode)
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
import Ouroboros.Consensus.Shelley.ShelleyHFC (ShelleyBlockHFC)
Expand Down Expand Up @@ -58,27 +58,33 @@ instance IOLike m => Protocol m (CardanoBlock StandardCrypto) where
ProtocolParamsShelley
ProtocolParamsAllegra
ProtocolParamsMary
ProtocolParamsAlonzo
(ProtocolParamsTransition ByronBlock (ShelleyBlock StandardShelley))
(ProtocolParamsTransition (ShelleyBlock StandardShelley) (ShelleyBlock StandardAllegra))
(ProtocolParamsTransition (ShelleyBlock StandardAllegra) (ShelleyBlock StandardMary))
(ProtocolParamsTransition (ShelleyBlock StandardMary) (ShelleyBlock StandardAlonzo))
protocolInfo (ProtocolInfoArgsCardano
paramsByron
paramsShelleyBased
paramsShelley
paramsAllegra
paramsMary
paramsAlonzo
paramsByronShelley
paramsShelleyAllegra
paramsAllegraMary) =
paramsAllegraMary
paramsMaryAlonzo) =
protocolInfoCardano
paramsByron
paramsShelleyBased
paramsShelley
paramsAllegra
paramsMary
paramsAlonzo
paramsByronShelley
paramsShelleyAllegra
paramsAllegraMary
paramsMaryAlonzo

instance ProtocolClient ByronBlockHFC where
data ProtocolClientInfoArgs ByronBlockHFC =
Expand Down
Loading