|
| 1 | +use std::{fs::File, io::Write, path::Path}; |
| 2 | + |
| 3 | +use slog::{warn, Logger}; |
| 4 | + |
| 5 | +use mithril_common::CardanoNetwork; |
| 6 | + |
| 7 | +use crate::MithrilResult; |
| 8 | + |
| 9 | +const CLEAN_FILE_NAME: &str = "clean"; |
| 10 | +const PROTOCOL_MAGIC_ID_FILENAME: &str = "protocolMagicId"; |
| 11 | + |
| 12 | +pub fn create_bootstrap_node_files( |
| 13 | + logger: &Logger, |
| 14 | + db_dir: &Path, |
| 15 | + network: &str, |
| 16 | +) -> MithrilResult<()> { |
| 17 | + if let Err(error) = File::create(db_dir.join(CLEAN_FILE_NAME)) { |
| 18 | + warn!( |
| 19 | + logger, "Could not create clean shutdown marker file in directory '{}'", db_dir.display(); |
| 20 | + "error" => error.to_string() |
| 21 | + ); |
| 22 | + }; |
| 23 | + |
| 24 | + if let Ok(network) = CardanoNetwork::from_code(network.to_string(), None) { |
| 25 | + let mut file = File::create(db_dir.join(PROTOCOL_MAGIC_ID_FILENAME))?; |
| 26 | + file.write_all(format!("{}", network.code()).as_bytes())?; |
| 27 | + }; |
| 28 | + |
| 29 | + Ok(()) |
| 30 | +} |
| 31 | + |
| 32 | +#[cfg(test)] |
| 33 | +mod test { |
| 34 | + use mithril_common::{temp_dir, CardanoNetwork}; |
| 35 | + use slog::o; |
| 36 | + |
| 37 | + use super::*; |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn create_bootstrap_node_files_creates_protocol_magic_id_and_clean_files_for_mainnet() { |
| 41 | + let db_dir = temp_dir!().join("db"); |
| 42 | + std::fs::create_dir_all(&db_dir).unwrap(); |
| 43 | + let network = CardanoNetwork::from_code("mainnet".to_string(), None).unwrap(); |
| 44 | + let logger = slog::Logger::root(slog::Discard, o!()); |
| 45 | + |
| 46 | + create_bootstrap_node_files(&logger, &db_dir, &network.to_string()).unwrap(); |
| 47 | + |
| 48 | + let file_content = |
| 49 | + std::fs::read_to_string(db_dir.join(PROTOCOL_MAGIC_ID_FILENAME)).unwrap(); |
| 50 | + assert_eq!(file_content, network.code().to_string()); |
| 51 | + |
| 52 | + assert!(db_dir.join(CLEAN_FILE_NAME).exists()); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn create_bootstrap_node_files_creates_protocol_magic_id_and_clean_files_for_preprod() { |
| 57 | + let db_dir = temp_dir!().join("db"); |
| 58 | + std::fs::create_dir_all(&db_dir).unwrap(); |
| 59 | + let network = CardanoNetwork::from_code("preprod".to_string(), None).unwrap(); |
| 60 | + let logger = slog::Logger::root(slog::Discard, o!()); |
| 61 | + |
| 62 | + create_bootstrap_node_files(&logger, &db_dir, &network.to_string()).unwrap(); |
| 63 | + |
| 64 | + let file_content = |
| 65 | + std::fs::read_to_string(db_dir.join(PROTOCOL_MAGIC_ID_FILENAME)).unwrap(); |
| 66 | + assert_eq!(file_content, network.code().to_string()); |
| 67 | + |
| 68 | + assert!(db_dir.join(CLEAN_FILE_NAME).exists()); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn create_bootstrap_node_files_creates_protocol_magic_id_and_clean_files_for_preview() { |
| 73 | + let db_dir = temp_dir!().join("db"); |
| 74 | + std::fs::create_dir_all(&db_dir).unwrap(); |
| 75 | + let network = CardanoNetwork::from_code("preview".to_string(), None).unwrap(); |
| 76 | + let logger = slog::Logger::root(slog::Discard, o!()); |
| 77 | + |
| 78 | + create_bootstrap_node_files(&logger, &db_dir, &network.to_string()).unwrap(); |
| 79 | + |
| 80 | + let file_content = |
| 81 | + std::fs::read_to_string(db_dir.join(PROTOCOL_MAGIC_ID_FILENAME)).unwrap(); |
| 82 | + assert_eq!(file_content, network.code().to_string()); |
| 83 | + |
| 84 | + assert!(db_dir.join(CLEAN_FILE_NAME).exists()); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn create_bootstrap_node_files_creates_protocol_magic_id_and_clean_files_for_testnet() { |
| 89 | + let db_dir = temp_dir!().join("db"); |
| 90 | + std::fs::create_dir_all(&db_dir).unwrap(); |
| 91 | + let network = CardanoNetwork::from_code("testnet".to_string(), None).unwrap(); |
| 92 | + let logger = slog::Logger::root(slog::Discard, o!()); |
| 93 | + |
| 94 | + create_bootstrap_node_files(&logger, &db_dir, &network.to_string()).unwrap(); |
| 95 | + |
| 96 | + let file_content = |
| 97 | + std::fs::read_to_string(db_dir.join(PROTOCOL_MAGIC_ID_FILENAME)).unwrap(); |
| 98 | + assert_eq!(file_content, network.code().to_string()); |
| 99 | + |
| 100 | + assert!(db_dir.join(CLEAN_FILE_NAME).exists()); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn create_bootstrap_node_files_does_not_create_protocol_magic_id_file_and_create_clean_file_for_devnet( |
| 105 | + ) { |
| 106 | + let db_dir = temp_dir!().join("db"); |
| 107 | + std::fs::create_dir_all(&db_dir).unwrap(); |
| 108 | + let network = CardanoNetwork::from_code("devnet".to_string(), Some(123)).unwrap(); |
| 109 | + let logger = slog::Logger::root(slog::Discard, o!()); |
| 110 | + |
| 111 | + create_bootstrap_node_files(&logger, &db_dir, &network.to_string()).unwrap(); |
| 112 | + |
| 113 | + assert!(!db_dir.join(PROTOCOL_MAGIC_ID_FILENAME).exists()); |
| 114 | + |
| 115 | + assert!(db_dir.join(CLEAN_FILE_NAME).exists()); |
| 116 | + } |
| 117 | +} |
0 commit comments