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

Deduct fees on failed transactions #1012

Merged
merged 31 commits into from
Aug 20, 2021

Conversation

janezpodhostnik
Copy link
Contributor

@janezpodhostnik janezpodhostnik commented Jul 22, 2021

closes: https://github.com/dapperlabs/flow-internal/issues/1449

Deduct fees on failed transactions

If transaction fails still try to deduct fees. Consequences of this change:

  • Even failing transactions will emit fee deduction events
  • This has the potential to put an account below minimum account balance.

There is currently an edge case: if the account minimum balance is less than the transaction fees the balance does not get reduced to 0. PR onflow/flow-core-contracts#221 addresses that issue.

DoD Checklist

  • If transaction fails:
    • don't commit state changes from the transaction
    • commit change to payers balance (and possibly go below account minimum balance)
    • commit change to sequence number
    • emit tx fee deduction events
  • if account is at minimum balance and is a payer for a transaction the transaction fails because the account is below minimum account balance after tx fees are deducted

@codecov-commenter
Copy link

codecov-commenter commented Jul 22, 2021

Codecov Report

Merging #1012 (3bf8f9d) into master (6caf5d3) will decrease coverage by 0.02%.
The diff coverage is 51.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1012      +/-   ##
==========================================
- Coverage   55.58%   55.55%   -0.03%     
==========================================
  Files         481      481              
  Lines       29434    29449      +15     
==========================================
+ Hits        16360    16361       +1     
- Misses      10843    10854      +11     
- Partials     2231     2234       +3     
Flag Coverage Δ
unittests 55.55% <51.85%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
fvm/transactionInvocator.go 73.48% <51.85%> (-6.02%) ⬇️
engine/common/synchronization/engine.go 67.20% <0.00%> (ø)
...ngine/common/synchronization/finalized_snapshot.go 71.79% <0.00%> (+5.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6caf5d3...3bf8f9d. Read the comment docs.

@janezpodhostnik janezpodhostnik marked this pull request as ready for review July 23, 2021 18:28
// applying contract changes
// this writes back the contract contents to accounts
// if any error occurs we fail the tx
// this needs to happen before checking limits, so that contract changes are committed to the state
updatedKeys, err := env.Commit()
if err != nil && txError == nil {
txError = fmt.Errorf("transaction invocation failed: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

So if we had feesError we ignore this newly assigned error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is correct. If there is a error with deducting fees (or with checking storage) the tx error should be hidden from the user. But we should probably still log it... I will add that.

// reset env
env = NewTransactionEnvironment(*ctx, vm, sth, programs, proc.Transaction, proc.TxIndex, span)

// try to deduct fees again, to get the fee deduction events
Copy link
Contributor

Choose a reason for hiding this comment

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

We could re-organise this so no second fees deduction code is run.
Something along the lines of

actualDelta = delta.NewChild()
err = runTx(actualDelta)
if err == nil {
  delta.Merge(actualDelta)
}
delta.Commit()

@m4ksio
Copy link
Contributor

m4ksio commented Aug 4, 2021

I might miss something, but we can't we just check the account balance before running a tx and simply fail it if the balance won't cover the fee?
Is it to prevent spamming?

@janezpodhostnik
Copy link
Contributor Author

The transaction could move funds to/from the payer account, so there is no way to know for sure if the payer will have enough funds before actually executing the transaction.

@janezpodhostnik
Copy link
Contributor Author

Found the flaky test. @m4ksio @ramtinms please check fa41b81

@janezpodhostnik janezpodhostnik changed the base branch from master to janez/fix-iterating-over-unordered-list August 18, 2021 15:17
Base automatically changed from janez/fix-iterating-over-unordered-list to master August 18, 2021 19:27
@janezpodhostnik
Copy link
Contributor Author

bors try

bors bot added a commit that referenced this pull request Aug 19, 2021
@onflow onflow deleted a comment from bors bot Aug 19, 2021
@bors
Copy link
Contributor

bors bot commented Aug 19, 2021

try

Build failed:

@janezpodhostnik
Copy link
Contributor Author

bors merge

@bors
Copy link
Contributor

bors bot commented Aug 20, 2021

@bors bors bot merged commit 4bcdeb1 into master Aug 20, 2021
@bors bors bot deleted the janez/deduct-fees-on-failed-transactions branch August 20, 2021 18:35
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.

None yet

4 participants