|
18 | 18 |
|
19 | 19 | use crate::{ |
20 | 20 | 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, |
22 | 24 | }; |
23 | | -use frame_support::{parameter_types, traits::GenesisBuild}; |
| 25 | +use frame_support::{parameter_types, traits::GenesisBuild, weights::Weight}; |
24 | 26 | use frame_support_test::TestRandomness; |
| 27 | +use parity_scale_codec::Decode; |
25 | 28 | use primitives::v1::{ |
26 | | - AuthorityDiscoveryId, Balance, BlockNumber, Header, SessionIndex, ValidatorIndex, |
| 29 | + AuthorityDiscoveryId, Balance, BlockNumber, Header, SessionIndex, UpwardMessage, ValidatorIndex, |
27 | 30 | }; |
28 | 31 | use sp_core::H256; |
29 | 32 | use sp_io::TestExternalities; |
@@ -128,7 +131,7 @@ parameter_types! { |
128 | 131 |
|
129 | 132 | impl crate::ump::Config for Test { |
130 | 133 | type Event = Event; |
131 | | - type UmpSink = crate::ump::mock_sink::MockUmpSink; |
| 134 | + type UmpSink = TestUmpSink; |
132 | 135 | type FirstMessageFactorPercent = FirstMessageFactorPercent; |
133 | 136 | } |
134 | 137 |
|
@@ -232,6 +235,41 @@ pub fn availability_rewards() -> HashMap<ValidatorIndex, usize> { |
232 | 235 | AVAILABILITY_REWARDS.with(|r| r.borrow().clone()) |
233 | 236 | } |
234 | 237 |
|
| 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 | + |
235 | 273 | pub struct TestRewardValidators; |
236 | 274 |
|
237 | 275 | impl inclusion::RewardValidators for TestRewardValidators { |
|
0 commit comments