Skip to content

Commit

Permalink
Use consistent source of truth for maximumOutputTokenQuantity in `c…
Browse files Browse the repository at this point in the history
…ardano-coin-selection`. (#4164)

## Issue

None. (Noticed while looking at ADP-3185.)

## Description

This PR adjusts the `cardano-coin-selection` library to use a consistent
source of truth for the maximum quantity of any individual asset allowed
in a transaction output. This is currently defined by the
`SelectionConstraints.maximumOutputTokenQuantity` field.

In `CoinSelectionSpec`, this field is currently hard-wired to the value
of `testMaximumOutputTokenQuantity`, which has a value of `maxBound
@Word64`.
  • Loading branch information
jonathanknowles authored Oct 17, 2023
2 parents 6a7df14 + e219a77 commit 8cb3be2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 8 additions & 7 deletions lib/coin-selection/lib/Cardano/CoinSelection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId, TokenMap )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity )
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( txOutMaxTokenQuantity )
import Control.Monad.Random.Class
( MonadRandom (..) )
import Control.Monad.Random.NonRandom
Expand Down Expand Up @@ -778,7 +776,7 @@ verifySelectionOutputTokenQuantitiesWithinLimit cs _ps selection =
verifyEmpty errors FailureToVerifySelectionOutputTokenQuantitiesWithinLimit
where
errors :: [SelectionOutputTokenQuantityExceedsLimitError ctx]
errors = verifyOutputTokenQuantities =<<
errors = verifyOutputTokenQuantities cs =<<
selectionChangeOutputsWithDummyAddresses cs selection

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -1209,12 +1207,15 @@ deriving instance SelectionContext ctx =>
-- protocol.
--
verifyOutputTokenQuantities
:: (Address ctx, TokenBundle)
:: SelectionConstraints ctx
-> (Address ctx, TokenBundle)
-> [SelectionOutputTokenQuantityExceedsLimitError ctx]
verifyOutputTokenQuantities out =
verifyOutputTokenQuantities cs out =
[ SelectionOutputTokenQuantityExceedsLimitError
{address, asset, quantity, quantityMaxBound = txOutMaxTokenQuantity}
{address, asset, quantity, quantityMaxBound}
| let address = fst out
, (asset, quantity) <- TokenMap.toFlatList $ (snd out) ^. #tokens
, quantity > txOutMaxTokenQuantity
, quantity > quantityMaxBound
]
where
quantityMaxBound = maximumOutputTokenQuantity cs
4 changes: 1 addition & 3 deletions lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genAssetId, genTokenMap, shrinkTokenMap )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..) )
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( txOutMaxTokenQuantity )
import Control.Monad
( forM_ )
import Control.Monad.Trans.Except
Expand Down Expand Up @@ -611,7 +609,7 @@ genOutputsToCover = do
]
where
limit :: Natural
limit = unTokenQuantity txOutMaxTokenQuantity
limit = unTokenQuantity testMaximumOutputTokenQuantity

shrinkOutputsToCover
:: [(TestAddress, TokenBundle)] -> [[(TestAddress, TokenBundle)]]
Expand Down

0 comments on commit 8cb3be2

Please sign in to comment.