Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance reporting of coin selections in the log. #2668

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2685,9 +2685,9 @@ instance ToText WalletLog where
"|utxo| = "+|UTxOIndex.size utxo|+" " <>
"#recipients = "+|NE.length recipients|+""
MsgSelectionDone (Left e) ->
"Failed to select assets: "+|| e ||+""
"Failed to select assets:\n"+|| e ||+""
MsgSelectionDone (Right s) ->
"Assets selected successfully: "+| s |+""
"Assets selected successfully:\n"+| s |+""
MsgMigrationUTxOBefore summary ->
"About to migrate the following distribution: \n" <> pretty summary
MsgMigrationUTxOAfter summary ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,7 @@ import Data.Ord
import Data.Set
( Set )
import Fmt
( Buildable (..)
, Builder
, blockListF
, blockListF'
, nameF
, tupleF
, unlinesF
)
( Buildable (..), Builder, blockListF', nameF, tupleF, unlinesF )
import GHC.Generics
( Generic )
import GHC.Stack
Expand Down Expand Up @@ -243,24 +236,92 @@ data SelectionResult change = SelectionResult
deriving (Generic, Eq, Show)

instance Buildable (SelectionResult TokenBundle) where
build = buildSelectionResult (blockListF . fmap TokenBundle.Flat)
build = buildSelectionResult id

instance Buildable (SelectionResult TxOut) where
build = buildSelectionResult (blockListF . fmap build)
build = buildSelectionResult (view #tokens)

buildSelectionResult
:: ([change] -> Builder)
:: forall change . (change -> TokenBundle)
-> SelectionResult change
-> Builder
buildSelectionResult changeF s@SelectionResult{inputsSelected,extraCoinSource} =
mconcat
[ nameF "inputs selected" (inputsF inputsSelected)
, nameF "extra coin input" (build extraCoinSource)
, nameF "outputs covered" (build $ outputsCovered s)
, nameF "change generated" (changeF $ changeGenerated s)
, nameF "size utxo remaining" (build $ UTxOIndex.size $ utxoRemaining s)
]
buildSelectionResult changeToBundle s =
-- We first give a fixed-length summary, and then give more detailed
-- information:
informationSummarized <>
informationDetailed
where
-- A fixed-length summary of a selection that includes several data points,
-- where each data point can be serialized as a single line of text.
informationSummarized = mconcat
[ nameF "computed fee"
$ build computedFee
, nameF "total ada balance in"
$ build totalAdaBalanceIn
, nameF "total ada balance out"
$ build totalAdaBalanceOut
, nameF "ada balance of selected inputs"
$ build adaBalanceOfSelectedInputs
, nameF "ada balance of extra input"
$ build adaBalanceOfExtraInput
, nameF "ada balance of requested outputs"
$ build adaBalanceOfRequestedOutputs
, nameF "ada balance of generated change outputs"
$ build adaBalanceOfGeneratedChangeOutputs
, nameF "number of selected inputs"
$ build $ length $ view #inputsSelected s
, nameF "number of requested outputs"
$ build $ length $ view #outputsCovered s
, nameF "number of generated change outputs"
$ build $ length $ view #changeGenerated s
, nameF "number of unique non-ada assets in selected inputs"
$ build
$ Set.size
$ F.foldMap (TokenBundle.getAssets . view #tokens . snd)
$ view #inputsSelected s
, nameF "number of unique non-ada assets in requested outputs"
$ build
$ Set.size
$ F.foldMap (TokenBundle.getAssets . view #tokens)
$ view #outputsCovered s
, nameF "number of unique non-ada assets in generated change outputs"
$ build
$ Set.size
$ F.foldMap (TokenBundle.getAssets . changeToBundle)
$ view #changeGenerated s
, nameF "size of remaining utxo"
$ build $ UTxOIndex.size $ utxoRemaining s
]
where
computedFee = Coin.distance totalAdaBalanceIn totalAdaBalanceOut

totalAdaBalanceIn =
adaBalanceOfSelectedInputs <> adaBalanceOfExtraInput
totalAdaBalanceOut =
adaBalanceOfGeneratedChangeOutputs <> adaBalanceOfRequestedOutputs

adaBalanceOfSelectedInputs =
F.foldMap (view (#tokens . #coin) . snd) $ view #inputsSelected s
adaBalanceOfExtraInput =
F.fold (view #extraCoinSource s)
adaBalanceOfGeneratedChangeOutputs =
F.foldMap (view #coin . changeToBundle) $ view #changeGenerated s
adaBalanceOfRequestedOutputs =
F.foldMap (view (#tokens . #coin)) $ view #outputsCovered s

-- Detailed information about a selection.
informationDetailed = mconcat
[ nameF "inputs"
$ inputsF $ view #inputsSelected s
, nameF "outputs"
$ build $ view #outputsCovered s
, nameF "change outputs"
$ changeF $ view #changeGenerated s
]

changeF :: [change] -> Builder
changeF = blockListF' "+" (build . TokenBundle.Flat . changeToBundle)

inputsF :: NonEmpty (TxIn, TxOut) -> Builder
inputsF = blockListF' "+" tupleF

Expand Down