Skip to content

Commit

Permalink
chainHead: Use IntoIter interface instead of Vec
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Nov 13, 2023
1 parent f2a1cfa commit b552fb9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 6 additions & 4 deletions substrate/client/rpc-spec-v2/src/chain_head/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,14 @@ where
follow_subscription: String,
hash_or_hashes: ListOrValue<Block::Hash>,
) -> RpcResult<()> {
let hashes = match hash_or_hashes {
ListOrValue::Value(hash) => vec![hash],
ListOrValue::List(hashes) => hashes,
let result = match hash_or_hashes {
ListOrValue::Value(hash) =>
self.subscriptions.unpin_blocks(&follow_subscription, [hash]),
ListOrValue::List(hashes) =>
self.subscriptions.unpin_blocks(&follow_subscription, hashes),
};

match self.subscriptions.unpin_blocks(&follow_subscription, hashes) {
match result {
Ok(()) => Ok(()),
Err(SubscriptionManagementError::SubscriptionAbsent) => {
// Invalid invalid subscription ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,16 +753,16 @@ impl<Block: BlockT, BE: Backend<Block>> SubscriptionsInner<Block, BE> {
pub fn unpin_blocks(
&mut self,
sub_id: &str,
hashes: Vec<Block::Hash>,
hashes: impl IntoIterator<Item = Block::Hash> + Clone,
) -> Result<(), SubscriptionManagementError> {
let Some(sub) = self.subs.get_mut(sub_id) else {
return Err(SubscriptionManagementError::SubscriptionAbsent)
};

// Ensure that all blocks are part of the subscription before removing individual
// blocks.
for hash in &hashes {
if !sub.contains_block(*hash) {
for hash in hashes.clone() {
if !sub.contains_block(hash) {
return Err(SubscriptionManagementError::BlockHashAbsent);
}
}
Expand All @@ -771,8 +771,8 @@ impl<Block: BlockT, BE: Backend<Block>> SubscriptionsInner<Block, BE> {
// thinking we borrow `&mut self` twice: once from `self.subs.get_mut` and once from
// `self.global_unregister_block`. Although the borrowing is correct, since different
// fields of the structure are borrowed, one at a time.
for hash in &hashes {
sub.unregister_block(*hash);
for hash in hashes.clone() {
sub.unregister_block(hash);
}

// Block have been removed from the subscription. Remove them from the global tracking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<Block: BlockT, BE: Backend<Block>> SubscriptionManagement<Block, BE> {
pub fn unpin_blocks(
&self,
sub_id: &str,
hashes: Vec<Block::Hash>,
hashes: impl IntoIterator<Item = Block::Hash> + Clone,
) -> Result<(), SubscriptionManagementError> {
let mut inner = self.inner.write();
inner.unpin_blocks(sub_id, hashes)
Expand Down

0 comments on commit b552fb9

Please sign in to comment.