Skip to content

Commit f4a792f

Browse files
committed
Add useful fns, change the way we create acc
1 parent 8fc57da commit f4a792f

File tree

23 files changed

+267
-157
lines changed

23 files changed

+267
-157
lines changed

hydra-cluster/test/Test/BlockfrostChainSpec.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ spec = around (onlyWithBlockfrostProjectFile . showLogsOnFailure "BlockfrostChai
104104
aliceChain `observesInTime'` OnCollectComTx{headId}
105105

106106
let snapshotVersion = 0
107-
let utxoHash = hashUTxO someUTxO
108-
utxoToCommitHash = hashUTxO @Tx mempty
109-
utxoToDecommitHash = hashUTxO @Tx mempty
110-
let accumulator = Accumulator.build [utxoHash, utxoToCommitHash, utxoToDecommitHash]
107+
let accumulator = Accumulator.buildFromUTxO someUTxO
111108
let snapshot =
112109
Snapshot
113110
{ headId

hydra-cluster/test/Test/DirectChainSpec.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,7 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
313313
aliceChain `observesInTime` OnCollectComTx{headId}
314314
let v = 0
315315
let snapshotVersion = 0
316-
utxoHash = hashUTxO someUTxO
317-
utxoToCommitHash = hashUTxO someUTxOToCommit
318-
utxoToDecommitHash = hashUTxO @Tx mempty
319-
accumulator = Accumulator.build [utxoHash, utxoToCommitHash, utxoToDecommitHash]
316+
accumulator = Accumulator.buildFromUTxO someUTxO
320317
let snapshot =
321318
Snapshot
322319
{ headId
@@ -465,10 +462,7 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
465462
_ -> Nothing
466463
let (inHead, toDecommit) = splitUTxO someUTxO
467464
-- Alice contests with some snapshot U1 -> successful
468-
let utxoHash = hashUTxO inHead
469-
utxoToCommitHash = hashUTxO @Tx mempty
470-
utxoToDecommitHash = hashUTxO toDecommit
471-
accumulator = Accumulator.build [utxoHash, utxoToCommitHash, utxoToDecommitHash]
465+
let accumulator = Accumulator.buildFromUTxO inHead
472466
let snapshot1 =
473467
Snapshot
474468
{ headId

hydra-node/src/Hydra/HeadLogic.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,7 @@ onOpenNetworkReqSn env ledger pendingDeposits currentSlot st otherParty sv sn re
425425
-- 𝑈 ← 𝑈_active ◦ Treq
426426
requireApplyTxs activeUTxO requestedTxs $ \u -> do
427427
let snapshotUTxO = u `withoutUTxO` fromMaybe mempty mUtxoToCommit
428-
utxoHash = hashUTxO snapshotUTxO
429-
utxoToCommitHash = hashUTxO $ fromMaybe mempty mUtxoToCommit
430-
utxoToDecommitHash = hashUTxO $ fromMaybe mempty mUtxoToDecommit
431-
accumulator = Accumulator.build [utxoHash, utxoToCommitHash, utxoToDecommitHash]
428+
accumulator = Accumulator.buildFromUTxO snapshotUTxO
432429
-- Spec: ŝ ← ̅S.s + 1
433430
-- NOTE: confSn == seenSn == sn here
434431
let nextSnapshot =

hydra-node/src/Hydra/Ledger/Simple.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ instance IsTx SimpleTx where
101101
, txOutputs = mempty
102102
}
103103

104+
-- \| For SimpleTx, we use a simple pair representation where both elements are the same output.
105+
toPairList = Set.toList
106+
107+
utxoToElement = toStrict . serialise . unSimpleTxOut
108+
104109
-- * Simple chain state
105110

106111
newtype SimpleChainState = SimpleChainState {slot :: ChainSlot}

hydra-node/test/Hydra/API/HTTPServerSpec.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import Hydra.Logging (nullTracer)
4444
import Hydra.Node.State (NodeState (..))
4545
import Hydra.Tx (ConfirmedSnapshot (..))
4646
import Hydra.Tx.Accumulator qualified as Accumulator
47-
import Hydra.Tx.IsTx (UTxOType, hashUTxO, txId)
47+
import Hydra.Tx.IsTx (UTxOType, txId)
4848
import Hydra.Tx.Snapshot (Snapshot (..))
4949
import System.FilePath ((</>))
5050
import System.IO.Unsafe (unsafePerformIO)
@@ -700,8 +700,7 @@ apiServerSpec = do
700700
prop "returns 200 OK on confirmed snapshot" $ do
701701
responseChannel <- newTChanIO
702702
let utxo' = mempty
703-
utxoHash = hashUTxO utxo'
704-
accumulator = Accumulator.build [utxoHash, hashUTxO @SimpleTx mempty, hashUTxO @SimpleTx mempty]
703+
accumulator = Accumulator.buildFromUTxO utxo'
705704
snapshot =
706705
Snapshot
707706
{ headId = testHeadId

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,7 @@ signedSnapshot ms =
770770
let u = realWorldModelUTxO (toCommit ms)
771771
in if UTxO.null u then Nothing else Just u
772772

773-
utxoHash = hashUTxO utxo
774-
utxoToCommitHash = hashUTxO @Tx $ fromMaybe mempty utxoToCommit
775-
utxoToDecommitHash = hashUTxO @Tx $ fromMaybe mempty utxoToDecommit
776-
accumulator = Accumulator.build [utxoHash, utxoToCommitHash, utxoToDecommitHash]
773+
accumulator = Accumulator.buildFromUTxO utxo
777774

778775
-- | A confirmed snapshot (either initial or later confirmed), based onTxTra
779776
-- 'signedSnapshot'.

hydra-node/test/Hydra/HeadLogicSnapshotSpec.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Hydra.Options (defaultContestationPeriod, defaultDepositPeriod)
1818
import Hydra.Tx.Accumulator qualified as Accumulator
1919
import Hydra.Tx.Crypto (sign)
2020
import Hydra.Tx.HeadParameters (HeadParameters (..))
21-
import Hydra.Tx.IsTx (IsTx, UTxOType, hashUTxO, txId)
21+
import Hydra.Tx.IsTx (IsTx, UTxOType, txId)
2222
import Hydra.Tx.Party (Party, deriveParty)
2323
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..), Snapshot (..), SnapshotNumber, SnapshotVersion, getSnapshot)
2424
import Test.Hydra.Tx.Fixture (
@@ -232,10 +232,7 @@ testSnapshot ::
232232
UTxOType tx ->
233233
Snapshot tx
234234
testSnapshot number version confirmed utxo =
235-
let utxoHash = hashUTxO utxo
236-
utxoToCommitHash = hashUTxO @tx mempty
237-
utxoToDecommitHash = hashUTxO @tx mempty
238-
accumulator = Accumulator.build [utxoHash, utxoToCommitHash, utxoToDecommitHash]
235+
let accumulator = Accumulator.buildFromUTxO utxo
239236
in Snapshot
240237
{ headId = testHeadId
241238
, version

hydra-node/test/Hydra/HeadLogicSpec.hs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ spec =
590590
let decommitTx1 = SimpleTx 1 (utxoRef 1) (utxoRef 3)
591591
decommitTx2 = SimpleTx 2 (utxoRef 2) (utxoRef 4)
592592
activeUTxO = utxoRefs [1, 2]
593-
utxoHash = hashUTxO activeUTxO
594593
snapshot =
595594
Snapshot
596595
{ headId = testHeadId
@@ -600,7 +599,7 @@ spec =
600599
, utxo = activeUTxO
601600
, utxoToCommit = Nothing
602601
, utxoToDecommit = Just $ utxoRefs [3]
603-
, accumulator = Accumulator.build [utxoHash, hashUTxO @SimpleTx mempty, hashUTxO @SimpleTx $ utxoRefs [3]]
602+
, accumulator = Accumulator.buildFromUTxO activeUTxO
604603
}
605604
s0 =
606605
inOpenState'
@@ -892,8 +891,7 @@ spec =
892891
prop "reject side load confirmed snapshot because wrong snapshot utxoToDecommit" $ \utxoToDecommit -> do
893892
getConfirmedSnapshot startingState `shouldBe` Just snapshot1
894893
let utxo' = utxoRef 3
895-
utxoHash = hashUTxO utxo'
896-
accumulator = Accumulator.build [utxoHash, hashUTxO @SimpleTx mempty, hashUTxO @SimpleTx $ fromMaybe mempty (Just utxoToDecommit)]
894+
accumulator = Accumulator.buildFromUTxO utxo'
897895
snapshot2 = Snapshot testHeadId 0 2 [tx2] utxo' Nothing (Just utxoToDecommit) accumulator
898896
multisig2 = aggregate [sign aliceSk snapshot2, sign bobSk snapshot2]
899897

@@ -904,8 +902,7 @@ spec =
904902
getConfirmedSnapshot startingState `shouldBe` Just snapshot1
905903

906904
let utxo' = utxoRef 3
907-
utxoHash = hashUTxO utxo'
908-
accumulator = Accumulator.build [utxoHash, hashUTxO @SimpleTx $ fromMaybe mempty (Just utxoToCommit), hashUTxO @SimpleTx mempty]
905+
accumulator = Accumulator.buildFromUTxO utxo'
909906
snapshot2 = Snapshot testHeadId 0 2 [tx2] utxo' (Just utxoToCommit) Nothing accumulator
910907
multisig2 = aggregate [sign aliceSk snapshot2, sign bobSk snapshot2]
911908

@@ -1430,14 +1427,13 @@ testSnapshot ::
14301427
UTxOType tx ->
14311428
Snapshot tx
14321429
testSnapshot number version confirmed utxo =
1433-
let utxoHash = hashUTxO utxo
1434-
in Snapshot
1435-
{ headId = testHeadId
1436-
, version
1437-
, number
1438-
, confirmed
1439-
, utxo
1440-
, utxoToCommit = mempty
1441-
, utxoToDecommit = mempty
1442-
, accumulator = Accumulator.build [utxoHash, hashUTxO @tx mempty, hashUTxO @tx mempty]
1443-
}
1430+
Snapshot
1431+
{ headId = testHeadId
1432+
, version
1433+
, number
1434+
, confirmed
1435+
, utxo
1436+
, utxoToCommit = mempty
1437+
, utxoToDecommit = mempty
1438+
, accumulator = Accumulator.buildFromUTxO utxo
1439+
}

hydra-node/test/Hydra/Model/Payment.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ instance IsTx Payment where
9494
result = Set.toList $ Set.fromList as \\ Set.fromList bs
9595
in second fromList <$> result
9696

97+
-- For Payment, UTxO is already a list of pairs
98+
toPairList = id
99+
100+
-- For Payment, just use show for serialization (consistent with hashUTxO)
101+
utxoToElement = encodeUtf8 . show @Text
102+
97103
applyTx :: UTxOType Payment -> Payment -> UTxOType Payment
98104
applyTx utxo Payment{from, to, value} =
99105
(to, value) : List.delete (from, value) utxo

hydra-tx/hydra-tx.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ library
102102
, ouroboros-consensus
103103
, ouroboros-consensus-cardano
104104
, plutus-ledger-api
105+
, plutus-tx
105106
, prettyprinter
106107
, QuickCheck
107108
, quickcheck-instances

0 commit comments

Comments
 (0)