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

Add a limit to the maximum number of chainHead_unstable_follow subscriptions #962

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions light-base/src/json_rpc_service/background/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,32 @@ impl<TPlat: PlatformRef> Background<TPlat> {
unreachable!()
};

let (mut subscription, rx) = {
let mut lock = self.chain_head_follow_tasks.lock().await;

// As mentioned in the spec, the JSON-RPC server accepts 2 or more subscriptions per
// JSON-RPC client. We choose to accept only exactly 2 in order to make sure that
// JSON-RPC client implementations are made aware of this limit. This number of 2 might
// be relaxed and/or configurable in the future.
if lock.len() >= 2 {
log::warn!(
target: &self.log_target,
"Rejected `chainHead_unstable_follow` subscription due to limit reached."
);
request.fail(json_rpc::parse::ErrorResponse::ApplicationDefined(
-32100,
"Maximum number of `chainHead_unstable_follow` subscriptions reached",
));
return;
}

let (tx, rx) = service::deliver_channel();
let subscription = request.accept();
lock.insert(subscription.subscription_id().to_owned(), tx);
(subscription, rx)
};
let subscription_id = subscription.subscription_id().to_owned();

let events = if with_runtime {
let subscribe_all = self
.runtime_service
Expand All @@ -106,15 +132,6 @@ impl<TPlat: PlatformRef> Background<TPlat> {
either::Right(self.sync_service.subscribe_all(32, false).await)
};

let (mut subscription, rx) = {
let (tx, rx) = service::deliver_channel();
let mut lock = self.chain_head_follow_tasks.lock().await;
let subscription = request.accept();
lock.insert(subscription.subscription_id().to_owned(), tx);
(subscription, rx)
};
let subscription_id = subscription.subscription_id().to_owned();

let (non_finalized_blocks, pinned_blocks_headers, events) = {
let mut pinned_blocks_headers =
HashMap::with_capacity_and_hasher(0, Default::default());
Expand Down
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- A JSON-RPC error is now returned if the JSON-RPC client tries to open more than two simultaneous `chainHead_unstable_follow` subscriptions, in accordance with the latest changes in the JSON-RPC API specification. ([#962](https://github.com/smol-dot/smoldot/pull/962))

## 1.0.13 - 2023-07-16

### Added
Expand Down