cryptonote_currency/
lib.rs

1use dirs;
2
3use cryptonote_config::Config;
4
5pub struct Currency {
6  pub config: Config,
7  pub is_home_dir: bool,
8}
9
10impl Currency {
11  // Get Files
12  fn get_files(&self, name: &String) -> String {
13    let mut path;
14    if cfg!(unix) && self.is_home_dir {
15      path = dirs::home_dir().unwrap();
16      path.push(format!(".{}", self.config.coinName));
17    } else {
18      path = dirs::data_dir().unwrap();
19      path.push(format!("{}", self.config.coinName));
20    }
21
22    path.push(name);
23
24    let filename = String::from(path.to_str().unwrap());
25    return filename;
26  }
27
28  pub fn get_block_main_file(&self) -> String {
29    return self.get_files(&self.config.files.main);
30  }
31
32  pub fn get_block_cache_file(&self) -> String {
33    return self.get_files(&self.config.files.cache);
34  }
35
36  pub fn get_block_index_file(&self) -> String {
37    return self.get_files(&self.config.files.index);
38  }
39
40  pub fn get_block_chain_index_file(&self) -> String {
41    return self.get_files(&self.config.files.chainIndex);
42  }
43
44  pub fn new(config: Config) -> Currency {
45    Currency {
46      config: config,
47      is_home_dir: false,
48    }
49  }
50
51  pub fn old(config: Config) -> Currency {
52    Currency {
53      config: config,
54      is_home_dir: true,
55    }
56  }
57}
58
59#[cfg(test)]
60
61mod tests {
62  use super::*;
63  use cryptonote_basic::Version;
64  use cryptonote_config::{BlockFiles, Config, NetType};
65
66  #[test]
67  fn should_get_block_main_file() {
68    let files = BlockFiles::new([
69      String::from("blocks.dat"),
70      String::from("blockindexes.dat"),
71      String::from("blockscache.dat"),
72      String::from("blockchainindices.dat"),
73    ]);
74    let config = Config::new(
75      0x3d,
76      files,
77      String::from("vigcoin"),
78      String::from("aaa"),
79      Version {
80        major: 1,
81        minor: 0,
82        patch: 0,
83      },
84      NetType::Main
85    );
86
87    let currency = Currency::old(config);
88    let mut path = dirs::data_dir().unwrap();
89    path.push(currency.config.coinName.clone());
90    assert!(currency.get_files(&String::from("blocks.dat")) == currency.get_block_main_file());
91    assert!(currency.get_files(&String::from("blockindexes.dat")) == currency.get_block_index_file());
92    assert!(currency.get_files(&String::from("blockscache.dat")) == currency.get_block_cache_file());
93    assert!(currency.get_files(&String::from("blockchainindices.dat")) == currency.get_block_chain_index_file());
94    println!("{}", currency.get_block_main_file());
95    println!("{}", currency.get_block_index_file());
96    println!("{}", currency.get_block_cache_file());
97    println!("{}", currency.get_block_chain_index_file());
98  }
99
100  #[test]
101  fn should_get_v2_block_file() {
102    let files = BlockFiles::new([
103      String::from("blocks.dat"),
104      String::from("blockindexes.dat"),
105      String::from("blockscache.dat"),
106      String::from("blockchainindices.dat"),
107    ]);
108    let config = Config::new(
109      0x3d,
110      files,
111      String::from("vigcoin"),
112      String::from("aaa"),
113      Version {
114        major: 2,
115        minor: 0,
116        patch: 0,
117      },
118      NetType::Test
119    );
120
121    let currency = Currency::new(config);
122    let mut path = dirs::data_dir().unwrap();
123    path.push(currency.config.coinName.clone());
124    assert!(currency.get_files(&String::from("blocks.dat")) == currency.get_block_main_file());
125    assert!(currency.get_files(&String::from("blockindexes.dat")) == currency.get_block_index_file());
126    assert!(currency.get_files(&String::from("blockscache.dat")) == currency.get_block_cache_file());
127    assert!(currency.get_files(&String::from("blockchainindices.dat")) == currency.get_block_chain_index_file());
128    println!("{}", currency.get_block_main_file());
129    println!("{}", currency.get_block_index_file());
130    println!("{}", currency.get_block_cache_file());
131    println!("{}", currency.get_block_chain_index_file());
132  }
133}