Chenyo's org-static-blog

Posts tagged "bnb":

14 Jul 2024

Parallel EVM: BEP-130

This is a personal note for BEP-130. BEP-130 is a proposal that introduces a parallel transaction execution mechanism on the BNB Smart Chain (BSC).

1. Blockchain fundamentals

1.1. System contract

  • Built-in contracts to perform system level operations, e,g., gas fee reward, cross chain communication.
  • Cannot be executed concurrently since they depend on the execution results of other transactions, e.g., a number of transaction made by an account at some timestamp.

1.2. Transaction execution phases

  • Block mining phase: received from the P2P transaction pool, could contain invalid transactions.
  • Block sync phase: the block is confirmed.

2. Design principle

  • Should always produce the same result as the current sequential execution.
  • Should be decoupled into existing or new modules with no circular dependency.
  • Should be configurable based on node hardware resources.
  • Keep it simple and smart.

3. Workflow

3.1. Dispatch factors

  • Is the slot idle or occupied?
  • Is there a same address contract running or pending in this slot?
  • Has the slot’s pending transactions size reached the max transactions queue size limitation?
  • Is there a big transaction index gap between the slot’s head transaction and the dispatched transaction?
  • Is the transaction contract likely to have high gas cost or a conflict rate?

3.2. Slot execution stages

  1. Execute the transaction \(Tx_i\)based on a specific worldstate, e.g., the state when the execution starts.
  2. Wait for the finalization of the previous transaction \(Tx_{i-1}\).
  3. Detect if there is any conflict between the state read by \(Tx_i\) and the state change after the execution of \(Tx_i\) starts.
  4. If a conflict is detected, re-execute \(Tx_{i}\) again based on the latest finalized worldstate.
  5. Finalize the state changed by \(Tx_i\) to the latest worldstate.
  6. The state changes are kept within each slot, and are merged to the main StateDB once the execution is done.
  7. The first transaction in a block can be immediately finalized.
  8. If \(Tx_i\) and \(Tx_{i-1}\) are in the same slot, \(Tx_i\) can immediately start conflict detection.
  9. Re-executed transaction can be immediately finalized as it reads the latest worldstate.

3.3. Conflict detection

  • Detection items: storage key/value pair; account balance; contract content and status.
  • Overlap reads without write, or hardcode writes without read are not conflicts.
Tags: evm parallel-evm bnb
07 Jul 2024

Parallel EVM: BNB chain

This is a personal note for BNB chain-blog.

1. Blockchain fundamentals

1.1. Why is parallel EVM not easy

  • Lack of visibility of potential transaction conflict.
  • Blockchains experience transaction bursts, e.g., >70M transactions per day.

1.2. A Parallel EVM ideas

  • Run multiple EVM instances concurrently on different threads.
  • Execute transactions independently on each thread and later merge a finial state update.
  • Parallel EVM scheme

1.3. Block STM algorithm

  • Optimistic parallelism: assigns transactions to various threads.
  • Software transaction memory (STM): detect conflicts when transactions try to modify the same shared state simultaneously.
  • Conflict resolution: when conflicts are detected, the offending transactions are discarded without affecting the blockchain state and are re-executed.

2. BNB Parallel EVM 1.0: Infrastructure

  • Proposal: BEP-130 (2022)
  • Dispatcher: distributes transactions across threads to optimize throughput.
  • Parallel execution engine: execute transactions independently on each thread.
  • Local stateDB: each thread maintains a local stateDB to record state access.
  • Conflict detection: detect conflicts and re-execute conflicting transactions.
  • State commit: the finalized results are committed to the global state DB.

3. BNB Parallel EVM 2.0: Performance enhancement

  • Dispatcher: combine both static and dynamic dispatch strategies.
  • Execution engine: streaming pipeline to enable smooth transaction processing.
  • Conflict detection: ensure data integrity while minimizing unnecessary re-execution.
  • Memory: shared memory pools and light copying techniques to reduce memory footprint.
  • The overall performance ranges from 20% to 50%.

4. BNB Parallel EVM 3.0: Production

4.1. Hint-based dispatcher

  • leverages external hint providers to analyze transactions and generate predictions about potential state access conflicts.
  • Simple hints include read/write state sets; advanced hints incorporate weak/strong ordering for optimal parallelism.
  • Conflicting transactions are assigned to the same slot.
  • Transactions with no conflicts are distributed across different slots.
  • Conflict detector remains as a backup for handling unforeseen conflicts.

4.2. Seamless BNB chain ecosystem integration

  • Modularization and reconstructing.
  • Thorough testing and validation.

5. Comparison with other solutions

Solutions TX dependency check Conflict resolution StateDB optimization
BlockSTM tracks at execution re-execution N/A
Polygon minimal metadata solution reduced re-execution N/A
Monad static analysis reduced re-execution Monad DB
Sei tracks at execution re-execution SeiDB
Neon EVM and Solana Sealevel contract provided reduced re-execution depends on Solana
BNBChain hint info reduced or eliminated re-execution Thread local DB

6. Other optimizations

  • Opcode-level optimization: fine-tuning individual EVM instructions for maximum efficiency.
  • Compilation optimization: JIT/AOT compilation paradigms; instruction-level parallelism (SIMD).
  • Database sharding: distribute data across multiple databases.
  • Concurrent node execution.
Tags: evm parallel-evm bnb
Other posts