Skip to content

Conversation

@niran
Copy link
Contributor

@niran niran commented Oct 28, 2025

Adds state root calculation timing with flashblock state support to the metering RPC to provide visibility into state root computation performance as an independent metric.

TODO

  • Add tests with state changes across blocks and flashblocks based on the upcoming refactored flashblocks-rpc test suite

Key Changes

1. Flashblock State Integration

  • Bundles now execute on top of pending flashblock state (not just canonical block)
  • Accurately accounts for nonces, balances, storage, and code changes from pending transactions
  • Adds state_flashblock_index to response

2. State Root Optimization

  • New FlashblockTrieCache: caches latest flashblock's trie nodes
  • Computes flashblock state root once, reuses for all subsequent bundle metering calls
  • Cache keyed by (block_hash, flashblock_index)
  • Uses TrieInput.prepend_cached() to combine cached flashblock trie with bundle changes
  • Significant performance improvement for repeated metering on same flashblock

3. State Root Metrics

  • Separately tracks state root calculation time
  • New state_root_time_us field in response
  • Flashblock trie computation excluded from timing metrics

4. Flashblocks-RPC Fixes (discovered during implementation)

  • Fixed incorrect database layering: Changed from CacheDB → State to State → CacheDB → StateProviderDatabase
  • Added bundle state tracking: PendingBlocks now tracks accumulated BundleState alongside cache
  • Added state accumulation: Uses with_bundle_prestate() to accumulate state changes across flashblocks
  • These fixes enable accurate state root calculations for metering and potentially benefit other flashblock state consumers

Testing

  • Updated all metering tests for flashblock state parameter
  • 37 tests passing, CI checks passing ✅

Dependencies: Bumped tips-core to 86b275c0fd63226c3fb85ac5512033f99b67d0f5

Base automatically changed from metering-rpc to main October 29, 2025 17:36
niran added 7 commits October 29, 2025 12:47
Add state root calculation after transaction execution and measure its
time independently. The state root calculation time is included in the
total execution time and also tracked separately.
Update tips-core dependency to include state_root_time_us field in
MeterBundleResponse. This exposes the state root calculation time as
an independent metric in the metering RPC response.
Add unit test to verify total_execution_time_us >= state_root_time_us
invariant and RPC integration test to verify state_root_time_us is
properly exposed in the response.
Integrate flashblocks state into metering to execute bundles on top of
pending flashblock state rather than canonical block state. This ensures
metered gas usage and execution time accurately reflect the effects of
pending transactions (nonces, balances, storage, code changes).

Implementation:
- Add flashblocks-rpc dependency to metering crate
- Update meter_bundle() to accept optional db_cache parameter
- Implement three-layer state architecture:
  1. StateProviderDatabase (canonical block base state)
  2. CacheDB (applies flashblock pending changes via cache)
  3. State wrapper (for EVM builder compatibility)
- Update MeteringApiImpl to accept FlashblocksState
- Get pending blocks and db_cache from flashblocks when available
- Fall back to canonical block state when no flashblocks available
- Update response to include flashblock_index in logs
- Require flashblocks to be enabled for metering RPC
- Update all tests to pass FlashblocksState parameter

The metering RPC now uses the same state as flashblocks eth_call,
ensuring consistent simulation results.
Corrects the database architecture to properly layer State -> CacheDB -> StateProviderDatabase,
and ensures both cache and bundle state accumulate across flashblocks for accurate state root
calculations in bundle metering.

Key changes:
- Fix database layering: State now wraps CacheDB instead of CacheDB wrapping State
- Track accumulated bundle state in PendingBlocks alongside cache
- Apply bundle prestate when building state to include previous flashblocks changes
- Use merge_transitions before take_bundle to properly extract accumulated state
niran added 3 commits October 30, 2025 01:05
Adds a single-entry cache for the latest flashblock's trie nodes, allowing
bundle metering operations to reuse the cached trie computation instead of
recalculating from scratch each time.

Key changes:
- Add FlashblockTrieCache: thread-safe single-entry cache for latest flashblock
- Add FlashblockTrieData: contains trie updates and hashed state for reuse
- Cache keyed by block hash and flashblock index for accuracy
- Compute flashblock trie once per flashblock, reuse for all bundle operations
- Extract flashblock trie calculation outside timing metrics
- Use TrieInput.prepend_cached() to combine flashblock + bundle tries

The cache replaces previous entries when a new flashblock is cached, as it's
designed only for the current/latest flashblock, not historical ones.
@niran niran changed the title feat: Add state root calculation time to metering RPC feat: Add state root calculation time from latest flashblock state to metering RPC Oct 30, 2025
@cb-heimdall
Copy link
Collaborator

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
Sum 1

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.

3 participants