Skip to content

Commit

Permalink
Remove DuplicateRecordFields from HeadState and Head and rename fields
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Oct 18, 2024
1 parent a6ef190 commit af16d0f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 121 deletions.
183 changes: 91 additions & 92 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-specialize #-}
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:conservative-optimisation #-}
Expand Down Expand Up @@ -66,10 +65,10 @@ headValidator ::
Bool
headValidator oldState input ctx =
case (oldState, input) of
(Initial{contestationPeriod, parties, headId}, CollectCom) ->
checkCollectCom ctx (contestationPeriod, parties, headId)
(Initial{parties, headId}, Abort) ->
checkAbort ctx headId parties
(Initial{initialContestationPeriod, initialParties, initialHeadId}, CollectCom) ->
checkCollectCom ctx (initialContestationPeriod, initialParties, initialHeadId)
(Initial{initialParties, initialHeadId}, Abort) ->
checkAbort ctx initialHeadId initialParties
(Open openDatum, Increment redeemer) ->
checkIncrement ctx openDatum redeemer
(Open openDatum, Decrement redeemer) ->
Expand Down Expand Up @@ -155,7 +154,7 @@ checkCollectCom ctx@ScriptContext{scriptContextTxInfo = txInfo} (contestationPer
where
mustCollectUtxoHash =
traceIfFalse $(errorCode IncorrectUtxoHash) $
utxoHash == hashPreSerializedCommits collectedCommits
openUtxoHash == hashPreSerializedCommits collectedCommits

mustInitVersion =
traceIfFalse $(errorCode IncorrectVersion) $
Expand All @@ -170,11 +169,11 @@ checkCollectCom ctx@ScriptContext{scriptContextTxInfo = txInfo} (contestationPer
otherValueOut == notCollectedValueIn - txInfoFee txInfo

OpenDatum
{ utxoHash
, parties = parties'
, contestationPeriod = contestationPeriod'
, headId = headId'
, version = version'
{ openUtxoHash
, openParties = parties'
, openContestationPeriod = contestationPeriod'
, openHeadId = headId'
, openVersion = version'
} = decodeHeadOutputOpenDatum ctx

headAddress = getHeadAddress ctx
Expand Down Expand Up @@ -263,14 +262,14 @@ checkIncrement ctx@ScriptContext{scriptContextTxInfo = txInfo} openBefore redeem

headOutValue = foldMap txOutValue $ txInfoOutputs txInfo

IncrementRedeemer{signature, snapshotNumber, increment} = redeemer
IncrementRedeemer{incrementSignature, incrementSnapshotNumber, increment} = redeemer

claimedDepositIsSpent =
traceIfFalse $(errorCode DepositNotSpent) $
depositRef == increment && spendsOutput txInfo (txOutRefId depositRef) (txOutRefIdx depositRef)

checkSnapshotSignature =
verifySnapshotSignature nextParties (nextHeadId, prevVersion, snapshotNumber, nextUtxoHash, depositHash, emptyHash) signature
verifySnapshotSignature nextParties (nextHeadId, prevVersion, incrementSnapshotNumber, nextUtxoHash, depositHash, emptyHash) incrementSignature

mustIncreaseVersion =
traceIfFalse $(errorCode VersionNotIncremented) $
Expand All @@ -281,18 +280,18 @@ checkIncrement ctx@ScriptContext{scriptContextTxInfo = txInfo} openBefore redeem
headInValue <> depositValue == headOutValue

OpenDatum
{ parties = prevParties
, contestationPeriod = prevCperiod
, headId = prevHeadId
, version = prevVersion
{ openParties = prevParties
, openContestationPeriod = prevCperiod
, openHeadId = prevHeadId
, openVersion = prevVersion
} = openBefore

OpenDatum
{ utxoHash = nextUtxoHash
, parties = nextParties
, contestationPeriod = nextCperiod
, headId = nextHeadId
, version = nextVersion
{ openUtxoHash = nextUtxoHash
, openParties = nextParties
, openContestationPeriod = nextCperiod
, openHeadId = nextHeadId
, openVersion = nextVersion
} = decodeHeadOutputOpenDatum ctx
{-# INLINEABLE checkIncrement #-}

Expand All @@ -311,7 +310,7 @@ checkDecrement ctx openBefore redeemer =
&& mustBeSignedByParticipant ctx prevHeadId
where
checkSnapshotSignature =
verifySnapshotSignature nextParties (nextHeadId, prevVersion, snapshotNumber, nextUtxoHash, emptyHash, decommitUtxoHash) signature
verifySnapshotSignature nextParties (nextHeadId, prevVersion, decrementSnapshotNumber, nextUtxoHash, emptyHash, decommitUtxoHash) decrementSignature

mustDecreaseValue =
traceIfFalse $(errorCode HeadValueIsNotPreserved) $
Expand All @@ -323,21 +322,21 @@ checkDecrement ctx openBefore redeemer =

decommitUtxoHash = hashTxOuts decommitOutputs

DecrementRedeemer{signature, snapshotNumber, numberOfDecommitOutputs} = redeemer
DecrementRedeemer{decrementSignature, decrementSnapshotNumber, numberOfDecrementOutputs} = redeemer

OpenDatum
{ parties = prevParties
, contestationPeriod = prevCperiod
, headId = prevHeadId
, version = prevVersion
{ openParties = prevParties
, openContestationPeriod = prevCperiod
, openHeadId = prevHeadId
, openVersion = prevVersion
} = openBefore

OpenDatum
{ utxoHash = nextUtxoHash
, parties = nextParties
, contestationPeriod = nextCperiod
, headId = nextHeadId
, version = nextVersion
{ openUtxoHash = nextUtxoHash
, openParties = nextParties
, openContestationPeriod = nextCperiod
, openHeadId = nextHeadId
, openVersion = nextVersion
} = decodeHeadOutputOpenDatum ctx

-- NOTE: head output + whatever is decommitted needs to be equal to the head input.
Expand All @@ -346,7 +345,7 @@ checkDecrement ctx openBefore redeemer =

-- 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.
decommitOutputs = take numberOfDecommitOutputs (tail outputs)
decommitOutputs = take numberOfDecrementOutputs (tail outputs)

outputs = txInfoOutputs txInfo

Expand All @@ -365,19 +364,19 @@ checkClose ctx openBefore redeemer =
mustNotMintOrBurn txInfo
&& hasBoundedValidity
&& checkDeadline
&& mustBeSignedByParticipant ctx headId
&& mustBeSignedByParticipant ctx openHeadId
&& mustNotChangeVersion
&& mustBeValidSnapshot
&& mustInitializeContesters
&& mustPreserveValue
&& mustNotChangeParameters (parties', parties) (cperiod', cperiod) (headId', headId)
&& mustNotChangeParameters (parties', openParties) (cperiod', cperiod) (headId', openHeadId)
where
OpenDatum
{ parties
, utxoHash = initialUtxoHash
, contestationPeriod = cperiod
, headId
, version
{ openParties
, openUtxoHash = initialUtxoHash
, openContestationPeriod = cperiod
, openHeadId
, openVersion
} = openBefore

mustPreserveValue =
Expand All @@ -393,54 +392,54 @@ checkClose ctx openBefore redeemer =
tMax - tMin <= cp

ClosedDatum
{ snapshotNumber = snapshotNumber'
, utxoHash = utxoHash'
{ closedSnapshotNumber = snapshotNumber'
, closedUtxoHash = utxoHash'
, deltaUTxOHash = deltaUTxOHash'
, parties = parties'
, closedParties = parties'
, contestationDeadline = deadline
, contestationPeriod = cperiod'
, headId = headId'
, closedContestationPeriod = cperiod'
, closedHeadId = headId'
, contesters = contesters'
, version = version'
, closedVersion = version'
} = decodeHeadOutputClosedDatum ctx

mustNotChangeVersion =
traceIfFalse $(errorCode MustNotChangeVersion) $
version' == version
version' == openVersion

mustBeValidSnapshot =
case redeemer of
CloseInitial ->
traceIfFalse $(errorCode FailedCloseInitial) $
version == 0
openVersion == 0
&& snapshotNumber' == 0
&& utxoHash' == initialUtxoHash
CloseUnusedDec{signature} ->
CloseUnusedDec{closeUnusedDecSignature} ->
traceIfFalse $(errorCode FailedCloseCurrent) $
verifySnapshotSignature
parties
(headId, version, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
signature
CloseUsedDec{signature, alreadyDecommittedUTxOHash} ->
openParties
(openHeadId, openVersion, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
closeUnusedDecSignature
CloseUsedDec{closeUsedDecSignature, alreadyDecommittedCloseUTxOHash} ->
traceIfFalse $(errorCode FailedCloseOutdated) $
deltaUTxOHash' == emptyHash
&& verifySnapshotSignature
parties
(headId, version - 1, snapshotNumber', utxoHash', emptyHash, alreadyDecommittedUTxOHash)
signature
CloseUnusedInc{signature, alreadyCommittedUTxOHash} ->
openParties
(openHeadId, openVersion - 1, snapshotNumber', utxoHash', emptyHash, alreadyDecommittedCloseUTxOHash)
closeUsedDecSignature
CloseUnusedInc{closeUnusedIncSignature, alreadyCommittedUTxOHash} ->
traceIfFalse $(errorCode FailedCloseCurrent) $
verifySnapshotSignature
parties
(headId, version, snapshotNumber', utxoHash', alreadyCommittedUTxOHash, emptyHash)
signature
CloseUsedInc{signature} ->
openParties
(openHeadId, openVersion, snapshotNumber', utxoHash', alreadyCommittedUTxOHash, emptyHash)
closeUnusedIncSignature
CloseUsedInc{closeUsedIncSignature} ->
traceIfFalse $(errorCode FailedCloseOutdated) $
deltaUTxOHash' == emptyHash
&& verifySnapshotSignature
parties
(headId, version - 1, snapshotNumber', utxoHash', deltaUTxOHash', emptyHash)
signature
openParties
(openHeadId, openVersion - 1, snapshotNumber', utxoHash', deltaUTxOHash', emptyHash)
closeUsedIncSignature

checkDeadline =
traceIfFalse $(errorCode IncorrectClosedContestationDeadline) $
Expand Down Expand Up @@ -476,12 +475,12 @@ checkContest ctx closedDatum redeemer =
&& mustNotChangeVersion
&& mustBeNewer
&& mustBeValidSnapshot
&& mustBeSignedByParticipant ctx headId
&& mustBeSignedByParticipant ctx closedHeadId
&& checkSignedParticipantContestOnlyOnce
&& mustBeWithinContestationPeriod
&& mustUpdateContesters
&& mustPushDeadline
&& mustNotChangeParameters (parties', parties) (contestationPeriod', contestationPeriod) (headId', headId)
&& mustNotChangeParameters (parties', closedParties) (contestationPeriod', closedContestationPeriod) (headId', closedHeadId)
&& mustPreserveValue
where
mustPreserveValue =
Expand All @@ -494,27 +493,27 @@ checkContest ctx closedDatum redeemer =

mustBeNewer =
traceIfFalse $(errorCode TooOldSnapshot) $
snapshotNumber' > snapshotNumber
snapshotNumber' > closedSnapshotNumber

mustNotChangeVersion =
traceIfFalse $(errorCode MustNotChangeVersion) $
version' == version
version' == closedVersion

mustBeValidSnapshot =
case redeemer of
ContestCurrent{signature} ->
ContestCurrent{contestCurrentSignature} ->
traceIfFalse $(errorCode FailedContestCurrent) $
verifySnapshotSignature
parties
(headId, version, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
signature
ContestOutdated{signature, alreadyDecommittedUTxOHash} ->
closedParties
(closedHeadId, closedVersion, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
contestCurrentSignature
ContestOutdated{contestOutdatedSignature, alreadyDecommittedContestUTxOHash} ->
traceIfFalse $(errorCode FailedContestOutdated) $
deltaUTxOHash' == emptyHash
&& verifySnapshotSignature
parties
(headId, version - 1, snapshotNumber', utxoHash', emptyHash, alreadyDecommittedUTxOHash)
signature
closedParties
(closedHeadId, closedVersion - 1, snapshotNumber', utxoHash', emptyHash, alreadyDecommittedContestUTxOHash)
contestOutdatedSignature

mustBeWithinContestationPeriod =
case ivTo (txInfoValidRange txInfo) of
Expand All @@ -530,32 +529,32 @@ checkContest ctx closedDatum redeemer =
contestationDeadline' == contestationDeadline
else
traceIfFalse $(errorCode MustPushDeadline) $
contestationDeadline' == addContestationPeriod contestationDeadline contestationPeriod
contestationDeadline' == addContestationPeriod contestationDeadline closedContestationPeriod

mustUpdateContesters =
traceIfFalse $(errorCode ContesterNotIncluded) $
contesters' == contester : contesters

ClosedDatum
{ contestationDeadline
, contestationPeriod
, parties
, snapshotNumber
, closedContestationPeriod
, closedParties
, closedSnapshotNumber
, contesters
, headId
, version
, closedHeadId
, closedVersion
} = closedDatum

ClosedDatum
{ snapshotNumber = snapshotNumber'
, utxoHash = utxoHash'
{ closedSnapshotNumber = snapshotNumber'
, closedUtxoHash = utxoHash'
, deltaUTxOHash = deltaUTxOHash'
, parties = parties'
, closedParties = parties'
, contestationDeadline = contestationDeadline'
, contestationPeriod = contestationPeriod'
, headId = headId'
, closedContestationPeriod = contestationPeriod'
, closedHeadId = headId'
, contesters = contesters'
, version = version'
, closedVersion = version'
} = decodeHeadOutputClosedDatum ctx

ScriptContext{scriptContextTxInfo = txInfo} = ctx
Expand All @@ -581,7 +580,7 @@ checkFanout ::
Integer ->
Bool
checkFanout ScriptContext{scriptContextTxInfo = txInfo} closedDatum numberOfFanoutOutputs numberOfDecommitOutputs =
mustBurnAllHeadTokens minted headId parties
mustBurnAllHeadTokens minted closedHeadId closedParties
&& hasSameUTxOHash
&& hasSameUTxOToDecommitHash
&& afterContestationDeadline
Expand All @@ -590,7 +589,7 @@ checkFanout ScriptContext{scriptContextTxInfo = txInfo} closedDatum numberOfFano

hasSameUTxOHash =
traceIfFalse $(errorCode FanoutUTxOHashMismatch) $
fannedOutUtxoHash == utxoHash
fannedOutUtxoHash == closedUtxoHash

hasSameUTxOToDecommitHash =
traceIfFalse $(errorCode FanoutUTxOToDecommitHashMismatch) $
Expand All @@ -600,7 +599,7 @@ checkFanout ScriptContext{scriptContextTxInfo = txInfo} closedDatum numberOfFano

decommitUtxoHash = hashTxOuts $ take numberOfDecommitOutputs $ drop numberOfFanoutOutputs txInfoOutputs

ClosedDatum{utxoHash, deltaUTxOHash, parties, headId, contestationDeadline} = closedDatum
ClosedDatum{closedUtxoHash, deltaUTxOHash, closedParties, closedHeadId, contestationDeadline} = closedDatum

TxInfo{txInfoOutputs} = txInfo

Expand Down
Loading

0 comments on commit af16d0f

Please sign in to comment.