|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use eth2::types::Hash256; |
| 5 | +use eyre::Result; |
| 6 | +use tokio::io::{AsyncReadExt, AsyncWriteExt}; |
| 7 | + |
| 8 | +use crate::storage::{BackfillProcesses, BACKFILL_LOCK}; |
| 9 | +use crate::{BlobData, LockFile, StorageReader, StorageWriter}; |
| 10 | + |
| 11 | +pub struct FSStorage { |
| 12 | + pub(crate) dir: PathBuf, |
| 13 | +} |
| 14 | + |
| 15 | +impl FSStorage { |
| 16 | + pub async fn new(dir: PathBuf) -> Result<Self> { |
| 17 | + Ok(Self { dir }) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +#[async_trait] |
| 22 | +impl StorageReader for FSStorage { |
| 23 | + async fn read_blob_data(&self, hash: Hash256) -> Result<BlobData> { |
| 24 | + let path = self.dir.join(format!("{:x}", hash)); |
| 25 | + let mut file = tokio::fs::File::open(path).await?; |
| 26 | + let mut data = Vec::new(); |
| 27 | + file.read_to_end(&mut data).await?; |
| 28 | + Ok(serde_json::from_slice(&data)?) |
| 29 | + } |
| 30 | + |
| 31 | + async fn exists(&self, hash: Hash256) -> bool { |
| 32 | + self.dir.join(format!("{:x}", hash)).exists() |
| 33 | + } |
| 34 | + |
| 35 | + async fn read_lock_file(&self) -> Result<LockFile> { |
| 36 | + let path = self.dir.join("lockfile"); |
| 37 | + let mut file = tokio::fs::File::open(path).await?; |
| 38 | + let mut data = Vec::new(); |
| 39 | + file.read_to_end(&mut data).await?; |
| 40 | + Ok(serde_json::from_slice(&data)?) |
| 41 | + } |
| 42 | + |
| 43 | + async fn read_backfill_processes(&self) -> Result<BackfillProcesses> { |
| 44 | + BACKFILL_LOCK.lock(); |
| 45 | + let path = self.dir.join("backfill_processes"); |
| 46 | + let mut file = tokio::fs::File::open(path).await?; |
| 47 | + let mut data = Vec::new(); |
| 48 | + file.read_to_end(&mut data).await?; |
| 49 | + Ok(serde_json::from_slice(&data)?) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[async_trait] |
| 54 | +impl StorageWriter for FSStorage { |
| 55 | + async fn write_blob_data(&self, blob_data: BlobData) -> Result<()> { |
| 56 | + let path = self |
| 57 | + .dir |
| 58 | + .join(format!("{:x}", blob_data.header.beacon_block_hash)); |
| 59 | + tokio::fs::create_dir_all(path.parent().unwrap()).await?; |
| 60 | + let mut file = tokio::fs::File::create(path).await?; |
| 61 | + file.write_all(&serde_json::to_vec(&blob_data)?).await?; |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + async fn write_lock_file(&self, lock_file: LockFile) -> Result<()> { |
| 66 | + let path = self.dir.join("lockfile"); |
| 67 | + tokio::fs::create_dir_all(path.parent().unwrap()).await?; |
| 68 | + let mut file = tokio::fs::File::create(path).await?; |
| 69 | + file.write_all(&serde_json::to_vec(&lock_file)?).await?; |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | + |
| 73 | + async fn write_backfill_process(&self, backfill_process: BackfillProcesses) -> Result<()> { |
| 74 | + BACKFILL_LOCK.lock(); |
| 75 | + let path = self.dir.join("backfill_processes"); |
| 76 | + tokio::fs::create_dir_all(path.parent().unwrap()).await?; |
| 77 | + let mut file = tokio::fs::File::create(path).await?; |
| 78 | + file.write_all(&serde_json::to_vec(&backfill_process)?) |
| 79 | + .await?; |
| 80 | + Ok(()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use crate::storage::{ |
| 87 | + create_test_blob_data, create_test_lock_file, create_test_test_backfill_processes, |
| 88 | + }; |
| 89 | + use tokio::io; |
| 90 | + |
| 91 | + use super::*; |
| 92 | + |
| 93 | + #[tokio::test] |
| 94 | + async fn test_fs_storage() { |
| 95 | + let storage = FSStorage::new(PathBuf::from("test_dir")).await.unwrap(); |
| 96 | + tokio::fs::create_dir_all(&storage.dir).await.unwrap(); |
| 97 | + let blob_data = create_test_blob_data(); |
| 98 | + assert!(storage |
| 99 | + .read_blob_data(blob_data.header.beacon_block_hash) |
| 100 | + .await |
| 101 | + .is_err_and(|e| e.downcast_ref::<io::Error>().is_some())); |
| 102 | + storage.write_blob_data(blob_data.clone()).await.unwrap(); |
| 103 | + assert_eq!( |
| 104 | + storage |
| 105 | + .read_blob_data(blob_data.header.beacon_block_hash) |
| 106 | + .await |
| 107 | + .unwrap(), |
| 108 | + blob_data |
| 109 | + ); |
| 110 | + let lock_file = create_test_lock_file(); |
| 111 | + assert!(storage |
| 112 | + .read_lock_file() |
| 113 | + .await |
| 114 | + .is_err_and(|e| e.downcast_ref::<io::Error>().is_some())); |
| 115 | + storage.write_lock_file(lock_file.clone()).await.unwrap(); |
| 116 | + assert_eq!(storage.read_lock_file().await.unwrap(), lock_file); |
| 117 | + let test_backfill_processes = create_test_test_backfill_processes(); |
| 118 | + assert!(storage |
| 119 | + .read_backfill_processes() |
| 120 | + .await |
| 121 | + .is_err_and(|e| e.downcast_ref::<io::Error>().is_some())); |
| 122 | + storage |
| 123 | + .write_backfill_process(test_backfill_processes.clone()) |
| 124 | + .await |
| 125 | + .unwrap(); |
| 126 | + assert_eq!( |
| 127 | + storage.read_backfill_processes().await.unwrap(), |
| 128 | + test_backfill_processes |
| 129 | + ); |
| 130 | + clean_dir(&storage.dir); |
| 131 | + } |
| 132 | + |
| 133 | + #[tokio::test] |
| 134 | + async fn test_fs_storage_exists() { |
| 135 | + let storage = FSStorage::new(PathBuf::from("test_dir")).await.unwrap(); |
| 136 | + tokio::fs::create_dir_all(&storage.dir).await.unwrap(); |
| 137 | + let blob_data = create_test_blob_data(); |
| 138 | + assert!(!storage.exists(blob_data.header.beacon_block_hash).await); |
| 139 | + storage.write_blob_data(blob_data.clone()).await.unwrap(); |
| 140 | + assert!(storage.exists(blob_data.header.beacon_block_hash).await); |
| 141 | + clean_dir(&storage.dir); |
| 142 | + } |
| 143 | + |
| 144 | + fn clean_dir(dir: &PathBuf) { |
| 145 | + if dir.exists() { |
| 146 | + std::fs::remove_dir_all(dir).unwrap(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments