Skip to content

feat(rpc): add shared base-layer blockhash registry#1332

Open
0xzrf wants to merge 2 commits into
magicblock-labs:masterfrom
0xzrf:singleton_bh_updator
Open

feat(rpc): add shared base-layer blockhash registry#1332
0xzrf wants to merge 2 commits into
magicblock-labs:masterfrom
0xzrf:singleton_bh_updator

Conversation

@0xzrf

@0xzrf 0xzrf commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Shared BaseLayerBlockhash registry refreshed every 10s at validator startup; consumers read lock-free instead of calling getLatestBlockhash per transaction. Closes #1258. Ephemeral blockhash path unchanged.

Changes

  • magicblock-rpc-client: BaseLayerBlockhash + background refresher; MagicblockRpcClient::new_with_registry; registry-first get_latest_blockhash() with legacy fallback; invalidate_cached_blockhash() clears registry too.
  • magicblock-api: spawn refresher in try_from_config; pass registry to committor, claim-fees, domain registry.
  • magicblock-committor-service: ChainConfig.blockhash_registry; single new_with_registry call in committor processor.
  • magicblock-validator-admin: claim-fees reads from registry, RPC fallback on miss.

Breaking Changes

  • None (operators)

No config, CLI, disk, or ephemeral RPC changes. Internal API only: claim_fees, ClaimFeesTask::start, DomainRegistryManager constructors/static helpers, and ChainConfig gain an optional BaseLayerBlockhash param (defaults to None in test helpers).

Test Plan

  • [x ] Existing tests pass (committor tests still use registry-less clients)

No new unit tests — wiring only; stale-blockhash retries still go through existing invalidation paths.

Summary by CodeRabbit

  • Performance Improvements
    • Implemented shared blockhash caching with automatic background refresh to reduce RPC load.
    • Transaction construction now prioritizes cached blockhashes, improving speed and reducing network dependency.
    • Validator operations including registration, unregistration, and fee claiming now benefit from optimized blockhash handling.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@0xzrf, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 54 minutes and 6 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0f882487-699d-4c5d-a608-c3804639a569

📥 Commits

Reviewing files that changed from the base of the PR and between 28fc49f and 7e93727.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • magicblock-api/src/domain_registry_manager.rs
  • magicblock-api/src/magic_validator.rs
  • magicblock-committor-service/src/committor_processor.rs
  • magicblock-committor-service/src/config.rs
  • magicblock-rpc-client/Cargo.toml
  • magicblock-rpc-client/src/lib.rs
  • magicblock-validator-admin/src/claim_fees.rs
📝 Walkthrough

Walkthrough

This PR introduces a shared BaseLayerBlockhash registry in magicblock-rpc-client backed by arc_swap for lock-free concurrent access. A background task refreshes the cached blockhash periodically using a CancellationToken. MagicblockRpcClient gains an optional registry field and a new new_with_registry constructor; get_latest_blockhash prefers the registry, and invalidate_cached_blockhash also clears it. The registry is propagated through ChainConfig, CommittorProcessor, DomainRegistryManager, ClaimFeesTask/claim_fees, and MagicValidator, all of which now prefer the cached value over direct RPC calls for latest-blockhash lookups.

Assessment against linked issues

Objective Addressed Explanation
Create a singleton registry that provides a valid blockhash to minimize RPC usage [#1258]
Multiple services consume the registry instead of fetching independently [#1258]
Registry is refreshed by a single background task rather than per-call [#1258]

Suggested reviewers

  • taco-paco
  • thlorenz
  • GabrielePicco
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@magicblock-rpc-client/src/lib.rs`:
- Around line 276-285: The get() method returns any cached blockhash value
without checking its age, causing stale blockhashes to persist even after the
refresher fails. Modify the get() method to validate that the cached blockhash
has not exceeded the base-layer blockhash lifetime before returning it. Compare
the fetched_at timestamp in the CachedBlockhash against the current time, and
return None if the cached value is too old, allowing the fallback path to use
RPC instead.
- Around line 259-260: The struct CachedBlockhash is defined as private but the
public method BaseLayerBlockhash::get() returns this private type, which causes
a visibility mismatch that prevents external crates from using the return value.
Either make CachedBlockhash public to expose the metadata fields to external
consumers, or change the visibility of BaseLayerBlockhash::get() to pub(crate)
if the metadata is intended to be internal-only.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: da8df474-db34-4843-82e6-6313f351c750

📥 Commits

Reviewing files that changed from the base of the PR and between 9fef493 and 28fc49f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • magicblock-api/src/domain_registry_manager.rs
  • magicblock-api/src/magic_validator.rs
  • magicblock-committor-service/src/committor_processor.rs
  • magicblock-committor-service/src/config.rs
  • magicblock-rpc-client/Cargo.toml
  • magicblock-rpc-client/src/lib.rs
  • magicblock-validator-admin/src/claim_fees.rs

Comment thread magicblock-rpc-client/src/lib.rs
Comment thread magicblock-rpc-client/src/lib.rs
@0xzrf 0xzrf force-pushed the singleton_bh_updator branch from 5589c64 to 7e93727 Compare June 19, 2026 04:06
@thlorenz

thlorenz commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@copilot resolve the merge conflicts in this pull request

@thlorenz thlorenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I found one blocking freshness issue: the registry can keep serving an expired blockhash indefinitely if the refresher stops succeeding, which would make every registry-backed transaction fail until the process restarts or a retry path invalidates it.

I also noted one small cleanup for an unused variable.

}

/// Returns the cached base-layer blockhash, if available.
pub fn latest_blockhash(&self) -> Option<Hash> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should apply a freshness check before exposing the registry value.
If refresh_once starts failing after a value has been stored, the old value remains in ArcSwapOption and every caller will keep signing with it forever. That affects both MagicblockRpcClient::get_latest_blockhash() and direct users like claim fees/domain registry, since they treat any registry value as valid and never fall back to RPC.
Once the blockhash ages past Solana's validity window, those transactions can fail continuously until a successful refresh or process restart.

Can we expire the cached value here (or in get()) based on fetched_at, and return None when it is too old so callers fall back to a direct fetch? The TTL should be longer than the 10s refresh interval but comfortably below blockhash expiry.

@@ -71,22 +71,12 @@ impl CommittorProcessor {
);
let rpc_client = Arc::new(rpc_client);
let websocket_uri = chain_config.websocket_uri.clone();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This local is no longer used after the constructor simplification, and cargo check reports it as an unused variable.
Please remove it to keep the crate warning-free.

warning: unused variable: `websocket_uri`
  --> magicblock-committor-service/src/committor_processor.rs:73:13
   |
73 |         let websocket_uri = chain_config.websocket_uri.clone();
   |             ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_websocket_uri`
   |
   = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

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.

feat: base layer blockhash registry

2 participants