1use crate::{
24 offchain::{
25 self, storage::InMemOffchainStorage, HttpError, HttpRequestId as RequestId,
26 HttpRequestStatus as RequestStatus, OffchainOverlayedChange, OffchainStorage,
27 OpaqueNetworkState, StorageKind, Timestamp, TransactionPool,
28 },
29 OpaquePeerId,
30};
31use std::{
32 collections::{BTreeMap, VecDeque},
33 sync::Arc,
34};
35
36use parking_lot::RwLock;
37
38#[derive(Debug, Default, PartialEq, Eq)]
40pub struct PendingRequest {
41 pub method: String,
43 pub uri: String,
45 pub meta: Vec<u8>,
47 pub headers: Vec<(String, String)>,
49 pub body: Vec<u8>,
51 pub sent: bool,
53 pub response: Option<Vec<u8>>,
55 pub read: usize,
57 pub response_headers: Vec<(String, String)>,
59}
60
61#[derive(Debug, Clone, Default)]
63pub struct TestPersistentOffchainDB {
64 persistent: Arc<RwLock<InMemOffchainStorage>>,
65}
66
67impl TestPersistentOffchainDB {
68 const PREFIX: &'static [u8] = b"";
69
70 pub fn new() -> Self {
72 Self { persistent: Arc::new(RwLock::new(InMemOffchainStorage::default())) }
73 }
74
75 pub fn apply_offchain_changes(
77 &mut self,
78 changes: impl Iterator<Item = ((Vec<u8>, Vec<u8>), OffchainOverlayedChange)>,
79 ) {
80 let mut me = self.persistent.write();
81 for ((_prefix, key), value_operation) in changes {
82 match value_operation {
83 OffchainOverlayedChange::SetValue(val) =>
84 me.set(Self::PREFIX, key.as_slice(), val.as_slice()),
85 OffchainOverlayedChange::Remove => me.remove(Self::PREFIX, key.as_slice()),
86 }
87 }
88 }
89
90 pub fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
92 OffchainStorage::get(self, Self::PREFIX, key)
93 }
94}
95
96impl OffchainStorage for TestPersistentOffchainDB {
97 fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
98 self.persistent.write().set(prefix, key, value);
99 }
100
101 fn remove(&mut self, prefix: &[u8], key: &[u8]) {
102 self.persistent.write().remove(prefix, key);
103 }
104
105 fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>> {
106 self.persistent.read().get(prefix, key)
107 }
108
109 fn compare_and_set(
110 &mut self,
111 prefix: &[u8],
112 key: &[u8],
113 old_value: Option<&[u8]>,
114 new_value: &[u8],
115 ) -> bool {
116 self.persistent.write().compare_and_set(prefix, key, old_value, new_value)
117 }
118}
119
120#[derive(Debug, Default)]
124pub struct OffchainState {
125 pub requests: BTreeMap<RequestId, PendingRequest>,
127 expected_requests: VecDeque<PendingRequest>,
129 pub persistent_storage: TestPersistentOffchainDB,
131 pub local_storage: InMemOffchainStorage,
133 pub seed: [u8; 32],
135 pub timestamp: Timestamp,
137}
138
139impl OffchainState {
140 pub fn fulfill_pending_request(
142 &mut self,
143 id: u16,
144 expected: PendingRequest,
145 response: impl Into<Vec<u8>>,
146 response_headers: impl IntoIterator<Item = (String, String)>,
147 ) {
148 match self.requests.get_mut(&RequestId(id)) {
149 None => {
150 panic!("Missing pending request: {:?}.\n\nAll: {:?}", id, self.requests);
151 },
152 Some(req) => {
153 assert_eq!(*req, expected);
154 req.response = Some(response.into());
155 req.response_headers = response_headers.into_iter().collect();
156 },
157 }
158 }
159
160 fn fulfill_expected(&mut self, id: u16) {
161 if let Some(mut req) = self.expected_requests.pop_back() {
162 let response = req.response.take().expect("Response checked when added.");
163 let headers = std::mem::take(&mut req.response_headers);
164 self.fulfill_pending_request(id, req, response, headers);
165 }
166 }
167
168 pub fn expect_request(&mut self, expected: PendingRequest) {
176 if expected.response.is_none() {
177 panic!("Expected request needs to have a response.");
178 }
179 self.expected_requests.push_front(expected);
180 }
181}
182
183impl Drop for OffchainState {
184 fn drop(&mut self) {
185 if !self.expected_requests.is_empty() && !std::thread::panicking() {
187 panic!("Unfulfilled expected requests: {:?}", self.expected_requests);
188 }
189 }
190}
191
192#[derive(Clone, Default, Debug)]
194pub struct TestOffchainExt(pub Arc<RwLock<OffchainState>>);
195
196impl TestOffchainExt {
197 pub fn new() -> (Self, Arc<RwLock<OffchainState>>) {
199 let ext = Self::default();
200 let state = ext.0.clone();
201 (ext, state)
202 }
203
204 pub fn with_offchain_db(
206 offchain_db: TestPersistentOffchainDB,
207 ) -> (Self, Arc<RwLock<OffchainState>>) {
208 let (ext, state) = Self::new();
209 ext.0.write().persistent_storage = offchain_db;
210 (ext, state)
211 }
212}
213
214impl offchain::Externalities for TestOffchainExt {
215 fn is_validator(&self) -> bool {
216 true
217 }
218
219 fn network_state(&self) -> Result<OpaqueNetworkState, ()> {
220 Ok(OpaqueNetworkState { peer_id: Default::default(), external_addresses: vec![] })
221 }
222
223 fn timestamp(&mut self) -> Timestamp {
224 self.0.read().timestamp
225 }
226
227 fn sleep_until(&mut self, deadline: Timestamp) {
228 self.0.write().timestamp = deadline;
229 }
230
231 fn random_seed(&mut self) -> [u8; 32] {
232 self.0.read().seed
233 }
234
235 fn http_request_start(
236 &mut self,
237 method: &str,
238 uri: &str,
239 meta: &[u8],
240 ) -> Result<RequestId, ()> {
241 let mut state = self.0.write();
242 let id = RequestId(state.requests.len() as u16);
243 state.requests.insert(
244 id,
245 PendingRequest {
246 method: method.into(),
247 uri: uri.into(),
248 meta: meta.into(),
249 ..Default::default()
250 },
251 );
252 Ok(id)
253 }
254
255 fn http_request_add_header(
256 &mut self,
257 request_id: RequestId,
258 name: &str,
259 value: &str,
260 ) -> Result<(), ()> {
261 let mut state = self.0.write();
262 if let Some(req) = state.requests.get_mut(&request_id) {
263 req.headers.push((name.into(), value.into()));
264 Ok(())
265 } else {
266 Err(())
267 }
268 }
269
270 fn http_request_write_body(
271 &mut self,
272 request_id: RequestId,
273 chunk: &[u8],
274 _deadline: Option<Timestamp>,
275 ) -> Result<(), HttpError> {
276 let mut state = self.0.write();
277
278 let sent = {
279 let req = state.requests.get_mut(&request_id).ok_or(HttpError::IoError)?;
280 req.body.extend(chunk);
281 if chunk.is_empty() {
282 req.sent = true;
283 }
284 req.sent
285 };
286
287 if sent {
288 state.fulfill_expected(request_id.0);
289 }
290
291 Ok(())
292 }
293
294 fn http_response_wait(
295 &mut self,
296 ids: &[RequestId],
297 _deadline: Option<Timestamp>,
298 ) -> Vec<RequestStatus> {
299 let state = self.0.read();
300
301 ids.iter()
302 .map(|id| match state.requests.get(id) {
303 Some(req) if req.response.is_none() => {
304 panic!("No `response` provided for request with id: {:?}", id)
305 },
306 None => RequestStatus::Invalid,
307 _ => RequestStatus::Finished(200),
308 })
309 .collect()
310 }
311
312 fn http_response_headers(&mut self, request_id: RequestId) -> Vec<(Vec<u8>, Vec<u8>)> {
313 let state = self.0.read();
314 if let Some(req) = state.requests.get(&request_id) {
315 req.response_headers
316 .clone()
317 .into_iter()
318 .map(|(k, v)| (k.into_bytes(), v.into_bytes()))
319 .collect()
320 } else {
321 Default::default()
322 }
323 }
324
325 fn http_response_read_body(
326 &mut self,
327 request_id: RequestId,
328 buffer: &mut [u8],
329 _deadline: Option<Timestamp>,
330 ) -> Result<usize, HttpError> {
331 let mut state = self.0.write();
332 if let Some(req) = state.requests.get_mut(&request_id) {
333 let response = req
334 .response
335 .as_mut()
336 .unwrap_or_else(|| panic!("No response provided for request: {:?}", request_id));
337
338 if req.read >= response.len() {
339 state.requests.remove(&request_id);
341 Ok(0)
342 } else {
343 let read = std::cmp::min(buffer.len(), response[req.read..].len());
344 buffer[0..read].copy_from_slice(&response[req.read..req.read + read]);
345 req.read += read;
346 Ok(read)
347 }
348 } else {
349 Err(HttpError::IoError)
350 }
351 }
352
353 fn set_authorized_nodes(&mut self, _nodes: Vec<OpaquePeerId>, _authorized_only: bool) {
354 unimplemented!()
355 }
356}
357
358impl offchain::DbExternalities for TestOffchainExt {
359 fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
360 let mut state = self.0.write();
361 match kind {
362 StorageKind::LOCAL => state.local_storage.set(b"", key, value),
363 StorageKind::PERSISTENT => state.persistent_storage.set(b"", key, value),
364 };
365 }
366
367 fn local_storage_clear(&mut self, kind: StorageKind, key: &[u8]) {
368 let mut state = self.0.write();
369 match kind {
370 StorageKind::LOCAL => state.local_storage.remove(b"", key),
371 StorageKind::PERSISTENT => state.persistent_storage.remove(b"", key),
372 };
373 }
374
375 fn local_storage_compare_and_set(
376 &mut self,
377 kind: StorageKind,
378 key: &[u8],
379 old_value: Option<&[u8]>,
380 new_value: &[u8],
381 ) -> bool {
382 let mut state = self.0.write();
383 match kind {
384 StorageKind::LOCAL =>
385 state.local_storage.compare_and_set(b"", key, old_value, new_value),
386 StorageKind::PERSISTENT =>
387 state.persistent_storage.compare_and_set(b"", key, old_value, new_value),
388 }
389 }
390
391 fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
392 let state = self.0.read();
393 match kind {
394 StorageKind::LOCAL => state.local_storage.get(TestPersistentOffchainDB::PREFIX, key),
395 StorageKind::PERSISTENT => state.persistent_storage.get(key),
396 }
397 }
398}
399
400#[derive(Default)]
402pub struct PoolState {
403 pub transactions: Vec<Vec<u8>>,
405}
406
407#[derive(Default)]
417pub struct TestTransactionPoolExt(Arc<RwLock<PoolState>>);
418
419impl TestTransactionPoolExt {
420 pub fn new() -> (Self, Arc<RwLock<PoolState>>) {
422 let ext = Self::default();
423 let state = ext.0.clone();
424 (ext, state)
425 }
426}
427
428impl TransactionPool for TestTransactionPoolExt {
429 fn submit_transaction(&mut self, extrinsic: Vec<u8>) -> Result<(), ()> {
430 self.0.write().transactions.push(extrinsic);
431 Ok(())
432 }
433}