|
| 1 | +// Copyright (c) 2025 RBB S.r.l |
| 2 | + |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// Licensed under the MIT License; |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +use std::{ |
| 17 | + collections::BTreeMap, |
| 18 | + path::{Path, PathBuf}, |
| 19 | + str::FromStr as _, |
| 20 | +}; |
| 21 | + |
| 22 | +use common::{ |
| 23 | + chain::GenBlock, |
| 24 | + primitives::{BlockHeight, Id, H256}, |
| 25 | +}; |
| 26 | +use utils::ensure; |
| 27 | + |
| 28 | +pub fn read_checkpoints_from_csv_file( |
| 29 | + csv_file: &Path, |
| 30 | +) -> Result<BTreeMap<BlockHeight, Id<GenBlock>>, CheckpontsFromCsvReadError> { |
| 31 | + let file = |
| 32 | + std::fs::File::open(csv_file).map_err(|err| CheckpontsFromCsvReadError::FileOpenError { |
| 33 | + file: csv_file.to_owned(), |
| 34 | + error: err.to_string(), |
| 35 | + })?; |
| 36 | + |
| 37 | + read_checkpoints_from_csv(file) |
| 38 | +} |
| 39 | + |
| 40 | +pub fn read_checkpoints_from_csv( |
| 41 | + csv: impl std::io::Read, |
| 42 | +) -> Result<BTreeMap<BlockHeight, Id<GenBlock>>, CheckpontsFromCsvReadError> { |
| 43 | + // Note: flexible(true) means that lines with different field counts are allowed. |
| 44 | + // Our fields count is fixed to 2 and we only specify this to simplify the tests, where |
| 45 | + // we check for specific errors. |
| 46 | + let mut reader = csv::ReaderBuilder::new().has_headers(false).flexible(true).from_reader(csv); |
| 47 | + let expected_fields_count = 2; |
| 48 | + |
| 49 | + let mut checkpoints = BTreeMap::new(); |
| 50 | + |
| 51 | + for (record_idx, result) in reader.records().enumerate() { |
| 52 | + let record = result.map_err(|err| CheckpontsFromCsvReadError::RecordReadError { |
| 53 | + error: err.to_string(), |
| 54 | + })?; |
| 55 | + |
| 56 | + ensure!( |
| 57 | + record.len() == expected_fields_count, |
| 58 | + CheckpontsFromCsvReadError::UnexpectedFieldsCount { |
| 59 | + record_idx, |
| 60 | + actual_fields_count: record.len(), |
| 61 | + expected_fields_count |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + let block_height = record |
| 66 | + .get(0) |
| 67 | + .expect("field is known to be present") |
| 68 | + .parse::<u64>() |
| 69 | + .map_err(|_| CheckpontsFromCsvReadError::BadBlockHeight { record_idx })?; |
| 70 | + |
| 71 | + let block_id = H256::from_str(record.get(1).expect("field is known to be present")) |
| 72 | + .map_err(|_| CheckpontsFromCsvReadError::BadBlockId { record_idx })?; |
| 73 | + |
| 74 | + let already_existed = |
| 75 | + checkpoints.insert(BlockHeight::new(block_height), Id::new(block_id)).is_some(); |
| 76 | + ensure!( |
| 77 | + !already_existed, |
| 78 | + CheckpontsFromCsvReadError::DuplicateCheckpoint { |
| 79 | + height: block_height |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + Ok(checkpoints) |
| 85 | +} |
| 86 | + |
| 87 | +#[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)] |
| 88 | +pub enum CheckpontsFromCsvReadError { |
| 89 | + #[error("Cannon open file '{file}': {error}")] |
| 90 | + FileOpenError { file: PathBuf, error: String }, |
| 91 | + |
| 92 | + #[error("Error reading a record: {error}")] |
| 93 | + RecordReadError { error: String }, |
| 94 | + |
| 95 | + #[error("Unexpected fields count in record {record_idx}: expected {expected_fields_count}, got {actual_fields_count}")] |
| 96 | + UnexpectedFieldsCount { |
| 97 | + record_idx: usize, |
| 98 | + actual_fields_count: usize, |
| 99 | + expected_fields_count: usize, |
| 100 | + }, |
| 101 | + |
| 102 | + #[error("Bad block height in record {record_idx}")] |
| 103 | + BadBlockHeight { record_idx: usize }, |
| 104 | + |
| 105 | + #[error("Bad block id in record {record_idx}")] |
| 106 | + BadBlockId { record_idx: usize }, |
| 107 | + |
| 108 | + #[error("Duplicate checkpoint at height {height}")] |
| 109 | + DuplicateCheckpoint { height: u64 }, |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use utils::concatln; |
| 115 | + |
| 116 | + use super::*; |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn correct_read() { |
| 120 | + let mk_id = |id_str| Id::new(H256::from_str(id_str).unwrap()); |
| 121 | + let data = concatln!( |
| 122 | + "500, C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0", |
| 123 | + "1000, 1DCFB22374DA757882EEF26AF2B2D3ABDD1A4887C744346F6413C8D0B51DEBDF", |
| 124 | + "1500, 3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9" |
| 125 | + ); |
| 126 | + let expected_checkpoints = BTreeMap::from([ |
| 127 | + ( |
| 128 | + BlockHeight::new(500), |
| 129 | + mk_id("C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0"), |
| 130 | + ), |
| 131 | + ( |
| 132 | + BlockHeight::new(1000), |
| 133 | + mk_id("1DCFB22374DA757882EEF26AF2B2D3ABDD1A4887C744346F6413C8D0B51DEBDF"), |
| 134 | + ), |
| 135 | + ( |
| 136 | + BlockHeight::new(1500), |
| 137 | + mk_id("3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9"), |
| 138 | + ), |
| 139 | + ]); |
| 140 | + |
| 141 | + let checkpoints = read_checkpoints_from_csv(data.as_bytes()).unwrap(); |
| 142 | + assert_eq!(checkpoints, expected_checkpoints); |
| 143 | + |
| 144 | + // Now write the csv to file and read it via `read_checkpoints_from_csv_file`. |
| 145 | + let temp_file = tempfile::NamedTempFile::new().unwrap(); |
| 146 | + std::fs::write(temp_file.path(), data.as_bytes()).unwrap(); |
| 147 | + let checkpoints_from_file = read_checkpoints_from_csv_file(temp_file.path()).unwrap(); |
| 148 | + assert_eq!(checkpoints_from_file, expected_checkpoints); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn bad_fields_count() { |
| 153 | + let data1 = concatln!( |
| 154 | + "500, C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0", |
| 155 | + "1000, 1DCFB22374DA757882EEF26AF2B2D3ABDD1A4887C744346F6413C8D0B51DEBDF, 111", |
| 156 | + "1500, 3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9" |
| 157 | + ); |
| 158 | + let err = read_checkpoints_from_csv(data1.as_bytes()).unwrap_err(); |
| 159 | + assert_eq!( |
| 160 | + err, |
| 161 | + CheckpontsFromCsvReadError::UnexpectedFieldsCount { |
| 162 | + record_idx: 1, |
| 163 | + actual_fields_count: 3, |
| 164 | + expected_fields_count: 2 |
| 165 | + } |
| 166 | + ); |
| 167 | + |
| 168 | + let data1 = concatln!( |
| 169 | + "500, C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0", |
| 170 | + "1000", |
| 171 | + "1500, 3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9" |
| 172 | + ); |
| 173 | + let err = read_checkpoints_from_csv(data1.as_bytes()).unwrap_err(); |
| 174 | + assert_eq!( |
| 175 | + err, |
| 176 | + CheckpontsFromCsvReadError::UnexpectedFieldsCount { |
| 177 | + record_idx: 1, |
| 178 | + actual_fields_count: 1, |
| 179 | + expected_fields_count: 2 |
| 180 | + } |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn bad_block_height() { |
| 186 | + let data = concatln!( |
| 187 | + "500, C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0", |
| 188 | + "X000, 1DCFB22374DA757882EEF26AF2B2D3ABDD1A4887C744346F6413C8D0B51DEBDF", |
| 189 | + "1500, 3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9" |
| 190 | + ); |
| 191 | + let err = read_checkpoints_from_csv(data.as_bytes()).unwrap_err(); |
| 192 | + assert_eq!( |
| 193 | + err, |
| 194 | + CheckpontsFromCsvReadError::BadBlockHeight { record_idx: 1 } |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn bad_block_id() { |
| 200 | + let data = concatln!( |
| 201 | + "500, C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0", |
| 202 | + "1000, XDCFB22374DA757882EEF26AF2B2D3ABDD1A4887C744346F6413C8D0B51DEBDF", |
| 203 | + "1500, 3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9" |
| 204 | + ); |
| 205 | + let err = read_checkpoints_from_csv(data.as_bytes()).unwrap_err(); |
| 206 | + assert_eq!( |
| 207 | + err, |
| 208 | + CheckpontsFromCsvReadError::BadBlockId { record_idx: 1 } |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + #[test] |
| 213 | + fn duplicate_checkpoint() { |
| 214 | + let data = concatln!( |
| 215 | + "500, C91C3DB7DFDCC296010546EC38F48A557D035DD0B34260BD6C5174709F8A7EB0", |
| 216 | + "500, 1DCFB22374DA757882EEF26AF2B2D3ABDD1A4887C744346F6413C8D0B51DEBDF", |
| 217 | + "1500, 3F81279C128FF628C8F4055DF89173DDAA6597DAB7636E8B12CA386E7864DFE9" |
| 218 | + ); |
| 219 | + let err = read_checkpoints_from_csv(data.as_bytes()).unwrap_err(); |
| 220 | + assert_eq!( |
| 221 | + err, |
| 222 | + CheckpontsFromCsvReadError::DuplicateCheckpoint { height: 500 } |
| 223 | + ); |
| 224 | + } |
| 225 | +} |
0 commit comments