Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 21061a5

Browse files
committedApr 2, 2025
PR review
Signed-off-by: Sasha Bogicevic <sasha.bogicevic@iohk.io>
1 parent e97b4e9 commit 21061a5

File tree

5 files changed

+14
-22
lines changed

5 files changed

+14
-22
lines changed
 

‎hydra-cardano-api/src/Hydra/Cardano/Api/TxOut.hs

-13
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,6 @@ findTxOutByScript utxo script =
101101
_ ->
102102
False
103103

104-
-- | Find all pub key outputs in some 'UTxO'
105-
findPubKeyOutputs ::
106-
UTxO ->
107-
Maybe UTxO
108-
findPubKeyOutputs utxo =
109-
case List.filter matchPubKey (UTxO.pairs utxo) of
110-
[] -> Nothing
111-
as -> Just $ UTxO.fromPairs as
112-
where
113-
matchPubKey = \case
114-
(_, TxOut (AddressInEra _ (ShelleyAddress _ (Ledger.KeyHashObj _) _)) _ _ _) -> True
115-
_ -> False
116-
117104
-- | Predicate to find or filter 'TxOut' owned by a key. This
118105
-- is better than comparing the full address as it does not require a network
119106
-- discriminator.

‎hydra-node/test/Hydra/BehaviorSpec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ toOnChainTx now = \case
11471147
, contestationDeadline = addUTCTime (toNominalDiffTime testContestationPeriod) now
11481148
}
11491149
FanoutTx{utxo, utxoToCommit, utxoToDecommit} ->
1150-
OnFanoutTx{headId = testHeadId, fanoutUTxO = utxo <> fromMaybe mempty utxoToCommit `withoutUTxO` fromMaybe mempty utxoToDecommit}
1150+
OnFanoutTx{headId = testHeadId, fanoutUTxO = utxo <> fromMaybe mempty utxoToCommit <> fromMaybe mempty utxoToDecommit}
11511151

11521152
testContestationPeriod :: ContestationPeriod
11531153
testContestationPeriod = UnsafeContestationPeriod 10

‎hydra-node/test/Hydra/HeadLogicSpec.hs

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import Data.List qualified as List
1818
import Data.Map (notMember)
1919
import Data.Map qualified as Map
2020
import Data.Set qualified as Set
21-
import GHC.Natural (naturalToInteger)
2221
import Hydra.API.ClientInput (ClientInput (SideLoadSnapshot))
2322
import Hydra.API.ServerOutput (DecommitInvalidReason (..))
2423
import Hydra.Cardano.Api (fromLedgerTx, genTxIn, mkVkAddress, toLedgerTx, txOutValue, unSlotNo, pattern TxValidityUpperBound)
@@ -726,10 +725,9 @@ spec =
726725
update bobEnv ledger (inClosedState threeParties) collectOtherHead
727726
`shouldBe` Error (NotOurHead{ourHeadId = testHeadId, otherHeadId})
728727

729-
prop "fanout utxo always relies on observed utxo" $ \i ->
728+
prop "fanout utxo always relies on observed utxo" $ \fanoutUTxO ->
730729
forAllShrink genClosedState shrink $ \closedState -> do
731-
let fanoutUTxO = utxoRef $ naturalToInteger i
732-
fanoutHead = observeTx $ OnFanoutTx{headId = testHeadId, fanoutUTxO}
730+
let fanoutHead = observeTx $ OnFanoutTx{headId = testHeadId, fanoutUTxO}
733731
let outcome = update bobEnv ledger closedState fanoutHead
734732
counterexample ("Outcome: " <> show outcome) $
735733
outcome `hasStateChangedSatisfying` \case

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,11 @@ instance
539539
Fanout{} ->
540540
case hydraState st of
541541
Final{finalUTxO} -> do
542-
let allOutputs = Data.Foldable.toList result
543-
pure $ all (`elem` allOutputs) (toTxOuts finalUTxO)
542+
-- NOTE: Sort `[TxOut]` by the address and values. We want to make
543+
-- sure that the fanout outputs match what we had in the open Head
544+
-- exactly.
545+
let sorted = sortOn (\o -> (txOutAddress o, selectLovelace (txOutValue o)))
546+
sorted (toTxOuts finalUTxO) === sorted (Data.Foldable.toList result)
544547
_ -> pure False
545548
_ -> pure True
546549

‎hydra-tx/src/Hydra/Tx/Fanout.hs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Hydra.Tx.Fanout where
33
import Hydra.Cardano.Api
44
import Hydra.Prelude
55

6+
import Cardano.Api.UTxO qualified as UTxO
67
import Hydra.Contract.Head qualified as Head
78
import Hydra.Contract.HeadState qualified as Head
89
import Hydra.Contract.MintAction (MintAction (..))
@@ -90,9 +91,12 @@ observeFanoutTx ::
9091
observeFanoutTx utxo tx = do
9192
let inputUTxO = resolveInputsUTxO utxo tx
9293
(headInput, headOutput) <- findTxOutByScript inputUTxO Head.validatorScript
93-
pubKeyOutputs <- findPubKeyOutputs (utxoFromTx tx)
94+
let txOutputs = utxoFromTx tx
9495
headId <- findStateToken headOutput
9596
findRedeemerSpending tx headInput
9697
>>= \case
97-
Head.Fanout{} -> pure FanoutObservation{headId, fanoutUTxO = pubKeyOutputs}
98+
Head.Fanout{numberOfFanoutOutputs, numberOfCommitOutputs, numberOfDecommitOutputs} -> do
99+
let allOutputs = fromIntegral $ numberOfFanoutOutputs + numberOfCommitOutputs + numberOfDecommitOutputs
100+
let fanoutUTxO = UTxO.fromPairs $ take allOutputs $ UTxO.pairs txOutputs
101+
pure FanoutObservation{headId, fanoutUTxO}
98102
_ -> Nothing

0 commit comments

Comments
 (0)
Please sign in to comment.