Skip to content

Commit 5def3a0

Browse files
vrom911v0d1ch
authored andcommitted
Add toCommit toDecommit utxo in accumulator, address review
1 parent 7a98e96 commit 5def3a0

File tree

17 files changed

+68
-34
lines changed

17 files changed

+68
-34
lines changed

hydra-cluster/test/Test/DirectChainSpec.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,15 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
313313
aliceChain `observesInTime` OnCollectComTx{headId}
314314
let v = 0
315315
let snapshotVersion = 0
316-
accumulator = Accumulator.buildFromUTxO someUTxO
316+
utxoToCommit = Just someUTxOToCommit
317+
accumulator = Accumulator.buildFromSnapshotUTxOs someUTxO utxoToCommit Nothing
317318
let snapshot =
318319
Snapshot
319320
{ headId
320321
, number = 1
321322
, utxo = someUTxO
322323
, confirmed = []
323-
, utxoToCommit = Just someUTxOToCommit
324+
, utxoToCommit = utxoToCommit
324325
, utxoToDecommit = Nothing
325326
, version = snapshotVersion
326327
, accumulator
@@ -462,15 +463,16 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
462463
_ -> Nothing
463464
let (inHead, toDecommit) = splitUTxO someUTxO
464465
-- Alice contests with some snapshot U1 -> successful
465-
let accumulator = Accumulator.buildFromUTxO inHead
466+
let utxoToDecommit = Just toDecommit
467+
let accumulator = Accumulator.buildFromSnapshotUTxOs inHead Nothing utxoToDecommit
466468
let snapshot1 =
467469
Snapshot
468470
{ headId
469471
, number = 1
470472
, utxo = inHead
471473
, confirmed = []
472474
, utxoToCommit = Nothing
473-
, utxoToDecommit = Just toDecommit
475+
, utxoToDecommit = utxoToDecommit
474476
, version = 0
475477
, accumulator
476478
}

hydra-node/src/Hydra/HeadLogic.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +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-
accumulator = Accumulator.buildFromUTxO snapshotUTxO
428+
accumulator = Accumulator.buildFromSnapshotUTxOs snapshotUTxO mUtxoToCommit mUtxoToDecommit
429429
-- Spec: ŝ ← ̅S.s + 1
430430
-- NOTE: confSn == seenSn == sn here
431431
let nextSnapshot =

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

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

773-
accumulator = Accumulator.buildFromUTxO utxo
773+
accumulator = Accumulator.buildFromSnapshotUTxOs utxo utxoToCommit utxoToDecommit
774774

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

hydra-node/test/Hydra/HeadLogicSpec.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ spec =
592592
let decommitTx1 = SimpleTx 1 (utxoRef 1) (utxoRef 3)
593593
decommitTx2 = SimpleTx 2 (utxoRef 2) (utxoRef 4)
594594
activeUTxO = utxoRefs [1, 2]
595+
utxoToDecommit = Just $ utxoRefs [3]
595596
snapshot =
596597
Snapshot
597598
{ headId = testHeadId
@@ -600,8 +601,8 @@ spec =
600601
, confirmed = []
601602
, utxo = activeUTxO
602603
, utxoToCommit = Nothing
603-
, utxoToDecommit = Just $ utxoRefs [3]
604-
, accumulator = Accumulator.buildFromUTxO activeUTxO
604+
, utxoToDecommit = utxoToDecommit
605+
, accumulator = Accumulator.buildFromSnapshotUTxOs activeUTxO Nothing utxoToDecommit
605606
}
606607
s0 =
607608
inOpenState'
@@ -893,8 +894,9 @@ spec =
893894
prop "reject side load confirmed snapshot because wrong snapshot utxoToDecommit" $ \utxoToDecommit -> do
894895
getConfirmedSnapshot startingState `shouldBe` Just snapshot1
895896
let utxo' = utxoRef 3
896-
accumulator = Accumulator.buildFromUTxO utxo'
897-
snapshot2 = Snapshot testHeadId 0 2 [tx2] utxo' Nothing (Just utxoToDecommit) accumulator
897+
utxoToDecom = Just utxoToDecommit
898+
accumulator = Accumulator.buildFromSnapshotUTxOs utxo' Nothing utxoToDecom
899+
snapshot2 = Snapshot testHeadId 0 2 [tx2] utxo' Nothing utxoToDecom accumulator
898900
multisig2 = aggregate [sign aliceSk snapshot2, sign bobSk snapshot2]
899901

900902
update bobEnv ledger startingState (ClientInput (SideLoadSnapshot $ ConfirmedSnapshot snapshot2 multisig2))
@@ -904,8 +906,9 @@ spec =
904906
getConfirmedSnapshot startingState `shouldBe` Just snapshot1
905907

906908
let utxo' = utxoRef 3
907-
accumulator = Accumulator.buildFromUTxO utxo'
908-
snapshot2 = Snapshot testHeadId 0 2 [tx2] utxo' (Just utxoToCommit) Nothing accumulator
909+
utxoToCom = Just utxoToCommit
910+
accumulator = Accumulator.buildFromSnapshotUTxOs utxo' utxoToCom Nothing
911+
snapshot2 = Snapshot testHeadId 0 2 [tx2] utxo' utxoToCom Nothing accumulator
909912
multisig2 = aggregate [sign aliceSk snapshot2, sign bobSk snapshot2]
910913

911914
update bobEnv ledger startingState (ClientInput (SideLoadSnapshot $ ConfirmedSnapshot snapshot2 multisig2))

hydra-tx/src/Hydra/Tx/Accumulator.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Hydra.Tx.Accumulator (
55
getAccumulatorHash,
66
build,
77
buildFromUTxO,
8+
buildFromSnapshotUTxOs,
89

910
-- * CRS (Common Reference String)
1011
generateCRS,
@@ -63,6 +64,34 @@ buildFromUTxO utxo =
6364
let elements = utxoToElement @tx <$> toPairList @tx utxo
6465
in build elements
6566

67+
-- | Build an accumulator from snapshot UTxOs, including commit and decommit UTxOs.
68+
--
69+
-- This function combines all UTxOs that could potentially be fanned out:
70+
-- - The main snapshot UTxO
71+
-- - UTxOs to be committed (deposited into the Head)
72+
-- - UTxOs to be decommitted (withdrawn from the Head)
73+
buildFromSnapshotUTxOs ::
74+
forall tx.
75+
IsTx tx =>
76+
-- | The main snapshot UTxO set
77+
UTxOType tx ->
78+
-- | UTxOs to be committed (if any)
79+
Maybe (UTxOType tx) ->
80+
-- | UTxOs to be decommitted (if any)
81+
Maybe (UTxOType tx) ->
82+
-- | The resulting accumulator containing all UTxOs
83+
HydraAccumulator
84+
buildFromSnapshotUTxOs utxo mUtxoToCommit mUtxoToDecommit =
85+
let
86+
-- Combine all UTxOs that could be fanned out
87+
-- Note: For Map-based UTxO types, `<>` performs union (left-biased for same keys)
88+
-- If utxoToCommit and utxoToDecommit overlap (protocol violation), union deduplicates by TxIn
89+
utxoToCommit = fromMaybe mempty mUtxoToCommit
90+
utxoToDecommit = fromMaybe mempty mUtxoToDecommit
91+
combinedUTxO = utxo <> utxoToCommit <> utxoToDecommit
92+
in
93+
buildFromUTxO @tx combinedUTxO
94+
6695
-- | Get a simple hash of the accumulator state.
6796
--
6897
-- This is a pure function that returns a deterministic hash of the accumulator's contents.

hydra-tx/src/Hydra/Tx/Close.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ closeTx scriptRegistry vk headId openVersion confirmedSnapshot startSlotNo (endS
148148
}
149149
where
150150
snapshot = getSnapshot confirmedSnapshot
151-
snapshotUtxo = Hydra.Tx.utxo snapshot
152-
closedAccumulatorHash = Accumulator.getAccumulatorHash $ Accumulator.buildFromUTxO snapshotUtxo
151+
closedAccumulatorHash = Accumulator.getAccumulatorHash $ accumulator snapshot
153152

154153
contestationDeadline =
155154
addContestationPeriod (posixFromUTCTime utcTime) openContestationPeriod

hydra-tx/src/Hydra/Tx/Contest.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ contestTx scriptRegistry vk headId contestationPeriod openVersion snapshot sig (
6767
& setTxValidityUpperBound (TxValidityUpperBound slotNo)
6868
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "ContestTx")
6969
where
70-
Snapshot{number, version, utxo, utxoToCommit, utxoToDecommit} = snapshot
70+
Snapshot{number, version, utxo, utxoToCommit, utxoToDecommit, accumulator} = snapshot
7171

7272
ClosedThreadOutput
7373
{ closedThreadUTxO = (headInput, headOutputBefore)
@@ -149,7 +149,7 @@ contestTx scriptRegistry vk headId contestationPeriod openVersion snapshot sig (
149149
, accumulatorHash = toBuiltin contestAccumulatorHash
150150
}
151151
where
152-
contestAccumulatorHash = Accumulator.getAccumulatorHash $ Accumulator.buildFromUTxO utxo
152+
contestAccumulatorHash = Accumulator.getAccumulatorHash accumulator
153153

154154
-- * Observation
155155

hydra-tx/src/Hydra/Tx/Decrement.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ decrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snap
6262

6363
utxoHash = toBuiltin $ hashUTxO @Tx utxo
6464

65-
decrementAccumulatorHash = Accumulator.getAccumulatorHash $ Accumulator.buildFromUTxO utxo
65+
decrementAccumulatorHash = Accumulator.getAccumulatorHash accumulator
6666

6767
HeadParameters{parties, contestationPeriod} = headParameters
6868

@@ -93,7 +93,7 @@ decrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snap
9393
, version = toInteger version + 1
9494
}
9595

96-
Snapshot{utxo, utxoToDecommit, number, version} = snapshot
96+
Snapshot{utxo, utxoToDecommit, number, version, accumulator} = snapshot
9797

9898
-- * Observation
9999

hydra-tx/src/Hydra/Tx/Increment.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ incrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snap
8282

8383
utxoHash = toBuiltin $ hashUTxO @Tx utxo
8484

85-
incrementAccumulatorHash = Accumulator.getAccumulatorHash $ Accumulator.buildFromUTxO utxo
85+
incrementAccumulatorHash = Accumulator.getAccumulatorHash accumulator
8686

8787
headDatumAfter =
8888
mkTxOutDatumInline $
@@ -107,7 +107,7 @@ incrementTx scriptRegistry vk headId headParameters (headInput, headOutput) snap
107107
ScriptWitness scriptWitnessInCtx $
108108
mkScriptWitness depositValidatorScript InlineScriptDatum depositRedeemer
109109

110-
Snapshot{utxo, utxoToCommit, version, number} = snapshot
110+
Snapshot{utxo, utxoToCommit, version, number, accumulator} = snapshot
111111

112112
-- * Observation
113113

hydra-tx/src/Hydra/Tx/Snapshot.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ instance IsTx tx => FromJSON (Snapshot tx) where
137137
Left _ -> fail "Failed to deserialize accumulator"
138138
Right acc -> pure $ Accumulator.HydraAccumulator acc
139139
_ -> do
140-
-- Reconstruct accumulator from utxo hashes for backward compatibility (or if empty)
141-
pure $ Accumulator.buildFromUTxO utxo
140+
-- Reconstruct accumulator from all UTxOs (including commit/decommit) for backward compatibility (or if empty)
141+
pure $ Accumulator.buildFromSnapshotUTxOs utxo utxoToCommit utxoToDecommit
142142
pure $ Snapshot{headId, version, number, confirmed, utxo, utxoToCommit, utxoToDecommit, accumulator}
143143
where
144144
parseBase16 :: Text -> Parser ByteString
@@ -156,12 +156,12 @@ instance (Arbitrary tx, Arbitrary (UTxOType tx), IsTx tx) => Arbitrary (Snapshot
156156
utxo <- arbitrary
157157
utxoToCommit <- arbitrary
158158
utxoToDecommit <- arbitrary
159-
let accumulator = Accumulator.buildFromUTxO utxo
159+
let accumulator = Accumulator.buildFromSnapshotUTxOs utxo utxoToCommit utxoToDecommit
160160
pure $ Snapshot{headId, version, number, confirmed, utxo, utxoToCommit, utxoToDecommit, accumulator}
161161

162162
-- NOTE: See note on 'Arbitrary (ClientInput tx)'
163163
shrink Snapshot{headId, version, number, utxo, confirmed, utxoToCommit, utxoToDecommit} =
164-
[ let accumulator = Accumulator.buildFromUTxO utxo'
164+
[ let accumulator = Accumulator.buildFromSnapshotUTxOs utxo' utxoToCommit' utxoToDecommit'
165165
in Snapshot headId version number confirmed' utxo' utxoToCommit' utxoToDecommit' accumulator
166166
| confirmed' <- shrink confirmed
167167
, utxo' <- shrink utxo
@@ -252,7 +252,7 @@ genConfirmedSnapshot headId version minSn utxo utxoToCommit utxoToDecommit sks
252252
-- FIXME: This is another nail in the coffin to our current modeling of
253253
-- snapshots
254254
number <- arbitrary `suchThat` (> minSn)
255-
let accumulator = Accumulator.buildFromUTxO utxo
255+
let accumulator = Accumulator.buildFromSnapshotUTxOs utxo utxoToCommit utxoToDecommit
256256
snapshot = Snapshot{headId, version, number, confirmed = [], utxo, utxoToCommit, utxoToDecommit, accumulator}
257257
let signatures = aggregate $ fmap (`sign` snapshot) sks
258258
pure $ ConfirmedSnapshot{snapshot, signatures}

0 commit comments

Comments
 (0)