Skip to content

Commit

Permalink
Merge pull request #186 from availproject/miguel/support-utility-no-o…
Browse files Browse the repository at this point in the history
…ption

Replace `Option<Vec<Vec<..>>` by `Vec<Vec<..>>`
  • Loading branch information
fmiguelgarcia authored Jul 27, 2023
2 parents 7c86473 + 252f8e4 commit 3c46e47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
5 changes: 2 additions & 3 deletions pallets/dactr/src/extensions/check_app_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ where
while let Some(call) = stack.pop() {
if let Some(DACall::<T>::submit_data { .. }) = call.is_sub_type() {
let next_app_id =
maybe_next_app_id.unwrap_or_else(|| <Pallet<T>>::peek_next_application_id());
maybe_next_app_id.get_or_insert_with(<Pallet<T>>::peek_next_application_id);
ensure!(
self.app_id() < next_app_id,
self.app_id() < *next_app_id,
InvalidTransaction::Custom(InvalidTransactionCustomId::InvalidAppId as u8)
);
maybe_next_app_id = Some(next_app_id);
} else {
match call.is_sub_type() {
Some(UtilityCall::<T>::batch { calls })
Expand Down
24 changes: 12 additions & 12 deletions pallets/system/src/submitted_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,30 @@ impl Extractor for () {
/// It is similar to `Extractor` but it uses `C` type for calls, instead of `AppExtrinsic`.
pub trait Filter<C> {
/// Returns the `data` field of `call` if it is a one or multiple valid `da_ctrl::submit_data` call.
fn filter(call: C, metrics: RcMetrics) -> Option<Vec<Vec<u8>>>;
fn filter(call: C, metrics: RcMetrics) -> Vec<Vec<u8>>;

/// This function processes a list of calls and returns their data as Vec<Vec<u8>>
fn process_calls(calls: Vec<C>, metrics: &RcMetrics) -> Option<Vec<Vec<u8>>>;
fn process_calls(calls: Vec<C>, metrics: &RcMetrics) -> Vec<Vec<u8>>;
}

#[cfg(any(feature = "std", test))]
impl<C> Filter<C> for () {
fn filter(_: C, _: RcMetrics) -> Option<Vec<Vec<u8>>> { None }
fn filter(_: C, _: RcMetrics) -> Vec<Vec<u8>> { vec![] }

fn process_calls(_: Vec<C>, _: &RcMetrics) -> Option<Vec<Vec<u8>>> { None }
fn process_calls(_: Vec<C>, _: &RcMetrics) -> Vec<Vec<u8>> { vec![] }
}

fn extract_and_inspect<E>(opaque: &OpaqueExtrinsic, metrics: RcMetrics) -> Option<Vec<Vec<u8>>>
fn extract_and_inspect<E>(opaque: &OpaqueExtrinsic, metrics: RcMetrics) -> Vec<Vec<u8>>
where
E: Extractor,
E::Error: Debug,
{
E::extract(opaque, metrics)
.inspect_err(|e| log::error!("Extractor cannot decode opaque: {e:?}"))
.ok()
.unwrap_or_default()
.into_iter()
.filter(|data| !data.is_empty())
.collect()
}

/// Construct a root hash of Binary Merkle Tree created from given filtered `app_extrincs`.
Expand All @@ -82,7 +84,7 @@ where
{
let metrics = Metrics::new_shared();
let submitted_data = opaque_itr
.filter_map(|ext| extract_and_inspect::<E>(ext, Rc::clone(&metrics)))
.map(|ext| extract_and_inspect::<E>(ext, Rc::clone(&metrics)))
.flatten();

root(submitted_data, Rc::clone(&metrics))
Expand All @@ -95,9 +97,7 @@ where
I: Iterator<Item = C>,
{
let metrics = Metrics::new_shared();
let submitted_data = calls
.filter_map(|c| F::filter(c, Rc::clone(&metrics)))
.flatten();
let submitted_data = calls.map(|c| F::filter(c, Rc::clone(&metrics))).flatten();
root(submitted_data, Rc::clone(&metrics))
}

Expand Down Expand Up @@ -167,7 +167,7 @@ where
{
let metrics = Metrics::new_shared();
let submitted_data = app_extrinsics
.filter_map(|ext| extract_and_inspect::<E>(ext, Rc::clone(&metrics)))
.map(|ext| extract_and_inspect::<E>(ext, Rc::clone(&metrics)))
.flatten()
.collect::<Vec<_>>();

Expand All @@ -190,7 +190,7 @@ where
{
let metrics = Metrics::new_shared();
let submitted_data = calls
.filter_map(|c| F::filter(c, Rc::clone(&metrics)))
.map(|c| F::filter(c, Rc::clone(&metrics)))
.flatten()
.collect::<Vec<_>>();

Expand Down
34 changes: 13 additions & 21 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,41 +169,34 @@ parameter_types! {

/// Filters and extracts `data` from `call` if it is a `DataAvailability::submit_data` type.
impl submitted_data::Filter<RuntimeCall> for Runtime {
fn filter(call: RuntimeCall, metrics: submitted_data::RcMetrics) -> Option<Vec<Vec<u8>>> {
fn filter(call: RuntimeCall, metrics: submitted_data::RcMetrics) -> Vec<Vec<u8>> {
metrics.borrow_mut().total_extrinsics += 1;

match call {
RuntimeCall::DataAvailability(da_control::Call::submit_data { data }) => {
RuntimeCall::DataAvailability(da_control::Call::submit_data { data })
if !data.is_empty() =>
{
let mut metrics = metrics.borrow_mut();
metrics.data_submit_leaves += 1;
metrics.data_submit_extrinsics += 1;
Some(vec![data.into_inner()])
vec![data.into_inner()]
},
RuntimeCall::Utility(pallet_utility::Call::batch { calls })
| RuntimeCall::Utility(pallet_utility::Call::batch_all { calls })
| RuntimeCall::Utility(pallet_utility::Call::force_batch { calls }) => {
Self::process_calls(calls, &metrics)
},
_ => None,
_ => vec![],
}
}

/// This function processes a list of calls and returns their data as Vec<Vec<u8>>
fn process_calls(
calls: Vec<RuntimeCall>,
metrics: &submitted_data::RcMetrics,
) -> Option<Vec<Vec<u8>>> {
let mut result = Vec::with_capacity(calls.len());
for call in calls {
if let Some(data) = Self::filter(call, Rc::clone(metrics)) {
result.extend(data);
}
}
if !result.is_empty() {
Some(result)
} else {
None
}
fn process_calls(calls: Vec<RuntimeCall>, metrics: &submitted_data::RcMetrics) -> Vec<Vec<u8>> {
calls
.into_iter()
.map(|call| Self::filter(call, Rc::clone(metrics)))
.flatten()
.collect()
}
}

Expand All @@ -217,8 +210,7 @@ impl submitted_data::Extractor for Runtime {
) -> Result<Vec<Vec<u8>>, Self::Error> {
let extrinsic = UncheckedExtrinsic::try_from(opaque)?;
let data =
<Runtime as submitted_data::Filter<RuntimeCall>>::filter(extrinsic.function, metrics)
.unwrap_or_default();
<Runtime as submitted_data::Filter<RuntimeCall>>::filter(extrinsic.function, metrics);

Ok(data)
}
Expand Down

0 comments on commit 3c46e47

Please # to comment.