Skip to content

Commit

Permalink
Use head script in collect tx
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Feb 6, 2023
1 parent 5f8d6c2 commit 93ec0f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Expand Up @@ -374,9 +374,9 @@ collect ::
Tx
collect ctx st = do
let commits = Map.fromList $ fmap tripleToPair initialCommits
in collectComTx networkId ownVerificationKey initialThreadOutput commits headId
in collectComTx networkId scriptRegistry ownVerificationKey initialThreadOutput commits headId
where
ChainContext{networkId, ownVerificationKey} = ctx
ChainContext{networkId, ownVerificationKey, scriptRegistry} = ctx

InitialState
{ initialThreadOutput
Expand Down
25 changes: 16 additions & 9 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Expand Up @@ -221,6 +221,8 @@ mkCommitDatum party headValidatorHash utxo headId =
-- i.e. driving the Head script state.
collectComTx ::
NetworkId ->
-- | Published Hydra scripts to reference.
ScriptRegistry ->
-- | Party who's authorizing this transaction
VerificationKey PaymentKey ->
-- | Everything needed to spend the Head state-machine output.
Expand All @@ -231,10 +233,11 @@ collectComTx ::
-- | Head id
HeadId ->
Tx
collectComTx networkId vk initialThreadOutput commits headId =
collectComTx networkId scriptRegistry vk initialThreadOutput commits headId =
unsafeBuildTransaction $
emptyTxBody
& addInputs ((headInput, headWitness) : (mkCommit <$> Map.toList commits))
& addReferenceInputs [commitScriptRef, headScriptRef]
& addOutputs [headOutput]
& addExtraRequiredSigners [verificationKeyHash vk]
where
Expand All @@ -243,14 +246,14 @@ collectComTx networkId vk initialThreadOutput commits headId =
(headInput, initialHeadOutput, ScriptDatumForTxIn -> headDatumBefore)
, initialParties
, initialContestationPeriod
} =
initialThreadOutput
} = initialThreadOutput
headWitness =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness headScript headDatumBefore headRedeemer
headScript =
fromPlutusScript @PlutusScriptV2 Head.validatorScript
headRedeemer =
toScriptData Head.CollectCom
BuildTxWith $
ScriptWitness scriptWitnessCtx $
mkScriptReference headScriptRef headScript headDatumBefore headRedeemer
headScript = fromPlutusScript @PlutusScriptV2 Head.validatorScript
headScriptRef = fst (headReference scriptRegistry)
headRedeemer = toScriptData Head.CollectCom
headOutput =
TxOut
(mkScriptAddress @PlutusScriptV2 networkId headScript)
Expand Down Expand Up @@ -280,7 +283,11 @@ collectComTx networkId vk initialThreadOutput commits headId =
, mkCommitWitness commitDatum
)
mkCommitWitness (ScriptDatumForTxIn -> commitDatum) =
BuildTxWith $ ScriptWitness scriptWitnessCtx $ mkScriptWitness commitScript commitDatum commitRedeemer
BuildTxWith $
ScriptWitness scriptWitnessCtx $
mkScriptReference commitScriptRef commitScript commitDatum commitRedeemer
commitScriptRef =
fst (commitReference scriptRegistry)
commitValue =
mconcat $ txOutValue . fst <$> Map.elems commits
commitScript =
Expand Down
4 changes: 4 additions & 0 deletions hydra-node/test/Hydra/Chain/Direct/Contract/CollectCom.hs
Expand Up @@ -22,6 +22,7 @@ import Hydra.Chain.Direct.Fixture (
testPolicyId,
testSeedInput,
)
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry)
import Hydra.Chain.Direct.Tx (
InitialThreadOutput (..),
assetNameFromVerificationKey,
Expand Down Expand Up @@ -60,11 +61,14 @@ healthyCollectComTx =
tx =
collectComTx
testNetworkId
scriptRegistry
somePartyCardanoVerificationKey
initialThreadOutput
((txOut &&& scriptData) <$> healthyCommits)
(mkHeadId testPolicyId)

scriptRegistry = genScriptRegistry `generateWith` 42

somePartyCardanoVerificationKey = flip generateWith 42 $ do
genForParty genVerificationKey <$> elements healthyParties

Expand Down

0 comments on commit 93ec0f8

Please sign in to comment.