xcmp-queue / less indexing (#1100)

* less indexing

* removed unneeded clone

Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
Squirrel
2022-03-22 20:56:58 +00:00
committed by GitHub
parent c6d910db51
commit 4497c1f686
+18 -16
View File
@@ -495,13 +495,15 @@ impl<T: Config> Pallet<T> {
} }
let mut s = <OutboundXcmpStatus<T>>::get(); let mut s = <OutboundXcmpStatus<T>>::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.push(OutboundChannelDetails::new(recipient));
s.len() - 1 s.last_mut().expect("can't be empty; a new element was just pushed; qed")
}); };
let have_active = s[index].last_index > s[index].first_index; let have_active = details.last_index > details.first_index;
let appended = have_active && let appended = have_active &&
<OutboundXcmpMessages<T>>::mutate(recipient, s[index].last_index - 1, |s| { <OutboundXcmpMessages<T>>::mutate(recipient, details.last_index - 1, |s| {
if XcmpMessageFormat::decode_with_depth_limit(MAX_XCM_DECODE_DEPTH, &mut &s[..]) != if XcmpMessageFormat::decode_with_depth_limit(MAX_XCM_DECODE_DEPTH, &mut &s[..]) !=
Ok(format) Ok(format)
{ {
@@ -514,15 +516,15 @@ impl<T: Config> Pallet<T> {
return true return true
}); });
if appended { 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 { } else {
// Need to add a new page. // Need to add a new page.
let page_index = s[index].last_index; let page_index = details.last_index;
s[index].last_index += 1; details.last_index += 1;
let mut new_page = format.encode(); let mut new_page = format.encode();
new_page.extend_from_slice(&data[..]); new_page.extend_from_slice(&data[..]);
<OutboundXcmpMessages<T>>::insert(recipient, page_index, new_page); <OutboundXcmpMessages<T>>::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;
<OutboundXcmpStatus<T>>::put(s); <OutboundXcmpStatus<T>>::put(s);
Ok(r) Ok(r)
} }
@@ -532,8 +534,8 @@ impl<T: Config> Pallet<T> {
/// block. /// block.
fn send_signal(dest: ParaId, signal: ChannelSignal) -> Result<(), ()> { fn send_signal(dest: ParaId, signal: ChannelSignal) -> Result<(), ()> {
let mut s = <OutboundXcmpStatus<T>>::get(); let mut s = <OutboundXcmpStatus<T>>::get();
if let Some(index) = s.iter().position(|item| item.recipient == dest) { if let Some(details) = s.iter_mut().find(|item| item.recipient == dest) {
s[index].signals_exist = true; details.signals_exist = true;
} else { } else {
s.push(OutboundChannelDetails::new(dest).with_signals()); s.push(OutboundChannelDetails::new(dest).with_signals());
} }
@@ -598,7 +600,7 @@ impl<T: Config> Pallet<T> {
Ok(xcm) => { Ok(xcm) => {
let location = (1, Parachain(sender.into())); let location = (1, Parachain(sender.into()));
match T::XcmExecutor::execute_xcm(location, xcm, max_weight) { 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))), Outcome::Complete(w) => (Ok(w), Event::Success(Some(hash))),
// As far as the caller is concerned, this was dispatched without error, so // As far as the caller is concerned, this was dispatched without error, so
// we just report the weight used. // we just report the weight used.
@@ -748,7 +750,7 @@ impl<T: Config> Pallet<T> {
let suspended = QueueSuspended::<T>::get(); let suspended = QueueSuspended::<T>::get();
let mut status = <InboundXcmpStatus<T>>::get(); // <- sorted. let mut status = <InboundXcmpStatus<T>>::get(); // <- sorted.
if status.len() == 0 { if status.is_empty() {
return 0 return 0
} }
@@ -857,10 +859,10 @@ impl<T: Config> Pallet<T> {
fn suspend_channel(target: ParaId) { fn suspend_channel(target: ParaId) {
<OutboundXcmpStatus<T>>::mutate(|s| { <OutboundXcmpStatus<T>>::mutate(|s| {
if let Some(index) = s.iter().position(|item| item.recipient == target) { if let Some(details) = s.iter_mut().find(|item| item.recipient == target) {
let ok = s[index].state == OutboundState::Ok; let ok = details.state == OutboundState::Ok;
debug_assert!(ok, "WARNING: Attempt to suspend channel that was not Ok."); debug_assert!(ok, "WARNING: Attempt to suspend channel that was not Ok.");
s[index].state = OutboundState::Suspended; details.state = OutboundState::Suspended;
} else { } else {
s.push(OutboundChannelDetails::new(target).with_suspended_state()); s.push(OutboundChannelDetails::new(target).with_suspended_state());
} }