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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::config::{Error, RootPath};
use aptos_types::transaction::Transaction;
use serde::{Deserialize, Serialize};
use std::{
fs::File,
io::{Read, Write},
path::PathBuf,
};
const GENESIS_DEFAULT: &str = "genesis.blob";
#[derive(Clone, Deserialize, PartialEq, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct ExecutionConfig {
#[serde(skip)]
pub genesis: Option<Transaction>,
pub genesis_file_location: PathBuf,
pub network_timeout_ms: u64,
pub concurrency_level: u16,
pub num_proof_reading_threads: u16,
}
impl std::fmt::Debug for ExecutionConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ExecutionConfig {{ genesis: ")?;
if self.genesis.is_some() {
write!(f, "Some(...)")?;
} else {
write!(f, "None")?;
}
write!(
f,
", genesis_file_location: {:?} ",
self.genesis_file_location
)
}
}
impl Default for ExecutionConfig {
fn default() -> ExecutionConfig {
ExecutionConfig {
genesis: None,
genesis_file_location: PathBuf::new(),
network_timeout_ms: 30_000,
concurrency_level: 1,
num_proof_reading_threads: 32,
}
}
}
impl ExecutionConfig {
pub fn load(&mut self, root_dir: &RootPath) -> Result<(), Error> {
if !self.genesis_file_location.as_os_str().is_empty() {
let path = root_dir.full_path(&self.genesis_file_location);
let mut file = File::open(&path).map_err(|e| Error::IO("genesis".into(), e))?;
let mut buffer = vec![];
file.read_to_end(&mut buffer)
.map_err(|e| Error::IO("genesis".into(), e))?;
let data = bcs::from_bytes(&buffer).map_err(|e| Error::BCS("genesis", e))?;
self.genesis = Some(data);
}
Ok(())
}
pub fn save(&mut self, root_dir: &RootPath) -> Result<(), Error> {
if let Some(genesis) = &self.genesis {
if self.genesis_file_location.as_os_str().is_empty() {
self.genesis_file_location = PathBuf::from(GENESIS_DEFAULT);
}
let path = root_dir.full_path(&self.genesis_file_location);
let mut file = File::create(&path).map_err(|e| Error::IO("genesis".into(), e))?;
let data = bcs::to_bytes(&genesis).map_err(|e| Error::BCS("genesis", e))?;
file.write_all(&data)
.map_err(|e| Error::IO("genesis".into(), e))?;
}
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use aptos_temppath::TempPath;
use aptos_types::{
transaction::{ChangeSet, Transaction, WriteSetPayload},
write_set::WriteSetMut,
};
#[test]
fn test_no_genesis() {
let (mut config, path) = generate_config();
assert_eq!(config.genesis, None);
let root_dir = RootPath::new_path(path.path());
let result = config.load(&root_dir);
assert!(result.is_ok());
assert_eq!(config.genesis_file_location, PathBuf::new());
}
#[test]
fn test_some_and_load_genesis() {
let fake_genesis = Transaction::GenesisTransaction(WriteSetPayload::Direct(
ChangeSet::new(WriteSetMut::new(vec![]).freeze().unwrap(), vec![]),
));
let (mut config, path) = generate_config();
config.genesis = Some(fake_genesis.clone());
let root_dir = RootPath::new_path(path.path());
config.save(&root_dir).expect("Unable to save");
assert_eq!(config.genesis_file_location, PathBuf::from(GENESIS_DEFAULT));
config.genesis = None;
let result = config.load(&root_dir);
assert!(result.is_ok());
assert_eq!(config.genesis, Some(fake_genesis));
}
fn generate_config() -> (ExecutionConfig, TempPath) {
let temp_dir = TempPath::new();
temp_dir.create_as_dir().expect("error creating tempdir");
let execution_config = ExecutionConfig::default();
(execution_config, temp_dir)
}
}