Skip to content

Commit

Permalink
Match nomenclature from Cardano.Api.Tx.Body and use upstream function…
Browse files Browse the repository at this point in the history
…s where possibly
  • Loading branch information
locallycompact committed Dec 8, 2024
1 parent b2103f4 commit 2b929b5
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 126 deletions.
4 changes: 2 additions & 2 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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, mkSimpleTx, mkTransferTx, unsafeBuildTransaction)
import Hydra.Ledger.Cardano (addTxIns, 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 @@ -459,7 +459,7 @@ singlePartyCommitsScriptBlueprint tracer workDir node hydraScriptsTxId =
let spendingTx =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs [(scriptIn, scriptWitness)]
& addTxIns [(scriptIn, scriptWitness)]
pure
( Aeson.object
[ "blueprintTx" .= spendingTx
Expand Down
8 changes: 4 additions & 4 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, unsafeBuildTransaction)
import Hydra.Ledger.Cardano.Builder (addTxIns, addTxInsReference, addTxInsSpending, unsafeBuildTransaction)
import Hydra.Ledger.Cardano.Evaluate (propTransactionEvaluates)
import Hydra.Tx.BlueprintTx (CommitBlueprintTx (..))
import Hydra.Tx.Commit (commitTx)
Expand Down Expand Up @@ -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
96 changes: 49 additions & 47 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,42 +33,55 @@ data InvalidTransactionException = InvalidTransactionException

instance Exception InvalidTransactionException

-- | 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)
addTxIns :: TxIns build -> TxBodyContent build -> TxBodyContent build
addTxIns txIns = modTxIns (<> txIns)

addTxInsSpending :: [TxIn] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addTxInsSpending txIns = addTxIns ((,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> txIns)

modTxInsReference :: (TxInsReference -> TxInsReference) -> TxBodyContent build -> TxBodyContent build
modTxInsReference f txBodyContent = txBodyContent{txInsReference = f (txInsReference txBodyContent)}

addTxInsReference :: [TxIn] -> TxBodyContent build -> TxBodyContent build
addTxInsReference txInsReference =
modTxInsReference
( \case
TxInsReferenceNone -> TxInsReference txInsReference
TxInsReference xs -> TxInsReference (xs <> txInsReference)
)

addTxInReference :: TxIn -> TxBodyContent build -> TxBodyContent build
addTxInReference txInReference = addTxInsReference [txInReference]

addTxOuts :: [TxOut CtxTx] -> TxBodyContent build -> TxBodyContent build
addTxOuts txOuts = modTxOuts (<> txOuts)

modTxInsCollateral :: (TxInsCollateral -> TxInsCollateral) -> TxBodyContent build -> TxBodyContent build
modTxInsCollateral f txBodyContent = txBodyContent{txInsCollateral = f (txInsCollateral txBodyContent)}

addTxInsCollateral :: [TxIn] -> TxBodyContent build -> TxBodyContent build
addTxInsCollateral txInsCollateral =
modTxInsCollateral
( \case
TxInsCollateralNone -> TxInsCollateral txInsCollateral
TxInsCollateral xs -> TxInsCollateral (xs <> txInsCollateral)
)

addTxInCollateral :: TxIn -> TxBodyContent build -> TxBodyContent build
addTxInCollateral txInCollateral = addTxInsCollateral [txInCollateral]

modExtraKeyWits :: (TxExtraKeyWitnesses -> TxExtraKeyWitnesses) -> TxBodyContent build -> TxBodyContent build
modExtraKeyWits f txBodyContent = txBodyContent{txExtraKeyWits = f (txExtraKeyWits txBodyContent)}

addExtraKeyWits :: [Hash PaymentKey] -> TxBodyContent build -> TxBodyContent build
addExtraKeyWits vks =
modExtraKeyWits
( \case
TxExtraKeyWitnessesNone ->
TxExtraKeyWitnesses vks
TxExtraKeyWitnesses vks' ->
TxExtraKeyWitnesses (vks' <> vks)
)

-- | Mint tokens with given plutus minting script and redeemer.
mintTokens :: ToScriptData redeemer => PlutusScript -> redeemer -> [(AssetName, Quantity)] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
Expand Down Expand Up @@ -100,13 +112,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}
16 changes: 8 additions & 8 deletions hydra-tx/src/Hydra/Tx/Abort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ 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,
addExtraKeyWits,
addTxIns,
addTxInsReference,
addTxOuts,
burnTokens,
unsafeBuildTransaction,
)
Expand Down Expand Up @@ -54,11 +54,11 @@ abortTx committedUTxO scriptRegistry vk (headInput, initialHeadOutput) headToken
Right $
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs ((headInput, headWitness) : initialInputs <> commitInputs)
& addReferenceInputs ([headScriptRef, initialScriptRef] <> [commitScriptRef | not $ null commitInputs])
& addOutputs reimbursedOutputs
& addTxIns ((headInput, headWitness) : initialInputs <> commitInputs)
& addTxInsReference ([headScriptRef, initialScriptRef] <> [commitScriptRef | not $ null commitInputs])
& addTxOuts reimbursedOutputs
& burnTokens headTokenScript Burn headTokens
& addExtraRequiredSigners [verificationKeyHash vk]
& addExtraKeyWits [verificationKeyHash vk]
where
headWitness =
BuildTxWith $
Expand Down
22 changes: 10 additions & 12 deletions hydra-tx/src/Hydra/Tx/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ 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,
setValidityLowerBound,
setValidityUpperBound,
addExtraKeyWits,
addTxIns,
addTxInsReference,
addTxOuts,
unsafeBuildTransaction,
)
import Hydra.Plutus.Extras.Time (posixFromUTCTime)
Expand Down Expand Up @@ -69,12 +67,12 @@ closeTx ::
closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endSlotNo, utcTime) openThreadOutput =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityLowerBound startSlotNo
& setValidityUpperBound endSlotNo
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts [headOutputAfter]
& addExtraKeyWits [verificationKeyHash vk]
& setTxValidityLowerBound (TxValidityLowerBound startSlotNo)
& setTxValidityUpperBound (TxValidityUpperBound endSlotNo)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CloseTx")
where
OpenThreadOutput
Expand Down
16 changes: 8 additions & 8 deletions hydra-tx/src/Hydra/Tx/CollectCom.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ 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,
addExtraKeyWits,
addTxIns,
addTxInsReference,
addTxOuts,
unsafeBuildTransaction,
)
import Hydra.Plutus (commitValidatorScript)
Expand Down Expand Up @@ -51,10 +51,10 @@ collectComTx ::
collectComTx networkId scriptRegistry vk headId headParameters (headInput, initialHeadOutput) commits utxoToCollect =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs ((headInput, headWitness) : (mkCommit <$> Map.keys commits))
& addReferenceInputs [commitScriptRef, headScriptRef]
& addOutputs [headOutput]
& addExtraRequiredSigners [verificationKeyHash vk]
& addTxIns ((headInput, headWitness) : (mkCommit <$> Map.keys commits))
& addTxInsReference [commitScriptRef, headScriptRef]
& addTxOuts [headOutput]
& addExtraKeyWits [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CollectComTx")
where
HeadParameters{parties, contestationPeriod} = headParameters
Expand Down
19 changes: 9 additions & 10 deletions hydra-tx/src/Hydra/Tx/Contest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ 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,
setValidityUpperBound,
addExtraKeyWits,
addTxIns,
addTxInsReference,
addTxOuts,
unsafeBuildTransaction,
)
import Hydra.Plutus.Orphans ()
Expand Down Expand Up @@ -62,11 +61,11 @@ contestTx ::
contestTx scriptRegistry vk headId contestationPeriod openVersion Snapshot{number, utxo, utxoToDecommit, version} sig (slotNo, _) closedThreadOutput =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityUpperBound slotNo
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts [headOutputAfter]
& addExtraKeyWits [verificationKeyHash vk]
& setTxValidityUpperBound (TxValidityUpperBound slotNo)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "ContestTx")
where
ClosedThreadOutput
Expand Down
16 changes: 8 additions & 8 deletions hydra-tx/src/Hydra/Tx/Decrement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ 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,
addExtraKeyWits,
addTxIns,
addTxInsReference,
addTxOuts,
unsafeBuildTransaction,
)
import Hydra.Tx.ContestationPeriod (toChain)
Expand Down Expand Up @@ -43,10 +43,10 @@ decrementTx ::
decrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snapshot signatures =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs (headOutput' : map toTxContext decommitOutputs)
& addExtraRequiredSigners [verificationKeyHash vk]
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts (headOutput' : map toTxContext decommitOutputs)
& addExtraKeyWits [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "DecrementTx")
where
headRedeemer =
Expand Down
15 changes: 7 additions & 8 deletions hydra-tx/src/Hydra/Tx/Fanout.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import Hydra.Contract.Head qualified as Head
import Hydra.Contract.HeadState qualified as Head
import Hydra.Contract.MintAction (MintAction (..))
import Hydra.Ledger.Cardano.Builder (
addInputs,
addOutputs,
addReferenceInputs,
addTxIns,
addTxInsReference,
addTxOuts,
burnTokens,
setValidityLowerBound,
unsafeBuildTransaction,
)
import Hydra.Tx.ScriptRegistry (ScriptRegistry (..))
Expand All @@ -37,11 +36,11 @@ fanoutTx ::
fanoutTx scriptRegistry utxo utxoToDecommit (headInput, headOutput) deadlineSlotNo headTokenScript =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs [(headInput, headWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs (orderedTxOutsToFanout <> orderedTxOutsToDecommit)
& addTxIns [(headInput, headWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts (orderedTxOutsToFanout <> orderedTxOutsToDecommit)
& burnTokens headTokenScript Burn headTokens
& setValidityLowerBound (deadlineSlotNo + 1)
& setTxValidityLowerBound (TxValidityLowerBound $ deadlineSlotNo + 1)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "FanoutTx")
where
headWitness =
Expand Down
19 changes: 9 additions & 10 deletions hydra-tx/src/Hydra/Tx/Increment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import Hydra.Contract.Deposit qualified as Deposit
import Hydra.Contract.Head qualified as Head
import Hydra.Contract.HeadState qualified as Head
import Hydra.Ledger.Cardano.Builder (
addExtraRequiredSigners,
addInputs,
addOutputs,
addReferenceInputs,
setValidityUpperBound,
addExtraKeyWits,
addTxIns,
addTxInsReference,
addTxOuts,
unsafeBuildTransaction,
)
import Hydra.Tx.ContestationPeriod (toChain)
Expand Down Expand Up @@ -48,11 +47,11 @@ incrementTx ::
incrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snapshot depositScriptUTxO upperValiditySlot =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs [(headInput, headWitness), (depositIn, depositWitness)]
& addReferenceInputs [headScriptRef]
& addOutputs [headOutput']
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityUpperBound upperValiditySlot
& addTxIns [(headInput, headWitness), (depositIn, depositWitness)]
& addTxInsReference [headScriptRef]
& addTxOuts [headOutput']
& addExtraKeyWits [verificationKeyHash vk]
& setTxValidityUpperBound (TxValidityUpperBound upperValiditySlot)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "IncrementTx")
where
headRedeemer =
Expand Down
Loading

0 comments on commit 2b929b5

Please sign in to comment.