gemachain_runtime/
append_vec.rs

1//! Persistent storage for accounts.
2//!
3//! For more information, see:
4//!
5//! <https://docs.gemachain.com/implemented-proposals/persistent-account-storage>
6
7use log::*;
8use memmap2::MmapMut;
9use serde::{Deserialize, Serialize};
10use gemachain_sdk::{
11    account::{Account, AccountSharedData, ReadableAccount},
12    clock::{Epoch, Slot},
13    hash::Hash,
14    pubkey::Pubkey,
15};
16use std::{
17    borrow::Borrow,
18    fs::{remove_file, OpenOptions},
19    io,
20    io::{Seek, SeekFrom, Write},
21    mem,
22    path::{Path, PathBuf},
23    sync::atomic::{AtomicUsize, Ordering},
24    sync::Mutex,
25};
26
27// Data placement should be aligned at the next boundary. Without alignment accessing the memory may
28// crash on some architectures.
29const ALIGN_BOUNDARY_OFFSET: usize = mem::size_of::<u64>();
30macro_rules! u64_align {
31    ($addr: expr) => {
32        ($addr + (ALIGN_BOUNDARY_OFFSET - 1)) & !(ALIGN_BOUNDARY_OFFSET - 1)
33    };
34}
35
36const MAXIMUM_APPEND_VEC_FILE_SIZE: usize = 16 * 1024 * 1024 * 1024; // 16 GiB
37
38pub type StoredMetaWriteVersion = u64;
39
40/// Meta contains enough context to recover the index from storage itself
41/// This struct will be backed by mmaped and snapshotted data files.
42/// So the data layout must be stable and consistent across the entire cluster!
43#[derive(Clone, PartialEq, Debug)]
44pub struct StoredMeta {
45    /// global write version
46    pub write_version: StoredMetaWriteVersion,
47    /// key for the account
48    pub pubkey: Pubkey,
49    pub data_len: u64,
50}
51
52/// This struct will be backed by mmaped and snapshotted data files.
53/// So the data layout must be stable and consistent across the entire cluster!
54#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq)]
55pub struct AccountMeta {
56    /// carats in the account
57    pub carats: u64,
58    /// the program that owns this account. If executable, the program that loads this account.
59    pub owner: Pubkey,
60    /// this account's data contains a loaded program (and is now read-only)
61    pub executable: bool,
62    /// the epoch at which this account will next owe rent
63    pub rent_epoch: Epoch,
64}
65
66impl<'a, T: ReadableAccount> From<&'a T> for AccountMeta {
67    fn from(account: &'a T) -> Self {
68        Self {
69            carats: account.carats(),
70            owner: *account.owner(),
71            executable: account.executable(),
72            rent_epoch: account.rent_epoch(),
73        }
74    }
75}
76
77impl<'a, T: ReadableAccount> From<Option<&'a T>> for AccountMeta {
78    fn from(account: Option<&'a T>) -> Self {
79        match account {
80            Some(account) => AccountMeta::from(account),
81            None => AccountMeta::default(),
82        }
83    }
84}
85
86/// References to account data stored elsewhere. Getting an `Account` requires cloning
87/// (see `StoredAccountMeta::clone_account()`).
88#[derive(PartialEq, Debug)]
89pub struct StoredAccountMeta<'a> {
90    pub meta: &'a StoredMeta,
91    /// account data
92    pub account_meta: &'a AccountMeta,
93    pub data: &'a [u8],
94    pub offset: usize,
95    pub stored_size: usize,
96    pub hash: &'a Hash,
97}
98
99impl<'a> StoredAccountMeta<'a> {
100    /// Return a new Account by copying all the data referenced by the `StoredAccountMeta`.
101    pub fn clone_account(&self) -> AccountSharedData {
102        AccountSharedData::from(Account {
103            carats: self.account_meta.carats,
104            owner: self.account_meta.owner,
105            executable: self.account_meta.executable,
106            rent_epoch: self.account_meta.rent_epoch,
107            data: self.data.to_vec(),
108        })
109    }
110
111    fn sanitize(&self) -> bool {
112        self.sanitize_executable() && self.sanitize_carats()
113    }
114
115    fn sanitize_executable(&self) -> bool {
116        // Sanitize executable to ensure higher 7-bits are cleared correctly.
117        self.ref_executable_byte() & !1 == 0
118    }
119
120    fn sanitize_carats(&self) -> bool {
121        // Sanitize 0 carats to ensure to be same as AccountSharedData::default()
122        self.account_meta.carats != 0 || self.clone_account() == AccountSharedData::default()
123    }
124
125    fn ref_executable_byte(&self) -> &u8 {
126        // Use extra references to avoid value silently clamped to 1 (=true) and 0 (=false)
127        // Yes, this really happens; see test_new_from_file_crafted_executable
128        let executable_bool: &bool = &self.account_meta.executable;
129        // UNSAFE: Force to interpret mmap-backed bool as u8 to really read the actual memory content
130        let executable_byte: &u8 = unsafe { &*(executable_bool as *const bool as *const u8) };
131        executable_byte
132    }
133}
134
135/// A thread-safe, file-backed block of memory used to store `Account` instances. Append operations
136/// are serialized such that only one thread updates the internal `append_lock` at a time. No
137/// restrictions are placed on reading. That is, one may read items from one thread while another
138/// is appending new items.
139#[derive(Debug, AbiExample)]
140pub struct AppendVec {
141    /// The file path where the data is stored.
142    path: PathBuf,
143
144    /// A file-backed block of memory that is used to store the data for each appended item.
145    map: MmapMut,
146
147    /// A lock used to serialize append operations.
148    append_lock: Mutex<()>,
149
150    /// The number of bytes used to store items, not the number of items.
151    current_len: AtomicUsize,
152
153    /// The number of bytes available for storing items.
154    file_size: u64,
155
156    /// True if the file should automatically be deleted when this AppendVec is dropped.
157    remove_on_drop: bool,
158}
159
160impl Drop for AppendVec {
161    fn drop(&mut self) {
162        if self.remove_on_drop {
163            if let Err(_e) = remove_file(&self.path) {
164                // promote this to panic soon.
165                // disabled due to many false positive warnings while running tests.
166                // blocked by rpc's upgrade to jsonrpc v17
167                //error!("AppendVec failed to remove {:?}: {:?}", &self.path, e);
168            }
169        }
170    }
171}
172
173impl AppendVec {
174    pub fn new(file: &Path, create: bool, size: usize) -> Self {
175        let initial_len = 0;
176        AppendVec::sanitize_len_and_size(initial_len, size).unwrap();
177
178        if create {
179            let _ignored = remove_file(file);
180        }
181
182        let mut data = OpenOptions::new()
183            .read(true)
184            .write(true)
185            .create(create)
186            .open(file)
187            .map_err(|e| {
188                panic!(
189                    "Unable to {} data file {} in current dir({:?}): {:?}",
190                    if create { "create" } else { "open" },
191                    file.display(),
192                    std::env::current_dir(),
193                    e
194                );
195            })
196            .unwrap();
197
198        // Theoretical performance optimization: write a zero to the end of
199        // the file so that we won't have to resize it later, which may be
200        // expensive.
201        data.seek(SeekFrom::Start((size - 1) as u64)).unwrap();
202        data.write_all(&[0]).unwrap();
203        data.seek(SeekFrom::Start(0)).unwrap();
204        data.flush().unwrap();
205
206        //UNSAFE: Required to create a Mmap
207        let map = unsafe { MmapMut::map_mut(&data) };
208        let map = map.unwrap_or_else(|e| {
209            error!(
210                "Failed to map the data file (size: {}): {}.\n
211                    Please increase sysctl vm.max_map_count or equivalent for your platform.",
212                size, e
213            );
214            std::process::exit(1);
215        });
216
217        AppendVec {
218            path: file.to_path_buf(),
219            map,
220            // This mutex forces append to be single threaded, but concurrent with reads
221            // See UNSAFE usage in `append_ptr`
222            append_lock: Mutex::new(()),
223            current_len: AtomicUsize::new(initial_len),
224            file_size: size as u64,
225            remove_on_drop: true,
226        }
227    }
228
229    pub fn set_no_remove_on_drop(&mut self) {
230        self.remove_on_drop = false;
231    }
232
233    pub fn new_empty_map(current_len: usize) -> Self {
234        let map = MmapMut::map_anon(1).unwrap_or_else(|e| {
235            error!(
236                "Failed to create VM map for snapshot. {:?}\n
237                        Please increase sysctl vm.max_map_count or equivalent for your platform.",
238                e
239            );
240            std::process::exit(1);
241        });
242
243        AppendVec {
244            path: PathBuf::from(String::default()),
245            map,
246            append_lock: Mutex::new(()),
247            current_len: AtomicUsize::new(current_len),
248            file_size: 0, // will be filled by set_file()
249            remove_on_drop: true,
250        }
251    }
252
253    fn sanitize_len_and_size(current_len: usize, file_size: usize) -> io::Result<()> {
254        if file_size == 0 {
255            Err(std::io::Error::new(
256                std::io::ErrorKind::Other,
257                format!("too small file size {} for AppendVec", file_size),
258            ))
259        } else if file_size > MAXIMUM_APPEND_VEC_FILE_SIZE {
260            Err(std::io::Error::new(
261                std::io::ErrorKind::Other,
262                format!("too large file size {} for AppendVec", file_size),
263            ))
264        } else if current_len > file_size {
265            Err(std::io::Error::new(
266                std::io::ErrorKind::Other,
267                format!("current_len is larger than file size ({})", file_size),
268            ))
269        } else {
270            Ok(())
271        }
272    }
273
274    pub fn flush(&self) -> io::Result<()> {
275        self.map.flush()
276    }
277
278    pub fn reset(&self) {
279        // This mutex forces append to be single threaded, but concurrent with reads
280        // See UNSAFE usage in `append_ptr`
281        let _lock = self.append_lock.lock().unwrap();
282        self.current_len.store(0, Ordering::Relaxed);
283    }
284
285    pub fn len(&self) -> usize {
286        self.current_len.load(Ordering::Relaxed)
287    }
288
289    pub fn is_empty(&self) -> bool {
290        self.len() == 0
291    }
292
293    pub fn capacity(&self) -> u64 {
294        self.file_size
295    }
296
297    pub fn file_name(slot: Slot, id: usize) -> String {
298        format!("{}.{}", slot, id)
299    }
300
301    pub fn new_from_file<P: AsRef<Path>>(path: P, current_len: usize) -> io::Result<(Self, usize)> {
302        let data = OpenOptions::new()
303            .read(true)
304            .write(true)
305            .create(false)
306            .open(&path)?;
307
308        let file_size = std::fs::metadata(&path)?.len();
309        AppendVec::sanitize_len_and_size(current_len, file_size as usize)?;
310
311        let map = unsafe {
312            let result = MmapMut::map_mut(&data);
313            if result.is_err() {
314                // for vm.max_map_count, error is: {code: 12, kind: Other, message: "Cannot allocate memory"}
315                info!("memory map error: {:?}. This may be because vm.max_map_count is not set correctly.", result);
316            }
317            result?
318        };
319
320        let new = AppendVec {
321            path: path.as_ref().to_path_buf(),
322            map,
323            append_lock: Mutex::new(()),
324            current_len: AtomicUsize::new(current_len),
325            file_size,
326            remove_on_drop: true,
327        };
328
329        let (sanitized, num_accounts) = new.sanitize_layout_and_length();
330        if !sanitized {
331            return Err(std::io::Error::new(
332                std::io::ErrorKind::Other,
333                "incorrect layout/length/data",
334            ));
335        }
336
337        Ok((new, num_accounts))
338    }
339
340    fn sanitize_layout_and_length(&self) -> (bool, usize) {
341        let mut offset = 0;
342
343        // This discards allocated accounts immediately after check at each loop iteration.
344        //
345        // This code should not reuse AppendVec.accounts() method as the current form or
346        // extend it to be reused here because it would allow attackers to accumulate
347        // some measurable amount of memory needlessly.
348        let mut num_accounts = 0;
349        while let Some((account, next_offset)) = self.get_account(offset) {
350            if !account.sanitize() {
351                return (false, num_accounts);
352            }
353            offset = next_offset;
354            num_accounts += 1;
355        }
356        let aligned_current_len = u64_align!(self.current_len.load(Ordering::Relaxed));
357
358        (offset == aligned_current_len, num_accounts)
359    }
360
361    /// Get a reference to the data at `offset` of `size` bytes if that slice
362    /// doesn't overrun the internal buffer. Otherwise return None.
363    /// Also return the offset of the first byte after the requested data that
364    /// falls on a 64-byte boundary.
365    fn get_slice(&self, offset: usize, size: usize) -> Option<(&[u8], usize)> {
366        let (next, overflow) = offset.overflowing_add(size);
367        if overflow || next > self.len() {
368            return None;
369        }
370        let data = &self.map[offset..next];
371        let next = u64_align!(next);
372
373        Some((
374            //UNSAFE: This unsafe creates a slice that represents a chunk of self.map memory
375            //The lifetime of this slice is tied to &self, since it points to self.map memory
376            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, size) },
377            next,
378        ))
379    }
380
381    /// Copy `len` bytes from `src` to the first 64-byte boundary after position `offset` of
382    /// the internal buffer. Then update `offset` to the first byte after the copied data.
383    fn append_ptr(&self, offset: &mut usize, src: *const u8, len: usize) {
384        let pos = u64_align!(*offset);
385        let data = &self.map[pos..(pos + len)];
386        //UNSAFE: This mut append is safe because only 1 thread can append at a time
387        //Mutex<()> guarantees exclusive write access to the memory occupied in
388        //the range.
389        unsafe {
390            let dst = data.as_ptr() as *mut u8;
391            std::ptr::copy(src, dst, len);
392        };
393        *offset = pos + len;
394    }
395
396    /// Copy each value in `vals`, in order, to the first 64-byte boundary after position `offset`.
397    /// If there is sufficient space, then update `offset` and the internal `current_len` to the
398    /// first byte after the copied data and return the starting position of the copied data.
399    /// Otherwise return None and leave `offset` unchanged.
400    fn append_ptrs_locked(&self, offset: &mut usize, vals: &[(*const u8, usize)]) -> Option<usize> {
401        let mut end = *offset;
402        for val in vals {
403            end = u64_align!(end);
404            end += val.1;
405        }
406
407        if (self.file_size as usize) < end {
408            return None;
409        }
410
411        let pos = u64_align!(*offset);
412        for val in vals {
413            self.append_ptr(offset, val.0, val.1)
414        }
415        self.current_len.store(*offset, Ordering::Relaxed);
416        Some(pos)
417    }
418
419    /// Return a reference to the type at `offset` if its data doesn't overrun the internal buffer.
420    /// Otherwise return None. Also return the offset of the first byte after the requested data
421    /// that falls on a 64-byte boundary.
422    fn get_type<'a, T>(&self, offset: usize) -> Option<(&'a T, usize)> {
423        let (data, next) = self.get_slice(offset, mem::size_of::<T>())?;
424        let ptr: *const T = data.as_ptr() as *const T;
425        //UNSAFE: The cast is safe because the slice is aligned and fits into the memory
426        //and the lifetime of the &T is tied to self, which holds the underlying memory map
427        Some((unsafe { &*ptr }, next))
428    }
429
430    /// Return account metadata for the account at `offset` if its data doesn't overrun
431    /// the internal buffer. Otherwise return None. Also return the offset of the first byte
432    /// after the requested data that falls on a 64-byte boundary.
433    pub fn get_account<'a>(&'a self, offset: usize) -> Option<(StoredAccountMeta<'a>, usize)> {
434        let (meta, next): (&'a StoredMeta, _) = self.get_type(offset)?;
435        let (account_meta, next): (&'a AccountMeta, _) = self.get_type(next)?;
436        let (hash, next): (&'a Hash, _) = self.get_type(next)?;
437        let (data, next) = self.get_slice(next, meta.data_len as usize)?;
438        let stored_size = next - offset;
439        Some((
440            StoredAccountMeta {
441                meta,
442                account_meta,
443                data,
444                offset,
445                stored_size,
446                hash,
447            },
448            next,
449        ))
450    }
451    pub fn get_account_test(&self, offset: usize) -> Option<(StoredMeta, AccountSharedData)> {
452        let (stored_account, _) = self.get_account(offset)?;
453        let meta = stored_account.meta.clone();
454        Some((meta, stored_account.clone_account()))
455    }
456
457    pub fn get_path(&self) -> PathBuf {
458        self.path.clone()
459    }
460
461    /// Return account metadata for each account, starting from `offset`.
462    pub fn accounts(&self, mut offset: usize) -> Vec<StoredAccountMeta> {
463        let mut accounts = vec![];
464        while let Some((account, next)) = self.get_account(offset) {
465            accounts.push(account);
466            offset = next;
467        }
468        accounts
469    }
470
471    /// Copy each account metadata, account and hash to the internal buffer.
472    /// Return the starting offset of each account metadata.
473    /// After each account is appended, the internal `current_len` is updated
474    /// and will be available to other threads.
475    pub fn append_accounts(
476        &self,
477        accounts: &[(StoredMeta, Option<&impl ReadableAccount>)],
478        hashes: &[impl Borrow<Hash>],
479    ) -> Vec<usize> {
480        let _lock = self.append_lock.lock().unwrap();
481        let mut offset = self.len();
482        let mut rv = Vec::with_capacity(accounts.len());
483        for ((stored_meta, account), hash) in accounts.iter().zip(hashes) {
484            let meta_ptr = stored_meta as *const StoredMeta;
485            let account_meta = AccountMeta::from(*account);
486            let account_meta_ptr = &account_meta as *const AccountMeta;
487            let data_len = stored_meta.data_len as usize;
488            let data_ptr = account
489                .map(|account| account.data())
490                .unwrap_or_default()
491                .as_ptr();
492            let hash_ptr = hash.borrow().as_ref().as_ptr();
493            let ptrs = [
494                (meta_ptr as *const u8, mem::size_of::<StoredMeta>()),
495                (account_meta_ptr as *const u8, mem::size_of::<AccountMeta>()),
496                (hash_ptr as *const u8, mem::size_of::<Hash>()),
497                (data_ptr, data_len),
498            ];
499            if let Some(res) = self.append_ptrs_locked(&mut offset, &ptrs) {
500                rv.push(res)
501            } else {
502                break;
503            }
504        }
505
506        // The last entry in this offset needs to be the u64 aligned offset, because that's
507        // where the *next* entry will begin to be stored.
508        rv.push(u64_align!(offset));
509
510        rv
511    }
512
513    /// Copy the account metadata, account and hash to the internal buffer.
514    /// Return the starting offset of the account metadata.
515    /// After the account is appended, the internal `current_len` is updated.
516    pub fn append_account(
517        &self,
518        storage_meta: StoredMeta,
519        account: &AccountSharedData,
520        hash: Hash,
521    ) -> Option<usize> {
522        let res = self.append_accounts(&[(storage_meta, Some(account))], &[&hash]);
523        if res.len() == 1 {
524            None
525        } else {
526            res.first().cloned()
527        }
528    }
529}
530
531pub mod test_utils {
532    use super::StoredMeta;
533    use rand::distributions::Alphanumeric;
534    use rand::{thread_rng, Rng};
535    use gemachain_sdk::account::AccountSharedData;
536    use gemachain_sdk::pubkey::Pubkey;
537    use std::fs::create_dir_all;
538    use std::path::PathBuf;
539
540    pub struct TempFile {
541        pub path: PathBuf,
542    }
543
544    impl Drop for TempFile {
545        fn drop(&mut self) {
546            let mut path = PathBuf::new();
547            std::mem::swap(&mut path, &mut self.path);
548            let _ignored = std::fs::remove_file(path);
549        }
550    }
551
552    pub fn get_append_vec_dir() -> String {
553        std::env::var("FARF_DIR").unwrap_or_else(|_| "farf/append_vec_tests".to_string())
554    }
555
556    pub fn get_append_vec_path(path: &str) -> TempFile {
557        let out_dir = get_append_vec_dir();
558        let rand_string: String = thread_rng().sample_iter(&Alphanumeric).take(30).collect();
559        let dir = format!("{}/{}", out_dir, rand_string);
560        let mut buf = PathBuf::new();
561        buf.push(&format!("{}/{}", dir, path));
562        create_dir_all(dir).expect("Create directory failed");
563        TempFile { path: buf }
564    }
565
566    pub fn create_test_account(sample: usize) -> (StoredMeta, AccountSharedData) {
567        let data_len = sample % 256;
568        let mut account = AccountSharedData::new(sample as u64, 0, &Pubkey::default());
569        account.set_data((0..data_len).map(|_| data_len as u8).collect());
570        let stored_meta = StoredMeta {
571            write_version: 0,
572            pubkey: Pubkey::default(),
573            data_len: data_len as u64,
574        };
575        (stored_meta, account)
576    }
577}
578
579#[cfg(test)]
580pub mod tests {
581    use super::test_utils::*;
582    use super::*;
583    use assert_matches::assert_matches;
584    use rand::{thread_rng, Rng};
585    use gemachain_sdk::{account::WritableAccount, timing::duration_as_ms};
586    use std::time::Instant;
587
588    impl AppendVec {
589        fn append_account_test(&self, data: &(StoredMeta, AccountSharedData)) -> Option<usize> {
590            self.append_account(data.0.clone(), &data.1, Hash::default())
591        }
592    }
593
594    impl<'a> StoredAccountMeta<'a> {
595        #[allow(clippy::cast_ref_to_mut)]
596        fn set_data_len_unsafe(&self, new_data_len: u64) {
597            // UNSAFE: cast away & (= const ref) to &mut to force to mutate append-only (=read-only) AppendVec
598            unsafe {
599                *(&self.meta.data_len as *const u64 as *mut u64) = new_data_len;
600            }
601        }
602
603        fn get_executable_byte(&self) -> u8 {
604            let executable_bool: bool = self.account_meta.executable;
605            // UNSAFE: Force to interpret mmap-backed bool as u8 to really read the actual memory content
606            let executable_byte: u8 = unsafe { std::mem::transmute::<bool, u8>(executable_bool) };
607            executable_byte
608        }
609
610        #[allow(clippy::cast_ref_to_mut)]
611        fn set_executable_as_byte(&self, new_executable_byte: u8) {
612            // UNSAFE: Force to interpret mmap-backed &bool as &u8 to write some crafted value;
613            unsafe {
614                *(&self.account_meta.executable as *const bool as *mut u8) = new_executable_byte;
615            }
616        }
617    }
618
619    #[test]
620    fn test_account_meta_default() {
621        let def1 = AccountMeta::default();
622        let def2 = AccountMeta::from(&Account::default());
623        assert_eq!(&def1, &def2);
624        let def2 = AccountMeta::from(&AccountSharedData::default());
625        assert_eq!(&def1, &def2);
626        let def2 = AccountMeta::from(Some(&AccountSharedData::default()));
627        assert_eq!(&def1, &def2);
628        let none: Option<&AccountSharedData> = None;
629        let def2 = AccountMeta::from(none);
630        assert_eq!(&def1, &def2);
631    }
632
633    #[test]
634    fn test_account_meta_non_default() {
635        let def1 = AccountMeta {
636            carats: 1,
637            owner: Pubkey::new_unique(),
638            executable: true,
639            rent_epoch: 3,
640        };
641        let def2_account = Account {
642            carats: def1.carats,
643            owner: def1.owner,
644            executable: def1.executable,
645            rent_epoch: def1.rent_epoch,
646            data: Vec::new(),
647        };
648        let def2 = AccountMeta::from(&def2_account);
649        assert_eq!(&def1, &def2);
650        let def2 = AccountMeta::from(&AccountSharedData::from(def2_account.clone()));
651        assert_eq!(&def1, &def2);
652        let def2 = AccountMeta::from(Some(&AccountSharedData::from(def2_account)));
653        assert_eq!(&def1, &def2);
654    }
655
656    #[test]
657    #[should_panic(expected = "too small file size 0 for AppendVec")]
658    fn test_append_vec_new_bad_size() {
659        let path = get_append_vec_path("test_append_vec_new_bad_size");
660        let _av = AppendVec::new(&path.path, true, 0);
661    }
662
663    #[test]
664    fn test_append_vec_new_from_file_bad_size() {
665        let file = get_append_vec_path("test_append_vec_new_from_file_bad_size");
666        let path = &file.path;
667
668        let _data = OpenOptions::new()
669            .read(true)
670            .write(true)
671            .create(true)
672            .open(&path)
673            .expect("create a test file for mmap");
674
675        let result = AppendVec::new_from_file(path, 0);
676        assert_matches!(result, Err(ref message) if message.to_string() == *"too small file size 0 for AppendVec");
677    }
678
679    #[test]
680    fn test_append_vec_sanitize_len_and_size_too_small() {
681        const LEN: usize = 0;
682        const SIZE: usize = 0;
683        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
684        assert_matches!(result, Err(ref message) if message.to_string() == *"too small file size 0 for AppendVec");
685    }
686
687    #[test]
688    fn test_append_vec_sanitize_len_and_size_maximum() {
689        const LEN: usize = 0;
690        const SIZE: usize = 16 * 1024 * 1024 * 1024;
691        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
692        assert_matches!(result, Ok(_));
693    }
694
695    #[test]
696    fn test_append_vec_sanitize_len_and_size_too_large() {
697        const LEN: usize = 0;
698        const SIZE: usize = 16 * 1024 * 1024 * 1024 + 1;
699        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
700        assert_matches!(result, Err(ref message) if message.to_string() == *"too large file size 17179869185 for AppendVec");
701    }
702
703    #[test]
704    fn test_append_vec_sanitize_len_and_size_full_and_same_as_current_len() {
705        const LEN: usize = 1024 * 1024;
706        const SIZE: usize = 1024 * 1024;
707        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
708        assert_matches!(result, Ok(_));
709    }
710
711    #[test]
712    fn test_append_vec_sanitize_len_and_size_larger_current_len() {
713        const LEN: usize = 1024 * 1024 + 1;
714        const SIZE: usize = 1024 * 1024;
715        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
716        assert_matches!(result, Err(ref message) if message.to_string() == *"current_len is larger than file size (1048576)");
717    }
718
719    #[test]
720    fn test_append_vec_one() {
721        let path = get_append_vec_path("test_append");
722        let av = AppendVec::new(&path.path, true, 1024 * 1024);
723        let account = create_test_account(0);
724        let index = av.append_account_test(&account).unwrap();
725        assert_eq!(av.get_account_test(index).unwrap(), account);
726    }
727
728    #[test]
729    fn test_append_vec_data() {
730        let path = get_append_vec_path("test_append_data");
731        let av = AppendVec::new(&path.path, true, 1024 * 1024);
732        let account = create_test_account(5);
733        let index = av.append_account_test(&account).unwrap();
734        assert_eq!(av.get_account_test(index).unwrap(), account);
735        let account1 = create_test_account(6);
736        let index1 = av.append_account_test(&account1).unwrap();
737        assert_eq!(av.get_account_test(index).unwrap(), account);
738        assert_eq!(av.get_account_test(index1).unwrap(), account1);
739    }
740
741    #[test]
742    fn test_append_vec_append_many() {
743        let path = get_append_vec_path("test_append_many");
744        let av = AppendVec::new(&path.path, true, 1024 * 1024);
745        let size = 1000;
746        let mut indexes = vec![];
747        let now = Instant::now();
748        for sample in 0..size {
749            let account = create_test_account(sample);
750            let pos = av.append_account_test(&account).unwrap();
751            assert_eq!(av.get_account_test(pos).unwrap(), account);
752            indexes.push(pos)
753        }
754        trace!("append time: {} ms", duration_as_ms(&now.elapsed()),);
755
756        let now = Instant::now();
757        for _ in 0..size {
758            let sample = thread_rng().gen_range(0, indexes.len());
759            let account = create_test_account(sample);
760            assert_eq!(av.get_account_test(indexes[sample]).unwrap(), account);
761        }
762        trace!("random read time: {} ms", duration_as_ms(&now.elapsed()),);
763
764        let now = Instant::now();
765        assert_eq!(indexes.len(), size);
766        assert_eq!(indexes[0], 0);
767        let mut accounts = av.accounts(indexes[0]);
768        assert_eq!(accounts.len(), size);
769        for (sample, v) in accounts.iter_mut().enumerate() {
770            let account = create_test_account(sample);
771            let recovered = v.clone_account();
772            assert_eq!(recovered, account.1)
773        }
774        trace!(
775            "sequential read time: {} ms",
776            duration_as_ms(&now.elapsed()),
777        );
778    }
779
780    #[test]
781    fn test_new_from_file_crafted_zero_carat_account() {
782        let file = get_append_vec_path("test_append");
783        let path = &file.path;
784        let mut av = AppendVec::new(path, true, 1024 * 1024);
785        av.set_no_remove_on_drop();
786
787        let pubkey = gemachain_sdk::pubkey::new_rand();
788        let owner = Pubkey::default();
789        let data_len = 3_u64;
790        let mut account = AccountSharedData::new(0, data_len as usize, &owner);
791        account.set_data(b"abc".to_vec());
792        let stored_meta = StoredMeta {
793            write_version: 0,
794            pubkey,
795            data_len,
796        };
797        let account_with_meta = (stored_meta, account);
798        let index = av.append_account_test(&account_with_meta).unwrap();
799        assert_eq!(av.get_account_test(index).unwrap(), account_with_meta);
800
801        av.flush().unwrap();
802        let accounts_len = av.len();
803        drop(av);
804        let result = AppendVec::new_from_file(path, accounts_len);
805        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
806    }
807
808    #[test]
809    fn test_new_from_file_crafted_data_len() {
810        let file = get_append_vec_path("test_new_from_file_crafted_data_len");
811        let path = &file.path;
812        let mut av = AppendVec::new(path, true, 1024 * 1024);
813        av.set_no_remove_on_drop();
814
815        let crafted_data_len = 1;
816
817        av.append_account_test(&create_test_account(10)).unwrap();
818
819        let accounts = av.accounts(0);
820        let account = accounts.first().unwrap();
821        account.set_data_len_unsafe(crafted_data_len);
822        assert_eq!(account.meta.data_len, crafted_data_len);
823
824        // Reload accounts and observe crafted_data_len
825        let accounts = av.accounts(0);
826        let account = accounts.first().unwrap();
827        assert_eq!(account.meta.data_len, crafted_data_len);
828
829        av.flush().unwrap();
830        let accounts_len = av.len();
831        drop(av);
832        let result = AppendVec::new_from_file(path, accounts_len);
833        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
834    }
835
836    #[test]
837    fn test_new_from_file_too_large_data_len() {
838        let file = get_append_vec_path("test_new_from_file_too_large_data_len");
839        let path = &file.path;
840        let mut av = AppendVec::new(path, true, 1024 * 1024);
841        av.set_no_remove_on_drop();
842
843        let too_large_data_len = u64::max_value();
844        av.append_account_test(&create_test_account(10)).unwrap();
845
846        let accounts = av.accounts(0);
847        let account = accounts.first().unwrap();
848        account.set_data_len_unsafe(too_large_data_len);
849        assert_eq!(account.meta.data_len, too_large_data_len);
850
851        // Reload accounts and observe no account with bad offset
852        let accounts = av.accounts(0);
853        assert_matches!(accounts.first(), None);
854
855        av.flush().unwrap();
856        let accounts_len = av.len();
857        drop(av);
858        let result = AppendVec::new_from_file(path, accounts_len);
859        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
860    }
861
862    #[test]
863    fn test_new_from_file_crafted_executable() {
864        let file = get_append_vec_path("test_new_from_crafted_executable");
865        let path = &file.path;
866        let mut av = AppendVec::new(path, true, 1024 * 1024);
867        av.set_no_remove_on_drop();
868        av.append_account_test(&create_test_account(10)).unwrap();
869        {
870            let mut executable_account = create_test_account(10);
871            executable_account.1.set_executable(true);
872            av.append_account_test(&executable_account).unwrap();
873        }
874
875        // reload accounts
876        let accounts = av.accounts(0);
877
878        // ensure false is 0u8 and true is 1u8 actually
879        assert_eq!(*accounts[0].ref_executable_byte(), 0);
880        assert_eq!(*accounts[1].ref_executable_byte(), 1);
881
882        let account = &accounts[0];
883        let crafted_executable = u8::max_value() - 1;
884
885        account.set_executable_as_byte(crafted_executable);
886
887        // reload crafted accounts
888        let accounts = av.accounts(0);
889        let account = accounts.first().unwrap();
890
891        // we can observe crafted value by ref
892        {
893            let executable_bool: &bool = &account.account_meta.executable;
894            // Depending on use, *executable_bool can be truthy or falsy due to direct memory manipulation
895            // assert_eq! thinks *executable_bool is equal to false but the if condition thinks it's not, contradictorily.
896            assert!(!*executable_bool);
897            const FALSE: bool = false; // keep clippy happy
898            if *executable_bool == FALSE {
899                panic!("This didn't occur if this test passed.");
900            }
901            assert_eq!(*account.ref_executable_byte(), crafted_executable);
902        }
903
904        // we can NOT observe crafted value by value
905        {
906            let executable_bool: bool = account.account_meta.executable;
907            assert!(!executable_bool);
908            assert_eq!(account.get_executable_byte(), 0); // Wow, not crafted_executable!
909        }
910
911        av.flush().unwrap();
912        let accounts_len = av.len();
913        drop(av);
914        let result = AppendVec::new_from_file(path, accounts_len);
915        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
916    }
917}