-
Notifications
You must be signed in to change notification settings - Fork 569
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
BEP-396: Accelerate Block Execution by TxDAG #396
Open
galaio
wants to merge
6
commits into
bnb-chain:master
Choose a base branch
from
galaio:bep396
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unclezoro
reviewed
Jun 18, 2024
unclezoro
reviewed
Jun 19, 2024
unclezoro
reviewed
Jun 19, 2024
unclezoro
reviewed
Jun 19, 2024
unclezoro
reviewed
Jun 19, 2024
unclezoro
reviewed
Jun 19, 2024
unclezoro
reviewed
Jun 19, 2024
wacyrkak
approved these changes
Jul 12, 2024
octavio12345300
approved these changes
Oct 24, 2024
octavio12345300
approved these changes
Oct 26, 2024
Magkoooh
approved these changes
Nov 25, 2024
Magkoooh
approved these changes
Nov 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BEP-396: Accelerate Block Execution by TxDAG
1. Summary
More and more blockchains are aware of the importance of parallel execution for performance improvement. Common parallel processors will optimistically execute transactions in parallel. When conflicts are found, they can only be re-executed to obtain the correct results, which wastes a lot of computing resources. And the worst case may cause performance degradation even worse than sequential execution.
This BEP introduces a transaction dependency graph,
TxDAG
, which provides accurate dependencies in the form of a directed acyclic graph (DAG) for parallelization instead of optimistically estimated dependencies when executing blocks, will greatly improve the efficiency of parallel execution by eliminating the re-execution caused by TX conflict.2. Abstract
This BEP introduces the transaction dependency graph,
TxDAG
, which is used to accelerate the validation of blockchain blocks. This structure has strict format requirements to ensure the correctness of dependencies and is compatible with multiple-chain execution processors.3. Status
Draft
4. Motivation
As the scale of transactions and state data grows, performance improvement is the key to providing a good user experience for blockchain. One of the current main optimization directions is to use an optimistic transaction parallel processor, which optimistically assumes that transactions have no dependencies and executes them in parallel, then continues to retry execution after verification fails until all transactions are verified.
However, these additional retries waste a lot of computing resources. The read-write set generated by transaction execution can easily build a dependency graph between transactions, called
TxDAG
. This data can be used in the validating block phase to accelerate block execution and further improve the overall performance of the blockchain.5. Specification
In BSC, the block interval is 3s, that is, the more transactions are executed within 3s, the better the chain performance and the greater the throughput.
The two parts that take the most time in the 3s are the validating block and the mining block. Validators often validate the correctness of the current block and continue to mine the next block based on the latest state.
To improve the performance of block execution, transactions need to be executed in parallel as much as possible to make full use of computing and IO resources. Imagine that if the dependencies of transactions are known, then the parallel execution path can be directly determined, but the dependencies need to be obtained by the read-write set after the transaction pre-execution (static analysis is not within the scope of this BEP).
So two different TX execution strategies can be identified:
In the mining phase, TXs of blocks are executed in parallel or serial to generate
TxDAG
. In the validating phase,TxDAG
is used to further improve the parallel execution efficiency.5.1 Tx Dependency
Assume that the execution of each transaction is atomic, and the input and output of the execution generate a state read-write set. In concurrent processing, read-only access to the resources is safe, but write operations require exclusive resources.
At the same time, all transactions in the blockchain have a preset order, and transactions with lower TxIndex have higher execution priority.
Therefore, the dependency constraint goes to, if transaction Txi writes
StateA
first, then transaction Txj readsStateA
and j>i is satisfied. That is to say, Txj depends on Txi, Txj must be executed after Txi.If a transaction reads and writes the same value at a memory location, the location can be considered read-only.
5.2 TxDAG Structure
The structure of
TxDAG
is defined as follows.This BEP can use
PlainTxDAGType
to extend more types.There is also a potential
TxDAG
"compression algorithm" to further optimize the size. For the DAG describing the dependencies, some are redundant because it only needs to ensure that the final execution path is consistent.Specific rules: If Txj depends on Txi, j>i, then Txj can ignore the same dependencies as Txi, and the execution path of Txj after the update remains the same as before.
This can keep the size of
TxDAG
as small as possible and avoid generating too largeTxDAG
and wasting network bandwidth.5.3 TxDAG Validation
Since there is a preset order of transactions in the block, Txi can't depend on Txj (i<j) in DAG, and it is also impossible for circular dependencies to occur.
The verification rules are as follows:
TxDAG
cannot exceed the maximum TxIndex of the block;5.4 TxDAG Forwarding
TxDAG is only meaningful if it is shared in the network. There are multiple ways to forward this metadata:
Any of the above methods can achieve sharing, especially using Gasless transactions can avoid Hard Fork, faster implementation and verification of TxDAG performance.
5.5 TxDAG Generation
The generation of
TxDAG
requires analysis of the transaction read-write set. You can set a read-write tracker in the EVM'sStateDB
. When reading the state from the DB, it is recorded in the read set. When creating or updating the state, it is recorded in the write set. When reverting the modification, the write set also needs to be reverted.Finally, the read-write dependency of the transaction is traversed to generate
TxDAG
.5.6 Validating Block
The easiest way to speed up validating blocks through
TxDAG
is to integrate with parallel processors. Usually, parallel processors only optimistically execute transactions in parallel. IfTxDAG
is input, the parallel processor can execute the entire block in the best parallel path. The parallel processor can easily find bad DAG.At the same time,
TxDAG
can be applied in state prefetcher scenarios, and the state can be loaded in advance through simple parallel prefetchers to improve the cache hit rate of the processor.It is worth mentioning that TxDAG provides dependencies. The processor can flexibly schedule transaction execution according to the characteristics of TxDAG, and can give priority to scheduling paths with more dependencies and longer execution time.
If the DAG generated locally by the validator does not match the block, this block should be rejected.
6. Rationale
6.1 GasFee Distribution
In the generation of
TxDAG
, there is a special state modification, GasFee distribution. In BSC, the GasFee of each transaction will be pre-saved inSystemAddress
and then distributed by the consensus engine.When collecting read-write sets, the write operation to SystemAddress will be ignored first, and the GasFee calculation needs to be delayed, which ensures the parallelism of the transaction.
When there is a read-write SystemAddress in the transaction, the
TxDAG
will indicate it needs to calculate GasFee in time, which may cause some transactions to be unable to be executed in parallel, or even all transactions can only be executed serially.6.2 System Transaction
In BSC,
Parlia
will add system transactions to distribute rewards, slash validators, etc. after assembling ordinary transactions.From the perspective of consensus, this part of the transaction can only be executed last, so this part of the transaction is not included in the read-write set and will be saved in the
TxDAG
as all dependencies on all previous Txs.7. Backwards Compatibility
7.1 Block Processor
TxDAG
can be used for Parallel Processor or Serial Processor, but both require DAG verification.For clients with only a Serial Processor,
TxDAG
can be passed to State Prefetcher to improve cache hit rate and performance.7.2 Hard Fork
This BEP needs to determine whether to introduce Hard Fork based on actual implementation.
8. Security Considerations
8.1 Malicious DAG
Malicious DAG can be divided into two categories, Wrong DAG and Bad DAG.
Wrong DAG may include wrong TxIndex, violate preset order, or even have circular dependencies, which can be quickly identified through verification and the block will be rejected.
Bad DAG is more subtle and may provide wrong dependencies, resulting in reduced parallelism or even conflicting DAGs. This type of DAG needs to be identified by the block processor, and if found, the block should be rejected.
8.2 Parallel Processor
When the parallel processor uses
TxDAG
for execution, it is still necessary to check whether there are conflicts between transactions to avoid network failures caused by Bad DAG.9. License
The content is licensed under CC0.