This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 629
[RCD-45] & [RCD-44] Review fee calculation entirely #3861
Merged
KtorZ
merged 3 commits into
release/2.0.0
from
KtorZ/RCD-45-RCD-44/review-fee-calculation
Nov 19, 2018
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ instance IsValue Core.Coin where | |
else a `Core.unsafeSubCoin` b | ||
valueRatio = \ a b -> coinToDouble a / coinToDouble b | ||
valueAdjust = \r d a -> coinFromDouble r (d * coinToDouble a) | ||
valueDiv = divCoin | ||
|
||
instance CoinSelDom Cardano where | ||
type Input Cardano = Core.TxIn | ||
|
@@ -191,14 +192,6 @@ feeOptions CoinSelectionOptions{..} = FeeOptions{ | |
type PickUtxo m = forall e. Core.Coin -- ^ Fee to still cover | ||
-> CoinSelT Core.Utxo e m (Maybe (Core.TxIn, Core.TxOutAux)) | ||
|
||
data CoinSelFinalResult = CoinSelFinalResult { | ||
csrInputs :: NonEmpty (Core.TxIn, Core.TxOutAux) | ||
-- ^ Picked inputs | ||
, csrOutputs :: NonEmpty Core.TxOutAux | ||
-- ^ Picked outputs | ||
, csrChange :: [Core.Coin] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved one level higher in the abstraction as |
||
|
||
-- | Run coin selection | ||
-- | ||
-- NOTE: Final UTxO is /not/ returned: coin selection runs /outside/ any wallet | ||
|
@@ -215,8 +208,8 @@ runCoinSelT :: forall m. Monad m | |
-> (forall utxo. PickFromUtxo utxo | ||
=> NonEmpty (Output (Dom utxo)) | ||
-> CoinSelT utxo CoinSelHardErr m [CoinSelResult (Dom utxo)]) | ||
-> CoinSelPolicy Core.Utxo m CoinSelFinalResult | ||
runCoinSelT opts pickUtxo policy (NE.sortBy (flip (comparing outVal)) -> request) utxo = do | ||
-> CoinSelPolicy Core.Utxo m (CoinSelFinalResult Cardano) | ||
runCoinSelT opts pickUtxo policy (NE.sortBy (flip (comparing outVal)) -> request) = | ||
-- NOTE: we sort the payees by output value, to maximise our chances of succees. | ||
-- In particular, let's consider a scenario where: | ||
-- | ||
|
@@ -233,44 +226,9 @@ runCoinSelT opts pickUtxo policy (NE.sortBy (flip (comparing outVal)) -> request | |
-- | ||
-- Therefore, just always considering them in order from large to small | ||
-- is probably a good idea. | ||
mSelection <- unwrapCoinSelT policy' utxo | ||
case mSelection of | ||
Left err -> return (Left err) | ||
Right ((css, additionalUtxo, additionalChange), _utxo') -> do | ||
let inps = concatMap selectedEntries | ||
(additionalUtxo : map coinSelInputs css) | ||
outs = map coinSelOutput css | ||
changesWithDust = splitChange additionalChange $ concatMap coinSelChange css | ||
let allInps = case inps of | ||
[] -> error "runCoinSelT: empty list of inputs" | ||
i:is -> i :| is | ||
originalOuts = case outs of | ||
[] -> error "runCoinSelT: empty list of outputs" | ||
o:os -> o :| os | ||
changes = changesRemoveDust (csoDustThreshold opts) changesWithDust | ||
return . Right $ CoinSelFinalResult allInps | ||
originalOuts | ||
changes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked moved inside |
||
evalCoinSelT policy' | ||
where | ||
-- we should have (x + (sum ls) = sum result), but this check could overflow. | ||
splitChange :: Value Cardano -> [Value Cardano] -> [Value Cardano] | ||
splitChange = go | ||
where | ||
go remaining [] = [remaining] | ||
-- we only create new change if for whatever reason there is none already | ||
-- or if is some overflow happens when we try to add. | ||
go remaining [a] = case valueAdd remaining a of | ||
Just newChange -> [newChange] | ||
Nothing -> [a, remaining] | ||
go remaining ls@(a : as) = | ||
let piece = divCoin remaining (length ls) | ||
newRemaining = unsafeValueSub remaining piece -- unsafe because of div. | ||
in case valueAdd piece a of | ||
Just newChange -> newChange : go newRemaining as | ||
Nothing -> a : go remaining as | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved in |
||
|
||
policy' :: CoinSelT Core.Utxo CoinSelHardErr m | ||
([CoinSelResult Cardano], SelectedUtxo Cardano, Value Cardano) | ||
policy' :: CoinSelT Core.Utxo CoinSelHardErr m (CoinSelFinalResult Cardano) | ||
policy' = do | ||
mapM_ validateOutput request | ||
css <- intInputGrouping (csoInputGrouping opts) | ||
|
@@ -346,7 +304,7 @@ validateOutput out = | |
random :: forall m. MonadRandom m | ||
=> CoinSelectionOptions | ||
-> Word64 -- ^ Maximum number of inputs | ||
-> CoinSelPolicy Core.Utxo m CoinSelFinalResult | ||
-> CoinSelPolicy Core.Utxo m (CoinSelFinalResult Cardano) | ||
random opts maxInps = | ||
runCoinSelT opts pickUtxo | ||
$ Random.random Random.PrivacyModeOn maxInps . NE.toList | ||
|
@@ -361,7 +319,7 @@ random opts maxInps = | |
largestFirst :: forall m. Monad m | ||
=> CoinSelectionOptions | ||
-> Word64 | ||
-> CoinSelPolicy Core.Utxo m CoinSelFinalResult | ||
-> CoinSelPolicy Core.Utxo m (CoinSelFinalResult Cardano) | ||
largestFirst opts maxInps = | ||
runCoinSelT opts pickUtxo | ||
$ LargestFirst.largestFirst maxInps . NE.toList | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splitChange
ranked higher up in the abstraction, so we need a way to divideValue dom
intok
pieces.