Skip to content

Commit

Permalink
Add new close redeemer types
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Oct 18, 2024
1 parent cb1d660 commit a6ef190
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
17 changes: 15 additions & 2 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -415,19 +415,32 @@ checkClose ctx openBefore redeemer =
version == 0
&& snapshotNumber' == 0
&& utxoHash' == initialUtxoHash
CloseUnused{signature} ->
CloseUnusedDec{signature} ->
traceIfFalse $(errorCode FailedCloseCurrent) $
verifySnapshotSignature
parties
(headId, version, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
signature
CloseUsed{signature, alreadyDecommittedUTxOHash} ->
CloseUsedDec{signature, alreadyDecommittedUTxOHash} ->
traceIfFalse $(errorCode FailedCloseOutdated) $
deltaUTxOHash' == emptyHash
&& verifySnapshotSignature
parties
(headId, version - 1, snapshotNumber', utxoHash', emptyHash, alreadyDecommittedUTxOHash)
signature
CloseUnusedInc{signature, alreadyCommittedUTxOHash} ->
traceIfFalse $(errorCode FailedCloseCurrent) $
verifySnapshotSignature
parties
(headId, version, snapshotNumber', utxoHash', alreadyCommittedUTxOHash, emptyHash)
signature
CloseUsedInc{signature} ->
traceIfFalse $(errorCode FailedCloseOutdated) $
deltaUTxOHash' == emptyHash
&& verifySnapshotSignature
parties
(headId, version - 1, snapshotNumber', utxoHash', deltaUTxOHash', emptyHash)
signature

checkDeadline =
traceIfFalse $(errorCode IncorrectClosedContestationDeadline) $
Expand Down
16 changes: 14 additions & 2 deletions hydra-plutus/src/Hydra/Contract/HeadState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,29 @@ data CloseRedeemer
= -- | Intial snapshot is used to close.
CloseInitial
| -- | Closing snapshot refers to the current state version
CloseUnused
CloseUnusedDec
{ signature :: [Signature]
-- ^ Multi-signature of a snapshot ξ
}
| -- | Closing snapshot refers to the previous state version
CloseUsed
CloseUsedDec
{ signature :: [Signature]
-- ^ Multi-signature of a snapshot ξ
, alreadyDecommittedUTxOHash :: Hash
-- ^ UTxO which was already decommitted ηω
}
| -- | Closing snapshot refers to the current state version
CloseUnusedInc
{ signature :: [Signature]
-- ^ Multi-signature of a snapshot ξ
, alreadyCommittedUTxOHash :: Hash
-- ^ UTxO which was already committed ηα
}
| -- | Closing snapshot refers to the previous state version
CloseUsedInc
{ signature :: [Signature]
-- ^ Multi-signature of a snapshot ξ
}
deriving stock (Show, Generic)

PlutusTx.unstableMakeIsData ''CloseRedeemer
Expand Down
32 changes: 24 additions & 8 deletions hydra-tx/src/Hydra/Tx/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,29 @@ closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endS
closeRedeemer =
case confirmedSnapshot of
InitialSnapshot{} -> Head.CloseInitial
ConfirmedSnapshot{signatures, snapshot = Snapshot{version, utxoToDecommit}}
| version == openVersion ->
Head.CloseUnused{signature = toPlutusSignatures signatures}
ConfirmedSnapshot{signatures, snapshot = Snapshot{version, utxoToCommit, utxoToDecommit}}
| version == openVersion
, isJust utxoToCommit ->
Head.CloseUnusedInc{signature = toPlutusSignatures signatures, alreadyCommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToCommit}
| version == openVersion
, isJust utxoToDecommit ->
Head.CloseUnusedDec{signature = toPlutusSignatures signatures}
| otherwise ->
-- NOTE: This will only work for version == openVersion - 1
Head.CloseUsed
{ signature = toPlutusSignatures signatures
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
}
if isJust utxoToCommit
then
Head.CloseUsedInc
{ signature = toPlutusSignatures signatures
}
else
if isJust utxoToDecommit
then
Head.CloseUsedDec
{ signature = toPlutusSignatures signatures
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
}
else
error "closeTx: unexpected snapshot"

headOutputAfter =
modifyTxOutDatum (const headDatumAfter) headOutputBefore
Expand All @@ -123,8 +137,10 @@ closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endS
toBuiltin . hashUTxO . utxo $ getSnapshot confirmedSnapshot
, deltaUTxOHash =
case closeRedeemer of
Head.CloseUnused{} ->
Head.CloseUnusedDec{} ->
toBuiltin . hashUTxO @Tx . fromMaybe mempty . utxoToDecommit $ getSnapshot confirmedSnapshot
Head.CloseUsedInc{} ->
toBuiltin . hashUTxO @Tx . fromMaybe mempty . utxoToCommit $ getSnapshot confirmedSnapshot
_ -> toBuiltin $ hashUTxO @Tx mempty
, parties = openParties
, contestationDeadline
Expand Down
4 changes: 2 additions & 2 deletions hydra-tx/test/Hydra/Tx/Contract/Close/CloseUnused.hs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ genCloseCurrentMutation (tx, _utxo) =
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
signature <- toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx)))
pure $ Head.Close Head.CloseUnused{signature}
pure $ Head.Close Head.CloseUnusedDec{signature}
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateSnapshotNumberButNotSignature <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (> healthyCurrentSnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
Expand Down Expand Up @@ -277,7 +277,7 @@ genCloseCurrentMutation (tx, _utxo) =
( Just $
toScriptData
( Head.Close
Head.CloseUnused
Head.CloseUnusedDec
{ signature = toPlutusSignatures $ healthySignature healthyCurrentSnapshot
}
)
Expand Down
10 changes: 5 additions & 5 deletions hydra-tx/test/Hydra/Tx/Contract/Close/CloseUsed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ genCloseOutdatedMutation (tx, _utxo) =
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
signature <- toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx)))
pure $ Head.Close Head.CloseUnused{signature}
pure $ Head.Close Head.CloseUnusedDec{signature}
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateSnapshotNumberButNotSignature <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (> healthyOutdatedSnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
Expand Down Expand Up @@ -303,7 +303,7 @@ genCloseOutdatedMutation (tx, _utxo) =
( Just $
toScriptData
( Head.Close
Head.CloseUnused
Head.CloseUnusedDec
{ signature =
toPlutusSignatures $
healthySignature healthyOutdatedSnapshot
Expand Down Expand Up @@ -334,7 +334,7 @@ genCloseOutdatedMutation (tx, _utxo) =
mutatedUTxOHash <- genHash `suchThat` (/= healthyUTxOToDecommitHash)
pure $
Head.Close
Head.CloseUsed
Head.CloseUsedDec
{ signature = toPlutusSignatures $ signatures healthyOutdatedConfirmedClosingSnapshot
, alreadyDecommittedUTxOHash = toBuiltin mutatedUTxOHash
}
Expand All @@ -349,15 +349,15 @@ genCloseOutdatedMutation (tx, _utxo) =
signature <- toPlutusSignatures <$> (arbitrary `suchThat` (/= signatures healthyOutdatedConfirmedClosingSnapshot))
pure $
Head.Close
Head.CloseUsed
Head.CloseUsedDec
{ signature
, alreadyDecommittedUTxOHash = toBuiltin healthyUTxOToDecommitHash
}
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateCloseType . ChangeHeadRedeemer <$> do
-- Close redeemer claims whether the snapshot is valid against current
-- or previous version. If we change it then it should cause invalid
-- signature error.
pure $ Head.Close Head.CloseUnused{signature = toPlutusSignatures $ signatures healthyOutdatedConfirmedClosingSnapshot}
pure $ Head.Close Head.CloseUnusedDec{signature = toPlutusSignatures $ signatures healthyOutdatedConfirmedClosingSnapshot}
]
where
genOversizedTransactionValidity = do
Expand Down
4 changes: 2 additions & 2 deletions hydra-tx/test/Hydra/Tx/Contract/ContractSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ spec = parallel $ do
propTransactionEvaluates healthyCloseInitialTx
prop "does not survive random adversarial mutations" $
propMutation healthyCloseInitialTx genCloseInitialMutation
describe "CloseUnused" $ do
describe "CloseUnusedDec" $ do
prop "is healthy" $
propTransactionEvaluates healthyCloseCurrentTx
prop "does not survive random adversarial mutations" $
propMutation healthyCloseCurrentTx genCloseCurrentMutation
describe "CloseUsed" $ do
describe "CloseUsedDec" $ do
prop "is healthy" $
propTransactionEvaluates healthyCloseOutdatedTx
prop "does not survive random adversarial mutations" $
Expand Down

0 comments on commit a6ef190

Please sign in to comment.