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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::config::{Error, RootPath, SecureBackend};
use diem_types::transaction::Transaction;
use serde::{Deserialize, Serialize};
use std::{
fs::File,
io::{Read, Write},
net::SocketAddr,
path::PathBuf,
};
const GENESIS_DEFAULT: &str = "genesis.blob";
#[derive(Clone, Deserialize, PartialEq, Eq, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct ExecutionConfig {
#[serde(skip)]
pub genesis: Option<Transaction>,
pub sign_vote_proposal: bool,
pub genesis_file_location: PathBuf,
pub service: ExecutionCorrectnessService,
pub backend: SecureBackend,
pub network_timeout_ms: u64,
}
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
)?;
write!(
f,
", sign_vote_proposal: {:?}, service: {:?}, backend: {:?} }}",
self.sign_vote_proposal, self.service, self.backend
)?;
self.service.fmt(f)
}
}
impl Default for ExecutionConfig {
fn default() -> ExecutionConfig {
ExecutionConfig {
genesis: None,
genesis_file_location: PathBuf::new(),
service: ExecutionCorrectnessService::Thread,
backend: SecureBackend::InMemoryStorage,
sign_vote_proposal: true,
network_timeout_ms: 30_000,
}
}
}
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(())
}
pub fn set_data_dir(&mut self, data_dir: PathBuf) {
if let SecureBackend::OnDiskStorage(backend) = &mut self.backend {
backend.set_data_dir(data_dir);
}
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum ExecutionCorrectnessService {
Local,
Process(RemoteExecutionService),
Serializer,
Thread,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteExecutionService {
pub server_address: SocketAddr,
}
#[cfg(test)]
mod test {
use super::*;
use diem_temppath::TempPath;
use diem_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)
}
}