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 support for multiple hashes in chainHead_unstable_unpin #814

Merged
merged 3 commits into from
Jun 25, 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
9 changes: 8 additions & 1 deletion lib/src/json_rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ define_methods! {
) -> (),
chainHead_unstable_unpin(
#[rename = "followSubscription"] follow_subscription: Cow<'a, str>,
hash: HashHexString
hash: HashHexStringSingleOrArray
) -> (),

chainSpec_unstable_chainName() -> Cow<'a, str>,
Expand Down Expand Up @@ -600,6 +600,13 @@ impl<'a> serde::Deserialize<'a> for HashHexString {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum HashHexStringSingleOrArray {
Single(HashHexString),
Array(Vec<HashHexString>),
}

/// Removes the length prefix at the beginning of `metadata`. Used for the `Metadata_metadata`
/// JSON-RPC request. Returns an error if there is no valid length prefix.
pub fn remove_metadata_length_prefix(
Expand Down
25 changes: 17 additions & 8 deletions light-base/src/json_rpc_service/background/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,23 +704,32 @@ impl<TPlat: PlatformRef> ChainHeadFollowTask<TPlat> {
follow_subscription: _,
hash,
} => {
let valid = {
if self.pinned_blocks_headers.remove(&hash.0).is_some() {
let all_hashes = match &hash {
methods::HashHexStringSingleOrArray::Single(hash) => {
either::Left(iter::once(&hash.0))
}
methods::HashHexStringSingleOrArray::Array(hashes) => {
either::Right(hashes.iter().map(|h| &h.0))
}
};

let is_valid = all_hashes
.clone()
.all(|hash| self.pinned_blocks_headers.contains_key(hash));

if is_valid {
for hash in all_hashes {
self.pinned_blocks_headers.remove(hash);
if let Subscription::WithRuntime {
subscription_id, ..
} = self.subscription
{
self.runtime_service
.unpin_block(subscription_id, &hash.0)
.unpin_block(subscription_id, hash)
.await;
}
true
} else {
false
}
};

if valid {
request.respond(methods::Response::chainHead_unstable_unpin(()));
} else {
request.fail(json_rpc::parse::ErrorResponse::InvalidParams);
Expand Down
1 change: 1 addition & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed

- The `chainHead_unstable_unpin` JSON-RPC function now accepts either a single hash or an array of hashes, in accordance with the latest changes in the JSON-RPC API specification. ([#814](https://github.com/smol-dot/smoldot/pull/814))
- Add support for the `descendants-values`, `descendants-hashes`, and `closest-ancestor-merkle-value` types for the `chainHead_unstable_storage` JSON-RPC function. ([#813](https://github.com/smol-dot/smoldot/pull/813))
- The `chainHead_unstable_storage` JSON-RPC function now accepts an array of `items` as parameter instead of a `key` and `type`, in accordance with the latest changes in the JSON-RPC API specification. ([#813](https://github.com/smol-dot/smoldot/pull/813))
- The `chainHead_unstable_storage` JSON-RPC function now generates `items` notifications containin an array of multiple `items`, in accordance with the latest changes in the JSON-RPC API specification. ([#813](https://github.com/smol-dot/smoldot/pull/813))
Expand Down