Skip to content

Commit

Permalink
Rename ContestOutdated to ContestUnusedDec
Browse files Browse the repository at this point in the history
Add Healthy module for contest mutations and ContestUnusedDec module for
corresponding mutations.
  • Loading branch information
v0d1ch committed Oct 30, 2024
1 parent cdc26ab commit 85f4c36
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 165 deletions.
2 changes: 1 addition & 1 deletion hydra-cluster/config/devnet/genesis-shelley.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"keyDeposit": 0,
"maxBlockBodySize": 65536,
"maxBlockHeaderSize": 1100,
"maxTxSize": 17900,
"maxTxSize": 18000,
"minFeeA": 44,
"minFeeB": 155381,
"minPoolCost": 0,
Expand Down
2 changes: 1 addition & 1 deletion hydra-plutus/scripts/mHead.plutus

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hydra-plutus/scripts/vHead.plutus

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ import Hydra.Contract.Commit (Commit (..))
import Hydra.Contract.Commit qualified as Commit
import Hydra.Contract.Deposit qualified as Deposit
import Hydra.Contract.HeadError (HeadError (..), errorCode)
import Hydra.Contract.HeadState (CloseRedeemer (..), ClosedDatum (..), ContestRedeemer (..), DecrementRedeemer (..), Hash, IncrementRedeemer (..), Input (..), OpenDatum (..), Signature, SnapshotNumber, SnapshotVersion, State (..))
import Hydra.Contract.HeadState (
CloseRedeemer (..),
ClosedDatum (..),
ContestRedeemer (..),
DecrementRedeemer (..),
Hash,
IncrementRedeemer (..),
Input (..),
OpenDatum (..),
Signature,
SnapshotNumber,
SnapshotVersion,
State (..),
)
import Hydra.Contract.Util (hasST, hashPreSerializedCommits, hashTxOuts, mustBurnAllHeadTokens, mustNotMintOrBurn, (===))
import Hydra.Data.ContestationPeriod (ContestationPeriod, addContestationPeriod, milliseconds)
import Hydra.Data.Party (Party (vkey))
Expand Down Expand Up @@ -522,8 +535,8 @@ checkContest ctx closedDatum redeemer =
parties
(headId, version, snapshotNumber', utxoHash', emptyHash, deltaUTxOHash')
signature
ContestOutdated{signature, alreadyDecommittedUTxOHash} ->
traceIfFalse $(errorCode FailedContestOutdated) $
ContestUsedDec{signature, alreadyDecommittedUTxOHash} ->
traceIfFalse $(errorCode FailedContestUsedDec) $
deltaUTxOHash' == emptyHash
&& verifySnapshotSignature
parties
Expand Down
6 changes: 4 additions & 2 deletions hydra-plutus/src/Hydra/Contract/HeadError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ data HeadError
| WrongNumberOfSigners
| SignerAlreadyContested
| FailedContestCurrent
| FailedContestOutdated
| FailedContestUsedDec
| FanoutUTxOHashMismatch
| LowerBoundBeforeContestationDeadline
| FanoutNoLowerBoundDefined
| FanoutUTxOToDecommitHashMismatch
| DepositNotSpent
| DepositInputNotFound
| HeadInputNotFound
| FailedContestUnusedDec

instance ToErrorCode HeadError where
toErrorCode = \case
Expand Down Expand Up @@ -102,7 +103,7 @@ instance ToErrorCode HeadError where
WrongNumberOfSigners -> "H37"
SignerAlreadyContested -> "H38"
FailedContestCurrent -> "H39"
FailedContestOutdated -> "H40"
FailedContestUsedDec -> "H40"
-- Fanout
FanoutUTxOHashMismatch -> "H41"
FanoutUTxOToDecommitHashMismatch -> "H42"
Expand All @@ -112,3 +113,4 @@ instance ToErrorCode HeadError where
DepositInputNotFound -> "H46"
HeadInputNotFound -> "H47"
FailedCloseAny -> "H48"
FailedContestUnusedDec -> "H49"
2 changes: 1 addition & 1 deletion hydra-plutus/src/Hydra/Contract/HeadState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ data ContestRedeemer
-- ^ Multi-signature of a snapshot ξ
}
| -- | Contesting snapshot refers to the previous state version
ContestOutdated
ContestUsedDec
{ signature :: [Signature]
-- ^ Multi-signature of a snapshot ξ
, alreadyDecommittedUTxOHash :: Hash
Expand Down
2 changes: 2 additions & 0 deletions hydra-tx/hydra-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ test-suite tests
Hydra.Tx.Contract.CollectCom
Hydra.Tx.Contract.Commit
Hydra.Tx.Contract.Contest.ContestCurrent
Hydra.Tx.Contract.Contest.ContestUsedDec
Hydra.Tx.Contract.Contest.Healthy
Hydra.Tx.Contract.ContractSpec
Hydra.Tx.Contract.Decrement
Hydra.Tx.Contract.Deposit
Expand Down
2 changes: 1 addition & 1 deletion hydra-tx/src/Hydra/Tx/Contest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ contestTx scriptRegistry vk headId contestationPeriod openVersion Snapshot{numbe
}
| otherwise =
-- NOTE: This will only work for version == openVersion - 1
Head.ContestOutdated
Head.ContestUsedDec
{ signature = toPlutusSignatures sig
, alreadyDecommittedUTxOHash = toBuiltin $ hashUTxO @Tx $ fromMaybe mempty utxoToDecommit
}
Expand Down
171 changes: 17 additions & 154 deletions hydra-tx/test/Hydra/Tx/Contract/Contest/ContestCurrent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ import Hydra.Plutus.Orphans ()
import Hydra.Tx (registryUTxO)
import Hydra.Tx.Contest (ClosedThreadOutput (..), contestTx)
import Hydra.Tx.ContestationPeriod (ContestationPeriod, fromChain)
import Hydra.Tx.Contract.Contest.Healthy (
healthyCloseSnapshotVersion,
healthyClosedHeadTxIn,
healthyClosedHeadTxOut,
healthyClosedState,
healthyContestSnapshotNumber,
healthyContestUTxOHash,
healthyContestUTxOToDecommitHash,
healthyContestationDeadline,
healthyContesterVerificationKey,
healthyOnChainContestationPeriod,
healthyOnChainParties,
healthyParticipants,
healthyParties,
healthySignature,
healthyContestTx,
)
import Hydra.Tx.Crypto (HydraKey, MultiSignature, aggregate, sign, toPlutusSignatures)
import Hydra.Tx.HeadId (mkHeadId)
import Hydra.Tx.Init (mkHeadOutput)
Expand Down Expand Up @@ -67,160 +84,6 @@ import Test.QuickCheck (arbitrarySizedNatural, elements, listOf, listOf1, oneof,
import Test.QuickCheck.Gen (choose)
import Test.QuickCheck.Instances ()

--
-- ContestTx
--

-- | Healthy contest tx where the contester is the first one to contest and
-- correctly pushing out the deadline by the contestation period.
healthyContestTx :: (Tx, UTxO)
healthyContestTx =
(tx, lookupUTxO)
where
lookupUTxO =
UTxO.singleton (healthyClosedHeadTxIn, healthyClosedHeadTxOut)
<> registryUTxO scriptRegistry

tx =
contestTx
scriptRegistry
healthyContesterVerificationKey
(mkHeadId testPolicyId)
healthyContestationPeriod
healthyCloseSnapshotVersion
healthyContestSnapshot
(healthySignature healthyContestSnapshotNumber)
(healthySlotNo, slotNoToUTCTime systemStart slotLength healthySlotNo)
closedThreadOutput

scriptRegistry = genScriptRegistry `generateWith` 42

closedThreadOutput =
ClosedThreadOutput
{ closedThreadUTxO = (healthyClosedHeadTxIn, healthyClosedHeadTxOut)
, closedParties =
healthyOnChainParties
, closedContestationDeadline = posixFromUTCTime healthyContestationDeadline
, closedContesters = []
}

healthyContestSnapshotNumber :: SnapshotNumber
healthyContestSnapshotNumber = 4

healthyCloseSnapshotVersion :: SnapshotVersion
healthyCloseSnapshotVersion = 4

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

healthyContestUTxO :: UTxO
healthyContestUTxO =
(genOneUTxOFor healthyContesterVerificationKey `suchThat` (/= healthyClosedUTxO))
`generateWith` 42

splittedContestUTxO :: (UTxO, UTxO)
splittedContestUTxO = splitUTxO healthyContestUTxO

splitUTxOInHead :: UTxO
splitUTxOInHead = fst splittedContestUTxO

splitUTxOToDecommit :: UTxO
splitUTxOToDecommit = snd splittedContestUTxO

healthyContestSnapshot :: Snapshot Tx
healthyContestSnapshot =
Snapshot
{ headId = mkHeadId testPolicyId
, number = healthyContestSnapshotNumber
, utxo = splitUTxOInHead
, confirmed = []
, utxoToCommit = Nothing
, utxoToDecommit = Just splitUTxOToDecommit
, version = healthyCloseSnapshotVersion
}

healthyClosedState :: Head.State
healthyClosedState =
Head.Closed
Head.ClosedDatum
{ snapshotNumber = fromIntegral healthyClosedSnapshotNumber
, utxoHash = healthyClosedUTxOHash
, deltaUTxOHash = mempty
, parties = healthyOnChainParties
, contestationDeadline = posixFromUTCTime healthyContestationDeadline
, contestationPeriod = healthyOnChainContestationPeriod
, headId = toPlutusCurrencySymbol testPolicyId
, contesters = []
, version = toInteger healthyCloseSnapshotVersion
}

healthyContestUTxOHash :: BuiltinByteString
healthyContestUTxOHash =
toBuiltin $ hashUTxO @Tx splitUTxOInHead

healthyContestUTxOToDecommitHash :: BuiltinByteString
healthyContestUTxOToDecommitHash =
toBuiltin $ hashUTxO @Tx splitUTxOToDecommit

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

healthyClosedSnapshotNumber :: SnapshotNumber
healthyClosedSnapshotNumber = 3

healthySlotNo :: SlotNo
healthySlotNo = arbitrary `generateWith` 42

healthyClosedHeadTxIn :: TxIn
healthyClosedHeadTxIn = generateWith arbitrary 42

healthyClosedHeadTxOut :: TxOut CtxUTxO
healthyClosedHeadTxOut =
mkHeadOutput testNetworkId testPolicyId headTxOutDatum
& addParticipationTokens healthyParticipants
where
headTxOutDatum = toUTxOContext (mkTxOutDatumInline healthyClosedState)

healthyOnChainContestationPeriod :: OnChain.ContestationPeriod
healthyOnChainContestationPeriod = OnChain.contestationPeriodFromDiffTime $ fromInteger healthyContestationPeriodSeconds

healthyContestationPeriod :: ContestationPeriod
healthyContestationPeriod = fromChain healthyOnChainContestationPeriod

healthyContestationPeriodSeconds :: Integer
healthyContestationPeriodSeconds = 10

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

healthyContesterVerificationKey :: VerificationKey PaymentKey
healthyContesterVerificationKey =
elements healthyParticipants `generateWith` 42

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

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

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

healthySignature :: SnapshotNumber -> MultiSignature (Snapshot Tx)
healthySignature number =
aggregate [sign sk snapshot | sk <- healthySigningKeys]
where
snapshot = healthyContestSnapshot{number}

healthyContestationDeadline :: UTCTime
healthyContestationDeadline =
addUTCTime
(fromInteger healthyContestationPeriodSeconds)
(slotNoToUTCTime systemStart slotLength healthySlotNo)

-- FIXME: Should try to mutate the 'closedAt' recorded time to something else
data ContestMutation
= -- | Ensures collectCom does not allow any output address but νHead.
Expand Down
21 changes: 21 additions & 0 deletions hydra-tx/test/Hydra/Tx/Contract/Contest/ContestUsedDec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE DuplicateRecordFields #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Hydra.Tx.Contract.Contest.ContestUsedDec where

import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)

import Hydra.Tx.Contract.Contest.Healthy ()
import Test.Hydra.Tx.Mutation (
SomeMutation (..),
)
import Test.QuickCheck (oneof)
import Test.QuickCheck.Instances ()

data ContestUsedDecMutation = ContestUsedDecMutation
deriving stock (Generic, Show, Enum, Bounded)

genContestUsedDecMutation :: (Tx, UTxO) -> Gen SomeMutation
genContestUsedDecMutation (_tx, _utxo) =
oneof []
Loading

0 comments on commit 85f4c36

Please sign in to comment.