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

txpool: Respect updates to block gas limit #9363

Conversation

jyellick
Copy link
Contributor

@jyellick jyellick commented Feb 2, 2024

There is a bug in the txpool which can manifest either as a race condition, or in more novel network configurations. When a transaction is classified as either having NotTooMuchGas or its complement, the classification will never change (unless a change to the sender account triggers reevaluation of that transaction).

If the block gas limit changes upwards, then any transactions previously not marked as NotTooMuchGas will remain ineligible for promotion. Similarly, if the block gas limit changes downwards, then any transactions previously marked as NotTooMuchGas may inappropriately remain as pending.

Although block gas limits may be relatively static in practice, there is additionally a race condition which can cause this bug to manifest. When the tx pool is first constructed, the gas limit is implicitly set to 0. Until the first update arrives with the configured gas limit all transactions received will be categorized as using too much gas and will never recover. This race condition was occurring in some integration tests we run.

This change adds a check in the OnNewBlock path which checks if the gas limit has changed, and if so triggers the re-evaluation of the NotTooMuchGas status for all transactions.

I am not an expert in the locking code for this path, but it looked like a lock was held and so these modifications should be safe. But please ensure that aspect is adequately reviewed.

@jyellick jyellick force-pushed the jyellick/respect-txpool-gas-updates branch 2 times, most recently from 6442500 to 23b5e7a Compare February 2, 2024 15:29
Copy link
Collaborator

@AskAlexSharov AskAlexSharov left a comment

Choose a reason for hiding this comment

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

txs are added a bit later - in: OnNewBlock -> addTxsOnNewBlock -> addLocked probably you need to fix there. (and maybe in onSenderStateChange)

@jyellick
Copy link
Contributor Author

jyellick commented Feb 5, 2024

txs are added a bit later - in: OnNewBlock -> addTxsOnNewBlock -> addLocked probably you need to fix there. (and maybe in onSenderStateChange)

I took a look in this path, but I don't understand your comment. Are you suggesting that the fix needs to be additionally there? Or that the fix should be moved to this path? The onSenderStateChange does already correctly classify transactions according to the current gas limit, but this path is not triggered by gas limit changes.

The general problem is that unlike with onSenderStateChange where changes to a particular account trigger re-processing of transactions for that account, changes to the block gas limit potentially impact every transaction in the pool. And, as best as I can tell there's no real indexing done for the NotTooMuchGas state. I suppose it's possible that maybe we could instead iterate over just the pending or queued pools depending on whether the gas limit has increased or decreased?

There is a bug in the txpool which can manifest either as a race
condition, or in more novel network configurations.  When a transaction
is classified as either having `NotTooMuchGas` or its complement, this
classification will never change unless a change to the sender account
triggers reevaluation of that transaction.

If the block gas limit changes upwards, then any transactions previously
not marked as `NotTooMuchGas` will remain ineligible for promotion.
Similarly, if the block gas limit changes downwards, then any
transactions previously marked as `NotTooMuchGas` may inappropriately
remain as pending.

Although block gas limits may be relatively static in practice, there is
additionally a race condition which can cause this bug to manifest.
When the tx pool is first constructed, the gas limit is implicitly set
to 0.  Until the first update arrives with the configured gas limit, all
transactions received will be categorized as using too much gas, and
will never recover.

This change adds a check in the `OnNewBlock` path which checks if the
gas limit has changed, and if so triggers the re-evaluation of the
`NotTooMuchGas` status for all transactions.  In the normal case of
consistent gas limits, this code path is not executed.
@jyellick jyellick force-pushed the jyellick/respect-txpool-gas-updates branch from 23b5e7a to 4108182 Compare February 23, 2024 04:32
@jyellick
Copy link
Contributor Author

Just rebased as this had gone into conflict -- happy to adapt this if there are changes needed, as the bug it resolves is real for us.

@AskAlexSharov
Copy link
Collaborator

yes, seems everything make sense.

@AskAlexSharov AskAlexSharov merged commit 48376d7 into ledgerwatch:devel Feb 23, 2024
7 checks passed
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

2 participants