-
Notifications
You must be signed in to change notification settings - Fork 859
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
Layered Transaction Pool #5290
Layered Transaction Pool #5290
Conversation
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
…oritized transaction for the sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
# Conflicts: # consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeBlockCreator.java # ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java
# Conflicts: # consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/blockcreation/MergeReorgTest.java # ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/LatestNonceProviderTest.java # ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetTransactionCountTest.java # ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockTransactionSelectorTest.java # ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/LegacyFeeMarketBlockTransactionSelectorTest.java # ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/LondonFeeMarketBlockTransactionSelectorTest.java # ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/AbstractIsolationTests.java # ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/PendingTransactions.java # ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java # ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolFactory.java # ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsSorter.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageSenderTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/PendingMultiTypesTransactionsTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsTestBase.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/BaseFeePendingTransactionsTest.java # ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/GasPricePendingTransactionsTest.java
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
…ive, instead force a reorg of the sender txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
…tModificationException Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
# Conflicts: # CHANGELOG.md # besu/src/main/java/org/hyperledger/besu/cli/options/unstable/TransactionPoolOptions.java # besu/src/test/java/org/hyperledger/besu/cli/options/TransactionPoolOptionsTest.java # ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/TransactionTestFixture.java # ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java # ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolConfiguration.java
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
I am going to merge this today since there are no more comments in the last weeks, the running instances are doing fine and the results are good. There are some improvements that can be done, but to avoid making this PR even bigger, I will create small PRs for them. |
Are there any benchmarks for difference in fee revenue? |
@hanniabu you can check this spreadsheet for a comparison https://github.com/hyperledger/besu/files/11137473/Layered.Transaction.Pool.metrics.ods |
Thanks 🙂 |
Hi @RogerHKW, and thanks for checking out the layered txpool, feedback is important, and I found that the description was lacking documentation of changed options, so I added the Changes in Besu options section.
This option has no effect (there is warning in the log if you use it with the layered txpool) since there is no more a fixed max number of transactions, but this number constantly changes, so instead of using a percentage a fixed number is used and it can be specified by
By default the max memory these 2 layers can use is 50MB (now I have documented how to change this) and I did also tests with 100GB, and updated the screenshot with the metrics from that test. I have not found issues increasing that value to 100GB, but haven't tried with more, so if you want to push the limit, please report any issue, and also check the memory related metrics to see if they to be tuned.
correct
The way demoting and promotions between the Prioritized and the Ready layers work, should prevent this, because always the least priority tx(1) is moved to Ready, so ideally Ready only has txs that are lower in value that Prioritized(2), and as soon as there is space in Prioritized (usually when a new block arrives and confirmed txs are removed), most valuable Ready txs are promoted(2). (1) Specifically the last tx for the sender of the least priority tx, to avoid nonce gaps |
This is a great answer and big help, thanks! If I understand it right, the transactions in the first layer are sorted by tip per gas unit and will be taken in descending order when filling a block ? The transactions in the second layer are sorted by max fee per gas unit? |
No problem, glad you are studying this, the feedback is also useful to make the code and the doc easier to understand.
Correct, the goal is to produce a block with the highest reward possible for the staker, in general this is a complex optimization problem, but in our case we take some shortcuts, since the blocks are seldomly full and so in the normal case we just need to put all the pending txs that are executable in that moment in the block, so the focus is more on which txs we keep in the pool for the future that could make the difference. |
* Introduce experimental layered transaction pool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * new Xlayered-tx-pool flag to enabled the new tx pool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Move pending transaction sorter tests in the sorter folder Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Unit tests for new and old transaction pool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: do not decrease size when promoting ready txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: remove tx from orderByFee when replaced Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: decrease size when removing confirmed txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: always recreate orderByFee for London fee market Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: transaction removal counter when txs added to block Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: update expected nonce when demoting a prioritized transaction Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: correctly remove expected nonce entry when removing the last prioritized transaction for the sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix NullPointerException when the replaced tx is not prioritized Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Replace postponed with spare transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix merge from main Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixed most tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix more tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename and reorg some classes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * More renaming and code clean up Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Refactor transaction pool metrics Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Stats log refined Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Cleanup unit tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve stats log Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove unnecessary test parameters Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix unit test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> # Conflicts: # ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java # ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java * Cancel older block creation tasks upon receiving a new one Signed-off-by: Simon Dudley <simon.dudley@consensys.net> * Fixes to expected next nonce for sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix promotion filter and use synchronized methods instead of blocks Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix metrics concurrent access issue Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Configuration options for the layered txpool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use long instead of Instant for PendingTransaction add time Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Move layered txpool clasess in a dedicated package Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove confirmed transaction from sparse set too Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fill gap on added tx Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix eviction on sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix remove from ready layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix remove from sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix for block added and confirmed txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes to sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix the filling of the gap when adding transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Layered pending transactions test and fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Distinguish between layer and comulative space used Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * unit tests for expected next nonce for sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Adding test for transaction selection Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Re-enable prioritized transaction tests and more fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * log stats Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * syncronized some methods, metrics update and dump transactions for replay Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Test that replay tx and fix for tx replacement across layers Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add missing copyright and fix replay test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add consistency check asserts Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix ready internalRemove Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Metrics tests improvements Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP: Transaction memory size estimation Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Complete pending transaction memory used computation Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve metrics Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename to specify that the limit is per layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update metric names in tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Adjust tx layer max capacity according to new tx memory size calculation Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix legacy transaction expiration tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix IndexOutOfBoundsException in sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Unique senders metric Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Ignore ReplayTest by default, fix logging of stats Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Log for replay renamings Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Document howto generate txpool replay Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Reduce max layer capacity Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * exclude transaction replay resource Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve compareByFee when effectivePriorityFee is 0 Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * More debug logs during transaction selection for a block Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use only one thread for building blocks so there is no risk of overlapping Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve transaction trace log making wei human readable Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use List instead of Set when getting all pending transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * More detailed log on adding remote txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Execute transaction broadcast aysnc after adding them to the txpool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Log time taken to add remote txs before their broadcast Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add missing header Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix unit tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add CHANGELOG entry Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Delete unneeded file Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename some layered txpool metrics to avoid conflict with existing metrics Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix pushing to next layers txs following an invalid one Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * In case of an unexpected error, log more data and do a consistency check Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix null check on wrong var Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix some codeql alerts Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix sparse gap calculation when invalidating Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Apply suggestions from doce review Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Only trigger consistency check if trace log is enable in case of unexpected error Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix replay of blocks with no transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix for negative gap when there is a reorg Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Implement code review suggestions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix for a case when deleting tx with zero gap in sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Delete redoundant tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update CHANGELOG.md Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/ReadyTransactions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/layered/AbstractPrioritizedTransactionsTestBase.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/SparseTransactions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update besu/src/main/java/org/hyperledger/besu/cli/options/unstable/TransactionPoolOptions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update besu/src/main/java/org/hyperledger/besu/cli/options/unstable/TransactionPoolOptions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Address code review suggestions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve logSender when there are no sparse txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix off by one error Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/TransactionsLayer.java Co-authored-by: Simon Dudley <simon.dudley@consensys.net> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Address code review suggestions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename fix Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Simplify the way reorgs are handled, by detecting a negative gap and deleting and readding all the txs for that sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Do not run consistency check on internal error since it is too expensive, instead force a reorg of the sender txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove invalid txs after the selection is complete to avoid ConcurrentModificationException Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update txpool defaults Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Tune default Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix merge Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> --------- Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> Signed-off-by: Simon Dudley <simon.dudley@consensys.net> Co-authored-by: Simon Dudley <simon.dudley@consensys.net> Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Co-authored-by: garyschulte <garyschulte@gmail.com>
* Introduce experimental layered transaction pool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * new Xlayered-tx-pool flag to enabled the new tx pool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Move pending transaction sorter tests in the sorter folder Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Unit tests for new and old transaction pool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: do not decrease size when promoting ready txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: remove tx from orderByFee when replaced Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: decrease size when removing confirmed txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: always recreate orderByFee for London fee market Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: transaction removal counter when txs added to block Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: update expected nonce when demoting a prioritized transaction Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix: correctly remove expected nonce entry when removing the last prioritized transaction for the sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix NullPointerException when the replaced tx is not prioritized Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Replace postponed with spare transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix merge from main Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixed most tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix more tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename and reorg some classes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * More renaming and code clean up Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Refactor transaction pool metrics Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Stats log refined Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Cleanup unit tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve stats log Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove unnecessary test parameters Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix unit test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> # Conflicts: # ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java # ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java * Cancel older block creation tasks upon receiving a new one Signed-off-by: Simon Dudley <simon.dudley@consensys.net> * Fixes to expected next nonce for sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix promotion filter and use synchronized methods instead of blocks Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix metrics concurrent access issue Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Configuration options for the layered txpool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use long instead of Instant for PendingTransaction add time Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Move layered txpool clasess in a dedicated package Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove confirmed transaction from sparse set too Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fill gap on added tx Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix eviction on sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix remove from ready layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix remove from sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix for block added and confirmed txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fixes to sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix the filling of the gap when adding transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Layered pending transactions test and fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Distinguish between layer and comulative space used Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * unit tests for expected next nonce for sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Adding test for transaction selection Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Re-enable prioritized transaction tests and more fixes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * log stats Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * syncronized some methods, metrics update and dump transactions for replay Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Test that replay tx and fix for tx replacement across layers Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add missing copyright and fix replay test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add consistency check asserts Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix ready internalRemove Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Metrics tests improvements Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * WIP: Transaction memory size estimation Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Complete pending transaction memory used computation Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve metrics Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename to specify that the limit is per layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update metric names in tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Adjust tx layer max capacity according to new tx memory size calculation Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix legacy transaction expiration tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix IndexOutOfBoundsException in sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Unique senders metric Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Ignore ReplayTest by default, fix logging of stats Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Log for replay renamings Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Document howto generate txpool replay Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Reduce max layer capacity Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * exclude transaction replay resource Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve compareByFee when effectivePriorityFee is 0 Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * More debug logs during transaction selection for a block Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use only one thread for building blocks so there is no risk of overlapping Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve transaction trace log making wei human readable Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use List instead of Set when getting all pending transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * More detailed log on adding remote txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Execute transaction broadcast aysnc after adding them to the txpool Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Log time taken to add remote txs before their broadcast Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add missing header Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix unit tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add CHANGELOG entry Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Delete unneeded file Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename some layered txpool metrics to avoid conflict with existing metrics Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix pushing to next layers txs following an invalid one Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * In case of an unexpected error, log more data and do a consistency check Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix null check on wrong var Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix some codeql alerts Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix sparse gap calculation when invalidating Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Apply suggestions from doce review Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Only trigger consistency check if trace log is enable in case of unexpected error Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix replay of blocks with no transactions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix for negative gap when there is a reorg Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Implement code review suggestions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix for a case when deleting tx with zero gap in sparse layer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Delete redoundant tests Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update CHANGELOG.md Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/ReadyTransactions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/layered/AbstractPrioritizedTransactionsTestBase.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/SparseTransactions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update besu/src/main/java/org/hyperledger/besu/cli/options/unstable/TransactionPoolOptions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update besu/src/main/java/org/hyperledger/besu/cli/options/unstable/TransactionPoolOptions.java Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Address code review suggestions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Improve logSender when there are no sparse txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix off by one error Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/TransactionsLayer.java Co-authored-by: Simon Dudley <simon.dudley@consensys.net> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Address code review suggestions Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Rename fix Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Simplify the way reorgs are handled, by detecting a negative gap and deleting and readding all the txs for that sender Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Do not run consistency check on internal error since it is too expensive, instead force a reorg of the sender txs Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove invalid txs after the selection is complete to avoid ConcurrentModificationException Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update txpool defaults Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Tune default Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Fix merge Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> --------- Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> Signed-off-by: Simon Dudley <simon.dudley@consensys.net> Co-authored-by: Simon Dudley <simon.dudley@consensys.net> Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com> Co-authored-by: garyschulte <garyschulte@gmail.com>
PR description
This implements a new transaction pool (txpool for brevity), with the main goal to better manage nonce gaps, i.e. the possibility that the list of transactions that we see for a sender could not be in order neither contiguous, that could happen just of the way there are broadcasted on the p2p network or intentionally to try to spam the txpool with non executable transactions (transactions that could not be included in a future block), so the goal is to try to keep in the pool transactions that could be selected for a future block proposal, and a the same time, without penalizing legitimate unordered transactions, that are only temporary non executable.
It is disabled by default, to enable use the option
Xlayered-tx-pool=true
The main idea is to organize the txpool in an arbitrary number of layers, where each layer has specific rules and constraints that determine if a transaction belong or not to that layer and also the way transactions move across layers.
Some design choices that apply to all layers are that a transaction can only be in one layer at any time, and that layers are chained by priority, so the first layer has the transactions that are candidate for a block proposal, and the last layer basically is where transactions are dropped. Layers are meant to be added and removed in case of specific future needs.
When adding a new transaction, it is first tried on the first layer, if it is not accepted then the next one is tried and so on.
Layers could be limited by transaction number of by space, and when a layer if full, it overflow to the next one and so on, instead when some space is freed, usually when transactions are removed since confirmed in a block, transactions from the next layer are promoted until there is space.
The current implementation is based on 3 layers, plus the last one that just drop every transaction when the previous layers are full. The 3 layers are, in order:
Prioritized:
This is where candidate transactions are selected for creating a new block.
Transaction are ordered by the effective priority fee, and it is limited by size, 2000 by default, to reduce the overhead of the sorting and because that number is enough to fill any block, at the current gas limit.
Does not allow nonce gaps, and the first transaction for each sender must be the next one for that sender.
Eviction is done removing the transaction with the higher nonce for the sender of the less valuable transaction, to avoid creating nonce gaps, evicted transactions go into the next layer Ready.
Ready:
Similar to the Prioritized, it does not allow nonce gaps, and the first transaction for each sender must be the next one for that sender, but it is limited by space instead of count, thus allowing many more transactions, think about this layer like a buffer for the Prioritized.
Since it is meant to keep ten to hundred of thousand of transactions, it does not have a full ordering, like the previous, but only the first transaction for each sender is ordered using a stable value that is the max fee per gas.
Eviction is the same as the Prioritized, and evicted transaction go into the next layer Sparse.
Sparse:
This is the first layer where nonce gaps are allowed and where the first transaction for a sender could not be the next expected one for that sender.
The main purpose of this layer is to act as a purgatory for temporary unordered and/or non contiguous transactions, so that they could become ready asap the missing transactions arrive, or they are eventually evicted.
It also keep the less valuable ready transactions, that are evicted from the previous layer.
It is limited by space, and eviction select the oldest transaction first, that is sent to the End Layer that just drop it.
When promoting to the prev layer Ready, only transactions that will not create nonce gaps are selected, for that we need to keep track of the nonce distance for each sender. So we can say that is ordered by nonce distance for promotion.
Changes in Besu options
When layered txpool is enabled some existing options are ignored and new options have been added, basically due to the fact that there is no more a limit in the number of transactions that the pool can contain, but the limit is the estimated amount of memory used.
Ignored options
--tx-pool-max-size
--tx-pool-limit-by-account-percentage
New options
Note: these options and their default values are subject to change while in the experimental phase.
--Xlayered-tx-pool-layer-max-capacity
the max amount of memory (in bytes) that a limited by space layer can use. Applies to Ready and Sparse layers. Default 50_000_000 bytes (50MB).--Xlayered-tx-pool-max-prioritized
the maximum number of transaction that the Prioritized layer can contain. This Layer is limited by size to reduce the work needed for sorting. Default is 2000, and has been chosen since it is more than enough to fill any block at the current gas limit.--Xlayered-tx-pool-max-future-by-sender
define the max difference between the incoming transaction nonce and the current sender nonce, for a transaction to be accepted in the txpool. This means that too far in the future transactions are ignored. Default is 200.I have run it on mainnet and compared it against the current implementation, using mitmproxy plus this custom addon to simulate a block production every other FcU, and collected the result in this document Layered Transaction Pool metrics.ods, the results are good, and on average the Layered txpool produces with 12% more transactions in less time.
A bunch of new metrics have been added, that could be used to graph totals and details about each layer, in the official Besu Full dashboard