-
Notifications
You must be signed in to change notification settings - Fork 7
/
impls.rs
460 lines (372 loc) · 13.2 KB
/
impls.rs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use std::{fmt::Write, str::Split};
use stellar_relay_lib::sdk::{
compound_types::{UnlimitedVarArray, XdrArchive},
types::{ScpEnvelope, ScpHistoryEntry, TransactionHistoryEntry, TransactionSet},
XdrCodec,
};
use crate::oracle::{
constants::MAX_ITEMS_IN_QUEUE, storage::traits::*, EnvelopesFileHandler, EnvelopesMap, Error,
Filename, SerializedData, Slot, SlotEncodedMap, TxSetMap, TxSetsFileHandler,
};
use super::{ScpArchiveStorage, TransactionsArchiveStorage};
impl FileHandler<EnvelopesMap> for EnvelopesFileHandler {
#[cfg(test)]
const PATH: &'static str = "./resources/test/scp_envelopes";
#[cfg(not(test))]
const PATH: &'static str = "./scp_envelopes";
fn deserialize_bytes(bytes: Vec<u8>) -> Result<EnvelopesMap, Error> {
let inside: SlotEncodedMap = bincode::deserialize(&bytes)?;
let mut m: EnvelopesMap = EnvelopesMap::new();
for (key, value) in inside.into_iter() {
if let Ok(envelopes) = UnlimitedVarArray::<ScpEnvelope>::from_xdr(value) {
m.push_back((key, envelopes.get_vec().to_vec()));
if m.len() > MAX_ITEMS_IN_QUEUE {
m.pop_front();
}
}
}
Ok(m)
}
fn check_slot_in_splitted_filename(slot_param: Slot, splits: &mut Split<char>) -> bool {
fn parse_slot(slot_opt: Option<&str>) -> Option<Slot> {
(slot_opt?).parse::<Slot>().ok()
}
if let Some(start_slot) = parse_slot(splits.next()) {
if let Some(end_slot) = parse_slot(splits.next()) {
return (slot_param >= start_slot) && (slot_param <= end_slot)
}
}
false
}
}
impl FileHandlerExt<EnvelopesMap> for EnvelopesFileHandler {
fn create_filename_and_data(data: &EnvelopesMap) -> Result<(Filename, SerializedData), Error> {
let mut filename: Filename = "".to_string();
let mut m: SlotEncodedMap = SlotEncodedMap::new();
let len = data.len();
for (idx, (key, value)) in data.iter().enumerate() {
if idx == 0 {
let _ = write!(filename, "{}_", key);
}
if idx == (len - 1) {
let _ = write!(filename, "{}", key);
}
let stellar_array = UnlimitedVarArray::new(value.clone())?;
m.insert(*key, stellar_array.to_xdr());
}
let res = bincode::serialize(&m)?;
Ok((filename, res))
}
}
impl FileHandler<TxSetMap> for TxSetsFileHandler {
#[cfg(test)]
const PATH: &'static str = "./resources/test/tx_sets";
#[cfg(not(test))]
const PATH: &'static str = "./tx_sets";
fn deserialize_bytes(bytes: Vec<u8>) -> Result<TxSetMap, Error> {
let inside: SlotEncodedMap = bincode::deserialize(&bytes)?;
let mut m: TxSetMap = TxSetMap::new();
for (key, value) in inside.into_iter() {
if let Ok(set) = TransactionSet::from_xdr(value) {
m.push_back((key, set));
if m.len() > MAX_ITEMS_IN_QUEUE {
m.pop_front();
}
}
}
Ok(m)
}
fn check_slot_in_splitted_filename(slot_param: Slot, splits: &mut Split<char>) -> bool {
EnvelopesFileHandler::check_slot_in_splitted_filename(slot_param, splits)
}
}
impl FileHandlerExt<TxSetMap> for TxSetsFileHandler {
fn create_filename_and_data(data: &TxSetMap) -> Result<(Filename, SerializedData), Error> {
let mut filename: Filename = "".to_string();
let mut m: SlotEncodedMap = SlotEncodedMap::new();
let len = data.len();
for (idx, (key, set)) in data.iter().enumerate() {
if idx == 0 {
let _ = write!(filename, "{}_", key);
}
if idx == (len - 1) {
let _ = write!(filename, "{}", key);
}
m.insert(*key, set.to_xdr());
}
Ok((filename, bincode::serialize(&m)?))
}
}
impl ArchiveStorage for ScpArchiveStorage {
type T = ScpHistoryEntry;
const STELLAR_HISTORY_BASE_URL: &'static str =
crate::oracle::constants::STELLAR_HISTORY_BASE_URL;
const PREFIX_URL: &'static str = "scp";
const PREFIX_FILENAME: &'static str = "";
}
impl ScpArchiveStorage {
pub async fn get_scp_archive(
slot_index: u32,
) -> Result<XdrArchive<<Self as ArchiveStorage>::T>, Error> {
let (url, file_name) = Self::get_url_and_file_name(slot_index);
//try to find xdr.gz file and decode. if error then download archive from horizon archive
// node and save
let mut result = Self::try_gz_decode_archive_file(&file_name);
if result.is_err() {
download_file_and_save(&url, &file_name).await?;
result = Self::try_gz_decode_archive_file(&file_name);
}
let data = result.unwrap();
Ok(Self::decode_xdr(data))
}
}
impl ArchiveStorage for TransactionsArchiveStorage {
type T = TransactionHistoryEntry;
const STELLAR_HISTORY_BASE_URL: &'static str =
crate::oracle::constants::STELLAR_HISTORY_BASE_URL_TRANSACTIONS;
const PREFIX_URL: &'static str = "transactions";
const PREFIX_FILENAME: &'static str = "txs-";
}
impl TransactionsArchiveStorage {
pub async fn get_transactions_archive(
slot_index: u32,
) -> Result<XdrArchive<<Self as ArchiveStorage>::T>, Error> {
let (url, file_name) = Self::get_url_and_file_name(slot_index);
//try to find xdr.gz file and decode. if error then download archive from horizon archive
// node and save
let mut result = Self::try_gz_decode_archive_file(&file_name);
if result.is_err() {
download_file_and_save(&url, &file_name).await?;
result = Self::try_gz_decode_archive_file(&file_name);
}
let data = result.unwrap();
Ok(Self::decode_xdr(data))
}
}
#[cfg(test)]
mod test {
use std::{
convert::{TryFrom, TryInto},
fs,
fs::File,
io::Read,
path::PathBuf,
};
use mockall::lazy_static;
use stellar_relay_lib::sdk::types::ScpHistoryEntry;
use crate::oracle::{
constants::MAX_SLOTS_PER_FILE,
errors::Error,
impls::ArchiveStorage,
storage::{
traits::{FileHandler, FileHandlerExt},
EnvelopesFileHandler, TxSetsFileHandler,
},
types::{LifoMap, Slot},
};
use super::ScpArchiveStorage;
lazy_static! {
static ref M_SLOTS_FILE: Slot =
Slot::try_from(MAX_SLOTS_PER_FILE - 1).expect("should convert just fine");
}
#[test]
fn find_file_by_slot_success() {
// ---------------- TESTS FOR ENVELOPES -----------
// finding first slot
{
let slot = 573112;
let expected_name = format!("{}_{}", slot, slot + *M_SLOTS_FILE);
let file_name =
EnvelopesFileHandler::find_file_by_slot(slot).expect("should return a file");
assert_eq!(&file_name, &expected_name);
}
// finding slot in the middle of the file
{
let first_slot = 573312;
let expected_name = format!("{}_{}", first_slot, first_slot + *M_SLOTS_FILE);
let slot = first_slot + 5;
let file_name =
EnvelopesFileHandler::find_file_by_slot(slot).expect("should return a file");
assert_eq!(&file_name, &expected_name);
}
// finding slot at the end of the file
{
let slot = 578490;
let expected_name = format!("{}_{}", slot - *M_SLOTS_FILE, slot);
let file_name =
EnvelopesFileHandler::find_file_by_slot(slot).expect("should return a file");
assert_eq!(&file_name, &expected_name);
}
// ---------------- TESTS FOR TX SETS -----------
// finding first slot
{
let slot = 42867088;
let expected_name = format!("{}_42867102", slot);
let file_name =
TxSetsFileHandler::find_file_by_slot(slot).expect("should return a file");
assert_eq!(&file_name, &expected_name);
}
// finding slot in the middle of the file
{
let first_slot = 42867103;
let expected_name = format!("{}_42867118", first_slot);
let slot = first_slot + 10;
let file_name =
TxSetsFileHandler::find_file_by_slot(slot).expect("should return a file");
assert_eq!(&file_name, &expected_name);
}
// finding slot at the end of the file
{
let slot = 42867134;
let expected_name = format!("42867119_{}", slot);
let file_name =
TxSetsFileHandler::find_file_by_slot(slot).expect("should return a file");
assert_eq!(&file_name, &expected_name);
}
}
#[test]
fn get_map_from_archives_success() {
// ---------------- TESTS FOR ENVELOPE -----------
{
let first_slot = 578291;
let last_slot = first_slot + *M_SLOTS_FILE;
let envelopes_map = EnvelopesFileHandler::get_map_from_archives(last_slot - 20)
.expect("should return envelopes map");
for (idx, (slot, _envs)) in envelopes_map.iter().enumerate() {
let expected_slot_num =
first_slot + u64::try_from(idx).expect("should return u64 data type");
assert_eq!(slot, &expected_slot_num);
}
let scp_envelopes =
envelopes_map.get_with_key(&last_slot).expect("should have scp envelopes");
for x in scp_envelopes {
assert_eq!(x.statement.slot_index, last_slot);
}
}
// ---------------- TEST FOR TXSETs -----------
{
let first_slot = 42867119;
let find_slot = first_slot + 15;
let txsets_map = TxSetsFileHandler::get_map_from_archives(find_slot)
.expect("should return txsets map");
assert!(txsets_map.get_with_key(&find_slot).is_some());
}
}
#[test]
fn get_map_from_archives_fail() {
// ---------------- TESTS FOR ENVELOPE -----------
{
let slot = 578491;
match EnvelopesFileHandler::get_map_from_archives(slot).expect_err("This should fail") {
Error::Other(err_str) => {
assert_eq!(err_str, format!("Cannot find file for slot {}", slot))
},
_ => assert!(false, "should fail"),
}
}
// ---------------- TEST FOR TXSETs -----------
{
let slot = 42867087;
match TxSetsFileHandler::get_map_from_archives(slot).expect_err("This should fail") {
Error::Other(err_str) => {
assert_eq!(err_str, format!("Cannot find file for slot {}", slot))
},
_ => assert!(false, "should fail"),
}
}
}
#[test]
fn write_to_file_success() {
// ---------------- TESTS FOR ENVELOPE -----------
{
let first_slot = 42867088;
let last_slot = 42867102;
let mut path = PathBuf::new();
path.push("./resources/test/scp_envelopes_for_testing");
path.push(&format!("{}_{}", first_slot, last_slot));
let mut file = File::open(path).expect("file should exist");
let mut bytes: Vec<u8> = vec![];
let _ = file.read_to_end(&mut bytes).expect("should be able to read until the end");
let mut env_map =
EnvelopesFileHandler::deserialize_bytes(bytes).expect("should generate a map");
// let's remove the first_slot and last_slot in the map, so we can create a new file.
env_map.remove_with_key(&first_slot);
env_map.remove_with_key(&last_slot);
let expected_filename = format!("{}_{}", first_slot + 1, last_slot - 1);
let actual_filename = EnvelopesFileHandler::write_to_file(&env_map)
.expect("should write to scp_envelopes directory");
assert_eq!(actual_filename, expected_filename);
let new_file = EnvelopesFileHandler::find_file_by_slot(first_slot + 2)
.expect("should return the same file");
assert_eq!(new_file, expected_filename);
// let's delete it
let path = EnvelopesFileHandler::get_path(&new_file);
fs::remove_file(path).expect("should be able to remove the newly added file.");
}
// ---------------- TEST FOR TXSETs -----------
{
let first_slot = 42867151;
let last_slot = 42867166;
let mut path = PathBuf::new();
path.push("./resources/test/tx_sets_for_testing");
path.push(&format!("{}_{}", first_slot, last_slot));
let mut file = File::open(path).expect("file should exist");
let mut bytes: Vec<u8> = vec![];
let _ = file.read_to_end(&mut bytes).expect("should be able to read until the end");
let mut txset_map =
TxSetsFileHandler::deserialize_bytes(bytes).expect("should generate a map");
// let's remove the first_slot and last_slot in the map, so we can create a new file.
txset_map.remove_with_key(&first_slot);
txset_map.remove_with_key(&last_slot);
let expected_filename = format!("{}_{}", first_slot + 1, last_slot - 1);
let actual_filename = TxSetsFileHandler::write_to_file(&txset_map)
.expect("should write to scp_envelopes directory");
assert_eq!(actual_filename, expected_filename);
let new_file = TxSetsFileHandler::find_file_by_slot(last_slot - 2)
.expect("should return the same file");
assert_eq!(new_file, expected_filename);
// let's delete it
let path = TxSetsFileHandler::get_path(&new_file);
fs::remove_file(path).expect("should be able to remove the newly added file.");
}
}
#[tokio::test]
async fn get_scp_archive_works() {
let slot_index = 30511500;
let scp_archive = ScpArchiveStorage::get_scp_archive(slot_index)
.await
.expect("should find the archive");
let slot_index_u32: u32 = slot_index.try_into().unwrap();
scp_archive
.get_vec()
.iter()
.find(|&scp_entry| {
if let ScpHistoryEntry::V0(scp_entry_v0) = scp_entry {
scp_entry_v0.ledger_messages.ledger_seq == slot_index_u32
} else {
false
}
})
.expect("slot index should be in archive");
let (_, file) = <ScpArchiveStorage as ArchiveStorage>::get_url_and_file_name(slot_index);
fs::remove_file(file).expect("should be able to remove the newly added file.");
}
#[tokio::test]
async fn get_transactions_archive_works() {
use super::TransactionsArchiveStorage;
//arrange
let slot_index = 30511500;
let (_url, ref filename) = TransactionsArchiveStorage::get_url_and_file_name(slot_index);
//act
let _transactions_archive =
TransactionsArchiveStorage::get_transactions_archive(slot_index)
.await
.expect("should find the archive");
//assert
TransactionsArchiveStorage::read_file_xdr(filename)
.expect("File with transactions should exists");
let (_, file) =
<TransactionsArchiveStorage as ArchiveStorage>::get_url_and_file_name(slot_index);
fs::remove_file(file).expect("should be able to remove the newly added file.");
}
}