Skip to content

Commit

Permalink
Merge #2738
Browse files Browse the repository at this point in the history
2738: Filling in more Alonzo API extensions for the Tx and TxBody r=dcoutts a=dcoutts



Co-authored-by: Duncan Coutts <duncan@well-typed.com>
Co-authored-by: Jordan Millar <jordan.millar@iohk.io>
  • Loading branch information
3 people authored May 28, 2021
2 parents 7d8ab95 + 6544ca8 commit 5430355
Show file tree
Hide file tree
Showing 10 changed files with 594 additions and 168 deletions.
7 changes: 4 additions & 3 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ module Cardano.Api (
-- | Constructing and inspecting transactions

-- ** Transaction bodies
TxBody,
getTransactionBodyContent,
TxBody(TxBody),
makeTransactionBody,
TxBodyContent(..),
TxBodyError(..),
Expand Down Expand Up @@ -175,6 +174,7 @@ module Cardano.Api (
TxMetadataInEra(..),
TxAuxScripts(..),
TxAuxScriptData(..),
TxExtraKeyWitnesses(..),
TxWithdrawals(..),
TxCertificates(..),
TxUpdateProposal(..),
Expand All @@ -195,6 +195,7 @@ module Cardano.Api (
ValidityLowerBoundSupportedInEra(..),
TxMetadataSupportedInEra(..),
AuxScriptsSupportedInEra(..),
TxExtraKeyWitnessesSupportedInEra(..),
WithdrawalsSupportedInEra(..),
CertificatesSupportedInEra(..),
UpdateProposalSupportedInEra(..),
Expand All @@ -207,6 +208,7 @@ module Cardano.Api (
validityLowerBoundSupportedInEra,
txMetadataSupportedInEra,
auxScriptsSupportedInEra,
extraKeyWitnessesSupportedInEra,
withdrawalsSupportedInEra,
certificatesSupportedInEra,
updateProposalSupportedInEra,
Expand All @@ -225,7 +227,6 @@ module Cardano.Api (
makeSignedTransaction,
KeyWitness,
makeByronKeyWitness,
makeByronTransaction,
ShelleyWitnessSigningKey(..),
makeShelleyKeyWitness,
makeShelleyBootstrapWitness,
Expand Down
8 changes: 8 additions & 0 deletions cardano-api/src/Cardano/Api/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Cardano.Api.Eras
, ShelleyBasedEra(..)
, IsShelleyBasedEra(..)
, InAnyShelleyBasedEra(..)
, shelleyBasedToCardanoEra

-- ** Mapping to era types from the Shelley ledger library
, ShelleyLedgerEra
Expand Down Expand Up @@ -276,6 +277,13 @@ data InAnyShelleyBasedEra thing where
-> InAnyShelleyBasedEra thing


shelleyBasedToCardanoEra :: ShelleyBasedEra era -> CardanoEra era
shelleyBasedToCardanoEra ShelleyBasedEraShelley = ShelleyEra
shelleyBasedToCardanoEra ShelleyBasedEraAllegra = AllegraEra
shelleyBasedToCardanoEra ShelleyBasedEraMary = MaryEra
shelleyBasedToCardanoEra ShelleyBasedEraAlonzo = AlonzoEra


-- ----------------------------------------------------------------------------
-- Cardano eras factored as Byron vs Shelley-based
--
Expand Down
145 changes: 145 additions & 0 deletions cardano-api/src/Cardano/Api/ProtocolParameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module Cardano.Api.ProtocolParameters (
toShelleyPParamsUpdate,
toShelleyProposedPPUpdates,
toShelleyUpdate,
toLedgerPParams,
fromShelleyPParams,
fromShelleyPParamsUpdate,
fromShelleyProposedPPUpdates,
Expand Down Expand Up @@ -73,6 +74,7 @@ import Control.Monad
import Data.Aeson (FromJSON (..), ToJSON (..), object, withObject,
withText, (.!=), (.:), (.:?), (.=))
import qualified Data.Aeson as Aeson
import Data.Bifunctor (bimap)

import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash.Class as Crypto
Expand All @@ -89,11 +91,14 @@ import qualified Shelley.Spec.Ledger.Genesis as Shelley
import qualified Shelley.Spec.Ledger.Keys as Shelley
import qualified Shelley.Spec.Ledger.PParams as Shelley

import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.PParams as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
-- TODO alonzo: eliminate this import and use things re-exported from the ledger lib
import qualified Plutus.V1.Ledger.Api as Plutus

import Cardano.Api.Address
import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
Expand Down Expand Up @@ -892,6 +897,146 @@ fromShelleyPParamsUpdate
}


toLedgerPParams
:: ShelleyBasedEra era
-> ProtocolParameters
-> Ledger.PParams (ShelleyLedgerEra era)
toLedgerPParams sbe pparams =
case sbe of
ShelleyBasedEraShelley -> toShelleyPParams pparams
ShelleyBasedEraAllegra -> toShelleyPParams pparams
ShelleyBasedEraMary -> toShelleyPParams pparams
ShelleyBasedEraAlonzo -> toAlonzoPParams pparams

toShelleyPParams :: ProtocolParameters -> Shelley.PParams ledgerera
toShelleyPParams ProtocolParameters {
protocolParamProtocolVersion,
protocolParamDecentralization,
protocolParamExtraPraosEntropy,
protocolParamMaxBlockHeaderSize,
protocolParamMaxBlockBodySize,
protocolParamMaxTxSize,
protocolParamTxFeeFixed,
protocolParamTxFeePerByte,
protocolParamMinUTxOValue,
protocolParamStakeAddressDeposit,
protocolParamStakePoolDeposit,
protocolParamMinPoolCost,
protocolParamPoolRetireMaxEpoch,
protocolParamStakePoolTargetNum,
protocolParamPoolPledgeInfluence,
protocolParamMonetaryExpansion,
protocolParamTreasuryCut
} =
Shelley.PParams
{ Shelley._protocolVersion
= let (maj, minor) = protocolParamProtocolVersion
in Shelley.ProtVer maj minor
, Shelley._d = Shelley.unitIntervalFromRational
protocolParamDecentralization
, Shelley._extraEntropy = toShelleyNonce protocolParamExtraPraosEntropy
, Shelley._maxBHSize = protocolParamMaxBlockHeaderSize
, Shelley._maxBBSize = protocolParamMaxBlockBodySize
, Shelley._maxTxSize = protocolParamMaxTxSize
, Shelley._minfeeB = protocolParamTxFeeFixed
, Shelley._minfeeA = protocolParamTxFeePerByte
, Shelley._minUTxOValue = toShelleyLovelace protocolParamMinUTxOValue
, Shelley._keyDeposit = toShelleyLovelace protocolParamStakeAddressDeposit
, Shelley._poolDeposit = toShelleyLovelace protocolParamStakePoolDeposit
, Shelley._minPoolCost = toShelleyLovelace protocolParamMinPoolCost
, Shelley._eMax = protocolParamPoolRetireMaxEpoch
, Shelley._nOpt = protocolParamStakePoolTargetNum
, Shelley._a0 = protocolParamPoolPledgeInfluence
, Shelley._rho = Shelley.unitIntervalFromRational
protocolParamMonetaryExpansion
, Shelley._tau = Shelley.unitIntervalFromRational
protocolParamTreasuryCut
}

toAlonzoPParams :: ProtocolParameters -> Alonzo.PParams ledgerera
toAlonzoPParams ProtocolParameters {
protocolParamProtocolVersion,
protocolParamDecentralization,
protocolParamExtraPraosEntropy,
protocolParamMaxBlockHeaderSize,
protocolParamMaxBlockBodySize,
protocolParamMaxTxSize,
protocolParamTxFeeFixed,
protocolParamTxFeePerByte,
protocolParamStakeAddressDeposit,
protocolParamStakePoolDeposit,
protocolParamMinPoolCost,
protocolParamPoolRetireMaxEpoch,
protocolParamStakePoolTargetNum,
protocolParamPoolPledgeInfluence,
protocolParamMonetaryExpansion,
protocolParamTreasuryCut,
protocolParamUTxOCostPerWord = Just utxoCostPerWord,
protocolParamCostModels,
protocolParamPrices = Just prices,
protocolParamMaxTxExUnits = Just maxTxExUnits,
protocolParamMaxBlockExUnits = Just maxBlockExUnits,
protocolParamMaxValueSize = Just maxValueSize
} =
Alonzo.PParams {
Alonzo._protocolVersion
= let (maj, minor) = protocolParamProtocolVersion
in Alonzo.ProtVer maj minor
, Alonzo._d = Shelley.unitIntervalFromRational
protocolParamDecentralization
, Alonzo._extraEntropy = toShelleyNonce protocolParamExtraPraosEntropy
, Alonzo._maxBHSize = protocolParamMaxBlockHeaderSize
, Alonzo._maxBBSize = protocolParamMaxBlockBodySize
, Alonzo._maxTxSize = protocolParamMaxTxSize
, Alonzo._minfeeB = protocolParamTxFeeFixed
, Alonzo._minfeeA = protocolParamTxFeePerByte
, Alonzo._keyDeposit = toShelleyLovelace protocolParamStakeAddressDeposit
, Alonzo._poolDeposit = toShelleyLovelace protocolParamStakePoolDeposit
, Alonzo._minPoolCost = toShelleyLovelace protocolParamMinPoolCost
, Alonzo._eMax = protocolParamPoolRetireMaxEpoch
, Alonzo._nOpt = protocolParamStakePoolTargetNum
, Alonzo._a0 = protocolParamPoolPledgeInfluence
, Alonzo._rho = Shelley.unitIntervalFromRational
protocolParamMonetaryExpansion
, Alonzo._tau = Shelley.unitIntervalFromRational
protocolParamTreasuryCut

-- New params in Alonzo:
, Alonzo._adaPerUTxOWord = toShelleyLovelace utxoCostPerWord
, Alonzo._costmdls = toAlonzoCostModels protocolParamCostModels
, Alonzo._prices = toAlonzoPrices prices
, Alonzo._maxTxExUnits = toAlonzoExUnits maxTxExUnits
, Alonzo._maxBlockExUnits = toAlonzoExUnits maxBlockExUnits
, Alonzo._maxValSize = maxValueSize
, Alonzo._collateralPercentage = error "TODO alonzo: toAlonzoPParams collateralPercentage"
, Alonzo._maxCollateralInputs = error "TODO alonzo: toAlonzoPParams maxCollateralInputs"
}
toAlonzoPParams ProtocolParameters { protocolParamUTxOCostPerWord = Nothing } =
error "fromProtocolParamsAlonzo: must specify protocolParamUTxOCostPerWord"
toAlonzoPParams ProtocolParameters { protocolParamPrices = Nothing } =
error "fromProtocolParamsAlonzo: must specify protocolParamPrices"
toAlonzoPParams ProtocolParameters { protocolParamMaxTxExUnits = Nothing } =
error "fromProtocolParamsAlonzo: must specify protocolParamMaxTxExUnits"
toAlonzoPParams ProtocolParameters { protocolParamMaxBlockExUnits = Nothing } =
error "fromProtocolParamsAlonzo: must specify protocolParamMaxBlockExUnits"
toAlonzoPParams ProtocolParameters { protocolParamMaxValueSize = Nothing } =
error "fromProtocolParamsAlonzo: must specify protocolParamMaxValueSize"


toAlonzoCostModels
:: Map AnyPlutusScriptVersion CostModel
-> Map Alonzo.Language Alonzo.CostModel
toAlonzoCostModels =
Map.fromList
. map (bimap toAlonzoScriptLanguage toAlonzoCostModel)
. Map.toList

toAlonzoScriptLanguage :: AnyPlutusScriptVersion -> Alonzo.Language
toAlonzoScriptLanguage (AnyPlutusScriptVersion PlutusScriptV1) = Alonzo.PlutusV1

toAlonzoCostModel :: CostModel -> Alonzo.CostModel
toAlonzoCostModel (CostModel m) = Alonzo.CostModel m

fromShelleyPParams :: Shelley.PParams ledgerera
-> ProtocolParameters
fromShelleyPParams
Expand Down
12 changes: 11 additions & 1 deletion cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ module Cardano.Api.Script (
fromShelleyScriptHash,
toAlonzoScriptData,
fromAlonzoScriptData,
toAlonzoLanguage,
fromAlonzoLanguage,

-- * Data family instances
AsType(..),
Expand Down Expand Up @@ -125,8 +127,9 @@ import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
import qualified Shelley.Spec.Ledger.Keys as Shelley
import qualified Shelley.Spec.Ledger.Scripts as Shelley

import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.Data as Alonzo
import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo

import Cardano.Api.Eras
import Cardano.Api.HasTypeProxy
Expand Down Expand Up @@ -300,6 +303,13 @@ instance Aeson.ToJSONKey AnyPlutusScriptVersion where
toText (AnyPlutusScriptVersion PlutusScriptV1) = "PlutusScriptV1"
toAesonEncoding = Aeson.text . toText

toAlonzoLanguage :: AnyPlutusScriptVersion -> Alonzo.Language
toAlonzoLanguage (AnyPlutusScriptVersion PlutusScriptV1) = Alonzo.PlutusV1

fromAlonzoLanguage :: Alonzo.Language -> AnyPlutusScriptVersion
fromAlonzoLanguage Alonzo.PlutusV1 = AnyPlutusScriptVersion PlutusScriptV1


class HasTypeProxy lang => IsScriptLanguage lang where
scriptLanguage :: ScriptLanguage lang

Expand Down
28 changes: 16 additions & 12 deletions cardano-api/src/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ getTxBody (ShelleyTx era tx) =
ShelleyBasedEraShelley -> getShelleyTxBody tx
ShelleyBasedEraAllegra -> getShelleyTxBody tx
ShelleyBasedEraMary -> getShelleyTxBody tx
ShelleyBasedEraAlonzo -> getAlonzoTxBody tx
ShelleyBasedEraAlonzo -> getAlonzoTxBody ScriptDataInAlonzoEra tx
where
getShelleyTxBody :: forall ledgerera.
ShelleyLedgerEra era ~ ledgerera
Expand All @@ -430,30 +430,32 @@ getTxBody (ShelleyTx era tx) =
} =
ShelleyTxBody era txbody
(Map.elems msigWits)
TxBodyNoRedeemers
(strictMaybeToMaybe txAuxiliaryData)

getAlonzoTxBody :: forall ledgerera.
ShelleyLedgerEra era ~ ledgerera
=> Ledger.Witnesses ledgerera ~ Alonzo.TxWitness ledgerera
=> Shelley.ShelleyBased ledgerera
=> Ledger.Tx ledgerera
=> ScriptDataSupportedInEra era
-> Ledger.Tx ledgerera
-> TxBody era
getAlonzoTxBody Shelley.Tx {
getAlonzoTxBody scriptDataInEra
Shelley.Tx {
Shelley.body = txbody,
Shelley.wits = Alonzo.TxWitness'
_addrWits
_bootWits
txscripts
_txdats
_txrdmrs,
redeemers,
Shelley.auxiliaryData = auxiliaryData
} =
ShelleyTxBody era txbody
(Map.elems txscripts)
(fromAlonzoRedeemers scriptDataInEra redeemers)
(strictMaybeToMaybe auxiliaryData)
--TODO: we will probably want to put the Alonzo data and
-- redeemer in the tx body here, and so that will use
-- the _txdats and _txrdmrs above.


getTxWitnesses :: forall era. Tx era -> [KeyWitness era]
getTxWitnesses (ByronTx Byron.ATxAux { Byron.aTaWitness = witnesses }) =
Expand Down Expand Up @@ -516,7 +518,9 @@ makeSignedTransaction witnesses (ByronTxBody txbody) =
(unAnnotated txbody)
(Vector.fromList [ w | ByronKeyWitness w <- witnesses ])

makeSignedTransaction witnesses (ShelleyTxBody era txbody txscripts txmetadata) =
makeSignedTransaction witnesses (ShelleyTxBody era txbody
txscripts redeemers
txmetadata) =
case era of
ShelleyBasedEraShelley -> makeShelleySignedTransaction txbody
ShelleyBasedEraAllegra -> makeShelleySignedTransaction txbody
Expand Down Expand Up @@ -564,7 +568,7 @@ makeSignedTransaction witnesses (ShelleyTxBody era txbody txscripts txmetadata)
(Map.fromList [ (Ledger.hashScript @ledgerera sw, sw)
| sw <- txscripts ])
(error "TODO alonzo: makeAlonzoSignedTransaction: datums")
(error "TODO alonzo: makeAlonzoSignedTransaction: redeemers"))
(toAlonzoRedeemers redeemers))
(maybeToStrictMaybe txmetadata)


Expand All @@ -574,7 +578,7 @@ makeByronKeyWitness :: forall key.
-> TxBody ByronEra
-> SigningKey key
-> KeyWitness ByronEra
makeByronKeyWitness _ (ShelleyTxBody era _ _ _) = case era of {}
makeByronKeyWitness _ (ShelleyTxBody era _ _ _ _) = case era of {}
makeByronKeyWitness nw (ByronTxBody txbody) =
let txhash :: Byron.Hash Byron.Tx
txhash = Byron.hashDecoded txbody
Expand Down Expand Up @@ -625,7 +629,7 @@ makeShelleyBootstrapWitness :: forall era.
makeShelleyBootstrapWitness _ ByronTxBody{} _ =
case shelleyBasedEra :: ShelleyBasedEra era of {}

makeShelleyBootstrapWitness nwOrAddr (ShelleyTxBody era txbody _ _) sk =
makeShelleyBootstrapWitness nwOrAddr (ShelleyTxBody era txbody _ _ _) sk =
case era of
ShelleyBasedEraShelley ->
makeShelleyBasedBootstrapWitness era nwOrAddr txbody sk
Expand Down Expand Up @@ -738,7 +742,7 @@ makeShelleyKeyWitness :: forall era
=> TxBody era
-> ShelleyWitnessSigningKey
-> KeyWitness era
makeShelleyKeyWitness (ShelleyTxBody era txbody _ _) =
makeShelleyKeyWitness (ShelleyTxBody era txbody _ _ _) =
case era of
ShelleyBasedEraShelley -> makeShelleyBasedKeyWitness txbody
ShelleyBasedEraAllegra -> makeShelleyBasedKeyWitness txbody
Expand Down
Loading

0 comments on commit 5430355

Please sign in to comment.