Skip to content

Commit

Permalink
Close Mutation: Set utxoToDecommit to some value
Browse files Browse the repository at this point in the history
Make sure to exercise the path where we change the utxoToDecommit hash
in the close datum in order to see invalid snapshot signature.
  • Loading branch information
v0d1ch committed May 29, 2024
1 parent c07d03f commit ee72e54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
54 changes: 32 additions & 22 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Close.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import Hydra.Chain.Direct.Contract.Mutation (
replacePolicyIdWith,
replaceSnapshotNumber,
replaceUtxoHash,
replaceUtxoToDecommitHash,
)
import Hydra.Chain.Direct.Fixture qualified as Fixture
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.State (splitUTxO)
import Hydra.Chain.Direct.TimeHandle (PointInTime)
import Hydra.Chain.Direct.Tx (ClosingSnapshot (..), OpenThreadOutput (..), UTxOHash (UTxOHash), closeTx, mkHeadId, mkHeadOutput)
import Hydra.ContestationPeriod (fromChain)
Expand Down Expand Up @@ -83,12 +85,13 @@ healthyCloseTx =

closingSnapshot :: ClosingSnapshot
closingSnapshot =
CloseWithConfirmedSnapshot
{ snapshotNumber = healthyCloseSnapshotNumber
, closeUtxoHash = UTxOHash $ hashUTxO @Tx healthyCloseUTxO
, closeUtxoToDecommitHash = UTxOHash $ hashUTxO @Tx mempty
, signatures = healthySignature healthyCloseSnapshotNumber
}
let (utxo, utxoToDecommit) = generateWith (splitUTxO healthyCloseUTxO) 42
in CloseWithConfirmedSnapshot
{ snapshotNumber = healthyCloseSnapshotNumber
, closeUtxoHash = UTxOHash $ hashUTxO @Tx utxo
, closeUtxoToDecommitHash = UTxOHash $ hashUTxO @Tx utxoToDecommit
, signatures = healthySignature healthyCloseSnapshotNumber
}

-- | Healthy close transaction for the specific case were we close a head
-- with the initial UtxO, that is, no snapshot have been agreed upon and
Expand Down Expand Up @@ -143,21 +146,33 @@ healthyOpenHeadTxOut headTxOutDatum =
mkHeadOutput Fixture.testNetworkId Fixture.testPolicyId headTxOutDatum
& addParticipationTokens healthyParticipants

healthyCloseSnapshotUTxO :: (UTxO, UTxO)
healthyCloseSnapshotUTxO = generateWith (splitUTxO healthyCloseUTxO) 42

healthySnapshot :: Snapshot Tx
healthySnapshot =
Snapshot
{ headId = mkHeadId Fixture.testPolicyId
, number = healthyCloseSnapshotNumber
, utxo = healthyCloseUTxO
, confirmed = []
, utxoToDecommit = Nothing
}
let (utxo, utxoToDecommit') = healthyCloseSnapshotUTxO
in Snapshot
{ headId = mkHeadId Fixture.testPolicyId
, number = healthyCloseSnapshotNumber
, utxo
, confirmed = []
, utxoToDecommit = Just utxoToDecommit'
}

healthyCloseUTxO :: UTxO
healthyCloseUTxO =
(genOneUTxOFor somePartyCardanoVerificationKey `suchThat` (/= healthyUTxO))
`generateWith` 42

healthyCloseUTxOHash :: BuiltinByteString
healthyCloseUTxOHash =
toBuiltin $ hashUTxO @Tx (fst healthyCloseSnapshotUTxO)

healthyCloseUTxOToDecommitHash :: BuiltinByteString
healthyCloseUTxOToDecommitHash =
toBuiltin $ hashUTxO @Tx (snd healthyCloseSnapshotUTxO)

healthyCloseSnapshotNumber :: SnapshotNumber
healthyCloseSnapshotNumber = 1

Expand Down Expand Up @@ -208,14 +223,6 @@ healthyContestationDeadline =
(fromInteger healthyContestationPeriodSeconds)
(snd healthyCloseUpperBoundPointInTime)

healthyClosedUTxOHash :: BuiltinByteString
healthyClosedUTxOHash =
toBuiltin $ hashUTxO @Tx healthyClosedUTxO

healthyClosedUTxO :: UTxO
healthyClosedUTxO =
genOneUTxOFor somePartyCardanoVerificationKey `generateWith` 42

data CloseMutation
= -- | Ensures collectCom does not allow any output address but νHead.
NotContinueContract
Expand Down Expand Up @@ -333,7 +340,7 @@ genCloseMutation (tx, _utxo) =
let signerAndOthers = somePartyCardanoVerificationKey : otherSigners
pure $ ChangeRequiredSigners (verificationKeyHash <$> signerAndOthers)
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateCloseUTxOHash . ChangeOutput 0 <$> do
mutatedUTxOHash <- genHash `suchThat` ((/= healthyClosedUTxOHash) . toBuiltin)
mutatedUTxOHash <- genHash `suchThat` ((/= healthyCloseUTxOHash) . toBuiltin)
pure $ modifyInlineDatum (replaceUtxoHash $ toBuiltin mutatedUTxOHash) headTxOut
, SomeMutation (pure $ toErrorCode IncorrectClosedContestationDeadline) MutateContestationDeadline <$> do
mutatedDeadline <- genMutatedDeadline
Expand Down Expand Up @@ -380,6 +387,9 @@ genCloseMutation (tx, _utxo) =
, SomeMutation (pure $ toErrorCode HeadValueIsNotPreserved) MutateValueInOutput <$> do
newValue <- genValue
pure $ ChangeOutput 0 (headTxOut{txOutValue = newValue})
, SomeMutation (pure $ toErrorCode SignatureVerificationFailed) MutateCloseUTxOHash . ChangeOutput 0 <$> do
mutatedUTxOHash <- genHash `suchThat` ((/= healthyCloseUTxOToDecommitHash) . toBuiltin)
pure $ modifyInlineDatum (replaceUtxoToDecommitHash $ toBuiltin mutatedUTxOHash) headTxOut
]
where
genOversizedTransactionValidity = do
Expand Down
22 changes: 22 additions & 0 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Mutation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,28 @@ replaceUtxoHash utxoHash = \case
}
otherState -> otherState

replaceUtxoToDecommitHash :: Head.Hash -> Head.State -> Head.State
replaceUtxoToDecommitHash utxoToDecommitHash' = \case
Head.Open{contestationPeriod, utxoHash, snapshotNumber, parties, headId} ->
Head.Open
{ Head.contestationPeriod = contestationPeriod
, Head.parties = parties
, Head.snapshotNumber = snapshotNumber
, Head.utxoHash
, Head.headId = headId
}
Head.Closed{parties, utxoHash, snapshotNumber, contestationDeadline, headId, contesters, contestationPeriod} ->
Head.Closed
{ Head.parties = parties
, Head.snapshotNumber = snapshotNumber
, Head.utxoHash
, Head.utxoToDecommitHash = utxoToDecommitHash'
, Head.contestationDeadline = contestationDeadline
, Head.contestationPeriod = contestationPeriod
, Head.headId = headId
, Head.contesters = contesters
}
otherState -> otherState
replaceContestationDeadline :: POSIXTime -> Head.State -> Head.State
replaceContestationDeadline contestationDeadline = \case
Head.Closed{snapshotNumber, utxoHash, utxoToDecommitHash, parties, headId, contesters, contestationPeriod} ->
Expand Down

0 comments on commit ee72e54

Please sign in to comment.