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

Validate debitGasFees execution in tx pool #2279

Merged
merged 6 commits into from
Mar 12, 2024
Merged

Conversation

karlb
Copy link
Contributor

@karlb karlb commented Mar 4, 2024

Description

Txs must not fail during debitGasFees execution during the state transition, so we must remove problematic txs during the tx pool validation. We do this by executing debitGasFees inside the tx pool validation and discarding the resulting state changes. This is not overly efficient, but still better than the alternatives that have been considered so far.

Other changes

  • Added ExecuteAndDiscardChanges to vm.EVMRunner. I would have liked to avoid this change, but StaticCall does not allow (even temporary) changes and I didn't see a better way.
  • extracted debitGasFeesSelector and creditGasFeesSelector and updated comment to show how to generate the selector with cast instead of python.
  • removed old check for sufficient ERC20 balance, since that is implicitly checked by executing debitGasFees and I want to avoid duplicate work
  • no espresso hardfork check for fee currencies in ValidateTransactorBalanceCoversTx, since we don't need to support older forks in the tx pool

Tested

Manually by sending txs via Viem to a local mycelo. Adding a unit test would be desirable.

I'm haven't tested that the state revert works as expected and am also not sure about the performance impact.

Backwards compatibility

At the time of writing, we don't have any fee currencies on mainnet that can fail in debitGasFees. The error message for fee currencies without sufficient balance will change with this PR. The new error will contain the revert message from the token's debitGasFees.

Copy link

github-actions bot commented Mar 4, 2024

Coverage from tests in ./e2e_test/... for ./consensus/istanbul/... at commit f7be29c

coverage: 51.2% of statements across all listed packages
coverage:  63.2% of statements in consensus/istanbul
coverage:  43.2% of statements in consensus/istanbul/announce
coverage:  56.0% of statements in consensus/istanbul/backend
coverage:   0.0% of statements in consensus/istanbul/backend/backendtest
coverage:  24.3% of statements in consensus/istanbul/backend/internal/replica
coverage:  65.9% of statements in consensus/istanbul/core
coverage:  50.0% of statements in consensus/istanbul/db
coverage:   0.0% of statements in consensus/istanbul/proxy
coverage:  64.2% of statements in consensus/istanbul/uptime
coverage:  51.8% of statements in consensus/istanbul/validator
coverage:  79.2% of statements in consensus/istanbul/validator/random

Copy link

github-actions bot commented Mar 4, 2024

5890 passed, 45 skipped

@karlb karlb force-pushed the karlb/check-debit-in-tx-pool branch from 2cbd161 to 8b1b5f2 Compare March 4, 2024 12:36
core/tx_pool.go Outdated Show resolved Hide resolved
contracts/erc20gas/erc20gas.go Show resolved Hide resolved
@karlb karlb force-pushed the karlb/check-debit-in-tx-pool branch from 8b1b5f2 to ac78803 Compare March 5, 2024 11:20
We need this somewhat unusual function to check if `debitGasFees` can be
successfully executed without keeping the resulting state changes.
Txs must not fail during debitGasFees execution during the state
transition, so we must remove problematic txs during the tx pool
validation.
@karlb karlb force-pushed the karlb/check-debit-in-tx-pool branch from ac78803 to 5f587ca Compare March 5, 2024 11:27
The pre-espresso case it not used anymore and can be safely removed.
@karlb karlb force-pushed the karlb/check-debit-in-tx-pool branch from 5f587ca to cb74c28 Compare March 5, 2024 11:37
@karlb karlb marked this pull request as ready for review March 5, 2024 12:52
core/tx_pool.go Outdated
cost := new(big.Int).SetUint64(tx.Gas())
cost.Mul(cost, tx.GasFeeCap())
cost.Add(cost, tx.Value())
if tx.GatewayFeeRecipient() != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wasn't support for gateway fees removed in gingerbread, could this be cleaned up as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, and I could also remove the "(2)" comment, since only once case is left.

return ErrInsufficientFunds
}
}
return erc20gas.TryDebitFees(tx, from, currentVMRunner)
Copy link
Contributor

@piersy piersy Mar 5, 2024

Choose a reason for hiding this comment

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

Rather than having an else statement we could simplify this function like this:

if tx.FeeCurrency() != nil {
  return erc20gas.TryDebitFees(tx, from, currentVMRunner)
}
...

Copy link
Contributor

@palango palango left a comment

Choose a reason for hiding this comment

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

Looks good.

We don't need to support the pre-gingerbread behaviour of the tx pool
anymore.
@karlb karlb enabled auto-merge (squash) March 12, 2024 10:59
@karlb karlb merged commit f7be29c into master Mar 12, 2024
24 checks passed
@karlb karlb deleted the karlb/check-debit-in-tx-pool branch March 12, 2024 11:08
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

3 participants