Skip to content

Commit

Permalink
Add checkSnapshot to checkDecrement
Browse files Browse the repository at this point in the history
Make sure the mutation of the snapshot number is successfull
  • Loading branch information
locallycompact authored and ch1bo committed Mar 7, 2024
1 parent b809b95 commit 8c302de
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
10 changes: 5 additions & 5 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Decrement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Hydra.Chain.Direct.Contract.Mutation (
SomeMutation (..),
modifyInlineDatum,
replaceParties,
replaceSnapshotNumber,
replaceSnapshotNumberInOpen,
)
import Hydra.Prelude hiding (label)

Expand Down Expand Up @@ -97,7 +97,7 @@ healthySnapshot =
let (utxoToDecommit', utxo) = splitDecommitUTxO healthyUTxO
in Snapshot
{ headId = mkHeadId testPolicyId
, number = healthySnapshotNumber
, number = succ healthySnapshotNumber
, utxo
, confirmed = []
, utxoToDecommit = Just utxoToDecommit'
Expand Down Expand Up @@ -161,9 +161,9 @@ genDecrementMutation (tx, _utxo) =
[ SomeMutation (Just $ toErrorCode ChangedParameters) MutatePartiesInOutput <$> do
mutatedParties <- arbitrary `suchThat` (/= healthyOnChainParties)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceParties mutatedParties) headTxOut
, SomeMutation (Just "H38") MutateSnapshotNumber <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (/= healthySnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumber $ toInteger mutatedSnapshotNumber) headTxOut
, SomeMutation (Just $ toErrorCode SnapshotNumberMismatch) MutateSnapshotNumber <$> do
mutatedSnapshotNumber <- arbitrarySizedNatural `suchThat` (< healthySnapshotNumber)
pure $ ChangeOutput 0 $ modifyInlineDatum (replaceSnapshotNumberInOpen $ toInteger mutatedSnapshotNumber) headTxOut
]
where
headTxOut = fromJust $ txOuts' tx !!? 0
11 changes: 11 additions & 0 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Mutation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,17 @@ replacePolicyInValue original replacement =
| pid == original -> (AssetId replacement an, q)
_ -> (aid, q)

replaceSnapshotNumberInOpen :: Head.SnapshotNumber -> Head.State -> Head.State
replaceSnapshotNumberInOpen snapshotNumber = \case
Head.Open{parties, utxoHash, headId, contestationPeriod} ->
Head.Open
{ Head.parties = parties
, Head.snapshotNumber = snapshotNumber
, Head.utxoHash = utxoHash
, Head.contestationPeriod = contestationPeriod
, Head.headId = headId
}
otherState -> otherState
replaceSnapshotNumber :: Head.SnapshotNumber -> Head.State -> Head.State
replaceSnapshotNumber snapshotNumber = \case
Head.Closed{parties, utxoHash, utxoToDecommitHash, contestationDeadline, headId, contesters, contestationPeriod} ->
Expand Down
16 changes: 12 additions & 4 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ headValidator oldState input ctx =
checkCollectCom ctx (contestationPeriod, parties, headId)
(Initial{parties, headId}, Abort) ->
checkAbort ctx headId parties
(Open{parties, contestationPeriod, headId}, Decrement{}) -> checkDecrement ctx parties contestationPeriod headId
(Open{parties, contestationPeriod, snapshotNumber, headId}, Decrement{signature}) -> checkDecrement ctx parties snapshotNumber contestationPeriod headId signature
(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 @@ -230,28 +230,36 @@ commitDatum input = do
checkDecrement ::
ScriptContext ->
[Party] ->
SnapshotNumber ->
ContestationPeriod ->
CurrencySymbol ->
[Signature] ->
Bool
checkDecrement ctx parties cperiod headPolicyId =
checkDecrement ctx parties snapshotNumber cperiod headPolicyId _signature =
mustNotChangeParameters
&& checkSnapshot
where
(_, parties', cperiod', headId') =
(_, parties', snapshotNumber', cperiod', headId') =
case fromBuiltinData @DatumType $ getDatum (headOutputDatum ctx) of
Just
Open
{ utxoHash
, parties = p
, headId
, contestationPeriod
} -> (utxoHash, p, contestationPeriod, headId)
, snapshotNumber = sn
} -> (utxoHash, p, sn, contestationPeriod, headId)
_ -> traceError $(errorCode WrongStateInOutputDatum)

mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
headId' == headPolicyId
&& parties' == parties
&& cperiod' == cperiod

checkSnapshot =
traceIfFalse $(errorCode SnapshotNumberMismatch) $
snapshotNumber' > snapshotNumber
{-# INLINEABLE checkDecrement #-}

-- | The close validator must verify that:
Expand Down
2 changes: 2 additions & 0 deletions hydra-plutus/src/Hydra/Contract/HeadError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data HeadError
| PartySignatureVerificationFailed
| NotPayingToHead
| NotAllValueCollected
| SnapshotNumberMismatch
deriving stock (Show)

instance ToErrorCode HeadError where
Expand Down Expand Up @@ -85,3 +86,4 @@ instance ToErrorCode HeadError where
PartySignatureVerificationFailed -> "H35"
NotPayingToHead -> "H36"
NotAllValueCollected -> "H37"
SnapshotNumberMismatch -> "H38"

0 comments on commit 8c302de

Please sign in to comment.