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

Filling in more Alonzo API extensions for the Tx and TxBody #2738

Merged
merged 8 commits into from
May 28, 2021
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
93 changes: 93 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,94 @@ 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 pparams =
Shelley.PParams
{ Shelley._protocolVersion = let (maj, minor) = protocolParamProtocolVersion pparams
in Shelley.ProtVer maj minor
, Shelley._d = Shelley.unitIntervalFromRational $ protocolParamDecentralization pparams
, Shelley._extraEntropy = toShelleyNonce $ protocolParamExtraPraosEntropy pparams
newhoggy marked this conversation as resolved.
Show resolved Hide resolved
, Shelley._maxBHSize = protocolParamMaxBlockHeaderSize pparams
, Shelley._maxBBSize = protocolParamMaxBlockBodySize pparams
, Shelley._maxTxSize = protocolParamMaxTxSize pparams
, Shelley._minfeeB = protocolParamTxFeeFixed pparams
, Shelley._minfeeA = protocolParamTxFeePerByte pparams
, Shelley._minUTxOValue = toShelleyLovelace $ protocolParamMinUTxOValue pparams
, Shelley._keyDeposit = toShelleyLovelace $ protocolParamStakeAddressDeposit pparams
, Shelley._poolDeposit = toShelleyLovelace $ protocolParamStakePoolDeposit pparams
, Shelley._minPoolCost = toShelleyLovelace $ protocolParamMinPoolCost pparams
, Shelley._eMax = protocolParamPoolRetireMaxEpoch pparams
, Shelley._nOpt = protocolParamStakePoolTargetNum pparams
, Shelley._a0 = protocolParamPoolPledgeInfluence pparams
, Shelley._rho = Shelley.unitIntervalFromRational $ protocolParamMonetaryExpansion pparams
, Shelley._tau = Shelley.unitIntervalFromRational $ protocolParamTreasuryCut pparams
}

toAlonzoPParams :: ProtocolParameters -> Alonzo.PParams ledgerera
toAlonzoPParams pparams =
Alonzo.PParams
{ Alonzo._protocolVersion = let (maj, minor) = protocolParamProtocolVersion pparams
in Alonzo.ProtVer maj minor
, Alonzo._d = Shelley.unitIntervalFromRational $ protocolParamDecentralization pparams
, Alonzo._extraEntropy = toShelleyNonce $ protocolParamExtraPraosEntropy pparams
newhoggy marked this conversation as resolved.
Show resolved Hide resolved
, Alonzo._maxBHSize = protocolParamMaxBlockHeaderSize pparams
, Alonzo._maxBBSize = protocolParamMaxBlockBodySize pparams
, Alonzo._maxTxSize = protocolParamMaxTxSize pparams
, Alonzo._minfeeB = protocolParamTxFeeFixed pparams
, Alonzo._minfeeA = protocolParamTxFeePerByte pparams
, Alonzo._keyDeposit = toShelleyLovelace $ protocolParamStakeAddressDeposit pparams
, Alonzo._poolDeposit = toShelleyLovelace $ protocolParamStakePoolDeposit pparams
, Alonzo._minPoolCost = toShelleyLovelace $ protocolParamMinPoolCost pparams
, Alonzo._eMax = protocolParamPoolRetireMaxEpoch pparams
, Alonzo._nOpt = protocolParamStakePoolTargetNum pparams
, Alonzo._a0 = protocolParamPoolPledgeInfluence pparams
, Alonzo._rho = Shelley.unitIntervalFromRational $ protocolParamMonetaryExpansion pparams
, Alonzo._tau = Shelley.unitIntervalFromRational $ protocolParamTreasuryCut pparams
, Alonzo._adaPerUTxOWord = case protocolParamUTxOCostPerWord pparams of
Just costPerByte -> toShelleyLovelace costPerByte
Nothing -> error "fromProtocolParamsAlonzo: Must specify _adaPerUTxOByte"
, Alonzo._costmdls = toAlonzoCostModels $ protocolParamCostModels pparams
, Alonzo._prices = case protocolParamPrices pparams of
Just prices -> toAlonzoPrices prices
Nothing -> error "fromProtocolParamsAlonzo: Must specify _prices"
, Alonzo._maxTxExUnits = case protocolParamMaxTxExUnits pparams of
Just eUnits -> toAlonzoExUnits eUnits
Nothing -> error "fromProtocolParamsAlonzo: Must specify _maxTxExUnits"
, Alonzo._maxBlockExUnits = case protocolParamMaxBlockExUnits pparams of
Just eUnits -> toAlonzoExUnits eUnits
Nothing -> error "fromProtocolParamsAlonzo: Must specify _maxBlockExUnits"
, Alonzo._maxValSize = case protocolParamMaxValueSize pparams of
Just maxSize -> maxSize
Nothing -> error "fromProtocolParamsAlonzo: Must specify _maxValSize"
, Alonzo._collateralPercentage = error "TODO alonzo: toAlonzoPParams collateralPercentage"
, Alonzo._maxCollateralInputs = error "TODO alonzo: toAlonzoPParams maxCollateralInputs"
}

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