Skip to content

Commit

Permalink
Simplify mustContinueHeadWith
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Feb 6, 2023
1 parent 1476b00 commit b6f0c1e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion hydra-plutus/src/Hydra/Contract/Commit.hs
Expand Up @@ -103,7 +103,7 @@ validator (_party, _headScriptHash, _commit, headId) r ctx =
headOutputValue =
case txInfoOutputs (scriptContextTxInfo ctx) of
[] -> mempty
(headOutput : _) -> txOutValue headOutput
(headOutput : _otherOutputs) -> txOutValue headOutput

compiledValidator :: CompiledCode ValidatorType
compiledValidator =
Expand Down
29 changes: 10 additions & 19 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Expand Up @@ -475,27 +475,18 @@ findParticipationTokens headCurrency (Value val) =

mustContinueHeadWith :: ScriptContext -> Address -> Integer -> Datum -> Bool
mustContinueHeadWith ScriptContext{scriptContextTxInfo = txInfo} headAddress changeValue datum =
checkOutputDatumAndValue [] (txInfoOutputs txInfo)
case txInfoOutputs txInfo of
[] -> traceError "no continuing head output"
[headOutput] -> checkHeadOutput headOutput
[headOutput, changeOutput] ->
checkHeadOutput headOutput
&& traceIfFalse "change value does not match" (lovelaceValue changeValue == txOutValue changeOutput)
_moreThanTwoOutputs -> traceError "more than 2 outputs"
where
lovelaceValue = assetClassValue (assetClass adaSymbol adaToken)
checkOutputDatumAndValue xs = \case
[] ->
traceError "no continuing head output"
(o : rest)
| txOutAddress o == headAddress ->
traceIfFalse "wrong output head datum" (findTxOutDatum txInfo o == datum)
&& checkOutputValue (xs <> rest)
(o : rest) ->
checkOutputDatumAndValue (o : xs) rest

checkOutputValue = \case
[] ->
True
[o]
| txOutAddress o /= headAddress ->
txOutValue o == lovelaceValue changeValue
_ ->
traceError "more than 2 outputs"
checkHeadOutput headOutput =
traceIfFalse "first output should be head address" (txOutAddress headOutput == headAddress)
&& traceIfFalse "wrong output head datum" (findTxOutDatum txInfo headOutput == datum)
{-# INLINEABLE mustContinueHeadWith #-}

continuingDatum :: ScriptContext -> Datum
Expand Down

0 comments on commit b6f0c1e

Please sign in to comment.