Skip to content

Commit

Permalink
Make CloseOutdated work again
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Jul 3, 2024
1 parent 17ac2ff commit 4accf1e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)

import Data.Maybe (fromJust)
import Hydra.Chain.Direct.Contract.Close.Healthy (healthyContestationDeadline, healthyContestationPeriodSeconds, healthyOnChainParties, healthyOpenDatum, healthyOpenHeadTxIn, healthyOpenHeadTxOut, healthySignature, healthySnapshot, somePartyCardanoVerificationKey, healthySplitUTxOInHead, healthyConfirmedClosingSnapshotTx)
import Hydra.Chain.Direct.Contract.Close.Healthy (healthyContestationDeadline, healthyContestationPeriodSeconds, healthyOnChainParties, healthyOpenDatum, healthyOpenHeadTxIn, healthyOpenHeadTxOut, healthySignature, healthySnapshot, somePartyCardanoVerificationKey, healthySplitUTxOInHead, healthyConfirmedClosingTx)
import Hydra.Chain.Direct.Contract.Gen (genHash, genMintedOrBurnedValue)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
Expand Down Expand Up @@ -52,7 +52,7 @@ healthyCurrentSnapshotVersion = 1
-- | Healthy close transaction for the generic case were we close a head
-- after one or more snapshot have been agreed upon between the members.
healthyCloseCurrentTx :: (Tx, UTxO)
healthyCloseCurrentTx = healthyConfirmedClosingSnapshotTx healthyCurrentSnapshot
healthyCloseCurrentTx = healthyConfirmedClosingTx healthyCurrentSnapshot

healthyCurrentSnapshot :: Snapshot Tx
healthyCurrentSnapshot = healthySnapshot healthyCurrentSnapshotNumber healthyCurrentSnapshotVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Hydra.Prelude hiding (label)

import Cardano.Api.UTxO as UTxO
import Data.Maybe (fromJust)
import Hydra.Chain.Direct.Contract.Close.Healthy (healthyCloseLowerBoundSlot, healthyCloseUpperBoundPointInTime, healthyContestationDeadline, healthyContestationPeriod, healthyOnChainParties, healthyOpenDatum, healthyOpenHeadTxIn, healthyOpenHeadTxOut, healthySnapshot, healthyUTxO, somePartyCardanoVerificationKey)
import Hydra.Chain.Direct.Contract.Close.Healthy (healthyCloseLowerBoundSlot, healthyCloseUpperBoundPointInTime, healthyContestationDeadline, healthyContestationPeriod, healthyOnChainParties, healthyOpenDatum, healthyOpenHeadTxIn, healthyOpenHeadTxOut, healthySnapshot, healthyUTxO, somePartyCardanoVerificationKey, scriptRegistry)
import Hydra.Chain.Direct.Contract.Mutation (Mutation (..), SomeMutation (..), modifyInlineDatum, replaceContestationDeadline)
import Hydra.Chain.Direct.Fixture qualified as Fixture
import Hydra.Chain.Direct.ScriptRegistry (ScriptRegistry, genScriptRegistry, registryUTxO)
Expand Down Expand Up @@ -61,9 +61,6 @@ healthyCloseInitialTx =
UTxO.singleton (healthyOpenHeadTxIn, healthyOpenHeadTxOut initialDatum)
<> registryUTxO scriptRegistry

scriptRegistry :: ScriptRegistry
scriptRegistry = genScriptRegistry `generateWith` 42

openThreadOutput :: OpenThreadOutput
openThreadOutput =
OpenThreadOutput
Expand Down
45 changes: 42 additions & 3 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Close/CloseOutdated.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module Hydra.Chain.Direct.Contract.Close.CloseOutdated where
import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)

import Cardano.Api.UTxO as UTxO
import Data.Maybe (fromJust)
import Hydra.Chain.Direct.Contract.Close.Healthy (healthyCloseLowerBoundSlot, healthyCloseUTxOHash, healthyCloseUpperBoundPointInTime, healthyConfirmedClosingSnapshot, healthyContestationDeadline, healthyContestationPeriod, healthyContestationPeriodSeconds, healthyOnChainParties, healthyOpenDatum, healthyOpenHeadTxIn, healthyOpenHeadTxOut, healthySignature, healthySplitUTxOInHead, healthySplitUTxOToDecommit, scriptRegistry, somePartyCardanoVerificationKey)
import Hydra.Chain.Direct.Contract.Gen (genHash, genMintedOrBurnedValue)
import Hydra.Chain.Direct.Contract.Mutation (
Mutation (..),
Expand All @@ -22,12 +24,19 @@ import Hydra.Chain.Direct.Contract.Mutation (
replaceSnapshotNumber,
replaceUtxoHash,
)
import Hydra.Chain.Direct.Contract.Close.Healthy (healthyContestationDeadline, healthyContestationPeriodSeconds, healthyOnChainParties, healthyOpenDatum, healthyOpenHeadTxIn, healthyOpenHeadTxOut, healthySignature, somePartyCardanoVerificationKey, healthySplitUTxOInHead, healthySplitUTxOToDecommit, healthyCloseUTxOHash, healthyConfirmedClosingSnapshot, healthyConfirmedClosingSnapshotTx)
import Hydra.Chain.Direct.Fixture qualified as Fixture
import Hydra.Chain.Direct.Tx (ClosingSnapshot (..), UTxOHash (UTxOHash), mkHeadId)
import Hydra.Chain.Direct.ScriptRegistry (registryUTxO)
import Hydra.Chain.Direct.Tx (
ClosingSnapshot (..),
OpenThreadOutput (..),
UTxOHash (UTxOHash),
closeTx,
mkHeadId,
)
import Hydra.Contract.Error (toErrorCode)
import Hydra.Contract.HeadError (HeadError (..))
import Hydra.Contract.HeadState qualified as Head
import Hydra.Contract.HeadState qualified as HeadState
import Hydra.Contract.HeadTokens (headPolicyId)
import Hydra.Contract.Util (UtilError (MintingOrBurningIsForbidden))
import Hydra.Crypto (MultiSignature (..), toPlutusSignatures)
Expand Down Expand Up @@ -69,8 +78,38 @@ healthyOutdatedConfirmedClosingSnapshot :: ClosingSnapshot
healthyOutdatedConfirmedClosingSnapshot = healthyConfirmedClosingSnapshot healthyOutdatedSnapshot

healthyCloseOutdatedTx :: (Tx, UTxO)
healthyCloseOutdatedTx = healthyConfirmedClosingSnapshotTx healthyOutdatedSnapshot
healthyCloseOutdatedTx =
(tx, lookupUTxO)
where
tx =
closeTx
scriptRegistry
somePartyCardanoVerificationKey
(healthyConfirmedClosingSnapshot healthyOutdatedSnapshot)
healthyCloseLowerBoundSlot
healthyCloseUpperBoundPointInTime
openThreadOutput
(mkHeadId Fixture.testPolicyId)
(healthyOutdatedSnapshotVersion + 1)

lookupUTxO :: UTxO' (TxOut CtxUTxO)
lookupUTxO =
UTxO.singleton (healthyOpenHeadTxIn, healthyOpenHeadTxOut datum)
<> registryUTxO scriptRegistry

datum :: TxOutDatum CtxUTxO
datum = toUTxOContext (mkTxOutDatumInline openDatum)

openDatum :: HeadState.State
openDatum = healthyOpenDatum healthyOutdatedSnapshot

openThreadOutput :: OpenThreadOutput
openThreadOutput =
OpenThreadOutput
{ openThreadUTxO = (healthyOpenHeadTxIn, healthyOpenHeadTxOut datum)
, openParties = healthyOnChainParties
, openContestationPeriod = healthyContestationPeriod
}

data CloseMutation
= -- | Ensures collectCom does not allow any output address but νHead.
Expand Down
18 changes: 9 additions & 9 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Close/Healthy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ module Hydra.Chain.Direct.Contract.Close.Healthy where
import Hydra.Cardano.Api
import Hydra.Prelude hiding (label)

import Cardano.Api.UTxO qualified as UTxO
import Hydra.Chain.Direct.Contract.Mutation (
addParticipationTokens,
)
import Hydra.Chain.Direct.Fixture qualified as Fixture
import Hydra.Chain.Direct.ScriptRegistry (ScriptRegistry, genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.State (splitUTxO)
import Hydra.Chain.Direct.TimeHandle (PointInTime)
import Hydra.Chain.Direct.Tx (mkHeadId, mkHeadOutput, ClosingSnapshot (..), UTxOHash (..), OpenThreadOutput (..), closeTx)
import Hydra.Chain.Direct.Tx (ClosingSnapshot (..), OpenThreadOutput (..), UTxOHash (..), closeTx, mkHeadId, mkHeadOutput)
import Hydra.ContestationPeriod (fromChain)
import Hydra.Contract.HeadState qualified as Head
import Hydra.Crypto (HydraKey, MultiSignature, aggregate, sign)
Expand All @@ -24,12 +26,10 @@ import Hydra.Ledger.Cardano.Evaluate (genValidityBoundsFromContestationPeriod)
import Hydra.Party (Party, deriveParty, partyToChain)
import Hydra.Plutus.Orphans ()
import Hydra.Snapshot as Snapshot (Snapshot (..), SnapshotNumber, SnapshotVersion)
import PlutusLedgerApi.V2 (toBuiltin, BuiltinByteString)
import PlutusLedgerApi.V2 (BuiltinByteString, toBuiltin)
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk, genForParty)
import Test.QuickCheck (elements)
import Test.QuickCheck.Instances ()
import qualified Cardano.Api.UTxO as UTxO
import Hydra.Chain.Direct.ScriptRegistry (registryUTxO, genScriptRegistry, ScriptRegistry)

healthySeed :: Int
healthySeed = 42
Expand All @@ -41,6 +41,9 @@ healthySplitUTxOInHead :: UTxO
healthySplitUTxOToDecommit :: UTxO
(healthySplitUTxOInHead, healthySplitUTxOToDecommit) = splitUTxO healthyUTxO

scriptRegistry :: ScriptRegistry
scriptRegistry = genScriptRegistry `generateWith` 42

-- NOTE: We need to use the contestation period when generating start/end tx
-- validity slots/time since if tx validity bound difference is bigger than
-- contestation period our close validator will fail
Expand Down Expand Up @@ -127,8 +130,8 @@ healthyConfirmedClosingSnapshot snapshot =
, version = Snapshot.version snapshot
}

healthyConfirmedClosingSnapshotTx :: Snapshot Tx -> (Tx, UTxO)
healthyConfirmedClosingSnapshotTx snapshot@Snapshot{version} =
healthyConfirmedClosingTx :: Snapshot Tx -> (Tx, UTxO)
healthyConfirmedClosingTx snapshot@Snapshot{version} =
(tx, lookupUTxO)
where
tx :: Tx
Expand All @@ -151,9 +154,6 @@ healthyConfirmedClosingSnapshotTx snapshot@Snapshot{version} =
UTxO.singleton (healthyOpenHeadTxIn, healthyOpenHeadTxOut datum)
<> registryUTxO scriptRegistry

scriptRegistry :: ScriptRegistry
scriptRegistry = genScriptRegistry `generateWith` healthySeed

openThreadOutput :: OpenThreadOutput
openThreadOutput =
OpenThreadOutput
Expand Down

0 comments on commit 4accf1e

Please sign in to comment.