Skip to content

Commit 10159df

Browse files
committed
Address review comments
Test filterExistingAssets
1 parent 3149865 commit 10159df

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ data FaucetLog
4444
deriving stock (Eq, Show, Generic)
4545
deriving anyclass (ToJSON)
4646

47-
-- | Create a specially marked "seed" UTXO containing requested 'Lovelace' by
47+
-- | Create a specially marked "seed" UTXO containing requested 'Value' by
4848
-- redeeming funds available to the well-known faucet.
4949
seedFromFaucet ::
5050
ChainBackend backend =>
5151
backend ->
5252
-- | Recipient of the funds
5353
VerificationKey PaymentKey ->
54-
-- | Amount to get from faucet
54+
-- | Value to get from faucet
5555
Value ->
5656
Tracer IO FaucetLog ->
5757
IO UTxO

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@ canDepositPartially tracer workDir blockTime backend hydraScriptsTxId =
13861386

13871387
getSnapshotUTxO n1 `shouldReturn` expectedDeposit
13881388

1389+
13891390
send n2 $ input "Close" []
13901391

13911392
deadline <- waitMatch (10 * blockTime) n2 $ \v -> do

hydra-cluster/src/HydraNode.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ requestCommitTx :: HydraClient -> UTxO -> IO Tx
192192
requestCommitTx client utxos =
193193
requestCommitTx' client utxos Nothing Nothing
194194

195-
-- | Helper to make it easy to obtain a commit tx using some wallet utxo and optional amount.
195+
-- | Helper to make it easy to obtain a commit tx using some wallet utxo
196+
-- optional amount of lovelace and optional map of assets.
196197
-- Create a commit tx using the hydra-node for later submission.
197198
requestCommitTx' :: HydraClient -> UTxO -> Maybe Coin -> Maybe (Map PolicyId PolicyAssets) -> IO Tx
198199
requestCommitTx' HydraClient{apiHost = Host{hostname, port}} utxos amount tokens =

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,27 @@ filterExistingAssets utxoToFilter utxoToLookup =
8282
let assets = valueToPolicyAssets val
8383
originalLovelace = selectLovelace val
8484
filteredAssets =
85-
foldMap (uncurry policyAssetsToValue) $
86-
Map.assocs $
87-
Map.mapWithKey
88-
( \pid (PolicyAssets x) ->
89-
let samePid = List.filter (\(pid', _) -> pid' == pid) forLookup
90-
in case List.lookup pid samePid of
91-
Nothing -> PolicyAssets x
92-
Just (PolicyAssets foundAssets) -> PolicyAssets $ x `Map.difference` foundAssets
93-
)
94-
assets
85+
foldMap (uncurry policyAssetsToValue) $
86+
filterAssets assets forLookup
9587
in (i, TxOut a (lovelaceToValue originalLovelace <> filteredAssets) d r)
96-
forLookup = concatMap (Map.toList . valueToPolicyAssets . txOutValue . snd) $ UTxO.toList utxoToLookup
88+
forLookup =
89+
-- NOTE: Uses a list to store all policies, preserving multiple entries
90+
-- with the same policyId but different assets. A Map would silently
91+
-- overwrite duplicates.
92+
concatMap (Map.toList . valueToPolicyAssets . txOutValue . snd) $ UTxO.toList utxoToLookup
93+
94+
-- | Filter the first argument map of assets in case any asset exists in the second argument.
95+
filterAssets :: Map PolicyId PolicyAssets -> [(PolicyId, PolicyAssets)] -> [(PolicyId, PolicyAssets)]
96+
filterAssets assets forLookup =
97+
Map.assocs $
98+
Map.mapWithKey
99+
( \pid (PolicyAssets x) ->
100+
let samePid = List.filter (\(pid', _) -> pid' == pid) forLookup
101+
in case List.lookup pid samePid of
102+
Nothing -> PolicyAssets x
103+
Just (PolicyAssets foundAssets) -> PolicyAssets $ x `Map.difference` foundAssets
104+
)
105+
assets
97106

98107
-- | Merges the two 'UTxO' favoring data coming from the first argument 'UTxO'.
99108
-- In case the same 'TxIn' was found in the first 'UTxO' - second 'UTxO' value

hydra-tx/test/Hydra/Tx/DepositSpec.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,10 @@ spec =
172172
valid `shouldBe` mempty -- All assets in policy must be valid for policy to be valid
173173
invalid `shouldBe` tokens
174174
it "splits multiassets correctly" $
175-
forAll (genUTxOWithAssetsSized 5 Nothing) $ \utxo ->
176-
forAll (prepareAssetMap utxo) $ \assets ->
177-
property $ propSplitMultiAssetCorrectly utxo assets
175+
forAll arbitrary $ \policyId ->
176+
forAll (genUTxOWithAssetsSized 5 (Just policyId)) $ \utxo ->
177+
forAll (prepareAssetMap utxo) $ \assets ->
178+
property $ propSplitMultiAssetCorrectly utxo assets
178179

179180
describe "property tests" $ do
180181
prop "preserves all input tokens (completeness)" propPreservesAllTokens
@@ -424,8 +425,8 @@ propSplitMultiAssetCorrectly utxo specifiedTokens =
424425
in all (checkValidTokenInUTxO utxoPolicyAssets) (Map.toList depositAssets)
425426
& cover 10 (containsPolicies utxoPolicyAssets specifiedTokens) "PolicyId's are completely present in the UTxO"
426427
& cover 10 (containsAssets utxoPolicyAssets specifiedTokens) "Assets are completely present in the UTxO"
428+
& cover 10 (Map.size specifiedTokens > 5) "Assets size > 5"
427429
& cover 1 (Map.null specifiedTokens) "Empty Assets"
428-
& cover 1 (Map.size specifiedTokens > 5) "Assets size > 5"
429430
& counterexample ("Valid tokens: " <> show toDeposit)
430431
& counterexample ("UTxO policy assets: " <> show utxoPolicyAssets)
431432
where

0 commit comments

Comments
 (0)