Skip to content

refactor(core): Refactor Patricia Merkle Trie (avoid compulsive hashing)#2687

Merged
azteca1998 merged 35 commits into
mainfrom
refactor-trie-avoid-compulsive-hashing
May 29, 2025
Merged

refactor(core): Refactor Patricia Merkle Trie (avoid compulsive hashing)#2687
azteca1998 merged 35 commits into
mainfrom
refactor-trie-avoid-compulsive-hashing

Conversation

@azteca1998
Copy link
Copy Markdown
Contributor

Motivation

Refactor the Patricia Merkle Trie to avoid rehashing the affected part of the trie every time it is modified. Delay hashing until its hash is required.

Replaces node hash references by either a NodeHash as before (for unbuffered nodes) or a node itself (for modified nodes).

Closes #issue_number

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2025

Lines of code report

Total lines added: 206
Total lines removed: 147
Total lines changed: 353

Detailed view
+------------------------------------------------------+-------+------+
| File                                                 | Lines | Diff |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/node.rs                    | 241   | +92  |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/node/branch.rs             | 538   | +3   |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/node/extension.rs          | 430   | +16  |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/node/leaf.rs               | 264   | -1   |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/rlp.rs                     | 121   | +14  |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/trie.rs                    | 870   | +60  |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/trie_iter.rs               | 94    | +4   |
+------------------------------------------------------+-------+------+
| ethrex/crates/common/trie/verify_range.rs            | 469   | -146 |
+------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/sync/state_healing.rs   | 136   | +8   |
+------------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/sync/storage_healing.rs | 115   | +9   |
+------------------------------------------------------+-------+------+

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2025

Benchmark for ffa87eb

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 40.6±2.67ms 38.1±1.49ms -6.16%
Trie/cita-trie insert 1k 3.7±0.03ms 3.8±0.05ms +2.70%
Trie/ethrex-trie insert 10k 132.8±2.12ms 159.1±2.13ms +19.80%
Trie/ethrex-trie insert 1k 11.1±0.15ms 13.7±0.40ms +23.42%

@edg-l edg-l added the performance Block execution throughput and performance in general label May 8, 2025
@github-actions
Copy link
Copy Markdown

Benchmark for 1c71d11

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 40.6±4.43ms 39.1±4.34ms -3.69%
Trie/cita-trie insert 1k 3.7±0.07ms 3.7±0.08ms 0.00%
Trie/ethrex-trie insert 10k 135.1±5.03ms 20.6±0.98s +15147.96%
Trie/ethrex-trie insert 1k 11.1±0.28ms 160.5±3.48ms +1345.95%

@github-actions
Copy link
Copy Markdown

Benchmark for f2fefcb

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 34.0±0.37ms 31.1±0.43ms -8.53%
Trie/cita-trie insert 1k 3.4±0.05ms 3.2±0.03ms -5.88%
Trie/ethrex-trie insert 10k 114.6±1.28ms 16.3±0.44s +14123.39%
Trie/ethrex-trie insert 1k 10.1±0.11ms 156.0±3.87ms +1444.55%

@github-actions
Copy link
Copy Markdown

Benchmark for eb0b028

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 37.0±1.10ms 36.8±1.95ms -0.54%
Trie/cita-trie insert 1k 3.7±0.04ms 3.6±0.02ms -2.70%
Trie/ethrex-trie insert 10k 132.3±2.41ms 18.4±0.60s +13807.79%
Trie/ethrex-trie insert 1k 11.3±0.38ms 164.7±3.50ms +1357.52%

@github-actions
Copy link
Copy Markdown

Benchmark for 3db09e5

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.6±0.76ms 35.6±0.51ms 0.00%
Trie/cita-trie insert 1k 3.7±0.04ms 3.7±0.04ms 0.00%
Trie/ethrex-trie insert 10k 128.1±2.26ms 16.8±0.43s +13014.75%
Trie/ethrex-trie insert 1k 11.1±0.18ms 158.2±2.57ms +1325.23%

@github-actions
Copy link
Copy Markdown

Benchmark for 80e45a7

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.9±0.68ms 36.5±1.44ms +1.67%
Trie/cita-trie insert 1k 3.7±0.04ms 3.7±0.03ms 0.00%
Trie/ethrex-trie insert 10k 128.9±1.16ms 18.7±0.75s +14407.37%
Trie/ethrex-trie insert 1k 11.1±0.21ms 163.2±1.26ms +1370.27%

@github-actions
Copy link
Copy Markdown

Benchmark for 48f8dd6

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.1±0.87ms 36.7±1.93ms +4.56%
Trie/cita-trie insert 1k 3.6±0.09ms 3.6±0.06ms 0.00%
Trie/ethrex-trie insert 10k 124.2±4.20ms 18.8±1.75s +15036.88%
Trie/ethrex-trie insert 1k 11.1±0.22ms 157.1±2.09ms +1315.32%

@github-actions
Copy link
Copy Markdown

Benchmark for e5cd2a7

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 37.7±0.98ms 35.8±1.01ms -5.04%
Trie/cita-trie insert 1k 3.7±0.06ms 3.6±0.03ms -2.70%
Trie/ethrex-trie insert 10k 138.2±1.63ms 27.7±2.37s +19943.42%
Trie/ethrex-trie insert 1k 11.4±0.07ms 159.2±2.45ms +1296.49%

@github-actions
Copy link
Copy Markdown

Benchmark for 87221b4

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 36.5±1.49ms 36.2±1.87ms -0.82%
Trie/cita-trie insert 1k 3.7±0.04ms 3.7±0.13ms 0.00%
Trie/ethrex-trie insert 10k 130.7±2.36ms 59.4±1.18ms -54.55%
Trie/ethrex-trie insert 1k 11.1±0.06ms 6.6±0.03ms -40.54%

@github-actions
Copy link
Copy Markdown

Benchmark for 13158c5

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 40.3±2.78ms 34.1±2.33ms -15.38%
Trie/cita-trie insert 1k 3.7±0.05ms 3.7±0.16ms 0.00%
Trie/ethrex-trie insert 10k 136.2±1.94ms 61.5±2.49ms -54.85%
Trie/ethrex-trie insert 1k 11.2±0.15ms 6.7±0.10ms -40.18%

@github-actions
Copy link
Copy Markdown

Benchmark for 37a4d2b

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 34.9±0.34ms 40.3±1.47ms +15.47%
Trie/cita-trie insert 1k 3.6±0.06ms 3.6±0.06ms 0.00%
Trie/ethrex-trie insert 10k 126.3±2.33ms 60.8±1.18ms -51.86%
Trie/ethrex-trie insert 1k 11.1±0.13ms 6.7±0.32ms -39.64%

@github-actions
Copy link
Copy Markdown

Benchmark for 4ff614d

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 38.9±1.87ms 38.8±2.24ms -0.26%
Trie/cita-trie insert 1k 3.7±0.02ms 3.7±0.14ms 0.00%
Trie/ethrex-trie insert 10k 131.6±2.97ms 59.8±0.89ms -54.56%
Trie/ethrex-trie insert 1k 11.2±0.62ms 6.7±0.10ms -40.18%

@github-actions
Copy link
Copy Markdown

Benchmark for 614171d

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 36.4±1.39ms 34.5±1.51ms -5.22%
Trie/cita-trie insert 1k 3.7±0.02ms 3.6±0.08ms -2.70%
Trie/ethrex-trie insert 10k 124.4±2.40ms 56.2±0.38ms -54.82%
Trie/ethrex-trie insert 1k 11.1±0.06ms 6.5±0.03ms -41.44%

@azteca1998 azteca1998 marked this pull request as ready for review May 23, 2025 11:32
@azteca1998 azteca1998 requested a review from a team as a code owner May 23, 2025 11:32
Copy link
Copy Markdown
Collaborator

@Arkenan Arkenan left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! A lot of great changes. Left some comments.

Comment thread crates/common/trie/trie.rs
Comment thread crates/common/trie/trie.rs
Comment thread crates/common/trie/trie.rs Outdated
Comment thread crates/common/trie/trie.rs Outdated
Comment thread crates/common/trie/node.rs
Comment thread crates/common/trie/node.rs
Comment thread crates/common/trie/node/branch.rs Outdated
Comment thread crates/common/trie/node/branch.rs Outdated
Comment thread crates/common/trie/verify_range.rs
Comment thread crates/l2/prover/bench/src/rpc/db.rs Outdated
@github-actions
Copy link
Copy Markdown

Benchmark for c0d893f

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.5±0.56ms 34.7±0.45ms -2.25%
Trie/cita-trie insert 1k 3.7±0.04ms 3.6±0.16ms -2.70%
Trie/ethrex-trie insert 10k 125.6±2.01ms 57.2±1.40ms -54.46%
Trie/ethrex-trie insert 1k 11.1±0.13ms 6.5±0.08ms -41.44%

@github-actions
Copy link
Copy Markdown

Benchmark for ac86e9e

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 36.3±1.64ms 36.2±1.52ms -0.28%
Trie/cita-trie insert 1k 3.6±0.05ms 3.6±0.17ms 0.00%
Trie/ethrex-trie insert 10k 129.3±2.72ms 57.1±0.61ms -55.84%
Trie/ethrex-trie insert 1k 11.0±0.05ms 6.6±0.05ms -40.00%

@github-actions
Copy link
Copy Markdown

Benchmark for 7908baf

Click to view benchmark
Test Base PR %
Trie/cita-trie insert 10k 35.2±0.90ms 36.6±1.68ms +3.98%
Trie/cita-trie insert 1k 3.6±0.03ms 3.7±0.13ms +2.78%
Trie/ethrex-trie insert 10k 134.8±1.70ms 58.6±2.67ms -56.53%
Trie/ethrex-trie insert 1k 11.1±0.22ms 6.6±0.04ms -40.54%

Copy link
Copy Markdown
Collaborator

@Arkenan Arkenan left a comment

Choose a reason for hiding this comment

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

LGTM

Comment thread crates/networking/p2p/sync/state_healing.rs
Comment thread crates/common/trie/node.rs
@azteca1998 azteca1998 added this pull request to the merge queue May 29, 2025
Merged via the queue into main with commit e92653e May 29, 2025
22 checks passed
@azteca1998 azteca1998 deleted the refactor-trie-avoid-compulsive-hashing branch May 29, 2025 13:40
pedrobergamini pushed a commit to pedrobergamini/ethrex that referenced this pull request Aug 24, 2025
…ng) (lambdaclass#2687)

**Motivation**

<!-- Why does this pull request exist? What are its goals? -->

Refactor the Patricia Merkle Trie to avoid rehashing the affected part
of the trie every time it is modified. Delay hashing until its hash is
required.

<!-- A clear and concise general description of the changes this PR
introduces -->

Replaces node hash references by either a `NodeHash` as before (for
unbuffered nodes) or a node itself (for modified nodes).

<!-- Link to issues: Resolves lambdaclass#111, Resolves lambdaclass#222 -->

Closes #issue_number
edg-l added a commit that referenced this pull request Apr 22, 2026
Migrate Amsterdam support from bal@v5.6.1 to bal@v5.7.0 (eels_commit
524b44617e410ab21b5122f0be5113b62a0e76ee, devnets/bal/4).

- EIP-7928 (PR #2730): widen BAL index from u16 to u32 across
  StorageChange/CodeChange/BalanceChange/NonceChange, recorder,
  checkpoint, BalAddressIndex and all helpers.
- EIP-8037 (PR #2687): dynamic cost_per_state_byte derived from
  block gas limit. New VM fields for per-block state-gas costs,
  gated by Fork::Amsterdam. 120M block gas keeps cpsb = 1174.
- EIP-8037 (PRs #2698, #2733): SSTORE 0->N->0 reservoir refill via
  clamp-and-spill; per-frame state_gas_refund absorbed on success,
  discarded on revert; remainder spills to VM-level pending reservoir.
- EIP-7976: calldata floor raised from 10 to 16 gas per token
  (Amsterdam-gated), with unweighted token count per spec.
- EIP-7981: access-list data bytes (20/addr, 32/key) folded into
  floor-token count; applies in both regular and floor arms; mempool
  intrinsic-gas path mirrored.
- EIP-8037 (PR #2689): top-level tx failure zeroes execution
  state_gas_used and refunds the reservoir; intrinsic state gas
  preserved. Routed through refund_sender.
- EIP-7708 (PR #2717): audit confirmed invariants (lex-ordered burn
  logs, no coinbase priority-fee log, SELFDESTRUCT-dest coalescing)
  already held; added regression tests.
- EIP-8037 (PR #2595): audit confirmed code-deposit state-gas
  discard on both oversized-code and deposit-OOG halts; added
  regression tests.

Infra: .fixtures_url_amsterdam, Makefile, hive config and docs
bumped to bal@v5.7.0 / devnets/bal/4.

Tests: 185 levm tests green; new files cover EIP-8037 CPSB, refund
clamp-and-spill, top-level reset, code-deposit discard, and combined
EIP-7976+7981 scenarios.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Block execution throughput and performance in general

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return InconsistentTrie when node should be in db but isn't Trie: hash on commit

5 participants