Skip to content

Commit

Permalink
Check that Selection.create and Selection.extend minimize the fee.
Browse files Browse the repository at this point in the history
It's really important that both of these functions minimize the fee.

So we check that:

1. Calling `balance` on a resultant selection leaves it unchanged.
2. Calling `minimizeFee` on a resultant fee excess leaves it unchanged.

In response to review feedback:

#2618 (comment)
  • Loading branch information
jonathanknowles committed Apr 22, 2021
1 parent 5c2b38d commit adf7dd0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Migration/Selection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module Cardano.Wallet.Primitive.Migration.Selection
-- * Extending selections
, extend

-- * Balancing selections
, balance

-- * Adding value to outputs
, addValueToOutputs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ import Test.QuickCheck
, vectorOf
, withMaxSuccess
, (.&&.)
, (===)
)
import Text.Pretty.Simple
( pShow )
Expand Down Expand Up @@ -181,9 +180,9 @@ prop_create_inner
-> Property
prop_create_inner mockConstraints inputs reward =
checkCoverage $
cover 40 (resultIsSelection result)
cover 50 (resultIsSelection result)
"Success" $
cover 10 (resultHasZeroFeeExcess result)
cover 50 (resultHasZeroFeeExcess result)
"Success with zero fee excess" $
cover 1 (resultHasInsufficientAda result)
"Failure due to insufficient ada" $
Expand All @@ -196,8 +195,26 @@ prop_create_inner mockConstraints inputs reward =
property True
Left (SelectionFull e) ->
property (selectionSizeMaximum e < selectionSizeRequired e)
Right selection ->
Selection.verify constraints selection === SelectionCorrect
Right selection -> makeReports $ testAll
$ verify
(correctness == SelectionCorrect)
"correctness == SelectionCorrect"
. verify
(Selection.balance constraints selection == Right selection)
"Rebalancing the selection leaves it unchanged"
. verify
(feeExcess selection == feeExcessExpected)
"feeExcess selection == feeExcessExpected"
where
makeReports
= report correctness
"correctness"
. report feeExcessExpected
"feeExcessExpected"
correctness =
Selection.verify constraints selection
(feeExcessExpected, _) =
minimizeFee constraints (feeExcess selection, outputs selection)
where
constraints = unMockTxConstraints mockConstraints
result = create constraints reward inputs
Expand Down Expand Up @@ -262,7 +279,7 @@ prop_extend_inner
-> MockSelection
-> (MockInputId, TokenBundle)
-> Property
prop_extend_inner mockConstraints selection input =
prop_extend_inner mockConstraints selectionOriginal input =
checkCoverage $
cover 40 (resultIsSelection result)
"Success" $
Expand All @@ -279,11 +296,29 @@ prop_extend_inner mockConstraints selection input =
property True
Left (SelectionFull e) ->
property (selectionSizeMaximum e < selectionSizeRequired e)
Right selectionExtended ->
Selection.verify constraints selectionExtended === SelectionCorrect
Right selection -> makeReports $ testAll
$ verify
(correctness == SelectionCorrect)
"correctness == SelectionCorrect"
. verify
(Selection.balance constraints selection == Right selection)
"Rebalancing the selection leaves it unchanged"
. verify
(feeExcess selection == feeExcessExpected)
"feeExcess selection == feeExcessExpected"
where
makeReports
= report correctness
"correctness"
. report feeExcessExpected
"feeExcessExpected"
correctness =
Selection.verify constraints selection
(feeExcessExpected, _) =
minimizeFee constraints (feeExcess selection, outputs selection)
where
constraints = unMockTxConstraints mockConstraints
result = extend constraints selection input
result = extend constraints selectionOriginal input

--------------------------------------------------------------------------------
-- Adding value to outputs
Expand Down

0 comments on commit adf7dd0

Please sign in to comment.