Skip to content
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

Implement admin_peerEvents for rpc module #7999

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

StevenChongHuo
Copy link
Contributor

These changes allow real time pub-sub through websocket on peer-related events (peerEvents), specifically, peerAdded and peerRemoved events. Once a client subscribes for peerEvents, A JSON-RPC notification with event payload and subscription id is sent to this client for every event matching the subscription topic (peerAdded or peerRemoved).

Users subscribe to peerEvents by sending a request as such:
{"jsonrpc": "2.0", "id": 1, "method": "admin_subscribe", "params": ["peerEvents"]}

Resolves #7816

Changes

  • Adds a new subscription type peerEvents.
  • Implements logic to create corresponding JsonRpc response.
  • Subscribes to IPeerPool.peerAdded and IPeerPool.peerRemoved events when requested.
  • Allows a differentiation between eth_subscribe and admin_subscribe in JsonRpcSubscriptionResponse class.

Types of changes

What types of changes does your code introduce?

  • Bugfix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a change that causes existing functionality not to work as expected)
  • Optimization
  • Refactoring
  • Documentation update
  • Build-related changes
  • Other

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

StevenChongHuo and others added 2 commits January 3, 2025 09:35
Allow real time pub-sub through websocket on peer_added and peer_removed events.
@StevenChongHuo StevenChongHuo self-assigned this Jan 3, 2025
Copy link
Member

@LukaszRozmej LukaszRozmej left a comment

Choose a reason for hiding this comment

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

Potentially we also need msgsend and msgrecv subscriptions, but they will be a bit harder to add as I believe we don't expose events yet.

Comment on lines 15 to 17
ResultWrapper<string> admin_subscribe(string subscriptionName, string? args = null);
[JsonRpcMethod(Description = "Unsubscribes from a subscription.", IsImplemented = true, IsSharable = false, Availability = RpcEndpoint.All & ~RpcEndpoint.Http)]
ResultWrapper<bool> admin_unsubscribe(string subscriptionId);
Copy link
Member

Choose a reason for hiding this comment

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

I think those methods need to be moved to AdminModule as we need to guarantee that they are available only when admin module is enabled? Or we can try adding that logic explicitly for subscriptions in RpcModuleProvider?

Copy link
Contributor Author

@StevenChongHuo StevenChongHuo Jan 8, 2025

Choose a reason for hiding this comment

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

Thank you for your comments! I am still looking into msgsend and msgrecv subscriptions. For now, these commits only deals with subscriptions to peerAdded and peerRemoved events.

I have moved the admin_subscribe and admin_unsubscribe methods to AdminRpcModule in the most recent commits. Previously, these methods are available when the Subscribe module is enabled; now they are only available when the Admin module is enabled.

Comment on lines +28 to +41
[JsonPropertyName("type")]
public string Type { get; set; }

[JsonPropertyName("peer")]
public string PeerId { get; set; }

[JsonPropertyName("local")]
public string LocalAddr { get; set; }

[JsonPropertyName("remote")]
public string RemoteAddr { get; set; }

[JsonPropertyName("error")]
public string? Err { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Imo name properties as they should be in response and you can drop JsonPropertyName attributes. Casing will be handled by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please explain the benefits of this? I haven't changed these codes for this reason: I think the content of a variable is clearer when the variable name is more explicit, e.g. LocalAddr is less ambiguous than local.

Copy link
Member

Choose a reason for hiding this comment

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

simplicity

+ Move admin_subscribe and admin_unsubsribe methods into the AdminRpcModule.

+ Move relevant tests to AdminModuleTest

+ Deal with other comments in PR review.
Copy link
Member

@LukaszRozmej LukaszRozmej left a comment

Choose a reason for hiding this comment

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

Almost done - what do you think about moving subscription registrations from SubscriptionManager to be close to actual modules themselves?

@@ -38,7 +40,8 @@ public SubscriptionFactory(ILogManager? logManager,
IFilterStore? filterStore,
IEthSyncingInfo ethSyncingInfo,
ISpecProvider specProvider,
IJsonSerializer jsonSerializer)
IJsonSerializer jsonSerializer,
IPeerPool peerPool)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of passing IPeerPool here and other specialized types from different modules, we should register subscriptions from the outside of this class?

Comment on lines +75 to +76
[SubscriptionType.AdminSubscription.PeerEvents] = CreateSubscriptionType((jsonRpcDuplexClient) =>
new PeerEventsSubscription(jsonRpcDuplexClient, logManager, _peerPool)),
Copy link
Member

Choose a reason for hiding this comment

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

I mean this kind of registrations could be done when setting up AdminModule and previous ones when setting up SubscribeModule. Would make this class bit simpler to construct and use. We could use extension methods for those registrations.

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.

Implement admin_peerEvents for rpc module
2 participants