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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::{ParachainInherentData, INHERENT_IDENTIFIER};
use codec::Decode;
use cumulus_primitives_core::{
relay_chain, InboundDownwardMessage, InboundHrmpMessage, ParaId, PersistedValidationData,
};
use sc_client_api::{Backend, StorageProvider};
use sp_api::BlockId;
use sp_core::twox_128;
use sp_inherents::{InherentData, InherentDataProvider};
use sp_runtime::traits::Block;
use std::collections::BTreeMap;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
pub struct MockValidationDataInherentDataProvider {
pub current_para_block: u32,
pub relay_offset: u32,
pub relay_blocks_per_para_block: u32,
pub xcm_config: MockXcmConfig,
pub raw_downward_messages: Vec<Vec<u8>>,
pub raw_horizontal_messages: Vec<(ParaId, Vec<u8>)>,
}
#[derive(Default)]
pub struct MockXcmConfig {
pub para_id: ParaId,
pub starting_dmq_mqc_head: relay_chain::Hash,
pub starting_hrmp_mqc_heads: BTreeMap<ParaId, relay_chain::Hash>,
}
pub struct ParachainSystemName(pub Vec<u8>);
impl Default for ParachainSystemName {
fn default() -> Self {
Self(b"ParachainSystem".to_vec())
}
}
impl MockXcmConfig {
pub fn new<B: Block, BE: Backend<B>, C: StorageProvider<B, BE>>(
client: &C,
parent_block: B::Hash,
para_id: ParaId,
parachain_system_name: ParachainSystemName,
) -> Self {
let starting_dmq_mqc_head = client
.storage(
&BlockId::Hash(parent_block),
&sp_storage::StorageKey(
[twox_128(¶chain_system_name.0), twox_128(b"LastDmqMqcHead")]
.concat()
.to_vec(),
),
)
.expect("We should be able to read storage from the parent block.")
.map(|ref mut raw_data| {
Decode::decode(&mut &raw_data.0[..]).expect("Stored data should decode correctly")
})
.unwrap_or_default();
let starting_hrmp_mqc_heads = client
.storage(
&BlockId::Hash(parent_block),
&sp_storage::StorageKey(
[twox_128(¶chain_system_name.0), twox_128(b"LastHrmpMqcHeads")]
.concat()
.to_vec(),
),
)
.expect("We should be able to read storage from the parent block.")
.map(|ref mut raw_data| {
Decode::decode(&mut &raw_data.0[..]).expect("Stored data should decode correctly")
})
.unwrap_or_default();
Self { para_id, starting_dmq_mqc_head, starting_hrmp_mqc_heads }
}
}
#[async_trait::async_trait]
impl InherentDataProvider for MockValidationDataInherentDataProvider {
fn provide_inherent_data(
&self,
inherent_data: &mut InherentData,
) -> Result<(), sp_inherents::Error> {
let relay_parent_number =
self.relay_offset + self.relay_blocks_per_para_block * self.current_para_block;
let mut sproof_builder = RelayStateSproofBuilder::default();
sproof_builder.para_id = self.xcm_config.para_id;
let mut downward_messages = Vec::new();
let mut dmq_mqc = crate::MessageQueueChain(self.xcm_config.starting_dmq_mqc_head);
for msg in &self.raw_downward_messages {
let wrapped = InboundDownwardMessage { sent_at: relay_parent_number, msg: msg.clone() };
dmq_mqc.extend_downward(&wrapped);
downward_messages.push(wrapped);
}
sproof_builder.dmq_mqc_head = Some(dmq_mqc.head());
let mut horizontal_messages = BTreeMap::<ParaId, Vec<InboundHrmpMessage>>::new();
for (para_id, msg) in &self.raw_horizontal_messages {
let wrapped = InboundHrmpMessage { sent_at: relay_parent_number, data: msg.clone() };
horizontal_messages.entry(*para_id).or_default().push(wrapped);
}
for (para_id, messages) in &horizontal_messages {
let mut channel_mqc = crate::MessageQueueChain(
*self
.xcm_config
.starting_hrmp_mqc_heads
.get(para_id)
.unwrap_or(&relay_chain::Hash::default()),
);
for message in messages {
channel_mqc.extend_hrmp(message);
}
sproof_builder.upsert_inbound_channel(*para_id).mqc_head = Some(channel_mqc.head());
}
let (relay_parent_storage_root, proof) = sproof_builder.into_state_root_and_proof();
inherent_data.put_data(
INHERENT_IDENTIFIER,
&ParachainInherentData {
validation_data: PersistedValidationData {
parent_head: Default::default(),
relay_parent_storage_root,
relay_parent_number,
max_pov_size: Default::default(),
},
downward_messages,
horizontal_messages,
relay_chain_state: proof,
},
)
}
async fn try_handle_error(
&self,
_: &sp_inherents::InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
None
}
}