forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
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
rpc-servers: Allow chainHead methods to be called from a single connection #22
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5989756
rpc-servers: Add chainHead middleware to capture subscription IDs
lexnv 21615a8
rpc-servers: Capture the method result as subscription ID
lexnv 0a5e2dc
rpc-servers: Cleanup the ResponseFuture
lexnv 5b6063f
rpc-servers: Use parkinglot instead of std mutex
lexnv cb7b67a
Update substrate/client/rpc-servers/src/middleware/chain_head.rs
lexnv 9ae9e9b
rpc-servers: Add parking lot dependency
lexnv a1a6a4b
Merge remote-tracking branch 'origin/lexnv/chainhead-connections' int…
lexnv 8e2b268
Merge remote-tracking branch 'origin/master' into lexnv/chainhead-con…
lexnv 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 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 |
---|---|---|
|
@@ -27,3 +27,4 @@ http = "0.2.8" | |
hyper = "0.14.27" | ||
futures = "0.3.29" | ||
pin-project = "1.1.3" | ||
parking_lot = "0.12.1" |
This file contains 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
237 changes: 237 additions & 0 deletions
237
substrate/client/rpc-servers/src/middleware/chain_head.rs
This file contains 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,237 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! RPC middleware to ensure chainHead methods are called from a single connection. | ||
|
||
use std::{ | ||
collections::HashSet, | ||
future::Future, | ||
pin::Pin, | ||
sync::Arc, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use jsonrpsee::{ | ||
server::middleware::rpc::RpcServiceT, | ||
types::{Params, Request}, | ||
MethodResponse, | ||
}; | ||
use parking_lot::Mutex; | ||
use pin_project::pin_project; | ||
|
||
/// The per connectin data needed to manage chainHead subscriptions. | ||
#[derive(Default)] | ||
pub struct ConnectionData { | ||
/// Active `chainHead_follow` subscriptions for this connection. | ||
subscriptions: HashSet<String>, | ||
} | ||
|
||
/// Layer to allow the `chainHead` RPC methods to be called from a single connection. | ||
#[derive(Clone)] | ||
pub struct ChainHeadLayer { | ||
connection_data: Arc<Mutex<ConnectionData>>, | ||
} | ||
|
||
impl ChainHeadLayer { | ||
/// Create a new [`ChainHeadLayer`]. | ||
pub fn new(connection_data: Arc<Mutex<ConnectionData>>) -> Self { | ||
Self { connection_data } | ||
} | ||
} | ||
|
||
impl<S> tower::Layer<S> for ChainHeadLayer { | ||
type Service = ChainHeadMiddleware<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
ChainHeadMiddleware::new(inner, self.connection_data.clone()) | ||
} | ||
} | ||
|
||
/// Chain head middleware. | ||
#[derive(Clone)] | ||
pub struct ChainHeadMiddleware<S> { | ||
service: S, | ||
connection_data: Arc<Mutex<ConnectionData>>, | ||
} | ||
|
||
impl<S> ChainHeadMiddleware<S> { | ||
/// Create a new chain head middleware. | ||
pub fn new(service: S, connection_data: Arc<Mutex<ConnectionData>>) -> ChainHeadMiddleware<S> { | ||
ChainHeadMiddleware { service, connection_data } | ||
} | ||
} | ||
|
||
impl<'a, S> RpcServiceT<'a> for ChainHeadMiddleware<S> | ||
where | ||
S: Send + Sync + RpcServiceT<'a>, | ||
{ | ||
type Future = ResponseFuture<S::Future>; | ||
|
||
fn call(&self, req: Request<'a>) -> Self::Future { | ||
const CHAIN_HEAD_FOLLOW: &str = "chainHead_unstable_follow"; | ||
const CHAIN_HEAD_CALL_METHODS: [&str; 8] = [ | ||
"chainHead_unstable_body", | ||
"chainHead_unstable_header", | ||
"chainHead_unstable_call", | ||
"chainHead_unstable_unpin", | ||
"chainHead_unstable_continue", | ||
"chainHead_unstable_storage", | ||
"chainHead_unstable_stopOperation", | ||
"chainHead_unstable_unfollow", | ||
]; | ||
|
||
let method_name = req.method_name(); | ||
|
||
// Intercept the subscription ID returned by the `chainHead_follow` method. | ||
if method_name == CHAIN_HEAD_FOLLOW { | ||
return ResponseFuture::Register { | ||
fut: self.service.call(req.clone()), | ||
connection_data: self.connection_data.clone(), | ||
} | ||
} | ||
|
||
// Ensure the subscription ID of those methods corresponds to a subscription ID | ||
// of this connection. | ||
if CHAIN_HEAD_CALL_METHODS.contains(&method_name) { | ||
let params = req.params(); | ||
let follow_subscription = get_subscription_id(params); | ||
|
||
if let Some(follow_subscription) = follow_subscription { | ||
if !self.connection_data.lock().subscriptions.contains(&follow_subscription) { | ||
return ResponseFuture::Ready { | ||
response: Some(MethodResponse::error( | ||
req.id(), | ||
jsonrpsee::types::error::ErrorObject::owned( | ||
-32602, | ||
"Invalid subscription ID", | ||
None::<()>, | ||
), | ||
)), | ||
}; | ||
} | ||
} | ||
} | ||
|
||
ResponseFuture::Forward { fut: self.service.call(req.clone()) } | ||
} | ||
} | ||
|
||
/// Extract the subscription ID from the provided parameters. | ||
/// | ||
/// We make the assumption that all `chainHead` methods are given the | ||
/// subscription ID as a first parameter. | ||
/// | ||
/// This method handles positional and named `camelCase` parameters. | ||
fn get_subscription_id<'a>(params: Params<'a>) -> Option<String> { | ||
// Support positional parameters. | ||
if let Ok(follow_subscription) = params.sequence().next::<String>() { | ||
return Some(follow_subscription) | ||
} | ||
|
||
// Support named parameters. | ||
let Ok(value) = params.parse::<serde_json::Value>() else { return None }; | ||
|
||
let serde_json::Value::Object(map) = value else { return None }; | ||
if let Some(serde_json::Value::String(subscription_id)) = map.get("followSubscription") { | ||
return Some(subscription_id.clone()) | ||
} | ||
|
||
None | ||
} | ||
|
||
/// Extract the result of a jsonrpc object. | ||
/// | ||
/// The function extracts the `result` field from the JSON-RPC response. | ||
/// | ||
/// In this example, the result is `tfMQUZekzJLorGlR`. | ||
/// ```ignore | ||
/// "{"jsonrpc":"2.0","result":"tfMQUZekzJLorGlR","id":0}" | ||
/// ``` | ||
fn get_method_result(response: &MethodResponse) -> Option<String> { | ||
if response.is_error() { | ||
return None | ||
} | ||
|
||
let result = response.as_result(); | ||
let Ok(value) = serde_json::from_str(result) else { return None }; | ||
|
||
let serde_json::Value::Object(map) = value else { return None }; | ||
let Some(serde_json::Value::String(res)) = map.get("result") else { return None }; | ||
|
||
Some(res.clone()) | ||
} | ||
|
||
/// Response future for chainHead middleware. | ||
#[pin_project(project = ResponseFutureProj)] | ||
pub enum ResponseFuture<F> { | ||
/// The response is propagated immediately without calling other layers. | ||
/// | ||
/// This is used in case of an error. | ||
Ready { | ||
/// The response provided to the client directly. | ||
/// | ||
/// This is `Option` to consume the value and return a `MethodResponse` | ||
/// from the projected structure. | ||
response: Option<MethodResponse>, | ||
}, | ||
|
||
/// Forward the call to another layer. | ||
Forward { | ||
/// The future response value. | ||
#[pin] | ||
fut: F, | ||
}, | ||
|
||
/// Forward the call to another layer and store the subscription ID of the response. | ||
Register { | ||
/// The future response value. | ||
#[pin] | ||
fut: F, | ||
/// Connection data that captures the subscription ID. | ||
connection_data: Arc<Mutex<ConnectionData>>, | ||
}, | ||
} | ||
|
||
impl<'a, F> std::fmt::Debug for ResponseFuture<F> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_str("ResponseFuture") | ||
} | ||
} | ||
|
||
impl<F: Future<Output = MethodResponse>> Future for ResponseFuture<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
|
||
match this { | ||
ResponseFutureProj::Ready { response } => | ||
Poll::Ready(response.take().expect("Value is set; qed")), | ||
ResponseFutureProj::Forward { fut } => fut.poll(cx), | ||
ResponseFutureProj::Register { fut, connection_data } => { | ||
let res = fut.poll(cx); | ||
if let Poll::Ready(response) = &res { | ||
if let Some(subscription_id) = get_method_result(response) { | ||
connection_data.lock().subscriptions.insert(subscription_id); | ||
} | ||
} | ||
res | ||
}, | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
//! JSON-RPC specific middleware. | ||
|
||
pub mod chain_head; | ||
pub mod metrics; | ||
|
||
pub use metrics::*; |
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.
@coderabbitai generate unit tests for this file.
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.
@Bullrich, based on the objectives and summary of the PR, here's a general approach to generating unit tests for the
chain_head.rs
file:ConnectionData Tests: Create tests to ensure that the
ConnectionData
struct correctly manages subscription IDs, associating them with unique connections. This could involve testing the addition, retrieval, and deletion of subscription IDs from theConnectionData
instance.ChainHeadLayer and ChainHeadMiddleware Tests: Develop tests to verify that these components intercept RPC requests as intended. This includes ensuring that they correctly identify
chainHead
method calls and apply the single-connection policy by validating subscription IDs against the stored connection data.Subscription ID Validation Tests: Implement tests to simulate scenarios where valid and invalid subscription IDs are used in
chainHead
operations. This should cover cases where subscription IDs are attempted to be used across multiple connections, ensuring such attempts are blocked as per the new policy.Mocking and Integration Tests: Utilize mocking frameworks to simulate the RPC server and connections. This will allow you to test the middleware's integration with the server and its behavior in a controlled environment, ensuring it operates as expected under various scenarios.
This outline should serve as a starting point for developing comprehensive unit tests for the new middleware functionality. It's important to adapt and extend these tests based on the specific logic and requirements of your implementation.