Skip to content

Commit

Permalink
cardano-api: 10.5
Browse files Browse the repository at this point in the history
  • Loading branch information
locallycompact committed Jan 2, 2025
1 parent 9329c74 commit 8aeeb5d
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 201 deletions.
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository cardano-haskell-packages
-- See CONTRIBUTING.md for information about when and how to update these.
index-state:
, hackage.haskell.org 2024-11-22T14:59:16Z
, cardano-haskell-packages 2024-12-05T13:51:16Z
, cardano-haskell-packages 2024-12-20T15:52:56Z

packages:
hydra-prelude
Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ library
, aeson >=2
, base >=4.16
, bytestring
, cardano-api ^>=10.4
, cardano-api ^>=10.5
, cardano-binary
, cardano-crypto-class
, cardano-ledger-allegra
Expand Down
8 changes: 5 additions & 3 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import Hydra.Cardano.Api (
Tx,
TxId,
UTxO,
addTxIns,
defaultTxBodyContent,
getTxBody,
getTxId,
getVerificationKey,
Expand Down Expand Up @@ -71,7 +73,7 @@ import Hydra.Cluster.Fixture (Actor (..), actorName, alice, aliceSk, aliceVk, bo
import Hydra.Cluster.Mithril (MithrilLog)
import Hydra.Cluster.Options (Options)
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, setNetworkId)
import Hydra.Ledger.Cardano (addInputs, emptyTxBody, mkSimpleTx, mkTransferTx, unsafeBuildTransaction)
import Hydra.Ledger.Cardano (mkSimpleTx, mkTransferTx, unsafeBuildTransaction)
import Hydra.Logging (Tracer, traceWith)
import Hydra.Options (DirectChainConfig (..), networkId, startChainFrom)
import Hydra.Tx (HeadId, IsTx (balance), Party, txId)
Expand Down Expand Up @@ -457,8 +459,8 @@ singlePartyCommitsScriptBlueprint tracer workDir node hydraScriptsTxId =
mkScriptWitness serializedScript (mkScriptDatum ()) (toScriptData ())
let spendingTx =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(scriptIn, scriptWitness)]
defaultTxBodyContent
& addTxIns [(scriptIn, scriptWitness)]
pure
( Aeson.object
[ "blueprintTx" .= spendingTx
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/Ledger/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mkSimpleTx (txin, TxOut owner valueIn datum refScript) (recipient, valueOut) sk
pure $ makeSignedTransaction witnesses body
where
bodyContent =
emptyTxBody
defaultTxBodyContent
{ txIns = [(txin, BuildTxWith $ KeyWitness KeyWitnessForSpending)]
, txOuts = outs
, txFee = TxFeeExplicit fee
Expand Down Expand Up @@ -170,7 +170,7 @@ mkRangedTx (txin, TxOut owner valueIn datum refScript) (recipient, valueOut) sk
pure $ makeSignedTransaction witnesses body
where
bodyContent =
emptyTxBody
defaultTxBodyContent
{ txIns = [(txin, BuildTxWith $ KeyWitness KeyWitnessForSpending)]
, txOuts =
TxOut @CtxTx recipient valueOut TxOutDatumNone ReferenceScriptNone
Expand Down
10 changes: 5 additions & 5 deletions hydra-node/test/Hydra/Chain/Direct/TxSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Hydra.Chain.Direct.Tx (
)
import Hydra.Contract.Dummy (dummyValidatorScript)
import Hydra.Contract.HeadTokens (headPolicyId)
import Hydra.Ledger.Cardano.Builder (addInputs, addReferenceInputs, addVkInputs, emptyTxBody, unsafeBuildTransaction)
import Hydra.Ledger.Cardano.Builder (addTxInsSpending, unsafeBuildTransaction)
import Hydra.Ledger.Cardano.Evaluate (propTransactionEvaluates)
import Hydra.Tx.BlueprintTx (CommitBlueprintTx (..))
import Hydra.Tx.Commit (commitTx)
Expand Down Expand Up @@ -222,7 +222,7 @@ propIsSubmapOf as bs =
genBlueprintTxWithUTxO :: Gen (UTxO, Tx)
genBlueprintTxWithUTxO =
fmap (second unsafeBuildTransaction) $
spendingPubKeyOutput (mempty, emptyTxBody)
spendingPubKeyOutput (mempty, defaultTxBodyContent)
>>= spendSomeScriptInputs
>>= addSomeReferenceInputs
>>= addValidityRange
Expand All @@ -234,7 +234,7 @@ genBlueprintTxWithUTxO =
utxoToSpend <- genUTxOAdaOnlyOfSize =<< choose (0, 3)
pure
( utxo <> utxoToSpend
, txbody & addVkInputs (toList $ UTxO.inputSet utxoToSpend)
, txbody & addTxInsSpending (toList $ UTxO.inputSet utxoToSpend)
)

spendSomeScriptInputs (utxo, txbody) = do
Expand All @@ -249,7 +249,7 @@ genBlueprintTxWithUTxO =
pure
( utxo <> utxoToSpend
, txbody
& addInputs
& addTxIns
( UTxO.pairs $
( \_ ->
BuildTxWith $
Expand All @@ -263,7 +263,7 @@ genBlueprintTxWithUTxO =
addSomeReferenceInputs (utxo, txbody) = do
txout <- genTxOutWithReferenceScript
txin <- arbitrary
pure (utxo <> UTxO.singleton (txin, txout), txbody & addReferenceInputs [txin])
pure (utxo <> UTxO.singleton (txin, txout), txbody & addTxInsReference [txin])

addValidityRange (utxo, txbody) = do
(start, end) <- arbitrary
Expand Down
103 changes: 4 additions & 99 deletions hydra-tx/src/Hydra/Ledger/Cardano/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Hydra.Ledger.Cardano.Builder where
import Hydra.Cardano.Api
import Hydra.Prelude

import Data.Default (def)
import Data.Map qualified as Map

-- * Executing
Expand Down Expand Up @@ -34,99 +33,15 @@ data InvalidTransactionException = InvalidTransactionException

instance Exception InvalidTransactionException

-- * Constructing

-- | An empty 'TxBodyContent' with all empty/zero values to be extended using
-- record updates.
--
-- NOTE: 'makeTransactionBody' throws when one tries to build a transaction
-- with scripts but no collaterals. This is unfortunate because collaterals are
-- currently added after by our integrated wallet.
--
-- Similarly, 'makeTransactionBody' throws when building a transaction with
-- scripts and no protocol parameters (needed to compute the script integrity
-- hash). This is also added by our wallet at the moment and this ugly
-- work-around will be removed eventually (related item
-- [215](https://github.com/cardano-scaling/hydra/issues/215).
--
-- So we currently bypass this by having default but seemingly innofensive
-- values for collaterals and protocol params in the 'empty' value
emptyTxBody :: TxBodyContent BuildTx
emptyTxBody =
TxBodyContent
mempty -- inputs
(TxInsCollateral mempty)
TxInsReferenceNone
mempty -- outputs
TxTotalCollateralNone
TxReturnCollateralNone
(TxFeeExplicit 0)
TxValidityNoLowerBound
TxValidityNoUpperBound
TxMetadataNone
TxAuxScriptsNone
TxExtraKeyWitnessesNone
(BuildTxWith $ Just $ LedgerProtocolParameters def)
TxWithdrawalsNone
TxCertificatesNone
TxUpdateProposalNone
TxMintValueNone
TxScriptValidityNone
Nothing
Nothing
Nothing
Nothing

-- | Add new inputs to an ongoing builder.
addInputs :: TxIns BuildTx -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addInputs ins tx =
tx{txIns = txIns tx <> ins}

addReferenceInputs :: [TxIn] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addReferenceInputs refs' tx =
tx
{ txInsReference = case txInsReference tx of
TxInsReferenceNone ->
TxInsReference refs'
TxInsReference refs ->
TxInsReference (refs <> refs')
}

-- | Like 'addInputs' but only for vk inputs which requires no additional data.
addVkInputs :: [TxIn] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addVkInputs ins =
addInputs ((,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> ins)

-- | Append new outputs to an ongoing builder.
addOutputs :: [TxOut CtxTx] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addOutputs outputs tx =
tx{txOuts = txOuts tx <> outputs}

-- | Add extra required key witnesses to a transaction.
addExtraRequiredSigners :: [Hash PaymentKey] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addExtraRequiredSigners vks tx =
tx{txExtraKeyWits = txExtraKeyWits'}
where
txExtraKeyWits' =
case txExtraKeyWits tx of
TxExtraKeyWitnessesNone ->
TxExtraKeyWitnesses vks
TxExtraKeyWitnesses vks' ->
TxExtraKeyWitnesses (vks' <> vks)
addTxInsSpending :: [TxIn] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addTxInsSpending ins =
addTxIns ((,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> ins)

-- | Mint tokens with given plutus minting script and redeemer.
mintTokens :: ToScriptData redeemer => PlutusScript -> redeemer -> [(AssetName, Quantity)] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
mintTokens script redeemer assets tx =
tx{txMintValue = TxMintValue mintedTokens'}
mintTokens script redeemer assets = addTxMintValue newTokens
where
mintedTokens =
case txMintValue tx of
TxMintValueNone -> mempty
TxMintValue t -> t

mintedTokens' =
Map.union mintedTokens newTokens

newTokens =
Map.fromList [(policyId, fmap (\(x, y) -> (x, y, BuildTxWith mintingWitness)) assets)]

Expand All @@ -141,13 +56,3 @@ mintTokens script redeemer assets tx =
burnTokens :: ToScriptData redeemer => PlutusScript -> redeemer -> [(AssetName, Quantity)] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
burnTokens script redeemer assets =
mintTokens script redeemer (fmap (second negate) assets)

-- | Set the upper validity bound for this transaction to some 'SlotNo'.
setValidityUpperBound :: SlotNo -> TxBodyContent BuildTx -> TxBodyContent BuildTx
setValidityUpperBound slotNo tx =
tx{txValidityUpperBound = TxValidityUpperBound slotNo}

-- | Set the lower validity bound for this transaction to some 'SlotNo'.
setValidityLowerBound :: SlotNo -> TxBodyContent BuildTx -> TxBodyContent BuildTx
setValidityLowerBound slotNo tx =
tx{txValidityLowerBound = TxValidityLowerBound slotNo}
15 changes: 5 additions & 10 deletions hydra-tx/src/Hydra/Tx/Abort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import Hydra.Contract.HeadState qualified as Head
import Hydra.Contract.Initial qualified as Initial
import Hydra.Contract.MintAction (MintAction (Burn))
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addReferenceInputs,
burnTokens,
emptyTxBody,
unsafeBuildTransaction,
)
import Hydra.Plutus (commitValidatorScript, initialValidatorScript)
Expand Down Expand Up @@ -54,12 +49,12 @@ abortTx committedUTxO scriptRegistry vk (headInput, initialHeadOutput) headToken
| otherwise =
Right $
unsafeBuildTransaction $
emptyTxBody
& addInputs ((headInput, headWitness) : initialInputs <> commitInputs)
& addReferenceInputs ([headScriptRef, initialScriptRef] <> [commitScriptRef | not $ null commitInputs])
& addOutputs reimbursedOutputs
defaultTxBodyContent
& addTxIns ((headInput, headWitness) : initialInputs <> commitInputs)
& addTxInsReference ([headScriptRef, initialScriptRef] <> [commitScriptRef | not $ null commitInputs])
& addTxOuts reimbursedOutputs
& burnTokens headTokenScript Burn headTokens
& addExtraRequiredSigners [verificationKeyHash vk]
& addTxExtraKeyWits [verificationKeyHash vk]
where
headWitness =
BuildTxWith $
Expand Down
21 changes: 7 additions & 14 deletions hydra-tx/src/Hydra/Tx/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ import Hydra.Data.ContestationPeriod (addContestationPeriod)
import Hydra.Data.ContestationPeriod qualified as OnChain
import Hydra.Data.Party qualified as OnChain
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addReferenceInputs,
emptyTxBody,
setValidityLowerBound,
setValidityUpperBound,
unsafeBuildTransaction,
)
import Hydra.Plutus.Extras.Time (posixFromUTCTime)
Expand Down Expand Up @@ -68,13 +61,13 @@ closeTx ::
Tx
closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endSlotNo, utcTime) openThreadOutput incrementalAction =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityLowerBound startSlotNo
& setValidityUpperBound endSlotNo
defaultTxBodyContent
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts [headOutputAfter]
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxValidityLowerBound (TxValidityLowerBound startSlotNo)
& setTxValidityUpperBound (TxValidityUpperBound endSlotNo)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CloseTx")
where
OpenThreadOutput
Expand Down
15 changes: 5 additions & 10 deletions hydra-tx/src/Hydra/Tx/CollectCom.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import Hydra.Contract.Commit qualified as Commit
import Hydra.Contract.Head qualified as Head
import Hydra.Contract.HeadState qualified as Head
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addReferenceInputs,
emptyTxBody,
unsafeBuildTransaction,
)
import Hydra.Plutus (commitValidatorScript)
Expand Down Expand Up @@ -51,11 +46,11 @@ collectComTx ::
Tx
collectComTx networkId scriptRegistry vk headId headParameters (headInput, initialHeadOutput) commits utxoToCollect =
unsafeBuildTransaction $
emptyTxBody
& addInputs ((headInput, headWitness) : (mkCommit <$> Map.keys commits))
& addReferenceInputs [commitScriptRef, headScriptRef]
& addOutputs [headOutput]
& addExtraRequiredSigners [verificationKeyHash vk]
defaultTxBodyContent
& addTxIns ((headInput, headWitness) : (mkCommit <$> Map.keys commits))
& addTxInsReference [commitScriptRef, headScriptRef]
& addTxOuts [headOutput]
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CollectComTx")
where
HeadParameters{parties, contestationPeriod} = headParameters
Expand Down
18 changes: 6 additions & 12 deletions hydra-tx/src/Hydra/Tx/Contest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import Hydra.Contract.HeadState qualified as Head
import Hydra.Data.ContestationPeriod (addContestationPeriod)
import Hydra.Data.Party qualified as OnChain
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addReferenceInputs,
emptyTxBody,
setValidityUpperBound,
unsafeBuildTransaction,
)
import Hydra.Plutus.Orphans ()
Expand Down Expand Up @@ -63,12 +57,12 @@ contestTx ::
Tx
contestTx scriptRegistry vk headId contestationPeriod openVersion snapshot sig (slotNo, _) closedThreadOutput incrementalAction =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityUpperBound slotNo
defaultTxBodyContent
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts [headOutputAfter]
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxValidityUpperBound (TxValidityUpperBound slotNo)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "ContestTx")
where
Snapshot{number, version, utxo, utxoToCommit, utxoToDecommit} = snapshot
Expand Down
15 changes: 5 additions & 10 deletions hydra-tx/src/Hydra/Tx/Decrement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import Hydra.Prelude
import Hydra.Contract.Head qualified as Head
import Hydra.Contract.HeadState qualified as Head
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addReferenceInputs,
emptyTxBody,
unsafeBuildTransaction,
)
import Hydra.Tx.ContestationPeriod (toChain)
Expand Down Expand Up @@ -43,11 +38,11 @@ decrementTx ::
Tx
decrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snapshot signatures =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs (headOutput' : map toTxContext decommitOutputs)
& addExtraRequiredSigners [verificationKeyHash vk]
defaultTxBodyContent
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts (headOutput' : map toTxContext decommitOutputs)
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "DecrementTx")
where
headRedeemer =
Expand Down
Loading

0 comments on commit 8aeeb5d

Please sign in to comment.