Skip to content

Commit

Permalink
Decrement on-chain: mustBeSignedByParticipant check
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed May 7, 2024
1 parent 32de7e7 commit 398e750
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
44 changes: 17 additions & 27 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Decrement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Hydra.Cardano.Api
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
SomeMutation (..),
addParticipationTokens,
modifyInlineDatum,
replaceParties,
replaceSnapshotNumberInOpen,
Expand Down Expand Up @@ -34,7 +35,7 @@ import Hydra.Ledger.Cardano (
genTxOut,
genVerificationKey,
)
import Hydra.Party (Party, deriveParty, partyToChain, vkey)
import Hydra.Party (Party, deriveParty, partyToChain)
import Hydra.Plutus.Orphans ()
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
import PlutusTx.Builtins (toBuiltin)
Expand Down Expand Up @@ -67,29 +68,31 @@ healthyDecrementTx =
, contestationPeriod = healthyContestationPeriod
}

somePartyCardanoVerificationKey =
elements healthyParticipants `generateWith` 42

scriptRegistry = genScriptRegistry `generateWith` 42

headInput = generateWith arbitrary 42

headOutput' =
headOutput =
mkHeadOutput testNetworkId testPolicyId (toUTxOContext $ mkTxOutDatumInline healthyDatum)
& addParticipationTokens healthyParticipants

headOutput = modifyTxOutValue (<> participationTokens) headOutput'

participationTokens =
valueFromList $
map
( \party ->
(AssetId testPolicyId (AssetName . serialiseToRawBytes . verificationKeyHash . vkey $ party), 1)
)
healthyParties
somePartyCardanoVerificationKey :: VerificationKey PaymentKey
somePartyCardanoVerificationKey =
elements healthyParticipants `generateWith` 42

healthySigningKeys :: [SigningKey HydraKey]
healthySigningKeys = [aliceSk, bobSk, carolSk]

healthyParticipants :: [VerificationKey PaymentKey]
healthyParticipants =
genForParty genVerificationKey <$> healthyParties

healthyParties :: [Party]
healthyParties = deriveParty <$> healthySigningKeys

healthyOnChainParties :: [OnChain.Party]
healthyOnChainParties = partyToChain <$> healthyParties

healthySignature :: MultiSignature (Snapshot Tx)
healthySignature = aggregate [sign sk healthySnapshot | sk <- healthySigningKeys]

Expand Down Expand Up @@ -119,10 +122,6 @@ healthyContestationPeriod :: ContestationPeriod
healthyContestationPeriod =
arbitrary `generateWith` 42

healthyParticipants :: [VerificationKey PaymentKey]
healthyParticipants =
genForParty genVerificationKey <$> healthyParties

healthyUTxO :: UTxO
healthyUTxO =
adaOnly
Expand All @@ -140,22 +139,13 @@ healthyDatum =
, headId = toPlutusCurrencySymbol testPolicyId
}

healthyParties :: [Party]
healthyParties = deriveParty <$> healthySigningKeys

healthyOnChainParties :: [OnChain.Party]
healthyOnChainParties = partyToChain <$> healthyParties

data DecrementMutation
= -- | Ensures parties do not change between head input datum and head output
-- datum.
MutatePartiesInOutput
| -- | Invalidates the tx by changing the snapshot number in resulting head
-- output.
--
-- Ensures the snapshot number is aligned.
MutateSnapshotNumber
| SnapshotSignatureInvalid
| -- | Produce invalid signature by changing signers in the redeemer
SnapshotSignatureInvalid
| -- | Ensures decrement is authenticated by one of the Head members by changing
Expand Down
5 changes: 3 additions & 2 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ checkDecrement ::
checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevSnapshotNumber prevCperiod prevHeadId signature =
mustNotChangeParameters
&& checkSnapshot
&& checkSignatures
&& checkSnapshotSignature
&& mustBeSignedByParticipant ctx prevHeadId
where
decommitUtxoHash = hashTxOuts $ tail (txInfoOutputs txInfo)
(nextUtxoHash, nextParties, nextSnapshotNumber, nextCperiod, nextHeadId) =
Expand All @@ -264,7 +265,7 @@ checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevS
traceIfFalse $(errorCode SnapshotNumberMismatch) $
nextSnapshotNumber > prevSnapshotNumber

checkSignatures =
checkSnapshotSignature =
verifySnapshotSignature nextParties nextHeadId nextSnapshotNumber nextUtxoHash decommitUtxoHash signature
{-# INLINEABLE checkDecrement #-}

Expand Down

0 comments on commit 398e750

Please sign in to comment.