(&self, block_dir: &Path)
| 125 | |
| 126 | #[derive(Clone, Debug, Serialize, Deserialize, Default)] |
| 127 | pub struct BlockIndex { |
| 128 | block_num_to_filename: HashMap<u64, String>, |
| 129 | hash_to_block_num: HashMap<B256, u64>, |
| 130 | } |
| 131 | |
| 132 | impl BlockIndex { |
| 133 | pub fn new() -> Self { |
| 134 | Self::default() |
| 135 | } |
| 136 | |
| 137 | pub fn add_block(&mut self, block_number: u64, block_hash: B256, filename: String) { |
| 138 | self.block_num_to_filename.insert(block_number, filename); |
| 139 | self.hash_to_block_num.insert(block_hash, block_number); |
| 140 | } |
| 141 | |
| 142 | pub fn get_block_file(&self, block_number: u64) -> Option<&String> { |
| 143 | self.block_num_to_filename.get(&block_number) |
| 144 | } |
| 145 | |
| 146 | pub fn get_block_number(&self, block_hash: &B256) -> Option<u64> { |
| 147 | self.hash_to_block_num.get(block_hash).copied() |
| 148 | } |
| 149 | |
| 150 | pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> anyhow::Result<()> { |
| 151 | let json = serde_json::to_string_pretty(self)?; |
| 152 | let mut temp_file = path.as_ref().to_path_buf(); |
| 153 | temp_file.set_extension("temp"); |
no test coverage detected