Skip to content

Commit

Permalink
Refactor the decrement validator
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed May 7, 2024
1 parent 203e782 commit 30f0568
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,22 @@ checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevS
&& mustBeSignedByParticipant ctx prevHeadId
&& mustPreserveValue
where
mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
prevHeadId == nextHeadId
&& prevParties == nextParties
&& prevCperiod == nextCperiod

checkSnapshot =
traceIfFalse $(errorCode SnapshotNumberMismatch) $
nextSnapshotNumber > prevSnapshotNumber

checkSnapshotSignature =
verifySnapshotSignature nextParties nextHeadId nextSnapshotNumber nextUtxoHash decommitUtxoHash signature

mustPreserveValue =
traceIfFalse $(errorCode HeadValueIsNotPreserved) $
headInValue === headOutValue
-- NOTE: head output + whatever is decommitted needs to be equal to the head input.
headOutValue = txOutValue $ head $ txInfoOutputs txInfo <> decommitOutputs

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

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 All @@ -269,18 +276,15 @@ checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevS
} -> (utxoHash, p, sn, contestationPeriod, headId)
_ -> traceError $(errorCode WrongStateInOutputDatum)

mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
prevHeadId == nextHeadId
&& prevParties == nextParties
&& prevCperiod == nextCperiod
-- NOTE: head output + whatever is decommitted needs to be equal to the head input.
-- headOutValue = foldMap txOutValue outputs

checkSnapshot =
traceIfFalse $(errorCode SnapshotNumberMismatch) $
nextSnapshotNumber > prevSnapshotNumber
headOutValue = txOutValue $ head outputs
headInValue = maybe mempty (txOutValue . txInInfoResolved) $ findOwnInput ctx

checkSnapshotSignature =
verifySnapshotSignature nextParties nextHeadId nextSnapshotNumber nextUtxoHash decommitUtxoHash signature
decommitOutputs = take numberOfDecommitOutputs (tail outputs)

outputs = txInfoOutputs txInfo
{-# INLINEABLE checkDecrement #-}

-- | The close validator must verify that:
Expand Down

0 comments on commit 30f0568

Please sign in to comment.