Skip to content

Commit

Permalink
Merge #3158
Browse files Browse the repository at this point in the history
3158: Remove all remaining usages of `TxOut` from coin selection implementation. r=jonathanknowles a=jonathanknowles

## Issue Number

ADP-1482

## Summary

This PR removes all remaining usages of `TxOut` from the following modules:
- `CoinSelection.Internal`
- `CoinSelection.Internal.Balance`

## Background

This work is part of ADP-1448, the goal of which is to generalize the types used by coin selection, so that coin selection modules do not depend on the `Address`, `TxIn`, `TxOut`, or `UTxO` types.

Co-authored-by: Jonathan Knowles <[email protected]>
  • Loading branch information
iohk-bors[bot] and jonathanknowles authored Mar 2, 2022
2 parents cc0f201 + 087da6c commit 56917c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4328,9 +4328,9 @@ instance IsServerError SelectionOutputSizeExceedsLimitError where
[ "One of the outputs you've specified contains too many assets. "
, "Try splitting these assets across two or more outputs. "
, "Destination address: "
, pretty (output ^. #address)
, pretty (fst output)
, ". Asset count: "
, pretty (TokenMap.size $ output ^. (#tokens . #tokens))
, pretty (TokenMap.size $ snd output ^. #tokens)
, "."
]
where
Expand Down
16 changes: 8 additions & 8 deletions lib/core/src/Cardano/Wallet/CoinSelection/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import Cardano.Wallet.Primitive.Types.TokenMap
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity )
import Cardano.Wallet.Primitive.Types.Tx
( TokenBundleSizeAssessment (..), TxOut (..), txOutMaxTokenQuantity )
( TokenBundleSizeAssessment (..), txOutMaxTokenQuantity )
import Cardano.Wallet.Primitive.Types.UTxOSelection
( UTxOSelection )
import Control.Monad
Expand Down Expand Up @@ -895,7 +895,7 @@ verifyEmptyUTxOError _cs SelectionParams {utxoAvailableForInputs} _e =

data FailureToVerifyInsufficientMinCoinValueError =
FailureToVerifyInsufficientMinCoinValueError
{ reportedOutput :: TxOut
{ reportedOutput :: (Address, TokenBundle)
, reportedMinCoinValue :: Coin
, verifiedMinCoinValue :: Coin
}
Expand All @@ -906,15 +906,15 @@ verifyInsufficientMinCoinValueError
verifyInsufficientMinCoinValueError cs _ps e =
verifyAll
[ reportedMinCoinValue == verifiedMinCoinValue
, reportedMinCoinValue > reportedOutput ^. (#tokens . #coin)
, reportedMinCoinValue > snd reportedOutput ^. #coin
]
FailureToVerifyInsufficientMinCoinValueError {..}
where
reportedOutput = e ^. #outputWithInsufficientAda
reportedMinCoinValue = e ^. #expectedMinCoinValue
verifiedMinCoinValue =
(cs ^. #computeMinimumAdaQuantity)
(reportedOutput ^. (#tokens . #tokens))
(snd reportedOutput ^. #tokens)

--------------------------------------------------------------------------------
-- Selection error verification: selection limit errors
Expand Down Expand Up @@ -1135,7 +1135,7 @@ verifySelectionOutputError cs ps = \case

newtype FailureToVerifySelectionOutputSizeExceedsLimitError =
FailureToVerifySelectionOutputSizeExceedsLimitError
{ outputReportedAsExceedingLimit :: TxOut }
{ outputReportedAsExceedingLimit :: (Address, TokenBundle) }
deriving (Eq, Show)

verifySelectionOutputSizeExceedsLimitError
Expand All @@ -1149,7 +1149,7 @@ verifySelectionOutputSizeExceedsLimitError cs _ps e =
TokenBundleSizeWithinLimit -> True
TokenBundleSizeExceedsLimit -> False
where
bundle = outputReportedAsExceedingLimit ^. #tokens
bundle = snd outputReportedAsExceedingLimit

outputReportedAsExceedingLimit = e ^. #outputThatExceedsLimit

Expand Down Expand Up @@ -1404,7 +1404,7 @@ data SelectionOutputError

newtype SelectionOutputSizeExceedsLimitError =
SelectionOutputSizeExceedsLimitError
{ outputThatExceedsLimit :: TxOut
{ outputThatExceedsLimit :: (Address, TokenBundle)
}
deriving (Eq, Generic, Show)

Expand All @@ -1421,7 +1421,7 @@ verifyOutputSize cs out
| withinLimit =
Nothing
| otherwise =
Just $ SelectionOutputSizeExceedsLimitError (uncurry TxOut out)
Just $ SelectionOutputSizeExceedsLimitError out
where
withinLimit :: Bool
withinLimit =
Expand Down
12 changes: 6 additions & 6 deletions lib/core/src/Cardano/Wallet/CoinSelection/Internal/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ import Cardano.Wallet.Primitive.Types.Coin
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..) )
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId, TokenMap )
( AssetId, Flat (..), TokenMap )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..) )
import Cardano.Wallet.Primitive.Types.Tx
( TokenBundleSizeAssessment (..)
, TokenBundleSizeAssessor (..)
, TxOut (..)
, txOutMaxCoin
, txOutMaxTokenQuantity
)
Expand Down Expand Up @@ -706,7 +705,7 @@ balanceMissing (BalanceInsufficientError available required) =
--
data InsufficientMinCoinValueError = InsufficientMinCoinValueError
{ outputWithInsufficientAda
:: !TxOut
:: !(Address, TokenBundle)
-- ^ The particular output that does not have the minimum coin quantity
-- expected by the protocol.
, expectedMinCoinValue
Expand All @@ -715,9 +714,10 @@ data InsufficientMinCoinValueError = InsufficientMinCoinValueError
} deriving (Generic, Eq, Show)

instance Buildable InsufficientMinCoinValueError where
build (InsufficientMinCoinValueError o c) = unlinesF
build (InsufficientMinCoinValueError (a, b) c) = unlinesF
[ nameF "Expected min coin value" (build c)
, nameF "TxOut" (build o)
, nameF "Address" (build a)
, nameF "Token bundle" (build (Flat b))
]

data UnableToConstructChangeError = UnableToConstructChangeError
Expand Down Expand Up @@ -903,7 +903,7 @@ performSelectionNonEmpty constraints params
| otherwise =
Just $ InsufficientMinCoinValueError
{ expectedMinCoinValue
, outputWithInsufficientAda = uncurry TxOut o
, outputWithInsufficientAda = o
}
where
expectedMinCoinValue = computeMinimumAdaQuantity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ import Cardano.Wallet.Primitive.Types.Tx
, TokenBundleSizeAssessor (..)
, TxIn (..)
, TxOut (..)
, txOutCoin
, txOutMaxTokenQuantity
)
import Cardano.Wallet.Primitive.Types.Tx.Gen
Expand Down Expand Up @@ -1049,7 +1048,7 @@ prop_performSelection mockConstraints params coverage =
property True
where
actualMinCoinValue
= txOutCoin . outputWithInsufficientAda
= view #coin . snd . outputWithInsufficientAda

onUnableToConstructChange :: UnableToConstructChangeError -> Property
onUnableToConstructChange e =
Expand Down

0 comments on commit 56917c2

Please sign in to comment.