Skip to content

Commit a15be69

Browse files
Match UTxO interface from cardano-api 10.17
1 parent 32a6b79 commit a15be69

File tree

34 files changed

+98
-95
lines changed

34 files changed

+98
-95
lines changed

hydra-cardano-api/src/Cardano/Api/UTxO.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Cardano.Api.Shelley (ReferenceScript (..))
1414
import Cardano.Ledger.Babbage ()
1515
import Data.Bifunctor (second)
1616
import Data.Coerce (coerce)
17-
import Data.Foldable qualified as F
1817
import Data.List qualified as List
1918
import Data.Map (Map)
2019
import Data.Map qualified as Map
@@ -36,8 +35,6 @@ newtype UTxO' out = UTxO
3635
deriving newtype
3736
( Eq
3837
, Show
39-
, Functor
40-
, Foldable
4138
, Semigroup
4239
, Monoid
4340
, ToJSON
@@ -90,10 +87,22 @@ difference a b = UTxO $ Map.difference (toMap a) (toMap b)
9087
-- to search for.
9188
containsOutputs :: Eq out => UTxO' out -> UTxO' out -> Bool
9289
containsOutputs utxoForSearching utxo =
93-
let allOutputs = F.toList utxoForSearching
94-
expectedOutputs = F.toList utxo
90+
let allOutputs = toList utxoForSearching
91+
expectedOutputs = toList utxo
9592
in all (`elem` allOutputs) expectedOutputs
9693

94+
map :: (TxOut CtxUTxO Era -> TxOut CtxUTxO Era) -> UTxO -> UTxO
95+
map f = UTxO . Map.map f . toMap
96+
97+
foldMap :: Monoid m => (TxOut CtxUTxO Era -> m) -> UTxO -> m
98+
foldMap fn = Prelude.foldMap fn . toMap
99+
100+
txOutputs :: UTxO -> [TxOut CtxUTxO Era]
101+
txOutputs = Map.elems . toMap
102+
103+
null :: UTxO -> Bool
104+
null = Map.null . toMap
105+
97106
-- * Type Conversions
98107

99108
-- | Transforms a UTxO containing tx outs from any era into Babbage era.

hydra-cluster/src/Hydra/Cluster/Faucet.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ findFaucetUTxO RunningNode{networkId, nodeSocket} lovelace = do
9292
(faucetVk, _) <- keysFor Faucet
9393
faucetUTxO <- queryUTxO networkId nodeSocket QueryTip [buildAddress faucetVk networkId]
9494
let foundUTxO = UTxO.filter (\o -> (selectLovelace . txOutValue) o >= lovelace) faucetUTxO
95-
when (null foundUTxO) $
95+
when (UTxO.null foundUTxO) $
9696
throwIO $
9797
FaucetHasNotEnoughFunds{faucetUTxO}
9898
pure foundUTxO
@@ -179,7 +179,7 @@ returnFundsToFaucet' tracer RunningNode{networkId, nodeSocket} senderSk = do
179179
let senderVk = getVerificationKey senderSk
180180
utxo <- queryUTxOFor networkId nodeSocket QueryTip senderVk
181181
returnAmount <-
182-
if null utxo
182+
if UTxO.null utxo
183183
then pure 0
184184
else retryOnExceptions tracer $ do
185185
let utxoValue = balance @Tx utxo

hydra-cluster/src/Hydra/Cluster/Scenarios.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ singlePartyUsesWithdrawZeroTrick tracer workDir node hydraScriptsTxId =
743743
recomputeIntegrityHash pparams [PlutusV3] $
744744
toLedgerTx tx
745745
& bodyTxL . collateralInputsTxBodyL .~ Set.map toLedgerTxIn (UTxO.inputSet utxoToCommit)
746-
& bodyTxL . totalCollateralTxBodyL .~ SJust (foldMap (selectLovelace . txOutValue) utxoToCommit)
746+
& bodyTxL . totalCollateralTxBodyL .~ SJust (UTxO.foldMap (selectLovelace . txOutValue) utxoToCommit)
747747
& bodyTxL . withdrawalsTxBodyL .~ Withdrawals (Map.singleton rewardAccount 0)
748748
& witsTxL . rdmrsTxWitsL .~ Redeemers (Map.singleton (ConwayRewarding $ AsIx 0) (redeemer, exUnits))
749749
& witsTxL . scriptTxWitsL .~ Map.singleton scriptHash script

hydra-cluster/src/Hydra/Generator.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ generateDemoUTxODataset ::
131131
generateDemoUTxODataset network nodeSocket faucetSk nClients nTxs = do
132132
-- Query available funds
133133
faucetUTxO <- queryUTxOFor network nodeSocket QueryTip faucetVk
134-
let (Coin fundsAvailable) = foldMap (selectLovelace . txOutValue) faucetUTxO
134+
let (Coin fundsAvailable) = UTxO.foldMap (selectLovelace . txOutValue) faucetUTxO
135135
-- Generate client datasets
136136
allPaymentKeys <- generate $ replicateM nClients genSigningKey
137137
clientFunds <- generate $ genClientFunds allPaymentKeys fundsAvailable

hydra-cluster/test/Test/DirectChainSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ spec = around (showLogsOnFailure "DirectChainSpec") $ do
166166
-- Expect that Alice got her committed value back to her
167167
-- external address
168168
utxo <- queryUTxO networkId nodeSocket QueryTip [aliceExternalAddress]
169-
let aliceValues = txOutValue <$> toList utxo
169+
let aliceValues = txOutValue <$> UTxO.txOutputs utxo
170170
aliceValues `shouldContain` [lovelaceToValue aliceCommitment]
171171

172172
it "cannot abort a non-participating head" $ \tracer ->

hydra-cluster/test/Test/GeneratorSpec.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Test.GeneratorSpec where
55
import Hydra.Prelude
66
import Test.Hydra.Prelude
77

8+
import Cardano.Api.UTxO qualified as UTxO
89
import Data.Text (unpack)
910
import Hydra.Cardano.Api (LedgerEra, UTxO, prettyPrintJSON, utxoFromTx)
1011
import Hydra.Chain.ChainState (ChainSlot (ChainSlot))
@@ -45,7 +46,7 @@ prop_keepsUTxOConstant =
4546
\Dataset{fundingTransaction, clientDatasets = [ClientDataset{txSequence}]} ->
4647
let initialUTxO = utxoFromTx fundingTransaction
4748
finalUTxO = foldl' (apply defaultGlobals ledgerEnv) initialUTxO txSequence
48-
in length finalUTxO == length initialUTxO
49+
in length (UTxO.txOutputs finalUTxO) == length (UTxO.txOutputs initialUTxO)
4950
& counterexample ("transactions: " <> prettyJSONString txSequence)
5051
& counterexample ("utxo: " <> prettyJSONString initialUTxO)
5152
& counterexample ("funding tx: " <> prettyJSONString fundingTransaction)

hydra-cluster/test/Test/Hydra/Cluster/FaucetSpec.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Test.Hydra.Cluster.FaucetSpec where
33
import Hydra.Prelude
44
import Test.Hydra.Prelude
55

6+
import Cardano.Api.UTxO qualified as UTxO
67
import CardanoClient (RunningNode (..))
78
import CardanoNode (withCardanoNodeDevnet)
89
import Control.Concurrent.Async (replicateConcurrently)
@@ -33,7 +34,7 @@ spec =
3334
vk <- generate genVerificationKey
3435
seedFromFaucet node vk 1_000_000 tracer
3536
-- 10 unique outputs
36-
length (fold utxos) `shouldBe` 10
37+
length (UTxO.txOutputs $ fold utxos) `shouldBe` 10
3738

3839
describe "returnFundsToFaucet" $ do
3940
it "does nothing if nothing to return" $ \(tracer, node) -> do
@@ -50,13 +51,13 @@ spec =
5051
returnFundsToFaucet tracer node actor
5152
remaining <- queryUTxOFor networkId nodeSocket QueryTip vk
5253
finalFaucetFunds <- queryUTxOFor networkId nodeSocket QueryTip faucetVk
53-
foldMap txOutValue remaining `shouldBe` mempty
54+
UTxO.foldMap txOutValue remaining `shouldBe` mempty
5455

5556
-- check the faucet has one utxo extra in the end
56-
length finalFaucetFunds `shouldBe` length initialFaucetFunds + 1
57+
length (UTxO.txOutputs finalFaucetFunds) `shouldBe` length (UTxO.txOutputs initialFaucetFunds) + 1
5758

58-
let initialFaucetValue = selectLovelace (foldMap txOutValue initialFaucetFunds)
59-
let finalFaucetValue = selectLovelace (foldMap txOutValue finalFaucetFunds)
59+
let initialFaucetValue = selectLovelace (UTxO.foldMap txOutValue initialFaucetFunds)
60+
let finalFaucetValue = selectLovelace (UTxO.foldMap txOutValue finalFaucetFunds)
6061
let difference = initialFaucetValue - finalFaucetValue
6162
-- difference between starting faucet amount and final one should
6263
-- just be the amount of paid fees
@@ -74,4 +75,4 @@ spec =
7475
-- it squashed the UTxO
7576
utxoAfter <- queryUTxOFor networkId nodeSocket QueryTip vk
7677

77-
length utxoAfter `shouldBe` 1
78+
length (UTxO.txOutputs utxoAfter) `shouldBe` 1

hydra-node/bench/tx-cost/TxCost.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ computeCommitCost = do
102102
Right tx ->
103103
case checkSizeAndEvaluate tx (utxo <> knownUtxo) of
104104
Just (txSize, memUnit, cpuUnit, minFee) ->
105-
pure $ Just (NumUTxO $ length utxo, txSize, memUnit, cpuUnit, minFee)
105+
pure $ Just (NumUTxO $ length $ UTxO.txOutputs utxo, txSize, memUnit, cpuUnit, minFee)
106106
Nothing ->
107107
pure Nothing
108108

@@ -309,4 +309,4 @@ serializedSize :: UTxO -> Natural
309309
serializedSize =
310310
fromIntegral
311311
. lengthOfByteString
312-
. foldMap (serialiseData . toBuiltinData . fromJust . toPlutusTxOut)
312+
. UTxO.foldMap (serialiseData . toBuiltinData . fromJust . toPlutusTxOut)

hydra-node/src/Hydra/Chain/Blockfrost/Client.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,6 @@ awaitUTxO networkId addresses txid i = do
548548
Left _e -> liftIO (threadDelay 1) >> go (n - 1)
549549
Right utxo' ->
550550
let wantedUTxO = UTxO.fromList $ List.filter (\(TxIn txid' _, _) -> txid' == txid) (UTxO.toList utxo')
551-
in if null wantedUTxO
551+
in if UTxO.null wantedUTxO
552552
then liftIO (threadDelay 1) >> go (n - 1)
553553
else pure utxo'

hydra-node/src/Hydra/Chain/CardanoClient.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ awaitTransaction networkId socket tx =
237237
ins = keys (UTxO.toMap $ utxoFromTx tx)
238238
go = do
239239
utxo <- queryUTxOByTxIn networkId socket QueryTip ins
240-
if null utxo
240+
if UTxO.null utxo
241241
then go
242242
else pure utxo
243243

@@ -260,7 +260,7 @@ awaitTransactionId networkId socket txid =
260260
txIn = TxIn txid (TxIx 0)
261261
go = do
262262
utxo <- queryUTxOByTxIn networkId socket QueryTip [txIn]
263-
if null utxo
263+
if UTxO.null utxo
264264
then go
265265
else pure utxo
266266

0 commit comments

Comments
 (0)