diff --git a/cumulus/pallets/xcmp-queue/src/lib.rs b/cumulus/pallets/xcmp-queue/src/lib.rs index 98fafb8846..e0c5b6e41e 100644 --- a/cumulus/pallets/xcmp-queue/src/lib.rs +++ b/cumulus/pallets/xcmp-queue/src/lib.rs @@ -495,13 +495,15 @@ impl Pallet { } let mut s = >::get(); - let index = s.iter().position(|item| item.recipient == recipient).unwrap_or_else(|| { + let details = if let Some(details) = s.iter_mut().find(|item| item.recipient == recipient) { + details + } else { s.push(OutboundChannelDetails::new(recipient)); - s.len() - 1 - }); - let have_active = s[index].last_index > s[index].first_index; + s.last_mut().expect("can't be empty; a new element was just pushed; qed") + }; + let have_active = details.last_index > details.first_index; let appended = have_active && - >::mutate(recipient, s[index].last_index - 1, |s| { + >::mutate(recipient, details.last_index - 1, |s| { if XcmpMessageFormat::decode_with_depth_limit(MAX_XCM_DECODE_DEPTH, &mut &s[..]) != Ok(format) { @@ -514,15 +516,15 @@ impl Pallet { return true }); if appended { - Ok((s[index].last_index - s[index].first_index - 1) as u32) + Ok((details.last_index - details.first_index - 1) as u32) } else { // Need to add a new page. - let page_index = s[index].last_index; - s[index].last_index += 1; + let page_index = details.last_index; + details.last_index += 1; let mut new_page = format.encode(); new_page.extend_from_slice(&data[..]); >::insert(recipient, page_index, new_page); - let r = (s[index].last_index - s[index].first_index - 1) as u32; + let r = (details.last_index - details.first_index - 1) as u32; >::put(s); Ok(r) } @@ -532,8 +534,8 @@ impl Pallet { /// block. fn send_signal(dest: ParaId, signal: ChannelSignal) -> Result<(), ()> { let mut s = >::get(); - if let Some(index) = s.iter().position(|item| item.recipient == dest) { - s[index].signals_exist = true; + if let Some(details) = s.iter_mut().find(|item| item.recipient == dest) { + details.signals_exist = true; } else { s.push(OutboundChannelDetails::new(dest).with_signals()); } @@ -598,7 +600,7 @@ impl Pallet { Ok(xcm) => { let location = (1, Parachain(sender.into())); match T::XcmExecutor::execute_xcm(location, xcm, max_weight) { - Outcome::Error(e) => (Err(e.clone()), Event::Fail(Some(hash), e)), + Outcome::Error(e) => (Err(e), Event::Fail(Some(hash), e)), Outcome::Complete(w) => (Ok(w), Event::Success(Some(hash))), // As far as the caller is concerned, this was dispatched without error, so // we just report the weight used. @@ -748,7 +750,7 @@ impl Pallet { let suspended = QueueSuspended::::get(); let mut status = >::get(); // <- sorted. - if status.len() == 0 { + if status.is_empty() { return 0 } @@ -857,10 +859,10 @@ impl Pallet { fn suspend_channel(target: ParaId) { >::mutate(|s| { - if let Some(index) = s.iter().position(|item| item.recipient == target) { - let ok = s[index].state == OutboundState::Ok; + if let Some(details) = s.iter_mut().find(|item| item.recipient == target) { + let ok = details.state == OutboundState::Ok; debug_assert!(ok, "WARNING: Attempt to suspend channel that was not Ok."); - s[index].state = OutboundState::Suspended; + details.state = OutboundState::Suspended; } else { s.push(OutboundChannelDetails::new(target).with_suspended_state()); }