Skip to content

Commit

Permalink
Move UTxO-type specific functions into top-level Migration module.
Browse files Browse the repository at this point in the history
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:

#2618 (comment)
  • Loading branch information
jonathanknowles committed Apr 20, 2021
1 parent 152d957 commit d481981
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
16 changes: 12 additions & 4 deletions lib/core/src/Cardano/Wallet/Primitive/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
--
Expand Down Expand Up @@ -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
18 changes: 1 addition & 17 deletions lib/core/src/Cardano/Wallet/Primitive/Migration/Planning.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ module Cardano.Wallet.Primitive.Migration.Planning
-- * UTxO entry categorization
, CategorizedUTxO (..)
, UTxOEntryCategory (..)
, categorizeUTxO
, categorizeUTxOEntries
, categorizeUTxOEntry
, uncategorizeUTxO
, uncategorizeUTxOEntries

) where
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d481981

Please sign in to comment.