Skip to content

Commit

Permalink
Add checkSnapshotNumber to checkClose
Browse files Browse the repository at this point in the history
This makes sure we are closing with new enough snapshot number.

Also adds changes to the spec.
  • Loading branch information
v0d1ch committed May 7, 2024
1 parent ef09456 commit 46e82a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/Chain/Direct/TxTraceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ instance StateModel Model where
snapshotNumber `elem` snapshots && snapshotNumber > latestSnapshot
-- TODO: assert what to decrement still there
(Open{latestSnapshot}, Close{snapshotNumber}) ->
snapshotNumber `elem` snapshots && snapshotNumber > latestSnapshot
snapshotNumber `elem` snapshots && snapshotNumber >= latestSnapshot
(Open{}, Contest{}) -> False
(Closed{latestSnapshot}, Contest{snapshotNumber}) ->
snapshotNumber `elem` snapshots && snapshotNumber > latestSnapshot
Expand Down
16 changes: 11 additions & 5 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ headValidator oldState input ctx =
checkAbort ctx headId parties
(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
(Open{parties, utxoHash = initialUtxoHash, contestationPeriod, headId, snapshotNumber}, Close{signature}) ->
checkClose ctx parties initialUtxoHash signature contestationPeriod headId snapshotNumber
(Closed{parties, snapshotNumber = closedSnapshotNumber, contestationDeadline, contestationPeriod, headId, contesters}, Contest{signature}) ->
checkContest ctx contestationDeadline contestationPeriod parties closedSnapshotNumber signature contesters headId
(Closed{parties, utxoHash, contestationDeadline, headId}, Fanout{numberOfFanoutOutputs}) ->
Expand Down Expand Up @@ -298,8 +298,9 @@ checkClose ::
[Signature] ->
ContestationPeriod ->
CurrencySymbol ->
SnapshotNumber ->
Bool
checkClose ctx parties initialUtxoHash sig cperiod headPolicyId =
checkClose ctx parties initialUtxoHash sig cperiod headPolicyId snapshotNumber =
mustNotMintOrBurn txInfo
&& hasBoundedValidity
&& checkDeadline
Expand All @@ -308,7 +309,12 @@ checkClose ctx parties initialUtxoHash sig cperiod headPolicyId =
&& mustInitializeContesters
&& mustPreserveValue
&& mustNotChangeParameters (parties', parties) (cperiod', cperiod) (headId', headPolicyId)
&& checkSnapshotNumber
where
checkSnapshotNumber =
traceIfFalse $(errorCode TooOldSnapshot) $
closedSnapshotNumber >= snapshotNumber

mustPreserveValue =
traceIfFalse $(errorCode HeadValueIsNotPreserved) $
val === val'
Expand All @@ -326,15 +332,15 @@ checkClose ctx parties initialUtxoHash sig cperiod headPolicyId =
case fromBuiltinData @DatumType $ getDatum (headOutputDatum ctx) of
Just
Closed
{ snapshotNumber
{ snapshotNumber = sn
, utxoHash
, utxoToDecommitHash
, parties = p
, contestationDeadline
, headId
, contesters
, contestationPeriod
} -> (snapshotNumber, utxoHash, utxoToDecommitHash, p, contestationDeadline, contestationPeriod, headId, contesters)
} -> (sn, utxoHash, utxoToDecommitHash, p, contestationDeadline, contestationPeriod, headId, contesters)
_ -> traceError $(errorCode WrongStateInOutputDatum)

checkSnapshot
Expand Down
2 changes: 1 addition & 1 deletion spec/onchain.tex
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ \subsection{Close Transaction}\label{sec:close-tx}

\item Closing snapshot number $s'$ is higher than the currently stored snapshot number $s$
\[
s' > s
s' \geq s
\]
\item Record the closed head state
\[
Expand Down

0 comments on commit 46e82a1

Please sign in to comment.