-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathconfig.rs
More file actions
69 lines (60 loc) · 2.09 KB
/
config.rs
File metadata and controls
69 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use serde::{Deserialize, Serialize};
use std::{fs::File, fs::OpenOptions, io::Read, io::Write};
#[derive(Debug, Deserialize, Serialize)]
pub struct ECDSAConfig {
pub private_key_store_path: String,
pub private_key_store_password: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct LastProcessedBlock {
pub last_processed_block: u64,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub eth_rpc_url: String,
pub eth_ws_url: String,
pub max_proofs_in_queue: u16,
pub proof_aggregation_service_address: String,
pub aligned_service_manager_address: String,
pub last_processed_block_filepath: String,
pub ecdsa: ECDSAConfig,
}
impl Config {
pub fn from_file(file_path: &str) -> Result<Config, Box<dyn std::error::Error>> {
let mut file = File::open(file_path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: Config = serde_yaml::from_str(&contents)?;
Ok(config)
}
pub fn get_last_processed_block(&self) -> Result<u64, Box<dyn std::error::Error>> {
match File::open(&self.last_processed_block_filepath) {
Err(_) => {
// if file doesn't exist, default 0
Ok(0)
}
Ok(mut file) => {
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let lpb: LastProcessedBlock = serde_json::from_str(&contents)?;
Ok(lpb.last_processed_block)
}
}
}
pub fn update_last_processed_block(
&self,
last_processed_block: u64,
) -> Result<(), Box<dyn std::error::Error>> {
let last_processed_block_struct = LastProcessedBlock {
last_processed_block,
};
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(&self.last_processed_block_filepath)?;
let content = serde_json::to_string(&last_processed_block_struct)?;
file.write_all(content.as_bytes())?;
Ok(())
}
}