Skip to content

Commit 0fbcffe

Browse files
authored
Merge pull request #1385 from input-output-hk/tx-trace-spec
Add a test suite for testing consecutive close/contest transactions
2 parents 4e1e1ed + c7171e2 commit 0fbcffe

File tree

12 files changed

+433
-44
lines changed

12 files changed

+433
-44
lines changed

hydra-node/hydra-node.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ test-suite tests
290290
Hydra.Chain.Direct.StateSpec
291291
Hydra.Chain.Direct.TimeHandleSpec
292292
Hydra.Chain.Direct.TxSpec
293+
Hydra.Chain.Direct.TxTraceSpec
293294
Hydra.Chain.Direct.WalletSpec
294295
Hydra.ContestationPeriodSpec
295296
Hydra.CryptoSpec
@@ -363,6 +364,7 @@ test-suite tests
363364
, lens-aeson
364365
, plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib} >=1.1.1.0
365366
, plutus-tx
367+
, pretty-simple
366368
, QuickCheck
367369
, quickcheck-dynamic >=3.3.1 && <3.4
368370
, quickcheck-instances

hydra-node/src/Hydra/Chain/Direct/State.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,9 @@ collect ctx headId headParameters utxoToCollect spendableUTxO = do
479479

480480
ChainContext{networkId, ownVerificationKey, scriptRegistry} = ctx
481481

482-
-- | Construct a close transaction based on the 'OpenState' and a confirmed
483-
-- snapshot.
484-
-- - 'SlotNo' parameter will be used as the 'Tx' lower bound.
485-
-- - 'PointInTime' parameter will be used as an upper validity bound and
486-
-- will define the start of the contestation period.
487-
-- NB: lower and upper bound slot difference should not exceed contestation period
482+
-- | Construct a close transaction spending the head output in given 'UTxO',
483+
-- head parameters, and a confirmed snapshot. NOTE: Lower and upper bound slot
484+
-- difference should not exceed contestation period.
488485
close ::
489486
ChainContext ->
490487
-- | Spendable UTxO containing head, initial and commit outputs
@@ -533,6 +530,7 @@ contest ::
533530
HeadId ->
534531
ContestationPeriod ->
535532
ConfirmedSnapshot Tx ->
533+
-- | Current slot and posix time to be used as the contestation time.
536534
PointInTime ->
537535
Either ContestTxError Tx
538536
contest ctx spendableUTxO headId contestationPeriod confirmedSnapshot pointInTime = do

hydra-node/src/Hydra/Snapshot.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ instance (Typeable tx, ToCBOR (UTxOType tx), ToCBOR (TxIdType tx)) => ToCBOR (Sn
8282
instance (Typeable tx, FromCBOR (UTxOType tx), FromCBOR (TxIdType tx)) => FromCBOR (Snapshot tx) where
8383
fromCBOR = Snapshot <$> fromCBOR <*> fromCBOR <*> fromCBOR <*> fromCBOR
8484

85-
-- | A snapshot that can be used to close a head with. Either the initial one, or when it was signed by all parties, i.e. it is confirmed.
85+
-- | A snapshot that can be used to close a head with. Either the initial one,
86+
-- or when it was signed by all parties, i.e. it is confirmed.
8687
data ConfirmedSnapshot tx
8788
= InitialSnapshot
88-
{ headId :: HeadId
89+
{ -- XXX: 'headId' is actually unused. Only 'getSnapshot' forces this to exist.
90+
headId :: HeadId
8991
, initialUTxO :: UTxOType tx
9092
}
9193
| ConfirmedSnapshot

hydra-node/test/Hydra/Chain/Direct/Contract/Abort.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Cardano.Api.UTxO qualified as UTxO
1111
import Data.List qualified as List
1212
import Data.Map qualified as Map
1313
import Hydra.Chain (HeadParameters (..))
14-
import Hydra.Chain.Direct.Contract.Gen (genForParty)
1514
import Hydra.Chain.Direct.Contract.Mutation (
1615
Mutation (..),
1716
SomeMutation (..),
@@ -42,7 +41,7 @@ import Hydra.Contract.Initial qualified as Initial
4241
import Hydra.Contract.InitialError (InitialError (STNotBurned))
4342
import Hydra.Ledger.Cardano (genAddressInEra, genVerificationKey)
4443
import Hydra.Party (Party, partyToChain)
45-
import Test.Hydra.Fixture (cperiod)
44+
import Test.Hydra.Fixture (cperiod, genForParty)
4645
import Test.QuickCheck (Property, choose, counterexample, elements, oneof, shuffle, suchThat)
4746

4847
--

hydra-node/test/Hydra/Chain/Direct/Contract/Close.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Hydra.Prelude hiding (label)
88

99
import Cardano.Api.UTxO as UTxO
1010
import Data.Maybe (fromJust)
11-
import Hydra.Chain.Direct.Contract.Gen (genForParty, genHash, genMintedOrBurnedValue)
11+
import Hydra.Chain.Direct.Contract.Gen (genHash, genMintedOrBurnedValue)
1212
import Hydra.Chain.Direct.Contract.Mutation (
1313
Mutation (..),
1414
SomeMutation (..),
@@ -24,7 +24,6 @@ import Hydra.Chain.Direct.Contract.Mutation (
2424
replaceSnapshotNumber,
2525
replaceUtxoHash,
2626
)
27-
import Hydra.Chain.Direct.Fixture (testNetworkId)
2827
import Hydra.Chain.Direct.Fixture qualified as Fixture
2928
import Hydra.Chain.Direct.ScriptRegistry (genScriptRegistry, registryUTxO)
3029
import Hydra.Chain.Direct.TimeHandle (PointInTime)
@@ -47,7 +46,7 @@ import Hydra.Plutus.Orphans ()
4746
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
4847
import PlutusLedgerApi.V1.Time (DiffMilliSeconds (..), fromMilliSeconds)
4948
import PlutusLedgerApi.V2 (BuiltinByteString, POSIXTime, PubKeyHash (PubKeyHash), toBuiltin)
50-
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk)
49+
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk, genForParty)
5150
import Test.QuickCheck (arbitrarySizedNatural, choose, elements, listOf1, oneof, suchThat)
5251
import Test.QuickCheck.Instances ()
5352

@@ -136,7 +135,7 @@ healthyOpenHeadTxIn = generateWith arbitrary 42
136135

137136
healthyOpenHeadTxOut :: TxOut CtxUTxO
138137
healthyOpenHeadTxOut =
139-
mkHeadOutput testNetworkId Fixture.testPolicyId headTxOutDatum
138+
mkHeadOutput Fixture.testNetworkId Fixture.testPolicyId headTxOutDatum
140139
& addParticipationTokens healthyParticipants
141140
where
142141
headTxOutDatum = toUTxOContext (mkTxOutDatumInline healthyOpenHeadDatum)
@@ -293,7 +292,7 @@ genCloseMutation :: (Tx, UTxO) -> Gen SomeMutation
293292
genCloseMutation (tx, _utxo) =
294293
oneof
295294
[ SomeMutation (Just $ toErrorCode NotPayingToHead) NotContinueContract <$> do
296-
mutatedAddress <- genAddressInEra testNetworkId
295+
mutatedAddress <- genAddressInEra Fixture.testNetworkId
297296
pure $ ChangeOutput 0 (modifyTxOutAddress (const mutatedAddress) headTxOut)
298297
, SomeMutation (Just $ toErrorCode SignatureVerificationFailed) MutateSignatureButNotSnapshotNumber . ChangeHeadRedeemer <$> do
299298
Head.Close . toPlutusSignatures <$> (arbitrary :: Gen (MultiSignature (Snapshot Tx)))

hydra-node/test/Hydra/Chain/Direct/Contract/CollectCom.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Data.List qualified as List
1111
import Data.Map qualified as Map
1212
import Data.Maybe (fromJust)
1313
import Hydra.Chain (HeadParameters (..))
14-
import Hydra.Chain.Direct.Contract.Gen (genForParty, genHash, genMintedOrBurnedValue)
14+
import Hydra.Chain.Direct.Contract.Gen (genHash, genMintedOrBurnedValue)
1515
import Hydra.Chain.Direct.Contract.Mutation (
1616
Mutation (..),
1717
SomeMutation (..),
@@ -52,6 +52,7 @@ import Hydra.OnChainId (OnChainId)
5252
import Hydra.Party (Party, partyToChain)
5353
import Hydra.Plutus.Orphans ()
5454
import PlutusTx.Builtins (toBuiltin)
55+
import Test.Hydra.Fixture (genForParty)
5556
import Test.QuickCheck (choose, elements, oneof, suchThat)
5657
import Test.QuickCheck.Instances ()
5758

hydra-node/test/Hydra/Chain/Direct/Contract/Contest.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Hydra.Prelude hiding (label)
99
import Data.Maybe (fromJust)
1010

1111
import Cardano.Api.UTxO as UTxO
12-
import Hydra.Chain.Direct.Contract.Gen (genForParty, genHash, genMintedOrBurnedValue)
12+
import Hydra.Chain.Direct.Contract.Gen (genHash, genMintedOrBurnedValue)
1313
import Hydra.Chain.Direct.Contract.Mutation (
1414
Mutation (..),
1515
SomeMutation (..),
@@ -48,7 +48,7 @@ import Hydra.Plutus.Orphans ()
4848
import Hydra.Snapshot (Snapshot (..), SnapshotNumber)
4949
import PlutusLedgerApi.V2 (BuiltinByteString, toBuiltin)
5050
import PlutusLedgerApi.V2 qualified as Plutus
51-
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk)
51+
import Test.Hydra.Fixture (aliceSk, bobSk, carolSk, genForParty)
5252
import Test.QuickCheck (arbitrarySizedNatural, elements, listOf, listOf1, oneof, suchThat, vectorOf)
5353
import Test.QuickCheck.Gen (choose)
5454
import Test.QuickCheck.Instances ()

hydra-node/test/Hydra/Chain/Direct/Contract/Gen.hs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
-- | Generators used in mutation testing framework
22
module Hydra.Chain.Direct.Contract.Gen where
33

4-
import Cardano.Crypto.Hash (hashToBytes)
5-
import Codec.CBOR.Magic (uintegerFromBytes)
64
import Data.ByteString qualified as BS
75
import Hydra.Cardano.Api
86
import Hydra.Chain.Direct.Fixture qualified as Fixtures
97
import Hydra.Contract.HeadTokens (headPolicyId)
108
import Hydra.Contract.Util (hydraHeadV1)
11-
import Hydra.Crypto (Hash (HydraKeyHash))
12-
import Hydra.Party (Party (..))
139
import Hydra.Prelude
1410
import PlutusTx.Builtins (fromBuiltin)
1511
import Test.QuickCheck (oneof, suchThat, vector)
1612

1713
-- * Party / key utilities
1814

19-
-- | Generate some 'a' given the Party as a seed. NOTE: While this is useful to
20-
-- generate party-specific values, it DOES depend on the generator used. For
21-
-- example, `genForParty genVerificationKey` and `genForParty (fst <$>
22-
-- genKeyPair)` do not yield the same verification keys!
23-
genForParty :: Gen a -> Party -> a
24-
genForParty gen Party{vkey} =
25-
generateWith gen seed
26-
where
27-
seed =
28-
fromIntegral
29-
. uintegerFromBytes
30-
. hydraKeyHashToBytes
31-
$ verificationKeyHash vkey
32-
33-
hydraKeyHashToBytes (HydraKeyHash h) = hashToBytes h
34-
3515
genBytes :: Gen ByteString
3616
genBytes = arbitrary
3717

hydra-node/test/Hydra/Chain/Direct/Contract/Init.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Hydra.Prelude
1010
import Cardano.Api.UTxO qualified as UTxO
1111
import Data.Maybe (fromJust)
1212
import Hydra.Chain (HeadParameters (..))
13-
import Hydra.Chain.Direct.Contract.Gen (genForParty)
1413
import Hydra.Chain.Direct.Contract.Mutation (
1514
Mutation (..),
1615
SomeMutation (..),
@@ -28,6 +27,7 @@ import Hydra.Ledger.Cardano (genOneUTxOFor, genValue)
2827
import Hydra.OnChainId (OnChainId, genOnChainId)
2928
import Hydra.Party (Party)
3029
import PlutusLedgerApi.Test.Examples qualified as Plutus
30+
import Test.Hydra.Fixture (genForParty)
3131
import Test.QuickCheck (choose, elements, oneof, suchThat, vectorOf)
3232
import Prelude qualified
3333

hydra-node/test/Hydra/Chain/Direct/TxSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import Data.Map qualified as Map
1717
import Data.Text qualified as T
1818
import Hydra.Cardano.Api.Pretty (renderTx)
1919
import Hydra.Chain (HeadParameters (..))
20-
import Hydra.Chain.Direct.Contract.Gen (genForParty)
2120
import Hydra.Chain.Direct.Fixture (
2221
epochInfo,
2322
pparams,
@@ -36,6 +35,7 @@ import Hydra.Contract.Initial qualified as Initial
3635
import Hydra.Ledger.Cardano (adaOnly, genOneUTxOFor, genVerificationKey)
3736
import Hydra.Ledger.Cardano.Evaluate (EvaluationReport, maxTxExecutionUnits)
3837
import Hydra.Party (Party)
38+
import Test.Hydra.Fixture (genForParty)
3939
import Test.QuickCheck (
4040
Property,
4141
choose,

0 commit comments

Comments
 (0)