-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add Cardano.Api.Tx.Compatible #644
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
-- | This module provides a way to construct a simple transaction over all eras. | ||
-- It is exposed for testing purposes only. | ||
module Cardano.Api.Tx.Compatible | ||
( AnyProtocolUpdate (..) | ||
, createCompatibleSignedTx | ||
) | ||
where | ||
|
||
import Cardano.Api.Eon.ConwayEraOnwards | ||
import Cardano.Api.Eon.ShelleyBasedEra | ||
import Cardano.Api.Eon.ShelleyToBabbageEra | ||
import Cardano.Api.ProtocolParameters | ||
import Cardano.Api.Script | ||
import Cardano.Api.Tx.Body | ||
import Cardano.Api.Tx.Sign | ||
import Cardano.Api.Value | ||
|
||
import qualified Cardano.Ledger.Api as L | ||
|
||
import Control.Error (catMaybes) | ||
import qualified Data.Map.Strict as Map | ||
import Data.Maybe.Strict | ||
import qualified Data.Sequence.Strict as Seq | ||
import Data.Set (fromList) | ||
import Lens.Micro | ||
|
||
data AnyProtocolUpdate era where | ||
ShelleyToBabbageProtocolUpdate | ||
:: ShelleyToBabbageEra era | ||
-> UpdateProposal | ||
-> AnyProtocolUpdate era | ||
ConwayEraOnwardsProtocolUpdate | ||
:: ConwayEraOnwards era | ||
-> TxProposalProcedures BuildTx era | ||
-> AnyProtocolUpdate era | ||
NoPParamsUpdate | ||
:: ShelleyBasedEra era | ||
-> AnyProtocolUpdate era | ||
|
||
createCompatibleSignedTx | ||
:: forall era | ||
. ShelleyBasedEra era | ||
-> [TxIn] | ||
-> [TxOut CtxTx era] | ||
-> [KeyWitness era] | ||
-> Lovelace | ||
-- ^ Fee | ||
-> AnyProtocolUpdate era | ||
-> Either ProtocolParametersConversionError (Tx era) | ||
createCompatibleSignedTx sbeF ins outs witnesses txFee' anyProtocolUpdate = | ||
shelleyBasedEraConstraints sbeF $ | ||
case anyProtocolUpdate of | ||
ShelleyToBabbageProtocolUpdate shelleyToBabbageEra updateProposal -> do | ||
let sbe = shelleyToBabbageEraToShelleyBasedEra shelleyToBabbageEra | ||
|
||
ledgerPParamsUpdate <- toLedgerUpdate sbe updateProposal | ||
|
||
let txbody = createCommonTxBody sbe ins outs txFee' | ||
bodyWithProtocolUpdate = | ||
shelleyToBabbageEraConstraints shelleyToBabbageEra $ | ||
txbody & L.updateTxBodyL .~ SJust ledgerPParamsUpdate | ||
finalTx = | ||
L.mkBasicTx bodyWithProtocolUpdate | ||
& L.witsTxL .~ shelleyToBabbageEraConstraints shelleyToBabbageEra allShelleyToBabbageWitnesses | ||
|
||
return $ ShelleyTx sbe finalTx | ||
NoPParamsUpdate sbe -> do | ||
let txbody = createCommonTxBody sbe ins outs txFee' | ||
finalTx = L.mkBasicTx txbody & L.witsTxL .~ shelleyBasedEraConstraints sbe allShelleyToBabbageWitnesses | ||
|
||
return $ ShelleyTx sbe finalTx | ||
ConwayEraOnwardsProtocolUpdate conwayOnwards proposalProcedures -> do | ||
let sbe = conwayEraOnwardsToShelleyBasedEra conwayOnwards | ||
proposals = convProposalProcedures proposalProcedures | ||
apiScriptWitnesses = scriptWitnessesProposing proposalProcedures | ||
ledgerScripts = convScripts apiScriptWitnesses | ||
referenceInputs = | ||
map toShelleyTxIn $ | ||
catMaybes [getScriptWitnessReferenceInput sWit | (_, AnyScriptWitness sWit) <- apiScriptWitnesses] | ||
sData = convScriptData sbe outs apiScriptWitnesses (BuildTxWith TxSupplementalDataNone) | ||
txbody = | ||
conwayEraOnwardsConstraints conwayOnwards $ | ||
createCommonTxBody sbe ins outs txFee' | ||
& L.referenceInputsTxBodyL .~ fromList referenceInputs | ||
& L.proposalProceduresTxBodyL | ||
.~ proposals | ||
|
||
finalTx = | ||
L.mkBasicTx txbody | ||
& L.witsTxL | ||
.~ conwayEraOnwardsConstraints conwayOnwards (allConwayEraOnwardsWitnesses sData ledgerScripts) | ||
|
||
return $ ShelleyTx sbe finalTx | ||
where | ||
shelleyKeywitnesses = | ||
fromList [w | ShelleyKeyWitness _ w <- witnesses] | ||
|
||
shelleyBootstrapWitnesses = | ||
fromList [w | ShelleyBootstrapWitness _ w <- witnesses] | ||
|
||
allConwayEraOnwardsWitnesses | ||
:: L.AlonzoEraTxWits (ShelleyLedgerEra era) | ||
=> L.EraCrypto (ShelleyLedgerEra era) ~ L.StandardCrypto | ||
=> TxBodyScriptData era -> [L.Script (ShelleyLedgerEra era)] -> L.TxWits (ShelleyLedgerEra era) | ||
allConwayEraOnwardsWitnesses sData ledgerScripts = | ||
let (datums, redeemers) = case sData of | ||
TxBodyScriptData _ ds rs -> (ds, rs) | ||
TxBodyNoScriptData -> (mempty, L.Redeemers mempty) | ||
in L.mkBasicTxWits | ||
& L.addrTxWitsL | ||
.~ shelleyKeywitnesses | ||
& L.bootAddrTxWitsL | ||
.~ shelleyBootstrapWitnesses | ||
& L.datsTxWitsL .~ datums | ||
& L.rdmrsTxWitsL .~ redeemers | ||
& L.scriptTxWitsL | ||
.~ Map.fromList | ||
[ (L.hashScript sw, sw) | ||
| sw <- ledgerScripts | ||
] | ||
|
||
allShelleyToBabbageWitnesses | ||
:: L.EraTxWits (ShelleyLedgerEra era) | ||
=> L.EraCrypto (ShelleyLedgerEra era) ~ L.StandardCrypto | ||
=> L.TxWits (ShelleyLedgerEra era) | ||
allShelleyToBabbageWitnesses = | ||
L.mkBasicTxWits | ||
& L.addrTxWitsL | ||
.~ shelleyKeywitnesses | ||
& L.bootAddrTxWitsL | ||
.~ shelleyBootstrapWitnesses | ||
|
||
createCommonTxBody | ||
:: ShelleyBasedEra era | ||
-> [TxIn] | ||
-> [TxOut ctx era] | ||
-> Lovelace | ||
-> L.TxBody (ShelleyLedgerEra era) | ||
createCommonTxBody era ins outs txFee' = | ||
palas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let txIns' = map toShelleyTxIn ins | ||
txOuts' = map (toShelleyTxOutAny era) outs | ||
in shelleyBasedEraConstraints era $ | ||
L.mkBasicTxBody | ||
& L.inputsTxBodyL | ||
.~ fromList txIns' | ||
& L.outputsTxBodyL | ||
.~ Seq.fromList txOuts' | ||
& L.feeTxBodyL | ||
.~ txFee' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Cardano.Api.Compatible | ||
( module Cardano.Api.Tx.Compatible | ||
) | ||
where | ||
|
||
import Cardano.Api.Tx.Compatible |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module name and the description is not quite clear for me. Is it supposed to be a module for support of transaction building for older eras once we remove them from API?
Maybe we should add
Internal
to the module name e.g.Cardano.Api.Tx.Internal.Compatible
, to mark that it's not meant to be directly used?I'm not sure about the name as well. Maybe we should name it something like
Old
orLegacy
to put emphasis that it's meant just for backwards compatibility for tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's meant to be directly used. It's supposed to house all functions that are expected to be backwards compatible across all shelley based eras. I'm not opposed to a rename but I'm not blocking the PR on this.