From d4819813bff15429724003d79aa489a77327a1fa Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Tue, 20 Apr 2021 06:43:22 +0000 Subject: [PATCH] Move `UTxO`-type specific functions into top-level `Migration` module. These functions manipulate concrete wallet types, so it makes more sense for them to be located in the top-level `Migration` module, which deals with concrete wallet types. Conversely, both `Migration.Planning` and `Migration.Selection` use abstract types for transaction input identifiers. In response to review feedback: https://github.com/input-output-hk/cardano-wallet/pull/2618#discussion_r615925639 --- .../src/Cardano/Wallet/Primitive/Migration.hs | 16 ++++++++++++---- .../Wallet/Primitive/Migration/Planning.hs | 18 +----------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/Primitive/Migration.hs b/lib/core/src/Cardano/Wallet/Primitive/Migration.hs index 7254a28ac57..80d29e190e0 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Migration.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Migration.hs @@ -29,13 +29,14 @@ import Cardano.Wallet.Primitive.Types.Coin import Cardano.Wallet.Primitive.Types.Tx ( TxConstraints (..), TxIn, TxOut ) import Cardano.Wallet.Primitive.Types.UTxO - ( UTxO ) + ( UTxO (..) ) import Data.Generics.Internal.VL.Lens ( view ) import Data.Generics.Labels () import qualified Cardano.Wallet.Primitive.Migration.Planning as Planning +import qualified Data.Map.Strict as Map -- | Represents a plan for migrating a 'UTxO' set. -- @@ -66,9 +67,16 @@ createPlan -> MigrationPlan s createPlan constraints utxo reward = MigrationPlan { selections = view #selections plan - , unselected = Planning.uncategorizeUTxO (view #unselected plan) + , unselected = uncategorizeUTxO (view #unselected plan) , totalFee = view #totalFee plan } where - categorizedUTxO = Planning.categorizeUTxO constraints utxo - plan = Planning.createPlan constraints categorizedUTxO reward + plan = Planning.createPlan constraints (categorizeUTxO utxo) reward + + categorizeUTxO :: UTxO -> Planning.CategorizedUTxO (TxIn, TxOut) + categorizeUTxO (UTxO u) = Planning.categorizeUTxOEntries constraints $ + (\(i, o) -> ((i, o), view #tokens o)) <$> Map.toList u + + uncategorizeUTxO :: Planning.CategorizedUTxO (TxIn, TxOut) -> UTxO + uncategorizeUTxO = + UTxO . Map.fromList . fmap fst . Planning.uncategorizeUTxOEntries diff --git a/lib/core/src/Cardano/Wallet/Primitive/Migration/Planning.hs b/lib/core/src/Cardano/Wallet/Primitive/Migration/Planning.hs index 78b507ba437..4d24858228e 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Migration/Planning.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Migration/Planning.hs @@ -28,10 +28,8 @@ module Cardano.Wallet.Primitive.Migration.Planning -- * UTxO entry categorization , CategorizedUTxO (..) , UTxOEntryCategory (..) - , categorizeUTxO , categorizeUTxOEntries , categorizeUTxOEntry - , uncategorizeUTxO , uncategorizeUTxOEntries ) where @@ -45,9 +43,7 @@ import Cardano.Wallet.Primitive.Types.Coin import Cardano.Wallet.Primitive.Types.TokenBundle ( TokenBundle (..) ) import Cardano.Wallet.Primitive.Types.Tx - ( TxConstraints (..), TxIn, TxOut ) -import Cardano.Wallet.Primitive.Types.UTxO - ( UTxO (..) ) + ( TxConstraints (..) ) import Data.Either ( isRight ) import Data.Functor @@ -63,7 +59,6 @@ import qualified Cardano.Wallet.Primitive.Migration.Selection as Selection import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle import qualified Data.Foldable as F import qualified Data.List as L -import qualified Data.Map.Strict as Map -------------------------------------------------------------------------------- -- Migration planning @@ -213,14 +208,6 @@ data CategorizedUTxO i = CategorizedUTxO } deriving (Eq, Show) -categorizeUTxO - :: TxSize s - => TxConstraints s - -> UTxO - -> CategorizedUTxO (TxIn, TxOut) -categorizeUTxO constraints (UTxO u) = categorizeUTxOEntries constraints $ - (\(i, o) -> ((i, o), view #tokens o)) <$> Map.toList u - categorizeUTxOEntries :: forall i s. TxSize s => TxConstraints s @@ -260,9 +247,6 @@ categorizeUTxOEntry constraints b coinIsIgnorable :: Coin -> Bool coinIsIgnorable c = c <= txInputCost constraints -uncategorizeUTxO :: CategorizedUTxO (TxIn, TxOut) -> UTxO -uncategorizeUTxO = UTxO . Map.fromList . fmap fst . uncategorizeUTxOEntries - uncategorizeUTxOEntries :: CategorizedUTxO i -> [(i, TokenBundle)] uncategorizeUTxOEntries utxo = mconcat [ supporters utxo