1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::*;
use frame_support::traits::Defensive;
pub trait IntoWeight {
fn into_weight(self) -> Weight;
}
impl IntoWeight for u64 {
fn into_weight(self) -> Weight {
Weight::from_parts(self, self)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen, TypeInfo, Debug)]
pub enum MessageOrigin {
Here,
There,
Everywhere(u32),
}
impl From<u32> for MessageOrigin {
fn from(i: u32) -> Self {
Self::Everywhere(i)
}
}
pub struct NoopMessageProcessor;
impl ProcessMessage for NoopMessageProcessor {
type Origin = MessageOrigin;
fn process_message(
_message: &[u8],
_origin: Self::Origin,
weight_limit: Weight,
) -> Result<(bool, Weight), ProcessMessageError> {
let weight = Weight::from_parts(1, 1);
if weight.all_lte(weight_limit) {
Ok((true, weight))
} else {
Err(ProcessMessageError::Overweight(weight))
}
}
}
pub fn msg<N: Get<u32>>(x: &'static str) -> BoundedSlice<u8, N> {
BoundedSlice::defensive_truncate_from(x.as_bytes())
}
pub fn vmsg(x: &'static str) -> Vec<u8> {
x.as_bytes().to_vec()
}
pub fn page<T: Config>(msg: &[u8]) -> PageOf<T> {
PageOf::<T>::from_message::<T>(msg.try_into().unwrap())
}
pub fn single_page_book<T: Config>() -> BookStateOf<T> {
BookState { begin: 0, end: 1, count: 1, ready_neighbours: None, message_count: 0, size: 0 }
}
pub fn empty_book<T: Config>() -> BookStateOf<T> {
BookState { begin: 0, end: 1, count: 1, ready_neighbours: None, message_count: 0, size: 0 }
}
pub fn full_page<T: Config>() -> (PageOf<T>, usize) {
let mut msgs = 0;
let mut page = PageOf::<T>::default();
for i in 0..u32::MAX {
let r = i.using_encoded(|d| page.try_append_message::<T>(d.try_into().unwrap()));
if r.is_err() {
break
} else {
msgs += 1;
}
}
assert!(msgs > 0, "page must hold at least one message");
(page, msgs)
}
pub fn book_for<T: Config>(page: &PageOf<T>) -> BookStateOf<T> {
BookState {
count: 1,
begin: 0,
end: 1,
ready_neighbours: None,
message_count: page.remaining.into() as u64,
size: page.remaining_size.into() as u64,
}
}
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
assert!(
!frame_system::Pallet::<T>::block_number().is_zero(),
"The genesis block has n o events"
);
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}
pub fn setup_bump_service_head<T: Config>(
current: <<T as Config>::MessageProcessor as ProcessMessage>::Origin,
next: <<T as Config>::MessageProcessor as ProcessMessage>::Origin,
) {
let mut book = single_page_book::<T>();
book.ready_neighbours = Some(Neighbours::<MessageOriginOf<T>> { prev: next.clone(), next });
ServiceHead::<T>::put(¤t);
BookStateFor::<T>::insert(¤t, &book);
}
pub fn knit<T: Config>(o: &<<T as Config>::MessageProcessor as ProcessMessage>::Origin) {
let mut b = BookStateFor::<T>::get(o);
b.ready_neighbours = crate::Pallet::<T>::ready_ring_knit(o).ok().defensive();
BookStateFor::<T>::insert(o, b);
}
pub fn unknit<T: Config>(o: &<<T as Config>::MessageProcessor as ProcessMessage>::Origin) {
let mut b = BookStateFor::<T>::get(o);
crate::Pallet::<T>::ready_ring_unknit(o, b.ready_neighbours.unwrap());
b.ready_neighbours = None;
BookStateFor::<T>::insert(o, b);
}
pub fn build_ring<T: Config>(
queues: &[<<T as Config>::MessageProcessor as ProcessMessage>::Origin],
) {
for queue in queues {
BookStateFor::<T>::insert(queue, empty_book::<T>());
}
for queue in queues {
knit::<T>(queue);
}
assert_ring::<T>(queues);
}
pub fn assert_ring<T: Config>(
queues: &[<<T as Config>::MessageProcessor as ProcessMessage>::Origin],
) {
for (i, origin) in queues.iter().enumerate() {
let book = BookStateFor::<T>::get(origin);
assert_eq!(
book.ready_neighbours,
Some(Neighbours {
prev: queues[(i + queues.len() - 1) % queues.len()].clone(),
next: queues[(i + 1) % queues.len()].clone(),
})
);
}
assert_eq!(ServiceHead::<T>::get(), queues.first().cloned());
}