Besu Documentation
repository·main·Indexed 23 days ago
https://github.com/besu-eth/besuBesu is an Apache 2.0 licensed, Mainnet-compatible Ethereum execution client written in Java, designed for public and private networks. This documentation covers development tasks including the Engine API architecture, OpenTelemetry tracing setup, the EVM Tool for testing and validation, and guidelines for creating acceptance test plugins.
What's inside Besu
- EVM Tool is a standalone EVM (Ethereum Virtual Machine) executor and test execution tool. Its primary purpose is the testing and validation of the EVM and its associated data structures.
Core Features in Besu 1.0
mainBesu version 1.0 introduced several foundational features for private and authenticated networks:
- IBFT 2.0 Support: Implementation of the Istanbul Byzantine Fault Tolerance 2.0 consensus mechanism.
- Permissioning: A system to control access to the network and specific RPC methods.
- JSON-RPC Authentication: Support for securing JSON-RPC interfaces, including JWT-based authentication.
- Genesis Contract Code: Support for including contract code directly in the genesis block.
- RLP Encode Subcommand: A new CLI subcommand for RLP encoding.
How ParallelStoredMerklePatriciaTrie works
mainThe
ParallelStoredMerklePatriciaTrieis designed to speed up batch updates to a Merkle Patricia Trie by processing independent branches across multiple CPU cores. Instead of updating keys one-by-one, it uses a three-phase approach:- Accumulation Phase: Collects all updates without applying them.
- Parallel Processing Phase: Recursively applies updates, splitting work into parallel tasks when beneficial.
- Persistence Phase: Writes all modified nodes to storage in a single batch.
Parallelization is achieved by grouping updates by their next hex nibble at branch nodes. If a group contains multiple updates and the branch has multiple active groups, the work is dispatched to a thread pool. Otherwise, it is processed sequentially to avoid task creation overhead.
Key architectural principles include:
- Immutability: Nodes are never modified in place; every update creates a new node instance, allowing safe concurrent reads.
- Independence: Updates to different children of a branch node (e.g.,
child[A]vschild[F]) are data-independent and can run in parallel. - Deferred Persistence: A
CommitCache(a thread-safe map) collects nodes during parallel processing to allow a single, batched I/O flush at the end.
Configure transaction pool limits
mainStarting with version 22.7.3, you can limit transaction pool consumption by a specific sender to a configurable percentage of the total pool size. This helps prevent a single sender from filling the pool with non-executable transactions that could lead to empty or semi-empty block proposals.How Engine API methods are architected and extended
mainThe Besu Engine API (
engine_*JSON-RPC methods) uses a strict versioning pattern based on sealed class hierarchies. This ensures that each new version of a method (e.g.,engine_forkchoiceUpdatedV2) extends the previous version (e.g.,engine_forkchoiceUpdatedV1) and only overrides the specific hooks required by the specification change.Key Architectural Components
- Sealed Class Hierarchies: Method series (like
EngineNewPayloadV*) are implemented as sealed classes where version $N$ extends $N-1$. The latest version is markedfinal. - Base Class: All versions extend
ExecutionEngineJsonRpcMethod, which handles fork-window validation usingminSupportedForkandfirstUnsupportedFork. - Constructor Pattern: Migrated series use a single
ExecutionEngineJsonRpcMethod.ConstructorArgumentsrecord (built viaConstructorArgumentsBuilder) instead of positional arguments. This allows theVersionSchedulerto manage versioning through a shared factory shape. - Data Structures: Request parameters (in
..internal.parameters) and results (in..internal.results) also follow sealed hierarchy patterns to mirror specification versions. - Hooks: Version classes override protected hooks (e.g.,
createResponse,validateParameters) rather than re-implementing the entire request flow.
- Sealed Class Hierarchies: Method series (like
Parallelization Decision Logic for Trie Updates
mainThe
ParallelStoredMerklePatriciaTrieuses specific rules to decide whether to spawn a new parallel task or continue processing sequentially. This prevents the overhead of thread management from outweighing the benefits of concurrency.A group of updates is assigned to parallel processing only if both of the following conditions are met:
- The group contains more than 1 update.
- The branch has more than 1 active group (i.e., updates are destined for different children).
Summary of behaviors:
- Single update in a group: Processed sequentially (faster for tiny tasks).
- Multiple updates to the same child: Processed sequentially at the current level, but may parallelize further down the tree if that child is a branch.
- Multiple updates to different children: Processed in parallel via a thread pool.
One-way database upgrade in Besu 22.4.0
mainIn version 22.4.0, a new column family was added to support backward sync.
Warning: This is a one-way upgrade. Once you upgrade your database to this version, you cannot roll back to a previous version of Besu.
How Bonsai cross-block cache and versioning work
mainWhen
bonsaiCrossBlockCacheEnabledis enabled, Besu uses a versioned cross-block cache to manage accounts and storage slots. This design allows a single shared cache to remain correct across reorgs, snapshots, and head reads by using a global monotonic version instead of block numbers.Key Concepts
- Global Monotonic Version: Managed by
VersionedCacheManager, this version increments on every successful world-state commit (viaCachedUpdater.commit()). It is independent of block height; even if a reorg causes a block height to repeat, the cache version continues to move forward, ensuring different histories do not incorrectly reuse cache generations. - Pinned Versions:
- Head Storage: The
cacheVersionis updated to match theglobalVersionafter every commit. - Snapshot Storage: When a snapshot is created (
BonsaiSnapshotWorldStateKeyValueStorage), it captures the currentcacheVersionand keeps it immutable. This pins the snapshot to a specific logical cache epoch.
- Head Storage: The
- Cache Entry Lifecycle: The cache maps a key to a
VersionedValue. If a write occurs at versionVand the existing entry is older (existing.version < V), the old entry is replaced. The cache does not retain multiple versions of the same key simultaneously.
Read Path Logic
For a read request
getFromCacheOrStorage(segment, key, version, storageGetter):- Cache Hit: Occurs if an entry exists and
cachedEntry.version <= readerVersion. The reader can see cached data as long as it is not newer than their pinned epoch. - Cache Miss: If no valid entry is found, the value is loaded from storage via
storageGetter. - Cache Population: A miss only populates the cache if
readerVersion == globalVersion. This ensures the moving head can warm the cache, while stale snapshots (wherereaderVersion < globalVersion) read from storage without overwriting the head's cache state with potentially stale data.
Cached Segments
The
VersionedCacheManageronly caches the following segments:ACCOUNT_INFO_STATEACCOUNT_STORAGE_STORAGE
- Global Monotonic Version: Managed by
Configure JWT authentication for Engine APIs in Besu 22.1.2
mainStarting with version 22.1.2, Besu adds JWT authentication to Engine APIs to support the Execution Layer (The Merge). This is required for secure communication between the Execution Client and the Consensus Client.How to analyze Besu profiling flamegraphs
mainAfter profiling, open the generated HTML file (e.g.,
/tmp/besu-profile.html) in a browser.Reading the Flamegraph:
- Top boxes: Represent methods actively running when CPU samples were taken.
- Boxes underneath: Represent the call stack leading up to the active methods.
- Width: Indicates the total time spent in that method (including its children).
- Wide boxes indicate potential performance hot spots.
- Height: Indicates the depth of the call chains.
Expose custom JSON-RPC or WebSocket methods via plugins
mainAs an early access feature available since version 21.10.1, Besu allows the use of plugins to expose custom JSON-RPC or WebSocket methods, enabling developers to extend the node's API surface.Configure bootnodes for onchain permissioning
mainWhen using onchain permissioning, non-validator bootnodes cannot peer with permissioned nodes.
Requirement: Ensure that all bootnodes are also configured as validators when using onchain permissioning.