Skip to content

Commit

Permalink
Move snapshot number from Contest redeemer to Closed datum in Head sc…
Browse files Browse the repository at this point in the history
…ript
  • Loading branch information
ffakenz committed Jan 16, 2023
1 parent 9cb4e1b commit 4fc64f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
11 changes: 8 additions & 3 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,7 @@ contestTx vk Snapshot{number, utxo} sig (slotNo, _) ClosedThreadOutput{closedThr
headRedeemer =
toScriptData
Head.Contest
{ snapshotNumber = toInteger number
, signature = toPlutusSignatures sig
{ signature = toPlutusSignatures sig
, utxoHash
}
headOutputAfter =
Expand Down Expand Up @@ -830,9 +829,10 @@ observeContestTx utxo tx = do
datum <- fromData $ toPlutusData oldHeadDatum
headId <- findStateToken headOutput
case (datum, redeemer) of
(Head.Closed{}, Head.Contest{snapshotNumber = onChainSnapshotNumber}) -> do
(Head.Closed{}, Head.Contest{}) -> do
(newHeadInput, newHeadOutput) <- findTxOutByScript @PlutusScriptV2 (utxoFromTx tx) headScript
newHeadDatum <- lookupScriptData tx newHeadOutput
let onChainSnapshotNumber = closedSnapshotNumber newHeadDatum
pure
ContestObservation
{ contestedThreadOutput =
Expand All @@ -847,6 +847,11 @@ observeContestTx utxo tx = do
where
headScript = fromPlutusScript Head.validatorScript

closedSnapshotNumber headDatum =
case fromData $ toPlutusData headDatum of
Just Head.Closed{snapshotNumber} -> snapshotNumber
_ -> error "wrong state in output datum"

data FanoutObservation = FanoutObservation

-- | Identify a fanout tx by lookup up the input spending the Head output and
Expand Down
29 changes: 19 additions & 10 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Contest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,29 @@ genContestMutation
mutatedSignature <- arbitrary :: Gen (MultiSignature (Snapshot Tx))
pure $
Head.Contest
{ snapshotNumber = toInteger healthyContestSnapshotNumber
, utxoHash = healthyContestUTxOHash
{ utxoHash = healthyContestUTxOHash
, signature = toPlutusSignatures mutatedSignature
}
, SomeMutation MutateToNonNewerSnapshot . ChangeHeadRedeemer <$> do
, SomeMutation MutateToNonNewerSnapshot <$> do
mutatedSnapshotNumber <- choose (0, toInteger healthyClosedSnapshotNumber)
pure $
Head.Contest
{ snapshotNumber = mutatedSnapshotNumber
, utxoHash = healthyContestUTxOHash
, signature =
toPlutusSignatures $
healthySignature (fromInteger mutatedSnapshotNumber)
}
Changes
[ ChangeHeadDatum $
Head.Closed
{ snapshotNumber = mutatedSnapshotNumber
, utxoHash = healthyClosedUTxOHash
, parties = healthyOnChainParties
, contestationDeadline = posixFromUTCTime healthyContestationDeadline
, headId = toPlutusCurrencySymbol testPolicyId
}
, ChangeHeadRedeemer $
Head.Contest
{ utxoHash = healthyContestUTxOHash
, signature =
toPlutusSignatures $
healthySignature (fromInteger mutatedSnapshotNumber)
}
]
, SomeMutation MutateRequiredSigner <$> do
newSigner <- verificationKeyHash <$> genVerificationKey
pure $ ChangeRequiredSigners [newSigner]
Expand Down
27 changes: 21 additions & 6 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import qualified PlutusTx.AssocMap as Map
import qualified PlutusTx.Builtins as Builtins

-- REVIEW: Functions not re-exported "as V2", but using the same data types.

import Plutus.V1.Ledger.Time (fromMilliSeconds)
import Plutus.V1.Ledger.Value (assetClass, assetClassValue, valueOf)

Expand All @@ -72,8 +73,8 @@ headValidator oldState input ctx =
checkAbort ctx headId parties
(Open{parties, utxoHash = initialUtxoHash, contestationPeriod, headId}, Close{snapshotNumber, utxoHash = closedUtxoHash, signature}) ->
checkClose ctx parties initialUtxoHash snapshotNumber closedUtxoHash signature contestationPeriod headId
(Closed{parties, snapshotNumber = closedSnapshotNumber, contestationDeadline, headId}, Contest{snapshotNumber = contestSnapshotNumber, utxoHash = contestUtxoHash, signature}) ->
checkContest ctx contestationDeadline parties closedSnapshotNumber contestSnapshotNumber contestUtxoHash signature headId
(Closed{parties, snapshotNumber = closedSnapshotNumber, contestationDeadline, headId}, Contest{utxoHash = contestUtxoHash, signature}) ->
checkContest ctx contestationDeadline parties closedSnapshotNumber contestUtxoHash signature headId
(Closed{utxoHash, contestationDeadline}, Fanout{numberOfFanoutOutputs}) ->
checkFanout utxoHash contestationDeadline numberOfFanoutOutputs ctx
_ ->
Expand Down Expand Up @@ -311,16 +312,13 @@ checkContest ::
POSIXTime ->
[Party] ->
-- | Snapshot number of the closed state.
-- XXX: Having two snapshot numbers here is FRAGILE
SnapshotNumber ->
-- | Snapshot number of the contestin snapshot.
SnapshotNumber ->
BuiltinByteString ->
[Signature] ->
-- | Head id
CurrencySymbol ->
Bool
checkContest ctx@ScriptContext{scriptContextTxInfo} contestationDeadline parties closedSnapshotNumber contestSnapshotNumber contestUtxoHash sig headPolicyId =
checkContest ctx@ScriptContext{scriptContextTxInfo} contestationDeadline parties closedSnapshotNumber contestUtxoHash sig headPolicyId =
mustBeNewer
&& mustBeMultiSigned
&& checkHeadOutputDatum
Expand All @@ -332,6 +330,23 @@ checkContest ctx@ScriptContext{scriptContextTxInfo} contestationDeadline parties
where
outValue =
maybe mempty (txOutValue . txInInfoResolved) $ findOwnInput ctx

contestSnapshotNumber =
case fromBuiltinData @DatumType $ getDatum continuingDatum of
Just Closed{snapshotNumber} -> snapshotNumber
_ -> traceError "wrong state in output datum"

continuingDatum =
case ownDatum of
NoOutputDatum -> traceError "no output datum"
OutputDatumHash dh -> fromMaybe (traceError "datum not found") $ findDatum dh scriptContextTxInfo
OutputDatum d -> d
where
ownDatum =
case getContinuingOutputs ctx of
[o] -> txOutDatum o
_ -> traceError "expected only one head output"

mustBeNewer =
traceIfFalse "too old snapshot" $ contestSnapshotNumber > closedSnapshotNumber

Expand Down
3 changes: 1 addition & 2 deletions hydra-plutus/src/Hydra/Contract/HeadState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ data Input
, signature :: [Signature]
}
| Contest
{ snapshotNumber :: SnapshotNumber
, utxoHash :: Hash
{ utxoHash :: Hash
, signature :: [Signature]
}
| Abort
Expand Down

0 comments on commit 4fc64f7

Please sign in to comment.