diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/db.rs | 25 |
1 files changed, 16 insertions, 9 deletions
| @@ -30,25 +30,32 @@ impl Db { | |||
| 30 | pub fn new(config: Config) -> Self { | 30 | pub fn new(config: Config) -> Self { |
| 31 | fs::create_dir_all(format!("blocks/{}", config.name)).unwrap(); | 31 | fs::create_dir_all(format!("blocks/{}", config.name)).unwrap(); |
| 32 | fs::create_dir_all(format!("users/{}", config.name)).unwrap(); | 32 | fs::create_dir_all(format!("users/{}", config.name)).unwrap(); |
| 33 | let mut db = Db::default(); | ||
| 34 | if let Some(block_path) = last_block_content(&config.name) { | ||
| 35 | db.populate_with_last_block(block_path); | ||
| 36 | } | ||
| 37 | |||
| 38 | if let Ok(users_path) = read_users(&config.name) { | ||
| 39 | db.populate_with_users(users_path); | ||
| 40 | } | ||
| 41 | 33 | ||
| 34 | // Load bots | ||
| 42 | let users: HashMap<Fingerprint, User> = get_friendly_users(); | 35 | let users: HashMap<Fingerprint, User> = get_friendly_users(); |
| 36 | |||
| 37 | // Read the list of users who can register | ||
| 43 | let preapproved_users = read_approved_users(); | 38 | let preapproved_users = read_approved_users(); |
| 44 | 39 | ||
| 45 | Db { | 40 | let mut db = Db { |
| 46 | blockchain: Arc::new(RwLock::new(Block::default())), | 41 | blockchain: Arc::new(RwLock::new(Block::default())), |
| 47 | pending_transactions: Arc::new(RwLock::new(HashMap::new())), | 42 | pending_transactions: Arc::new(RwLock::new(HashMap::new())), |
| 48 | users: Arc::new(RwLock::new(users)), | 43 | users: Arc::new(RwLock::new(users)), |
| 49 | config, | 44 | config, |
| 50 | preapproved_users, | 45 | preapproved_users, |
| 46 | }; | ||
| 47 | |||
| 48 | // Read blocks | ||
| 49 | if let Some(block_path) = last_block_content(&db.config.name) { | ||
| 50 | db.populate_with_last_block(block_path); | ||
| 51 | } | ||
| 52 | |||
| 53 | // Read users | ||
| 54 | if let Ok(users_path) = read_users(&db.config.name) { | ||
| 55 | db.populate_with_users(users_path); | ||
| 51 | } | 56 | } |
| 57 | |||
| 58 | return db; | ||
| 52 | } | 59 | } |
| 53 | 60 | ||
| 54 | fn populate_with_last_block(&mut self, path: String) { | 61 | fn populate_with_last_block(&mut self, path: String) { |
