Skip to content

Commit

Permalink
Use upstream functions for modifying transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
locallycompact committed Dec 9, 2024
1 parent b39d832 commit 8e3aeaa
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 126 deletions.
8 changes: 8 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ program-options
constraints:
quickcheck-instances==0.3.31,
data-default==0.7.1.3

source-repository-package
type: git
location: https://github.com/locallycompact/cardano-api
tag: 1307d2af04091d8a3c7e1d798d2b9cb0de7df0df
--sha256: sha256-GoCYnBrStRxoAniCvo08LScfAeY7b2SmBfCDgxrS74c=
subdir:
cardano-api
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 @@ -73,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, 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 @@ -460,7 +460,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
49 changes: 2 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,8 @@ 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)
addTxInsSpending :: [TxIn] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addTxInsSpending txIns = addTxIns ((,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> txIns)

-- | Mint tokens with given plutus minting script and redeemer.
mintTokens :: ToScriptData redeemer => PlutusScript -> redeemer -> [(AssetName, Quantity)] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
Expand Down Expand Up @@ -98,13 +63,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}
12 changes: 4 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,6 @@ 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,
unsafeBuildTransaction,
)
Expand Down Expand Up @@ -54,11 +50,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]
& addTxExtraKeyWits [verificationKeyHash vk]
where
headWitness =
BuildTxWith $
Expand Down
18 changes: 6 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,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,
setValidityLowerBound,
setValidityUpperBound,
unsafeBuildTransaction,
)
import Hydra.Plutus.Extras.Time (posixFromUTCTime)
Expand Down Expand Up @@ -69,12 +63,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]
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxValidityLowerBound (TxValidityLowerBound startSlotNo)
& setTxValidityUpperBound (TxValidityUpperBound endSlotNo)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CloseTx")
where
OpenThreadOutput
Expand Down
12 changes: 4 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,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,
unsafeBuildTransaction,
)
import Hydra.Plutus (commitValidatorScript)
Expand Down Expand Up @@ -51,10 +47,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]
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CollectComTx")
where
HeadParameters{parties, contestationPeriod} = headParameters
Expand Down
15 changes: 5 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,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,
setValidityUpperBound,
unsafeBuildTransaction,
)
import Hydra.Plutus.Orphans ()
Expand Down Expand Up @@ -62,11 +57,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]
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxValidityUpperBound (TxValidityUpperBound slotNo)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "ContestTx")
where
ClosedThreadOutput
Expand Down
12 changes: 4 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,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,
unsafeBuildTransaction,
)
import Hydra.Tx.ContestationPeriod (toChain)
Expand Down Expand Up @@ -43,10 +39,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)
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "DecrementTx")
where
headRedeemer =
Expand Down
12 changes: 4 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,7 @@ 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,
burnTokens,
setValidityLowerBound,
unsafeBuildTransaction,
)
import Hydra.Tx.ScriptRegistry (ScriptRegistry (..))
Expand All @@ -37,11 +33,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
15 changes: 5 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,6 @@ 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,
unsafeBuildTransaction,
)
import Hydra.Tx.ContestationPeriod (toChain)
Expand Down Expand Up @@ -48,11 +43,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']
& addTxExtraKeyWits [verificationKeyHash vk]
& setTxValidityUpperBound (TxValidityUpperBound upperValiditySlot)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "IncrementTx")
where
headRedeemer =
Expand Down
6 changes: 3 additions & 3 deletions hydra-tx/src/Hydra/Tx/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Hydra.Contract.HeadState qualified as Head
import Hydra.Contract.HeadTokens qualified as HeadTokens
import Hydra.Contract.Initial qualified as Initial
import Hydra.Contract.MintAction (MintAction (..))
import Hydra.Ledger.Cardano.Builder (addOutputs, addVkInputs, mintTokens, unsafeBuildTransaction)
import Hydra.Ledger.Cardano.Builder (addTxInsSpending, mintTokens, unsafeBuildTransaction)
import Hydra.Plutus (initialValidatorScript)
import Hydra.Tx.ContestationPeriod (toChain)
import Hydra.Tx.HeadParameters (HeadParameters (..))
Expand All @@ -29,8 +29,8 @@ initTx ::
initTx networkId seedTxIn participants parameters =
unsafeBuildTransaction $
defaultTxBodyContent
& addVkInputs [seedTxIn]
& addOutputs
& addTxInsSpending [seedTxIn]
& addTxOuts
( mkHeadOutputInitial networkId seedTxIn parameters
: map (mkInitialOutput networkId seedTxIn) participants
)
Expand Down
9 changes: 3 additions & 6 deletions hydra-tx/src/Hydra/Tx/Recover.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import Hydra.Cardano.Api
import Hydra.Contract.Commit qualified as Commit
import Hydra.Contract.Deposit qualified as Deposit
import Hydra.Ledger.Cardano.Builder (
addInputs,
addOutputs,
setValidityLowerBound,
unsafeBuildTransaction,
)
import Hydra.Tx (HeadId, mkHeadId)
Expand All @@ -27,9 +24,9 @@ recoverTx ::
recoverTx depositTxId deposited lowerBoundSlot =
unsafeBuildTransaction $
defaultTxBodyContent
& addInputs recoverInputs
& addOutputs depositOutputs
& setValidityLowerBound lowerBoundSlot
& addTxIns recoverInputs
& addTxOuts depositOutputs
& setTxValidityLowerBound (TxValidityLowerBound lowerBoundSlot)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "RecoverTx")
where
recoverInputs = (,depositWitness) <$> [TxIn depositTxId (TxIx 0)]
Expand Down

0 comments on commit 8e3aeaa

Please sign in to comment.