Skip to content

Commit a7c75f2

Browse files
committed
TxTrace: Generate separatelly values for utxo and utxo to decommit
1 parent 7f97ce6 commit a7c75f2

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
2020
import Hydra.Cardano.Api (
2121
SlotNo (..),
2222
mkTxOutDatumInline,
23+
renderUTxO,
2324
selectLovelace,
2425
throwError,
2526
txOutAddress,
@@ -39,7 +40,7 @@ import Hydra.Ledger (hashUTxO, utxoFromTx)
3940
import Hydra.Ledger.Cardano (Tx, adjustUTxO, genTxOut, genUTxO1)
4041
import Hydra.Ledger.Cardano.Evaluate (evaluateTx)
4142
import Hydra.Party (partyToChain)
42-
import Hydra.Snapshot (ConfirmedSnapshot (..), Snapshot (..), SnapshotNumber (..), number)
43+
import Hydra.Snapshot (ConfirmedSnapshot (..), Snapshot (..), SnapshotNumber (..), getSnapshot, number)
4344
import PlutusTx.Builtins (toBuiltin)
4445
import Test.Hydra.Fixture qualified as Fixture
4546
import Test.QuickCheck (Property, Smart (..), checkCoverage, cover, elements, forAll, frequency, ioProperty, oneof)
@@ -376,7 +377,7 @@ instance RunModel Model AppM where
376377
let fannedOut = utxoFromTx tx
377378
-- counterexamplePost ("Fanned out UTxO does not match: " <> renderUTxO fannedOut)
378379
-- counterexamplePost ("SnapshotUTxO: " <> renderUTxO (snapshotUTxO snapshot))
379-
guard $ sorted fannedOut == sorted (generateUTxOFromModelSnapshot snapshot)
380+
guard $ sorted fannedOut == sorted (fst $ generateUTxOFromModelSnapshot snapshot)
380381

381382
expectValid result $ \case
382383
Tx.Fanout{} -> pure ()
@@ -436,10 +437,11 @@ allActors :: [Actor]
436437
allActors = [Alice, Bob, Carol]
437438

438439
-- | A "random" UTxO distribution for a given 'ModelSnapshot'.
439-
generateUTxOFromModelSnapshot :: ModelSnapshot -> UTxO
440+
generateUTxOFromModelSnapshot :: ModelSnapshot -> (UTxO, UTxO)
440441
generateUTxOFromModelSnapshot snapshot =
441-
foldMap go (snapshotUTxO snapshot)
442-
<> foldMap go (decommitUTxO snapshot)
442+
( foldMap go (snapshotUTxO snapshot)
443+
, foldMap go (decommitUTxO snapshot)
444+
)
443445
where
444446
go modelUTxO =
445447
(`generateWith` fromEnum modelUTxO) $ genUTxO1 genTxOut
@@ -449,8 +451,7 @@ decommitSnapshot :: ModelSnapshot -> (Snapshot Tx, MultiSignature (Snapshot Tx))
449451
decommitSnapshot ms =
450452
(snapshot, signatures)
451453
where
452-
utxo = generateUTxOFromModelSnapshot ms{decommitUTxO = mempty}
453-
toDecommit = generateUTxOFromModelSnapshot ms{snapshotUTxO = mempty}
454+
(utxo, toDecommit) = generateUTxOFromModelSnapshot ms{snapshotUTxO = mempty}
454455
snapshot =
455456
Snapshot
456457
{ headId = mkHeadId Fixture.testPolicyId
@@ -472,7 +473,7 @@ signedSnapshot ms =
472473
{ headId = mkHeadId Fixture.testPolicyId
473474
, number = snapshotNumber ms
474475
, confirmed = []
475-
, utxo = generateUTxOFromModelSnapshot ms
476+
, utxo = fst $ generateUTxOFromModelSnapshot ms
476477
, utxoToDecommit = Nothing
477478
}
478479
signatures = aggregate [sign sk snapshot | sk <- [Fixture.aliceSk, Fixture.bobSk, Fixture.carolSk]]
@@ -487,7 +488,7 @@ confirmedSnapshot modelSnapshot@ModelSnapshot{snapshotNumber} =
487488
{ -- -- NOTE: The close validator would not check headId on close with
488489
-- initial snapshot, but we need to provide it still.
489490
headId = mkHeadId Fixture.testPolicyId
490-
, initialUTxO = generateUTxOFromModelSnapshot (ModelSnapshot 0 (fromList [A]) (fromList []))
491+
, initialUTxO = fst $ generateUTxOFromModelSnapshot (ModelSnapshot 0 (fromList [A]) (fromList []))
491492
}
492493
_ -> ConfirmedSnapshot{snapshot, signatures}
493494
where
@@ -508,7 +509,7 @@ openHeadUTxO =
508509
mkTxOutDatumInline
509510
Head.Open
510511
{ parties = partyToChain <$> [Fixture.alice, Fixture.bob, Fixture.carol]
511-
, utxoHash = toBuiltin $ hashUTxO @Tx $ generateUTxOFromModelSnapshot (ModelSnapshot 0 (fromList [A]) (fromList []))
512+
, utxoHash = toBuiltin $ hashUTxO @Tx $ fst $ generateUTxOFromModelSnapshot (ModelSnapshot 0 (fromList [A]) (fromList []))
512513
, contestationPeriod = CP.toChain Fixture.cperiod
513514
, headId = headIdToCurrencySymbol $ mkHeadId Fixture.testPolicyId
514515
, snapshotNumber = 0
@@ -534,15 +535,17 @@ newDecrementTx actor (snapshot, signatures) = do
534535
newCloseTx :: HasCallStack => Actor -> ConfirmedSnapshot Tx -> AppM Tx
535536
newCloseTx actor snapshot = do
536537
spendableUTxO <- get
537-
either (failure . show) pure $
538-
close
539-
(actorChainContext actor)
540-
spendableUTxO
541-
(mkHeadId Fixture.testPolicyId)
542-
Fixture.testHeadParameters
543-
snapshot
544-
lowerBound
545-
upperBound
538+
traceShow "close utxo" $
539+
traceShow (renderUTxO $ utxo $ getSnapshot snapshot) $
540+
either (failure . show) pure $
541+
close
542+
(actorChainContext actor)
543+
spendableUTxO
544+
(mkHeadId Fixture.testPolicyId)
545+
Fixture.testHeadParameters
546+
snapshot
547+
lowerBound
548+
upperBound
546549
where
547550
lowerBound = 0
548551

@@ -554,14 +557,16 @@ newCloseTx actor snapshot = do
554557
newContestTx :: HasCallStack => Actor -> ConfirmedSnapshot Tx -> AppM Tx
555558
newContestTx actor snapshot = do
556559
spendableUTxO <- get
557-
either (failure . show) pure $
558-
contest
559-
(actorChainContext actor)
560-
spendableUTxO
561-
(mkHeadId Fixture.testPolicyId)
562-
Fixture.cperiod
563-
snapshot
564-
currentTime
560+
traceShow "contest utxo" $
561+
traceShow (renderUTxO $ utxo $ getSnapshot snapshot) $
562+
either (failure . show) pure $
563+
contest
564+
(actorChainContext actor)
565+
spendableUTxO
566+
(mkHeadId Fixture.testPolicyId)
567+
Fixture.cperiod
568+
snapshot
569+
currentTime
565570
where
566571
currentTime = (0, posixSecondsToUTCTime 0)
567572

@@ -572,14 +577,16 @@ newFanoutTx :: Actor -> ModelSnapshot -> AppM (Either FanoutTxError Tx)
572577
newFanoutTx actor snapshot = do
573578
spendableUTxO <- get
574579
let (snapshot', _) = signedSnapshot snapshot
575-
pure $
576-
fanout
577-
(actorChainContext actor)
578-
spendableUTxO
579-
Fixture.testSeedInput
580-
(utxo snapshot')
581-
(utxoToDecommit snapshot')
582-
deadline
580+
traceShow "fanout utxo" $
581+
traceShow (renderUTxO $ utxo snapshot') $
582+
pure $
583+
fanout
584+
(actorChainContext actor)
585+
spendableUTxO
586+
Fixture.testSeedInput
587+
(utxo snapshot')
588+
(utxoToDecommit snapshot')
589+
deadline
583590
where
584591
CP.UnsafeContestationPeriod contestationPeriod = Fixture.cperiod
585592
deadline = SlotNo $ fromIntegral contestationPeriod * fromIntegral (length allActors)

0 commit comments

Comments
 (0)