Skip to content

Commit 2c9a336

Browse files
committed
feat(testing): deterministic libp2p keypair used for ocaml node
re: #188
1 parent 1d7c2c6 commit 2c9a336

File tree

8 files changed

+93
-48
lines changed

8 files changed

+93
-48
lines changed

node/testing/src/cluster/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct Cluster {
6464
ocaml_nodes: Vec<Option<OcamlNode>>,
6565

6666
rpc_counter: usize,
67+
ocaml_libp2p_keypair_i: usize,
6768

6869
verifier_srs: Arc<VerifierSRS>,
6970
block_verifier_index: Arc<VerifierIndex>,
@@ -102,6 +103,7 @@ impl Cluster {
102103
ocaml_nodes: Vec::new(),
103104

104105
rpc_counter: 0,
106+
ocaml_libp2p_keypair_i: 0,
105107

106108
verifier_srs: VERIFIER_SRS.clone(),
107109
block_verifier_index: BLOCK_VERIFIER_INDEX.clone(),
@@ -322,15 +324,17 @@ impl Cluster {
322324
let node = OcamlNode::start(OcamlNodeConfig {
323325
executable: self.config.ocaml_node_executable().clone(),
324326
dir: temp_dir,
327+
libp2p_keypair_i: self.ocaml_libp2p_keypair_i,
325328
libp2p_port: next_port().unwrap(),
326329
graphql_port: next_port().unwrap(),
327330
client_port: next_port().unwrap(),
328331
initial_peers: testing_config.initial_peers,
329332
daemon_json: testing_config.daemon_json,
330-
daemon_json_update_timestamp: testing_config.daemon_json_update_timestamp,
331333
})
332334
.expect("failed to start ocaml node");
333335

336+
self.ocaml_libp2p_keypair_i += 1;
337+
334338
self.ocaml_nodes.push(Some(node));
335339
ClusterOcamlNodeId::new_unchecked(node_i)
336340
}

node/testing/src/node/ocaml/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use serde::{Deserialize, Serialize};
99
pub struct OcamlNodeTestingConfig {
1010
pub initial_peers: Vec<P2pConnectionOutgoingInitOpts>,
1111
pub daemon_json: DaemonJson,
12-
pub daemon_json_update_timestamp: bool,
1312
}
1413

1514
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -30,12 +29,12 @@ pub struct OcamlNodeConfig {
3029
/// Command for mina executable.
3130
pub executable: OcamlNodeExecutable,
3231
pub dir: temp_dir::TempDir,
32+
pub libp2p_keypair_i: usize,
3333
pub libp2p_port: u16,
3434
pub graphql_port: u16,
3535
pub client_port: u16,
3636
pub initial_peers: Vec<P2pConnectionOutgoingInitOpts>,
3737
pub daemon_json: DaemonJson,
38-
pub daemon_json_update_timestamp: bool,
3938
}
4039

4140
#[derive(Serialize, Deserialize, Debug, Clone)]

node/testing/src/node/ocaml/mod.rs

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,25 @@ impl OcamlNode {
5555
};
5656
let peer_id = peer_id.parse()?;
5757

58-
if config.daemon_json_update_timestamp {
59-
todo!()
60-
} else {
61-
match &config.daemon_json {
62-
DaemonJson::Custom(path) => {
63-
std::fs::copy(path, &daemon_json_path).map_err(|err| {
64-
anyhow::anyhow!(
65-
"failed to copy daemon_json from: '{}', to: '{}'; error: {}",
66-
path,
67-
daemon_json_path.display(),
68-
err
69-
)
70-
})?;
71-
}
72-
DaemonJson::InMem(json) => {
73-
std::fs::write(&daemon_json_path, json.to_string()).map_err(|err| {
74-
anyhow::anyhow!(
75-
"failed to write InMem daemon.json to {}; error: {}",
76-
daemon_json_path.display(),
77-
err
78-
)
79-
})?;
80-
}
58+
match &config.daemon_json {
59+
DaemonJson::Custom(path) => {
60+
std::fs::copy(path, &daemon_json_path).map_err(|err| {
61+
anyhow::anyhow!(
62+
"failed to copy daemon_json from: '{}', to: '{}'; error: {}",
63+
path,
64+
daemon_json_path.display(),
65+
err
66+
)
67+
})?;
68+
}
69+
DaemonJson::InMem(json) => {
70+
std::fs::write(&daemon_json_path, json.to_string()).map_err(|err| {
71+
anyhow::anyhow!(
72+
"failed to write InMem daemon.json to {}; error: {}",
73+
daemon_json_path.display(),
74+
err
75+
)
76+
})?;
8177
}
8278
}
8379

@@ -164,6 +160,48 @@ impl OcamlNode {
164160
}
165161

166162
const PRIVKEY_PATH: &'static str = ".libp2p/key";
163+
const LIBP2P_KEYS: [(&'static str, &'static str); 10] = [
164+
(
165+
"12D3KooWKG1ZakBYEirEWdFYSstTEvxzTxCyuyaebojKthiESfWi",
166+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"7fZJwAwzGgwFGipwjVBTiinHt5NfjTFjSnhk8rA","pwsalt":"9Gz996pkoUjJ8docbT7fJvJg16iL","pwdiff":[134217728,6],"ciphertext":"7oHFyFJd9kWTfH6R7GvbHD4WXBw5JQeiQYBGUpFhiPuiAyEgi1BVbczrzS6njWJ9FkRdNgZqwmSo23GPX4Zs27m3U66dJvyahendHCndG3Wu9wi8yaees78AQpbsU7JRa7U5DyCs9d34QLwpsgrGC2CqtDHJD3K3YncxVDjk4CCKeHseukZXUvkFToqY9CZRLHgYXR29hiB8JyTgoQ4maDDBdqBpFRb6Rjfb3LX8WEat6NpTjWi4A9uvNyDqk68a2aAo8ofjP811SBYxjZY3PMdD4hs5UAAZqbUNA"}"#,
167+
),
168+
(
169+
"12D3KooWQoSDqpenkrjCnKCCLnwEMRVRqUfZJXqD8Y8awEnBAJZX",
170+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"8psKzkQReBbhoWYriYSNGFVKsb7GMARxfSZLWDF","pwsalt":"9MdeSxJxE33e5sxYhnmPQNTDxbKj","pwdiff":[134217728,6],"ciphertext":"6YnXZjwJue344VEkLnFJ62VY9E2QxZPEtDoZSBXnNzJUEEK5MVjcC63GeM37kVXTVvoj8r9C9i4mUbTiwjpL9wg4NqxkcJpTMVBe6WDsYYrtt7S9o6p4xWAjm1hvbxXcsTzPN361amo2ZNCAuMGpCWQPnxeZ69bwLQkn4vKeGkdiUMdnziNfhKRaFcya4C1dNNoz8kAWFRexzZrjvSBymzZCvZPgof1mApyzcoWuYtAdENqbNURg2DBv53nLetmqA9zLTcDbXYE5hTgkVzMHa6qiia4xAhDbrax4r"}"#,
171+
),
172+
(
173+
"12D3KooWEHSJwkn5ZdYVAcULpb3D3k6U2K4sU2BFh5xhoig1J4NK",
174+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"84F8XwEf5k2yPqgC8NSTdf5wQEw7kmRgEdyss91","pwsalt":"BeSN6EXqWBCx9fndvnib5BYnNNdc","pwdiff":[134217728,6],"ciphertext":"7ECwoC7vK5QRKrPZvrBmaWrTKJiHKq9fCZr8YyVPgfHFS2VQ1BeKgQPvoc5JHNhy2Yju8PDDKzS3zHCdRSGaVRX49VrxjSU9wg4Fj7EWPGEs6VNCQahudCovGFd69iqS8WvcDSSw6acaEstSssTFYV3mUviDbkRA5HM9fUvE8SBkg9rHeeghCQ4qLK7cFRywCx9P6nronv5b5yy15xJDjAp6h7fwNxA7daXGa3E8dhtsE6FaPCtefkLBKJFXzuo3CMdRcBMZTt2XVGHS27rxtn1j5jeT9y34EMA5t"}"#,
175+
),
176+
(
177+
"12D3KooWA1qcHYLWKZ7EUBMh3KKWbwys1DhH35WmH4y96scpntmv",
178+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"6ifkqZVQ8MXHP5StG5umfJ2HhuSEpwxx33bRD4e","pwsalt":"9qREDkkiXgKV7VtryRcAkrzGY41p","pwdiff":[134217728,6],"ciphertext":"6uYbsTw56Vzaj4Jb9vWVQLTNHk92Qv6kFgZrqit8gUBya1M76U4wuNqQ1XMk5iKFSXFxf9hjtKN1NAbruUTnoySaCdCuLfjVJwUpDJFWwjRV3vmFjmZch9YjAL4H81z89V52BZkGpSantUxqGMLSrJjz8z5Rrr3C7ZCvooZddFieFkFfDtLBfhAsB3U2usMh83L6VMPg5Hawn7krdznSVzagiS1ENDPR92LfsCvxVVGxSNvUbBbjFCdroDb7eYi8mshiC3nGQWDYRQu9kQkHC6xsgarTsumkXnYpd"}"#,
179+
),
180+
(
181+
"12D3KooWC14WLzaCT6fkR5WzzrayXDhmBpox7yWkif7Pg6Sk4uz1",
182+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"7rGH8gCM5UL9LvatyUP6Lka8b4Ev2o5Mbe5ou7L","pwsalt":"BKcQuXkYx6H4xfcLLGM8ykJP5a3D","pwdiff":[134217728,6],"ciphertext":"7tZEvuCoXyXRVxh9NNrMCRLXgXj8MHxNgwoesztLQTagMzKhf48EepzUReYEViNC2EpWb2h7yoJdXMUbDGUuSQdoM1eF3qum9rHtmU4xdv8SGBEP9q9YHb1n8YS2SEr7WNcN3DsX7cqrzfnSjDsXNZaGzR5CbrK7g3NGv8RVyT1uZF2VHHeapDY6nFCyKN9nUJKpizbbRguR25QwWyx2nzcKF3mGq2iuCNVN5Z6gqzk7fhD8XFayxj57MqwvWTRD6pLRBJcmqCF5L9ZqpKEHAjXMcv3nwkKBHJaAF"}"#,
183+
),
184+
(
185+
"12D3KooWRJEo19dU5eWgab1YrBPnK9HQA4SqDeQwx9NrankTcfSi",
186+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"6VMHa83xqvbDzAppmfe9B8wn7dzzdAC1fyP5bXs","pwsalt":"9cXcTqJGi651tpA1ZBKSPVBP2h3q","pwdiff":[134217728,6],"ciphertext":"783EDXabmg2PmWhrSqcDog82NhWNMWasKC4o2d1oxVDTDxhmH5yGcjY74wV8HH16DpJw4ZxzW8gUCyC7Mhx1hEG8kc7wn38yBsoAqGfkA34g9n4FYJzwHvAB7on7zK3cveh2jXF3TTt3Etg4advpM7LvbY2eE9pz95TU1pCagu7haB83JHn2qSnfSCcMUTjS9copfJgkVD6YQYUxJmVi9erYUufjJqiF9p4ciCSuicU5SaJVB3rpSaRt1VgWMXeg47qWXVg98byNadHi8PgQZNnFifJc4FUDPtHDj"}"#,
187+
),
188+
(
189+
"12D3KooWPTtAt3LXFqs8vbGL9VACn5wgcBm1jvaHZTGHnHzFq7c4",
190+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"8q2jNrxkiwzvk8LWuU8zWy93APjitkaZUKr3fDP","pwsalt":"B5YVY1p2yFfGRsjs2us7WnmhWqPq","pwdiff":[134217728,6],"ciphertext":"7xxCXkAf3DokjZQ6CtwbuuXMBeYB5p6KxQAqaFLx96yBAsqQaK3EprR3xDKVR78x7Zrzj4NXWrFow2cg4xtze12SS43t46E3QhSsYPohcuZzKJe4agGJMDZVHaqd1aAPtJd2CX1fZCrWmxpa3hB72H2EKYPFSG1FYv77cYxU45aJx3V1XQAEQtoYKP9FmL95xogJHVHQSe2xWrvga8CLY4qtshJWkwHP1mV59xam9WhhZZjZkSThYTJMW9f4NTQ2EwRuud9zReLkh8fGEvfoxFjMsw8NCVxrtdTi5"}"#,
191+
),
192+
(
193+
"12D3KooWLrjE3v7wZSCT4HsnXYmRsQnCWsaGcmU9mSJfPDRyjvpd",
194+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"7zKo7kmPLjoJdvgxyMp2jguDnGDnyZ49TWfajiZ","pwsalt":"ARyFY4GSnyTC8ZjhvEaCSq3QeGsU","pwdiff":[134217728,6],"ciphertext":"7FfzcAPsEM7Lv5JzL7rYqgrHGV5bBDVVTEukULpDaCMGbHJDRFkgjxx6c6gbxCRbMJKma9yMrr5zxHsT9tfYCU7PAzqgVqa87TfphNXmqdSrNKWZVTS5SGMXAqku6vfJ19PA7TzJdr7oHZSjoim5Lh8r2x9iUTto7tdCBy4xWJQXE7aQYQ2ybB95DjzA3CtK7ypjxnZJDnvYq8zgXx9netbAX3NdTtsRDKQwNmjBzoQiKWd5jrqMigfFNcRTnJdpEn8jYMfa4fmXsnUe9ziXvjYdH9DJQA1354UQq"}"#,
195+
),
196+
(
197+
"12D3KooWKPaZfwU42A9SDxyuDGWsLYiedudJvDAHY81UMXLLEgTe",
198+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"7QAvnHnfp1MMJgEYN8VVwLuwWF3GEMUG5DA2KWP","pwsalt":"9YP8yrJRfF6w4efUSTZrPRhoV8xZ","pwdiff":[134217728,6],"ciphertext":"6i8Efh1z9AZi2ExYLFBLsPGSAXQT72pgQNP9yPBfDwqr8dtQxvqdh6V8qXwjyVSiWLGs6zbUFQj1mJ17F9irLzUTFLQ9WG98HveuUJLxv2WPoEekb2AUntAYNkbgVEmtWEYytWqYZnJUx5g1cnLo5ENzrcDTDcbHbQnYVkyH2GNpNrH4WqjK3TyDS2PzKwTwBFVMyhK4BUDVtjJWxfHnQNSaxT7gsGFNQTJjizxpqaqFk7Nc11GPaJmJFJKoVYD4ozCkeR2RKM9Dk7ct49vTQvULqqETwJmK1yMTf"}"#,
199+
),
200+
(
201+
"12D3KooWKrWvpCzTJs45HS8c8Hbo9sfe65wBVCLWW2gyJDmnDDif",
202+
r#"{"box_primitive":"xsalsa20poly1305","pw_primitive":"argon2i","nonce":"8QEpCE7a48EdYXQ44ff9PXWDKw2AMmHMxc3nUaq","pwsalt":"8UUffeMbTFAwUwkamE1xWoVTJqDi","pwdiff":[134217728,6],"ciphertext":"93Wdv4Xq9794GWuZPZUPug2gfPpXD2dnLxSx9jGjtr7rTGv14W7JPieGJJ4zTw6T54x1NwyhH9HcDwsQxmUT364KibpbczuA9bnTFcp6ahoYpetrHB8FJTk7TprkmazqprJm7QDqJ97jyE7PuVNWg9NSbMRzet1c5Jxk2qfUYVdtSNgcQB5J5oUTibL6fc5UKZmfBoSixw3E3QFPnBRN8W7X3nfcHykK9eck2u5YJrv1gRYoupp2EX1CjWwKp3ebDa9bLLZiWTTSBKsj7uhLh5aCxgHpoPCyNcnaq"}"#,
203+
),
204+
];
167205

168206
fn privkey_path(dir: &Path) -> PathBuf {
169207
dir.join(Self::PRIVKEY_PATH)
@@ -178,18 +216,31 @@ impl OcamlNode {
178216
}
179217

180218
fn generate_libp2p_keypair(config: &OcamlNodeConfig, dir: &Path) -> anyhow::Result<String> {
181-
let mut child = config
182-
.cmd([("MINA_LIBP2P_PASS", ""), ("UMASK", "0700")])
183-
.args(["libp2p", "generate-keypair", "--privkey-path"])
184-
.arg(Self::privkey_path(dir))
185-
.spawn()?;
186-
if child.wait()?.success() {
187-
config.executable.kill(&config.dir);
188-
let peer_id = Self::read_peer_id(dir)?;
189-
Ok(peer_id)
190-
} else {
191-
anyhow::bail!("error generating keypair");
192-
}
219+
use std::{
220+
fs::OpenOptions,
221+
io::Write,
222+
os::unix::fs::{DirBuilderExt, OpenOptionsExt},
223+
};
224+
225+
let (peer_id, key) = Self::LIBP2P_KEYS[config.libp2p_keypair_i];
226+
let privkey_path = Self::privkey_path(dir);
227+
let privkey_parent_dir = privkey_path.as_path().parent().unwrap();
228+
std::fs::DirBuilder::new()
229+
.recursive(true)
230+
.mode(0o700)
231+
.create(privkey_parent_dir)?;
232+
233+
let mut file = OpenOptions::new()
234+
.create(true)
235+
.write(true)
236+
.mode(0o600)
237+
.open(&privkey_path)?;
238+
file.write_all(key.as_bytes())?;
239+
std::fs::write(
240+
privkey_path.with_extension("peerid"),
241+
format!("peerid:{peer_id}"),
242+
)?;
243+
Ok(peer_id.to_owned())
193244
}
194245

195246
fn read_stream<R: std::io::Read, W: std::io::Write>(
@@ -316,12 +367,12 @@ fn run_ocaml() {
316367
let mut node = OcamlNode::start(OcamlNodeConfig {
317368
executable: OcamlNodeExecutable::find_working().unwrap(),
318369
dir: temp_dir::TempDir::new().unwrap(),
370+
libp2p_keypair_i: 0,
319371
libp2p_port: 8302,
320372
graphql_port: 3086,
321373
client_port: 8301,
322374
initial_peers: Vec::new(),
323375
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
324-
daemon_json_update_timestamp: false,
325376
})
326377
.unwrap();
327378
let stdout = node.child.stdout.take().unwrap();

node/testing/src/scenarios/multi_node/basic_connectivity_peer_discovery.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ impl MultiNodeBasicConnectivityPeerDiscovery {
3232
let ocaml_seed_config = OcamlNodeTestingConfig {
3333
initial_peers: Vec::new(),
3434
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
35-
daemon_json_update_timestamp: false,
3635
};
3736

3837
let seed_a = runner.add_ocaml_node(ocaml_seed_config.clone());

node/testing/src/scenarios/multi_node/connection_discovery.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ impl RustNodeAsSeed {
3030
let ocaml_node_config = OcamlNodeTestingConfig {
3131
initial_peers: vec![rust_node_dial_addr],
3232
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
33-
daemon_json_update_timestamp: false,
3433
};
3534

3635
let ocaml_node0 = runner.add_ocaml_node(ocaml_node_config.clone());
@@ -127,7 +126,6 @@ impl OCamlToRust {
127126
let ocaml_node_config = OcamlNodeTestingConfig {
128127
initial_peers: vec![rust_node_dial_addr],
129128
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
130-
daemon_json_update_timestamp: false,
131129
};
132130

133131
let ocaml_node = runner.add_ocaml_node(ocaml_node_config.clone());
@@ -187,7 +185,6 @@ impl RustToOCaml {
187185
let ocaml_seed_config = OcamlNodeTestingConfig {
188186
initial_peers: Vec::new(),
189187
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
190-
daemon_json_update_timestamp: false,
191188
};
192189

193190
let seed_node = runner.add_ocaml_node(ocaml_seed_config);
@@ -264,7 +261,6 @@ impl OCamlToRustViaSeed {
264261
let ocaml_seed_config = OcamlNodeTestingConfig {
265262
initial_peers: Vec::new(),
266263
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
267-
daemon_json_update_timestamp: false,
268264
};
269265

270266
let seed_node = runner.add_ocaml_node(ocaml_seed_config.clone());
@@ -370,7 +366,6 @@ impl RustToOCamlViaSeed {
370366
let ocaml_seed_config = OcamlNodeTestingConfig {
371367
initial_peers: Vec::new(),
372368
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
373-
daemon_json_update_timestamp: false,
374369
};
375370

376371
let seed_node = runner.add_ocaml_node(ocaml_seed_config.clone());

node/testing/src/scenarios/solo_node/basic_connectivity_accept_incoming.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ impl SoloNodeBasicConnectivityAcceptIncoming {
115115
let node_id = runner.add_ocaml_node(OcamlNodeTestingConfig {
116116
initial_peers: vec![node_addr.clone()],
117117
daemon_json: DaemonJson::Custom("/var/lib/coda/berkeley.json".to_owned()),
118-
daemon_json_update_timestamp: false,
119118
});
120119
let node = runner.ocaml_node(node_id).unwrap();
121120
eprintln!("launching OCaml node {}", node.dial_addr());

node/testing/src/scenarios/solo_node/sync_to_genesis.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ impl SoloNodeSyncToGenesis {
2727
let ocaml_node_config = OcamlNodeTestingConfig {
2828
initial_peers: Vec::new(),
2929
daemon_json: runner.daemon_json_gen_with_counts("2023-12-25T09:00:00Z", 2, 2),
30-
daemon_json_update_timestamp: false,
3130
};
3231

3332
let ocaml_node = runner.add_ocaml_node(ocaml_node_config);

node/testing/src/simulator/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ impl Simulator {
7979
let ocaml_node_config = OcamlNodeTestingConfig {
8080
initial_peers: Vec::new(),
8181
daemon_json: runner.daemon_json_gen_with_counts("2023-12-25T09:00:00Z", 2, 2),
82-
daemon_json_update_timestamp: false,
8382
};
8483

8584
let ocaml_node = runner.add_ocaml_node(ocaml_node_config);

0 commit comments

Comments
 (0)