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

Fix transaction autobalancing when deregistering credential #718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

carbolymer
Copy link
Contributor

@carbolymer carbolymer commented Jan 7, 2025

Changelog

- description: |
    Fix transaction autobalancing when deregistering a credential
# uncomment types applicable to the change:
  type:
  # - feature        # introduces a new feature
  # - breaking       # the API has changed in a breaking way
  # - compatible     # the API has changed but is non-breaking
  # - optimisation   # measurable performance improvements
  # - refactoring    # QoL changes
   - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...

Context

The first step of autobalancing was not considering ADA inflow from deregistering a credential, so the resulting change could get negative and it was blowing up in ledger code.

Detailed cause explanation

The negative intermediate change value could be a result of the first call to:

calculateChangeValue sbe totalValueAtSpendableUTxO txbodycontent

calculateChangeValue considers only:

  • tx inputs
  • tx outputs
  • minted assets

so if there's not enough input value, meaning tx outs > tx ins, the calculated intermediate change gets negative. Note, that's a valid scenario if enough funds are coming in from certificate deposit return.

If that change is then used as a new transaction output (the change output), it will throw an exception in the ledger code.

The fix

Instead of precisely calculating the change in the first step of balancing, we can just use 2^64 Lovelace for the ada output (leaving minted assets as they were provided). This value can be arbitrary, because it's only used for the execution units and fees estimation, which do not depend on the exact change value.

The exact change is calculated in the last step of the autobalancing.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@carbolymer carbolymer force-pushed the mgalazyn/fix/autobalancing-when-credential-deregistration branch 2 times, most recently from b70d25e to 65f7e5b Compare January 8, 2025 14:32
@smelc
Copy link
Contributor

smelc commented Jan 9, 2025

LGTM. Ask me for approval when this is ready 👍

@carbolymer carbolymer force-pushed the mgalazyn/fix/autobalancing-when-credential-deregistration branch 3 times, most recently from 6577cbc to 45c03e1 Compare January 14, 2025 13:21
@carbolymer carbolymer force-pushed the mgalazyn/fix/autobalancing-when-credential-deregistration branch 3 times, most recently from e0d4049 to 8642abb Compare January 14, 2025 13:29
@carbolymer carbolymer marked this pull request as ready for review January 14, 2025 13:32
@carbolymer carbolymer force-pushed the mgalazyn/fix/autobalancing-when-credential-deregistration branch from 8642abb to c6bba1b Compare January 14, 2025 13:36
@carbolymer carbolymer changed the title Fix autobalancing, when deregistering credential Fix transaction autobalancing when deregistering credential Jan 14, 2025
Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

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

Can you add some more details as to exactly how you fixed this? It's not obvious to me what changes are the fix as they are overlapped with formatting changes.

@carbolymer carbolymer requested a review from Jimbo4350 January 16, 2025 14:32
Add a test case for that.

Co-authored-by: sourabhxyz <[email protected]>
@carbolymer carbolymer force-pushed the mgalazyn/fix/autobalancing-when-credential-deregistration branch from c6bba1b to 5416989 Compare January 16, 2025 14:33
Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

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

Still unclear. Separate the formatting changes and the semantic changes with git add --patch I understand the change now after reading the PR description. However we need to confirm that the change value does not influence the execution units calculation. I think it may as a larger change value means a larger script context. Confirm this with the plutus team.

@Jimbo4350 Jimbo4350 self-requested a review January 16, 2025 16:52
-- UTXO inputs, which inclue also non-ada assets
let totalValueAtUTxO =
fromLedgerValue sbe . calculateIncomingUTxOValue . Map.elems $ unUTxO utxo
-- this is a partial change: it does not include deposits, but we need to have non-ada assets in it
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to have non-ada assets in it?

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.

[BUG] - Incorrect change output computation inside makeTransactionBodyAutoBalance
3 participants