Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d9dc683

Browse files
gavofyorkshawntabrizipepyakin
authored andcommitted
Don't drop UMP queue items if weight exhausted (#3784)
* Requeue UMP queue items if weight exhausted * Reduce complexity and remove Deque * Formatting * Formatting * Avoid needless storage writes * Test * Formatting * Docs and cleanup * fmt * Remove now irrelevant comment. * Simplify `take_processed` by using `mem::take` * Clean up & fmt: use `upward_message` directly. * Grumbles Co-authored-by: Shawn Tabrizi <[email protected]> Co-authored-by: Sergei Shulepov <[email protected]>
1 parent b1bf4c1 commit d9dc683

File tree

2 files changed

+156
-238
lines changed

2 files changed

+156
-238
lines changed

runtime/parachains/src/mock.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
1919
use crate::{
2020
configuration, disputes, dmp, hrmp, inclusion, initializer, paras, paras_inherent, scheduler,
21-
session_info, shared, ump,
21+
session_info, shared,
22+
ump::{self, MessageId, UmpSink},
23+
ParaId,
2224
};
23-
use frame_support::{parameter_types, traits::GenesisBuild};
25+
use frame_support::{parameter_types, traits::GenesisBuild, weights::Weight};
2426
use frame_support_test::TestRandomness;
27+
use parity_scale_codec::Decode;
2528
use primitives::v1::{
26-
AuthorityDiscoveryId, Balance, BlockNumber, Header, SessionIndex, ValidatorIndex,
29+
AuthorityDiscoveryId, Balance, BlockNumber, Header, SessionIndex, UpwardMessage, ValidatorIndex,
2730
};
2831
use sp_core::H256;
2932
use sp_io::TestExternalities;
@@ -128,7 +131,7 @@ parameter_types! {
128131

129132
impl crate::ump::Config for Test {
130133
type Event = Event;
131-
type UmpSink = crate::ump::mock_sink::MockUmpSink;
134+
type UmpSink = TestUmpSink;
132135
type FirstMessageFactorPercent = FirstMessageFactorPercent;
133136
}
134137

@@ -232,6 +235,41 @@ pub fn availability_rewards() -> HashMap<ValidatorIndex, usize> {
232235
AVAILABILITY_REWARDS.with(|r| r.borrow().clone())
233236
}
234237

238+
std::thread_local! {
239+
static PROCESSED: RefCell<Vec<(ParaId, UpwardMessage)>> = RefCell::new(vec![]);
240+
}
241+
242+
/// Return which messages have been processed by `pocess_upward_message` and clear the buffer.
243+
pub fn take_processed() -> Vec<(ParaId, UpwardMessage)> {
244+
PROCESSED.with(|opt_hook| std::mem::take(&mut *opt_hook.borrow_mut()))
245+
}
246+
247+
/// An implementation of a UMP sink that just records which messages were processed.
248+
///
249+
/// A message's weight is defined by the first 4 bytes of its data, which we decode into a
250+
/// `u32`.
251+
pub struct TestUmpSink;
252+
impl UmpSink for TestUmpSink {
253+
fn process_upward_message(
254+
actual_origin: ParaId,
255+
actual_msg: &[u8],
256+
max_weight: Weight,
257+
) -> Result<Weight, (MessageId, Weight)> {
258+
let weight = match u32::decode(&mut &actual_msg[..]) {
259+
Ok(w) => w as Weight,
260+
Err(_) => return Ok(0), // same as the real `UmpSink`
261+
};
262+
if weight > max_weight {
263+
let id = sp_io::hashing::blake2_256(actual_msg);
264+
return Err((id, weight))
265+
}
266+
PROCESSED.with(|opt_hook| {
267+
opt_hook.borrow_mut().push((actual_origin, actual_msg.to_owned()));
268+
});
269+
Ok(weight)
270+
}
271+
}
272+
235273
pub struct TestRewardValidators;
236274

237275
impl inclusion::RewardValidators for TestRewardValidators {

0 commit comments

Comments
 (0)