use crate::*;
use alloc::vec::Vec;
use cumulus_primitives_core::relay_chain::BlockNumber as RelayBlockNumber;
use frame_support::{pallet_prelude::*, storage_alias, traits::HandleMessage};
pub(crate) const LOG: &str = "runtime::dmp-queue-export-xcms";
#[derive(Copy, Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct PageIndexData {
pub begin_used: PageCounter,
pub end_used: PageCounter,
pub overweight_count: OverweightIndex,
}
pub type OverweightIndex = u64;
pub type PageCounter = u32;
#[storage_alias]
pub type PageIndex<T: Config> = StorageValue<Pallet<T>, PageIndexData, ValueQuery>;
#[storage_alias]
pub type Pages<T: Config> = StorageMap<
Pallet<T>,
Blake2_128Concat,
PageCounter,
Vec<(RelayBlockNumber, Vec<u8>)>,
ValueQuery,
>;
#[storage_alias]
pub type Overweight<T: Config> = CountedStorageMap<
Pallet<T>,
Blake2_128Concat,
OverweightIndex,
(RelayBlockNumber, Vec<u8>),
OptionQuery,
>;
pub(crate) mod testing_only {
use super::*;
#[storage_alias]
pub type Configuration<T: Config> = StorageValue<Pallet<T>, u32>;
}
pub(crate) fn migrate_page<T: crate::Config>(p: PageCounter) -> Result<(), ()> {
let page = Pages::<T>::take(p);
log::debug!(target: LOG, "Migrating page #{p} with {} messages ...", page.len());
if page.is_empty() {
log::error!(target: LOG, "Page #{p}: EMPTY - storage corrupted?");
return Err(())
}
for (m, (block, msg)) in page.iter().enumerate() {
let Ok(bound) = BoundedVec::<u8, _>::try_from(msg.clone()) else {
log::error!(target: LOG, "[Page {p}] Message #{m}: TOO LONG - dropping");
continue
};
T::DmpSink::handle_message(bound.as_bounded_slice());
log::debug!(target: LOG, "[Page {p}] Migrated message #{m} from block {block}");
}
Ok(())
}
pub(crate) fn migrate_overweight<T: crate::Config>(i: OverweightIndex) -> Result<(), ()> {
let Some((block, msg)) = Overweight::<T>::take(i) else {
log::error!(target: LOG, "[Overweight {i}] Message: EMPTY - storage corrupted?");
return Err(())
};
let Ok(bound) = BoundedVec::<u8, _>::try_from(msg) else {
log::error!(target: LOG, "[Overweight {i}] Message: TOO LONG - dropping");
return Err(())
};
T::DmpSink::handle_message(bound.as_bounded_slice());
log::debug!(target: LOG, "[Overweight {i}] Migrated message from block {block}");
Ok(())
}