Skip to content

Commit

Permalink
Refactor redeemer construction for close/contest
Browse files Browse the repository at this point in the history
Also make sure that tx-trace tests produce valid close/contest snapshots
with respect to decommits.
  • Loading branch information
v0d1ch committed Nov 15, 2024
1 parent 16b2e10 commit 4ec3d9c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 69 deletions.
8 changes: 8 additions & 0 deletions hydra-node/test/Hydra/Chain/Direct/TxTraceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ instance StateModel Model where
else
snapshot.version `elem` (currentVersion : [currentVersion - 1 | currentVersion > 0])
)
&& (not (null snapshot.toDecommit) || (snapshot.version == currentVersion))
where
Model{utxoInHead = initialUTxOInHead} = initialState
Contest{actor, snapshot} ->
Expand All @@ -391,6 +392,9 @@ instance StateModel Model where
&& actor `notElem` alreadyContested
&& snapshot.version `elem` (currentVersion : [currentVersion - 1 | currentVersion > 0])
&& snapshot.number > closedSnapshotNumber
&& ( not (null snapshot.toDecommit)
|| (snapshot.version == currentVersion)
)
Fanout{utxo, deltaUTxO} ->
headState == Closed
&& utxo == utxoInHead
Expand All @@ -415,9 +419,13 @@ instance StateModel Model where
&& ( snapshot.number == 0
|| snapshot.version `elem` (currentVersion : [currentVersion - 1 | currentVersion > 0])
)
&& (not (null snapshot.toDecommit) || (snapshot.version == currentVersion))
Contest{snapshot} ->
headState == Closed
&& snapshot `elem` knownSnapshots
&& ( not (null snapshot.toDecommit)
|| (snapshot.version == currentVersion)
)
Fanout{} ->
headState == Closed

Expand Down
4 changes: 2 additions & 2 deletions hydra-plutus/scripts/mHead.plutus

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions hydra-plutus/scripts/vHead.plutus

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ checkContest ctx closedDatum redeemer =
traceIfFalse $(errorCode FailedContestUnusedDec) $
verifySnapshotSignature
parties
(headId, version - 1, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
(headId, version, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
signature
ContestUnusedInc{signature, alreadyCommittedUTxOHash} ->
traceIfFalse $(errorCode FailedContestUnusedInc) $
Expand Down
44 changes: 17 additions & 27 deletions hydra-tx/src/Hydra/Tx/Close.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DuplicateRecordFields #-}

module Hydra.Tx.Close where
Expand Down Expand Up @@ -100,21 +99,22 @@ closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endS
closeRedeemer =
case confirmedSnapshot of
InitialSnapshot{} -> Head.CloseInitial
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}
| version == openVersion
, isNothing utxoToCommit
, isNothing utxoToDecommit ->
Head.CloseAny{signature = toPlutusSignatures signatures}
| otherwise ->
ConfirmedSnapshot{signatures, snapshot = Snapshot{version, utxoToCommit, utxoToDecommit}} ->
if version == openVersion
then
if
| isJust utxoToCommit ->
Head.CloseUnusedInc
{ signature = toPlutusSignatures signatures
, alreadyCommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToCommit
}
| isJust utxoToDecommit ->
Head.CloseUnusedDec{signature = toPlutusSignatures signatures}
| isNothing utxoToCommit
, isNothing utxoToDecommit ->
Head.CloseAny{signature = toPlutusSignatures signatures}
| otherwise -> error "closeTx: unexpected to have both utxo to commit and decommit in the same snapshot."
else
-- NOTE: This will only work for version == openVersion - 1
case (isJust utxoToCommit, isJust utxoToDecommit) of
(True, False) ->
Expand All @@ -126,17 +126,7 @@ closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endS
{ signature = toPlutusSignatures signatures
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
}
(False, False) ->
if version /= openVersion
then
-- TODO: why CloseUnusedDec? we could also put CloseUsedInc
-- since there is no logic. We would have to know what
-- happened base on version and what else?
Head.CloseUsedDec
{ signature = toPlutusSignatures signatures
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
}
else Head.CloseAny{signature = toPlutusSignatures signatures}
(False, False) -> error $ "closeTx: both commit and decommit utxo empty but version not matching! snapshot version: " <> show version <> " open version: " <> show openVersion
-- TODO: can we get rid of these errors by modelling what we expect differently?
(True, True) -> error "closeTx: unexpected to have both utxo to commit and decommit in the same snapshot."

Expand Down
63 changes: 26 additions & 37 deletions hydra-tx/src/Hydra/Tx/Contest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,44 +130,33 @@ contestTx scriptRegistry vk headId contestationPeriod openVersion snapshot sig (

setContestRedeemer :: Snapshot Tx -> SnapshotVersion -> MultiSignature (Snapshot Tx) -> Head.ContestRedeemer
setContestRedeemer Snapshot{version, utxoToCommit, utxoToDecommit} openVersion sig =
if
| version == openVersion
, isJust utxoToDecommit ->
Head.ContestUnusedDec
{ signature = toPlutusSignatures sig
}
| version == openVersion
, isJust utxoToCommit ->
Head.ContestUnusedInc
{ signature = toPlutusSignatures sig
, alreadyCommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToCommit
}
| version == openVersion
, isNothing utxoToCommit
, isNothing utxoToDecommit ->
Head.ContestCurrent
{ signature = toPlutusSignatures sig
}
| otherwise ->
case (isJust utxoToCommit, isJust utxoToDecommit) of
(True, False) ->
Head.ContestUsedInc
if version == openVersion
then
if
| isJust utxoToDecommit ->
Head.ContestUnusedDec
{ signature = toPlutusSignatures sig
}
(False, True) ->
Head.ContestUsedDec
| isJust utxoToCommit ->
Head.ContestUnusedInc
{ signature = toPlutusSignatures sig
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
, alreadyCommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToCommit
}
(False, False) ->
if version /= openVersion
then
-- TODO: why ContestUnusedDec? we could also put ContestUsedInc
-- since there is no logic. We would have to know what
-- happened base on version and what else?
Head.ContestUsedDec
{ signature = toPlutusSignatures sig
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
}
else Head.ContestCurrent{signature = toPlutusSignatures sig}
(True, True) -> error "contestTx: unexpected to have both utxo to commit and decommit in the same snapshot."
| isNothing utxoToCommit
, isNothing utxoToDecommit ->
Head.ContestCurrent
{ signature = toPlutusSignatures sig
}
| otherwise -> error "contestTx: unexpected to have both utxo to commit and decommit in the same snapshot."
else case (isJust utxoToCommit, isJust utxoToDecommit) of
(True, False) ->
Head.ContestUsedInc
{ signature = toPlutusSignatures sig
}
(False, True) ->
Head.ContestUsedDec
{ signature = toPlutusSignatures sig
, alreadyDecommittedUTxOHash = toBuiltin . hashUTxO $ fromMaybe mempty utxoToDecommit
}
(False, False) -> error $ "contestTx: both commit and decommit utxo empty but version not the same! snapshot version: " <> show version <> " open version: " <> show openVersion
(True, True) -> error "contestTx: unexpected to have both utxo to commit and decommit in the same snapshot."

0 comments on commit 4ec3d9c

Please sign in to comment.