-
Notifications
You must be signed in to change notification settings - Fork 110
fix(client): accumulate RPC extension hooks instead of replacing #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
niran
wants to merge
1
commit into
main
Choose a base branch
from
fix/rpc-extension-hook-accumulation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //! Wrapper around the OP node builder that accumulates hooks instead of replacing them. | ||
|
|
||
| use eyre::Result; | ||
| use reth_node_builder::{ | ||
| NodeAdapter, NodeComponentsBuilder, | ||
| node::FullNode, | ||
| rpc::{RethRpcAddOns, RpcContext}, | ||
| }; | ||
| use std::fmt; | ||
|
|
||
| use crate::OpBuilder; | ||
| use crate::types::{OpAddOns, OpComponentsBuilder, OpNodeTypes}; | ||
|
|
||
| /// Convenience alias for the OP node adapter type used by the reth builder. | ||
| pub(crate) type OpNodeAdapter = NodeAdapter< | ||
| OpNodeTypes, | ||
| <OpComponentsBuilder as NodeComponentsBuilder<OpNodeTypes>>::Components, | ||
| >; | ||
|
|
||
| /// Convenience alias for the OP Eth API type exposed by the reth RPC add-ons. | ||
| type OpEthApi = <OpAddOns as RethRpcAddOns<OpNodeAdapter>>::EthApi; | ||
|
|
||
| /// Convenience alias for the full OP node handle produced after launch. | ||
| type OpFullNode = FullNode<OpNodeAdapter, OpAddOns>; | ||
|
|
||
| /// Alias for the RPC context used by Base extensions. | ||
| pub type BaseRpcContext<'a> = RpcContext<'a, OpNodeAdapter, OpEthApi>; | ||
|
|
||
| /// Hook type for extending RPC modules. | ||
| type RpcModuleHook = Box<dyn FnMut(&mut BaseRpcContext<'_>) -> Result<()> + Send + 'static>; | ||
|
|
||
| /// Hook type for node-started callbacks. | ||
| type NodeStartedHook = Box<dyn FnMut(OpFullNode) -> Result<()> + Send + 'static>; | ||
|
|
||
| /// A thin wrapper over [`OpBuilder`] that accumulates RPC and node-start hooks. | ||
| pub struct BaseBuilder { | ||
| builder: OpBuilder, | ||
| rpc_hooks: Vec<RpcModuleHook>, | ||
| node_started_hooks: Vec<NodeStartedHook>, | ||
| } | ||
|
|
||
| impl BaseBuilder { | ||
| /// Create a new BaseBuilder wrapping the provided OP builder. | ||
| pub const fn new(builder: OpBuilder) -> Self { | ||
| Self { builder, rpc_hooks: Vec::new(), node_started_hooks: Vec::new() } | ||
| } | ||
|
|
||
| /// Consumes the wrapper and returns the inner builder after installing the accumulated hooks. | ||
| pub fn build(self) -> OpBuilder { | ||
| let BaseBuilder { mut builder, mut rpc_hooks, node_started_hooks } = self; | ||
|
|
||
| if !rpc_hooks.is_empty() { | ||
| builder = builder.extend_rpc_modules(move |mut ctx: BaseRpcContext<'_>| { | ||
| for hook in rpc_hooks.iter_mut() { | ||
| hook(&mut ctx)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| if !node_started_hooks.is_empty() { | ||
| builder = builder.on_node_started(move |full_node: OpFullNode| { | ||
| let mut hooks = node_started_hooks; | ||
| for hook in hooks.iter_mut() { | ||
| hook(full_node.clone())?; | ||
| } | ||
| Ok(()) | ||
| }); | ||
| } | ||
|
|
||
| builder | ||
| } | ||
|
|
||
| /// Adds an RPC hook that will run when RPC modules are configured. | ||
| pub fn add_rpc_module<F>(mut self, hook: F) -> Self | ||
| where | ||
| F: FnOnce(&mut BaseRpcContext<'_>) -> Result<()> + Send + 'static, | ||
| { | ||
| let mut hook = Some(hook); | ||
| self.rpc_hooks.push(Box::new(move |ctx| { | ||
| if let Some(hook) = hook.take() { | ||
| hook(ctx)?; | ||
| } | ||
| Ok(()) | ||
| })); | ||
| self | ||
| } | ||
|
|
||
| /// Adds a node-started hook that will run after the node has started. | ||
| pub fn add_node_started_hook<F>(mut self, hook: F) -> Self | ||
| where | ||
| F: FnOnce(OpFullNode) -> Result<()> + Send + 'static, | ||
| { | ||
| let mut hook = Some(hook); | ||
| self.node_started_hooks.push(Box::new(move |node| { | ||
| if let Some(hook) = hook.take() { | ||
| hook(node)?; | ||
| } | ||
| Ok(()) | ||
| })); | ||
| self | ||
| } | ||
|
|
||
| /// Launches the node after applying accumulated hooks, delegating to the provided closure. | ||
| pub fn launch_with_fn<L, R>(self, launcher: L) -> R | ||
| where | ||
| L: FnOnce(OpBuilder) -> R, | ||
| { | ||
| launcher(self.build()) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Debug for BaseBuilder { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("BaseBuilder").finish_non_exhaustive() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to have client-node depend on base-metering (as it introduces a circular dependency).
This is a very useful test (and one that was missing!), but for now lets just add an issue for adding this. We can either add it to our system test framework or have a top level client client
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sidenote, I wonder where tests like these should live. They're not crossing top-level boundaries (i.e. need both builder + client) - but also are not limited to a single crate.
Do we introduce some tests within the
bin/...crates to have tests that are constrained to only builder or client - but work across multiple sub-packages?Or should they just go alongside the system tests when we do those even though they dont require the builder particularly