-
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
Conversation
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
…o lexnv/chainhead-connections
WalkthroughThe modifications introduce a mechanism in Substrate's RPC layer to ensure Changes
Assessment against linked issues
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration 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.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files ignored due to path filters (2)
Cargo.lock
is excluded by:!**/*.lock
substrate/client/rpc-servers/Cargo.toml
is excluded by:!**/*.toml
Files selected for processing (3)
- substrate/client/rpc-servers/src/lib.rs (4 hunks)
- substrate/client/rpc-servers/src/middleware/chain_head.rs (1 hunks)
- substrate/client/rpc-servers/src/middleware/mod.rs (1 hunks)
Additional comments: 11
substrate/client/rpc-servers/src/middleware/mod.rs (1)
- 21-21: The addition of the
chain_head
module is correctly implemented and aligns with the PR objectives to introduce middleware forchainHead
operations. This change is straightforward and follows Rust's module declaration syntax and naming conventions.substrate/client/rpc-servers/src/middleware/chain_head.rs (7)
- 37-42: The
ConnectionData
struct is well-defined, serving as a container for activechainHead_follow
subscriptions associated with a connection. This struct is crucial for tracking subscription IDs and ensuring they are exclusive to the connection that generated them, aligning with the PR's objectives.- 44-54: The
ChainHeadLayer
struct and its constructor methodnew
are correctly implemented. This struct plays a key role in enabling thechainHead
RPC methods to be called from a single connection by associating them withConnectionData
. The use ofArc<Mutex<ConnectionData>>
ensures thread-safe access to the connection data.- 65-76: The
ChainHeadMiddleware
struct and its constructor methodnew
are correctly implemented. This middleware intercepts RPC requests to enforce the single-connection policy forchainHead
operations. The structure and logic are consistent with the PR's objectives.- 85-130: The logic within the
call
method ofChainHeadMiddleware
correctly intercepts and validates subscription IDs forchainHead
operations. It ensures that subscription IDs generated bychainHead_follow
are used exclusively within the same connection context. This implementation effectively addresses the PR's security and integrity concerns.- 134-155: The
get_subscription_id
function correctly extracts the subscription ID from both positional and named parameters. This function is essential for the middleware's operation, ensuring that the subscription ID is correctly retrieved from RPC requests for validation.- 157-177: The
get_method_result
function is well-implemented, extracting theresult
field from the JSON-RPC response. This function is crucial for registering new subscription IDs returned by thechainHead_follow
method.- 179-237: The
ResponseFuture
enum and its implementation of theFuture
trait are correctly designed to handle different outcomes of the middleware's operation. This includes immediate error responses, forwarding calls to other layers, and registering new subscription IDs. The use ofpin_project
for handling the futures is appropriate and follows Rust's asynchronous programming patterns.substrate/client/rpc-servers/src/lib.rs (3)
- 58-58: The import of
ChainHeadLayer
andConnectionData
from thechain_head
module is correctly implemented. This import is necessary for the subsequent integration of the middleware into the server startup logic.- 153-154: The creation of the
chain_head_data
instance usingArc<Mutex<ConnectionData>>
is correctly implemented. This instance is essential for managing subscription IDs and their association with specific connections, aligning with the PR's objectives.- 169-170: The integration of the
ChainHeadLayer
into the server configuration through thelayer
method is correctly implemented. This step is crucial for enforcing the single-connection policy forchainHead
operations by utilizing the middleware.
|
||
//! RPC middleware to ensure chainHead methods are called from a single connection. | ||
|
||
use std::{ |
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.
… import message (paritytech#4021) Sometimes you need to debug some issues just by the logs and reconstruct what happened. In these scenarios it would be nice to know if a block was imported as best block, and what it parent was. So here I propose to change the output of the informant to this: ``` 2024-04-05 20:38:22.004 INFO ⋮substrate: [Parachain] ✨ Imported #18 (0xe7b3…4555 -> 0xbd6f…ced7) 2024-04-05 20:38:24.005 INFO ⋮substrate: [Parachain] ✨ Imported #19 (0xbd6f…ced7 -> 0x4dd0…d81f) 2024-04-05 20:38:24.011 INFO ⋮substrate: [jobless-children-5352] 🌟 Imported #42 (0xed2e…27fc -> 0x718f…f30e) 2024-04-05 20:38:26.005 INFO ⋮substrate: [Parachain] ✨ Imported #20 (0x4dd0…d81f -> 0x6e85…e2b8) 2024-04-05 20:38:28.004 INFO ⋮substrate: [Parachain] 🌟 Imported #21 (0x6e85…e2b8 -> 0xad53…2a97) 2024-04-05 20:38:30.004 INFO ⋮substrate: [Parachain] 🌟 Imported #22 (0xad53…2a97 -> 0xa874…890f) ``` --------- Co-authored-by: Bastian Köcher <git@kchr.de>
* Copy node-template over from Substrate repo Got the template at rev=6e6d06c33911 * Use dependencies from crates.io + stop renaming on import * Remove template pallet * Stop using crates.io dependencies Instead they're going to be pinned at v2.0.0-alpha.2 at commit `2afecf81ee19b8a6edb364b419190ea47c4a4a31` until something stable comes along. * Remove LICENSE * Change references of `node-template` to `bridge-node` * Remove README * Fix some missed node-template references * Add WASM toolchain to CI * Be more specific about nightly version to use * Maybe don't tie to a specific nightly * Use composite accounts * Update to use lazy reaping * Only use Development chain config
This PR adds middleware for the JSON rpc servers to ensure that the chainHead methods are called from a single connection.
The middleware intercepts the subscription IDs generated by the
chainHead_follow
.Any further chainHead function call (ie
chainHead_storage
) must provide a subscription ID that was generated by the middleware's connection.A middleware per connection is created in this context.
Closes paritytech#3207
Copied from paritytech#3343
Summary by CodeRabbit
chainHead
method calls, ensuring they are handled from a single connection for better performance and reliability.chainHead
methods.