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

Optimistic parallelization of transactions to improve performance #7296

Merged
merged 49 commits into from
Jul 19, 2024

Conversation

matkt
Copy link
Contributor

@matkt matkt commented Jul 5, 2024

PR description

The idea of this PR is to create a system for parallelizing transactions in an optimistic way. What's the parallelization mechanism we're proposing?

We plan to execute all transactions within a block in parallel, without checking for conflicts between them. We're starting with the assumption that everything can be parallelized. Then, without waiting for the results of these background executions, we'll process the block sequentially and fix conflict.

matkt and others added 23 commits July 2, 2024 17:00
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Initialize ConcurrentHashMap used in the accumulator to avoid collisions

Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
@matkt matkt force-pushed the feature/block_per_improvment branch from 46c7e3a to 69f36b6 Compare July 10, 2024 09:48
Signed-off-by: Karim Taam <[email protected]>
@matkt matkt force-pushed the feature/block_per_improvment branch from 64ed174 to 6baafdc Compare July 10, 2024 12:37
matkt and others added 4 commits July 10, 2024 14:53
Signed-off-by: Karim Taam <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
…ns and the number of conflicting transactions

Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Karim Taam <[email protected]>
matkt and others added 4 commits July 12, 2024 16:27
Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Ameziane H <[email protected]>
@@ -484,6 +483,9 @@ public TransactionProcessingResult processTransaction(
final Wei coinbaseWeiDelta =
coinbaseCalculator.price(usedGas, transactionGasPrice, blockHeader.getBaseFee());

operationTracer.traceBeforeRewardTransaction(worldUpdater, transaction, coinbaseWeiDelta);
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems unrelated

Copy link
Contributor

Choose a reason for hiding this comment

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

This was a hint used by Karim to handle the miningBeneficiary case. He used the tracing feature to access the context of miningBeneficiary in ParallelizedConcurrentTransactionProcessor.runTransaction, and check if the address of the miner was touched before executing the transaction.

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 it is necessary for the PR and for conflict detection. I explain it in the comments normally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact I use it to know if we touched the address of the beneficiary before paying the reward. If we touch it before it is a conflict if we touch it just for the reward it's ok

Copy link
Contributor

@garyschulte garyschulte Jul 18, 2024

Choose a reason for hiding this comment

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

Sorry for the late feedback here, but wouldn't paying the reward to the coinbase affect consensus? If one of the parallel transactions was from the coinbase, couldn't its result be affected by interim transaction rewards if the coinbase submitted a transaction that invoked BALANCE opcode?

I think we need to flatly ban coinbase from parallelization for this reason.

this ban exists already in the conflict detection, I just missed it because it was for miningBeneficiary and I was looking for coinbase

@ahamlat ahamlat enabled auto-merge (squash) July 18, 2024 09:11
@ahamlat ahamlat disabled auto-merge July 18, 2024 09:33
@ahamlat ahamlat enabled auto-merge (squash) July 18, 2024 09:33
@ahamlat ahamlat disabled auto-merge July 18, 2024 09:33
@ahamlat ahamlat enabled auto-merge (squash) July 18, 2024 09:45
@ahamlat ahamlat requested a review from fab-10 July 18, 2024 10:23
Copy link
Contributor

@garyschulte garyschulte left a comment

Choose a reason for hiding this comment

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

I think transactions sent from a coinbase are a problem, and they should be banned from parallelization period.

If I am wrong, I would love to see a test block that proved parallelized coinbase transactions which use BALANCE opcode are safe/deterministic.

@@ -484,6 +483,9 @@ public TransactionProcessingResult processTransaction(
final Wei coinbaseWeiDelta =
coinbaseCalculator.price(usedGas, transactionGasPrice, blockHeader.getBaseFee());

operationTracer.traceBeforeRewardTransaction(worldUpdater, transaction, coinbaseWeiDelta);
Copy link
Contributor

@garyschulte garyschulte Jul 18, 2024

Choose a reason for hiding this comment

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

Sorry for the late feedback here, but wouldn't paying the reward to the coinbase affect consensus? If one of the parallel transactions was from the coinbase, couldn't its result be affected by interim transaction rewards if the coinbase submitted a transaction that invoked BALANCE opcode?

I think we need to flatly ban coinbase from parallelization for this reason.

this ban exists already in the conflict detection, I just missed it because it was for miningBeneficiary and I was looking for coinbase

@matkt
Copy link
Contributor Author

matkt commented Jul 18, 2024

I think transactions sent from a coinbase are a problem, and they should be banned from parallelization period.

If I am wrong, I would love to see a test block that proved parallelized coinbase transactions which use BALANCE opcode are safe/deterministic.

A transaction that will interact with the coinbase to send, receive, or simply read or modify throughout the transaction will be in the accumulator at the time of this event #7296 (comment).

Therefore, before paying the reward, If we detect this coinbase access, it will necessarily be banned from parallelization, and that's why we have this code. We track the state of the accumulator before paying the reward because it allows us to detect these transactions that need the correct state of the coinbase, and we ban them. This allows us to differentiate a simple payment of transaction fees, which will not impact the transaction's behavior, from a transaction whose behavior will depend on the coinbase. Then we pay the rewards in the sequential part to have the correct coinbase balance for the start of each transaction. Thus, if a transaction is banned due to its access to the coinbase, it will no longer have a problem when processed sequentially because the balance of a coinbase has been correctly updated by previous transactions during the sequential part

Here is the part of the code where we ban transactions that have access to coinbase https://github.com/hyperledger/besu/pull/7296/files#diff-53805bb7138bc804441822c2d6acca3a1c72a26926563bc898b28eec5654c812R48

So unless I did not understand your remark for me it is already managed and was precisely an important element of the PR

@garyschulte
Copy link
Contributor

So unless I did not understand your remark for me it is already managed and was precisely an important element of the PR

AFAIK, we do not need a transaction to directly interact with the coinbase in order to alter its state.

My thinking is that each transaction pays coinbase some bit at its conclusion. In a serial execution context this just means that any future transactions in the block would see the coinbase with an increased balance.

If in that same block, the coinbase is the originator of a transaction, nothing else would conflict with it because we are not considering coinbase reward as a conflict. If the coinbase transaction attempts to read its balance in isolation, without the tx rewards that might/would have accumulated before it executed its transaction, then a parallel coinbase tx would report a different value when executing the BALANCE opcode than if it had been executed serially.

@garyschulte
Copy link
Contributor

garyschulte commented Jul 18, 2024

So unless I did not understand your remark for me it is already managed and was precisely an important element of the PR

AFAIK, we do not need a transaction to directly interact with the coinbase in order to alter its state.

talking oob with Karim, he pointed out that the reason I didn't see the coinbase conflict detection is because I was searching for "coinbase", and the check is using "miningBeneficiary":
https://github.com/hyperledger/besu/pull/7296/files#diff-53805bb7138bc804441822c2d6acca3a1c72a26926563bc898b28eec5654c812R48

@garyschulte garyschulte dismissed their stale review July 18, 2024 21:44

We should add explicit test coverage before this gets out of experimental, but otherwise lgtm

Comment on lines +165 to +171
if (transactionCollisionDetector
.getAddressesTouchedByTransaction(
transaction, Optional.of(roundWorldStateUpdater))
.contains(miningBeneficiary)) {
contextBuilder.isMiningBeneficiaryTouchedPreRewardByTransaction(true);
}
contextBuilder.miningBeneficiaryReward(miningReward);
Copy link
Contributor

Choose a reason for hiding this comment

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

Running gradle check with code coverage doesn't show this covered. Definitely would be nice to have this covered in test

Copy link
Contributor

@fab-10 fab-10 left a comment

Choose a reason for hiding this comment

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

The idea behind this feature make sense, and as first step looks good to me, and I have nothing else to add, just waiting for the approval from @garyschulte since he has much more knowledge on this part.

Copy link
Contributor

@garyschulte garyschulte left a comment

Choose a reason for hiding this comment

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

Lets merge and continue to test 👍

@ahamlat ahamlat merged commit 30c96c7 into hyperledger:main Jul 19, 2024
40 checks passed
gconnect pushed a commit to gconnect/besu that referenced this pull request Aug 26, 2024
…perledger#7296)

Optimistic transaction parallelization execution during block processing to improve the performances. This feature can enabled with a flag --Xbonsai-parallel-tx-processing-enabled=true

Signed-off-by: Karim Taam <[email protected]>
Co-authored-by: Ameziane H <[email protected]>
Co-authored-by: garyschulte <[email protected]>
Signed-off-by: gconnect <[email protected]>
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.

4 participants