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

Implement distributeSurplus and use in balanceTransaction #3223

Merged
merged 1 commit into from
Apr 19, 2022

Conversation

Anviking
Copy link
Member

@Anviking Anviking commented Apr 6, 2022

  • Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from balanceTransaction
  • Fee- and change-related padding before fee-minimisation is increased.
  • The padding after fee-minimisation is (in most cases) removed (see goldens; we are no longer overpaying the fee with 176 lovelace)
  • The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
  • [ ] Implement guardReasonableExcessFee based on 2 * minUTxOValue, rather than using the hard-coded limit of 20 ada.
  • [ ] Extra: adjust error types (could be moved to separate PR)
  • Look over comments & polish etc

Comments

Issue Number

ADP-1514

:: TxFeeAndChange
-> TxFeeAndChange
guardReasonableExcessFee fc@(TxFeeAndChange feeToBurn _)
| feeToBurn > minAda <> minAda =
Copy link
Member Author

@Anviking Anviking Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think technically this could be problematic on a network where the fee increase derived from coin-value-encoding is greater than 2*minAda — e.g. if minAda is tiny or 0...

Maybe we can add feePerByte * 16 to the right hand side… (or keep the static 20 ada one 🤷‍♂️ if it gets too complicated)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up just dropping guardReasonableExcessFee completely. I think it's more trouble than it's worth.

Comment on lines 62 to 63
3.050000,ErrBalanceTxSelectAssets (ErrSelectAssetsSelectionError (SelectionCollateralErrorOf (SelectionCollateralError {largestCombinationAvailable = fromList [(WalletUTxO {txIn = TxIn {inputId = Hash "00000000000000000000000000000000", inputIx = 0}, address = Address "`\177\229\224\251t\200l\128\USdhA\224|\219B\223\139\130\239<\228\229|\181A.w"},Coin 3050000)], minimumSelectionAmount = Coin 3050246})))
3.100000,0.612226,0.612050
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we see that

  1. 528 more lovelace is needed to succeed (at least inferred from the error message)
  2. When we are succeeding, fees are no longer 176 lovelace higher than needed

@Anviking Anviking force-pushed the anviking/ADP-1514/costOfIncreasingCoin branch from a285cec to ab82b9d Compare April 7, 2022 14:04
@Anviking Anviking self-assigned this Apr 7, 2022
@Anviking Anviking force-pushed the anviking/ADP-1514/distributeSurplus branch 4 times, most recently from 1e70528 to dec9f1a Compare April 7, 2022 18:36
@Anviking Anviking marked this pull request as ready for review April 7, 2022 18:51
Base automatically changed from anviking/ADP-1514/costOfIncreasingCoin to master April 8, 2022 02:57
@Anviking Anviking force-pushed the anviking/ADP-1514/distributeSurplus branch 3 times, most recently from 6995119 to 3e941e2 Compare April 8, 2022 14:37
@Anviking
Copy link
Member Author

Anviking commented Apr 8, 2022

bors try

iohk-bors bot added a commit that referenced this pull request Apr 8, 2022
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 8, 2022

try

Build failed:

  src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs:1745:15: 
  1) API Specifications, NEW_SHELLEY_TRANSACTIONS, TRANS_NEW_BALANCE_02c - Cannot balance when I cannot afford collateral
       "{\"code\":\"insufficient_collateral\",\"message\":\"I'm unable to create this transaction because the balance of pure ada UTxOs in your wallet is insufficient to cover the minimum amount of collateral required. I need an ada amount of at least: 4.279500 The largest combination of pure ada UTxOs I could find is: [2.853000] To fix this, you'll need to add one or more pure ada UTxOs to your wallet that can cover the minimum amount required.\"}" does not contain "I need an ada amount of at least: 4.278900"
       While verifying value:
         ( Status
             { statusCode = 403
             , statusMessage = "Forbidden"
             }
         , Left
             ( ClientError
                 ( Object
                     ( fromList
                         [
                             ( "code"
                             , String "insufficient_collateral"
                             )
                         ,
                             ( "message"
                             , String "I'm unable to create this transaction because the balance of pure ada UTxOs in your wallet is insufficient to cover the minimum amount of collateral required. I need an ada amount of at least: 4.279500 The largest combination of pure ada UTxOs I could find is: [2.853000] To fix this, you'll need to add one or more pure ada UTxOs to your wallet that can cover the minimum amount required."
                             )
                         ]
                     )
                 )
             )
         )

Probably the value just needs to be adjusted.

@Anviking Anviking force-pushed the anviking/ADP-1514/distributeSurplus branch from 3e941e2 to 8248eca Compare April 8, 2022 19:12
@Anviking
Copy link
Member Author

bors try

iohk-bors bot added a commit that referenced this pull request Apr 10, 2022
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 10, 2022

try

Build failed:

Copy link
Contributor

@jonathanknowles jonathanknowles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Anviking

I have some suggestions, but I think this PR can be merged as is!

I'll open a second PR with the suggestions.

Thanks for making this PR!

@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 19, 2022
3223: Implement `distributeSurplus` and use in `balanceTransaction` r=Anviking a=Anviking

- [x] Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from `balanceTransaction`
- [x] Fee- and change-related padding before fee-minimisation is _increased_.
- [x] The padding _after_ fee-minimisation is (in most cases) _removed_ (see goldens; we are no longer overpaying the fee with 176 lovelace)
- [x] The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
- <s>[ ] Implement `guardReasonableExcessFee` based on `2 * minUTxOValue`, rather than using the hard-coded limit of 20 ada. </s>
- <s>[ ] Extra: adjust error types (could be moved to separate PR)</s>
- [x] Look over comments & polish etc

### Comments

- Depends on #3215 

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1514

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

Build failed:

test/unit/Main.hs:1:1: error:
--
  | `hspec-discover' failed in phase `Haskell pre-processor'. (Exit code: 127)
  | \|
  | 1 \| {-# OPTIONS_GHC -F -pgmF hspec-discover #-}
  | \| ^

@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 19, 2022
3223: Implement `distributeSurplus` and use in `balanceTransaction` r=Anviking a=Anviking

- [x] Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from `balanceTransaction`
- [x] Fee- and change-related padding before fee-minimisation is _increased_.
- [x] The padding _after_ fee-minimisation is (in most cases) _removed_ (see goldens; we are no longer overpaying the fee with 176 lovelace)
- [x] The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
- <s>[ ] Implement `guardReasonableExcessFee` based on `2 * minUTxOValue`, rather than using the hard-coded limit of 20 ada. </s>
- <s>[ ] Extra: adjust error types (could be moved to separate PR)</s>
- [x] Look over comments & polish etc

### Comments

- Depends on #3215 

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1514

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

Build failed:

- Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from balanceTransaction
- Fee- and change-related padding before fee-minimisation is increased.
- The padding after fee-minimisation is (in most cases) removed (see goldens; we are no longer overpaying the fee with 176 lovelace)
- The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.

I ended up dropping the sanity check that any burned fee was less than
20 ada. I considered replacing the hard-coded limit with the following:
```
-- Increasing the fee by much should only happen if coin-selection is unable
-- to construct a change output respecting the minUTxOValue.
guardReasonableExcessFee
    :: TxFeeAndChange
    -> TxFeeAndChange
guardReasonableExcessFee fc@(TxFeeAndChange feeToBurn _)
    | feeToBurn > limit =
        error $ unwords
            [ "final redundant safety check in balanceTransaction:"
            , "burning more than 2 * minUTxOValue in fees is unreasonable"
            ]
    | otherwise = fc

  where
    -- We let 2*minAda be the limit rather than just minAda to account
    -- for overestimations in coin-selection etc. Precision doesn't
    -- matter here.
    limit = minAda
        <> minAda
        <> Coin (ceiling $ 16 * perByteFee)

    -- NOTE: The change output we've failed to create would only have
    -- contained ada, so passing 'TokenMap.empty' should be reasonable.
    minAda = txOutputMinimumAdaQuantity
        (view #constraints tl pp)
        TokenMap.empty

    LinearFee LinearFunction {slope = perByteFee} =
        view (#txParameters . #getFeePolicy) pp
```

but decided it was too complex to be worth it.
@Anviking Anviking force-pushed the anviking/ADP-1514/distributeSurplus branch from 8248eca to 1f69c03 Compare April 19, 2022 13:14
@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 19, 2022
3223: Implement `distributeSurplus` and use in `balanceTransaction` r=Anviking a=Anviking

- [x] Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from `balanceTransaction`
- [x] Fee- and change-related padding before fee-minimisation is _increased_.
- [x] The padding _after_ fee-minimisation is (in most cases) _removed_ (see goldens; we are no longer overpaying the fee with 176 lovelace)
- [x] The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
- <s>[ ] Implement `guardReasonableExcessFee` based on `2 * minUTxOValue`, rather than using the hard-coded limit of 20 ada. </s>
- <s>[ ] Extra: adjust error types (could be moved to separate PR)</s>
- [x] Look over comments & polish etc

### Comments

- Depends on #3215 

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1514

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

Build failed:

@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 19, 2022
3223: Implement `distributeSurplus` and use in `balanceTransaction` r=Anviking a=Anviking

- [x] Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from `balanceTransaction`
- [x] Fee- and change-related padding before fee-minimisation is _increased_.
- [x] The padding _after_ fee-minimisation is (in most cases) _removed_ (see goldens; we are no longer overpaying the fee with 176 lovelace)
- [x] The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
- <s>[ ] Implement `guardReasonableExcessFee` based on `2 * minUTxOValue`, rather than using the hard-coded limit of 20 ada. </s>
- <s>[ ] Extra: adjust error types (could be moved to separate PR)</s>
- [x] Look over comments & polish etc

### Comments

- Depends on #3215 

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1514

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

Build failed:

@Anviking
Copy link
Member Author

Anviking commented Apr 19, 2022

Merging manually. except I can't even seem able to do that...

@Anviking Anviking enabled auto-merge (rebase) April 19, 2022 14:38
@Anviking
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 19, 2022
3223: Implement `distributeSurplus` and use in `balanceTransaction` r=Anviking a=Anviking

- [x] Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from `balanceTransaction`
- [x] Fee- and change-related padding before fee-minimisation is _increased_.
- [x] The padding _after_ fee-minimisation is (in most cases) _removed_ (see goldens; we are no longer overpaying the fee with 176 lovelace)
- [x] The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
- <s>[ ] Implement `guardReasonableExcessFee` based on `2 * minUTxOValue`, rather than using the hard-coded limit of 20 ada. </s>
- <s>[ ] Extra: adjust error types (could be moved to separate PR)</s>
- [x] Look over comments & polish etc

### Comments

- Depends on #3215 

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1514

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
@Anviking
Copy link
Member Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

Already running a review

@Anviking Anviking merged commit 9e750dd into master Apr 19, 2022
@Anviking Anviking deleted the anviking/ADP-1514/distributeSurplus branch April 19, 2022 15:29
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

This PR was included in a batch that successfully built, but then failed to merge into master (it was a non-fast-forward update). It will be automatically retried.

iohk-bors bot added a commit that referenced this pull request Apr 19, 2022
3223: Implement `distributeSurplus` and use in `balanceTransaction` r=Anviking a=Anviking

- [x] Factors out fee-minimisation logic such that it can be made more sophisticated and tested separately from `balanceTransaction`
- [x] Fee- and change-related padding before fee-minimisation is _increased_.
- [x] The padding _after_ fee-minimisation is (in most cases) _removed_ (see goldens; we are no longer overpaying the fee with 176 lovelace)
- [x] The fee minimisation is more robust, and should work regardless of ProtocolParameters, whether a 5 ada fee is required, or a 21 lovelace one.
- <s>[ ] Implement `guardReasonableExcessFee` based on `2 * minUTxOValue`, rather than using the hard-coded limit of 20 ada. </s>
- <s>[ ] Extra: adjust error types (could be moved to separate PR)</s>
- [x] Look over comments & polish etc

### Comments

- Depends on #3215 

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

ADP-1514

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 19, 2022

This PR was included in a batch that successfully built, but then failed to merge into master. It will not be retried.

Additional information:

{"message":"At least 1 approving review is required by reviewers with write access.","documentation_url":"https://docs.github.com/articles/about-protected-branches"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants