Skip to content

Commit

Permalink
Add number of decommit outputs to the Decrement redeemer
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed May 7, 2024
1 parent 003b362 commit 203e782
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
8 changes: 7 additions & 1 deletion hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,13 @@ decrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snap
& addOutputs (headOutput' : map toTxContext (maybe [] toList utxoToDecommit))
& addExtraRequiredSigners [verificationKeyHash vk]
where
headRedeemer = toScriptData $ Head.Decrement (toPlutusSignatures signatures)
headRedeemer =
toScriptData $
Head.Decrement
{ signature = toPlutusSignatures signatures
, numberOfDecommitOutputs =
fromIntegral $ length $ maybe [] toList utxoToDecommit
}
utxoHash = toBuiltin $ hashUTxO @Tx utxo

HeadParameters{parties, contestationPeriod} = headParameters
Expand Down
19 changes: 10 additions & 9 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Decrement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ healthyUTxO = adaOnly <$> generateWith (genUTxOSized 3) 42

healthyDatum :: Head.State
healthyDatum =
Head.Open
{ utxoHash = toBuiltin $ hashUTxO @Tx healthyUTxO
, parties = healthyOnChainParties
, contestationPeriod = toChain healthyContestationPeriod
, snapshotNumber = toInteger healthySnapshotNumber
, headId = toPlutusCurrencySymbol testPolicyId
}
let (_utxoToDecommit', utxo) = splitDecommitUTxO healthyUTxO
in Head.Open
{ utxoHash = toBuiltin $ hashUTxO @Tx utxo
, parties = healthyOnChainParties
, contestationPeriod = toChain healthyContestationPeriod
, snapshotNumber = toInteger healthySnapshotNumber
, headId = toPlutusCurrencySymbol testPolicyId
}

data DecrementMutation
= -- | Ensures parties do not change between head input datum and head output
Expand All @@ -160,7 +161,7 @@ data DecrementMutation
deriving stock (Generic, Show, Enum, Bounded)

genDecrementMutation :: (Tx, UTxO) -> Gen SomeMutation
genDecrementMutation (tx, _utxo) =
genDecrementMutation (tx, utxo) =
oneof
[ SomeMutation (Just $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
Expand All @@ -169,7 +170,7 @@ genDecrementMutation (tx, _utxo) =
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (< healthySnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumberInOpen $ toInteger mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) SnapshotSignatureInvalid . ChangeHeadRedeemer <$> do
Head.Decrement . toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx)))
Head.Decrement . toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx))) <*> pure (fromIntegral $ length utxo - 1)
, SomeMutation (Just $ toErrorCode SignerIsNotAParticipant) MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey `suchThat` (/= somePartyCardanoVerificationKey)
pure $ ChangeRequiredSigners [newSigner]
Expand Down
9 changes: 5 additions & 4 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ headValidator oldState input ctx =
checkCollectCom ctx (contestationPeriod, parties, headId)
(Initial{parties, headId}, Abort) ->
checkAbort ctx headId parties
(Open{parties, contestationPeriod, snapshotNumber, headId}, Decrement{signature}) ->
checkDecrement ctx parties snapshotNumber contestationPeriod headId signature
(Open{parties, contestationPeriod, snapshotNumber, headId}, Decrement{signature, numberOfDecommitOutputs}) ->
checkDecrement ctx parties snapshotNumber contestationPeriod headId signature numberOfDecommitOutputs
(Open{parties, utxoHash = initialUtxoHash, contestationPeriod, headId}, Close{signature}) ->
checkClose ctx parties initialUtxoHash signature contestationPeriod headId
(Closed{parties, snapshotNumber = closedSnapshotNumber, contestationDeadline, contestationPeriod, headId, contesters}, Contest{signature}) ->
Expand Down Expand Up @@ -235,8 +235,9 @@ checkDecrement ::
ContestationPeriod ->
CurrencySymbol ->
[Signature] ->
Integer ->
Bool
checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevSnapshotNumber prevCperiod prevHeadId signature =
checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevSnapshotNumber prevCperiod prevHeadId signature numberOfDecommitOutputs =
mustNotChangeParameters
&& checkSnapshot
&& checkSnapshotSignature
Expand All @@ -251,7 +252,7 @@ checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevS

headInValue = maybe mempty (txOutValue . txInInfoResolved) $ findOwnInput ctx

decommitOutputs = tail (txInfoOutputs txInfo)
decommitOutputs = take numberOfDecommitOutputs (tail $ txInfoOutputs txInfo)

-- NOTE: we always assume Head output is the first one so we pick all other
-- outputs of a decommit tx to calculate the expected hash.
Expand Down
1 change: 1 addition & 0 deletions hydra-plutus/src/Hydra/Contract/HeadState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ data Input
= CollectCom
| Decrement
{ signature :: [Signature]
, numberOfDecommitOutputs :: Integer
}
| Close
{ signature :: [Signature]
Expand Down

0 comments on commit 203e782

Please sign in to comment.