Skip to content

Commit 002e5d0

Browse files
committed
feat(testing): record addition of rust/ocaml nodes in scenario as steps
re: #188
1 parent 0cdfe1d commit 002e5d0

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

node/testing/src/cluster/mod.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use openmina_node_native::{http_server, rpc::RpcService, NodeService, RpcSender}
3838
use rand::{rngs::StdRng, SeedableRng};
3939
use serde::Serialize;
4040

41-
use crate::node::{OcamlStep, TestPeerId};
41+
use crate::node::{DaemonJson, OcamlStep, TestPeerId};
4242
use crate::{
4343
network_debugger::Debugger,
4444
node::{
@@ -385,6 +385,14 @@ impl Cluster {
385385
.map(|(i, node)| (ClusterNodeId::new_unchecked(i), node))
386386
}
387387

388+
pub fn ocaml_nodes_iter(&self) -> impl Iterator<Item = (ClusterOcamlNodeId, &OcamlNode)> {
389+
self.ocaml_nodes
390+
.iter()
391+
.enumerate()
392+
.filter_map(|(i, node)| node.as_ref().map(|node| (i, node)))
393+
.map(|(i, node)| (ClusterOcamlNodeId::new_unchecked(i), node))
394+
}
395+
388396
pub fn node(&self, node_id: ClusterNodeId) -> Option<&Node> {
389397
self.nodes.get(node_id.index())
390398
}
@@ -545,6 +553,45 @@ impl Cluster {
545553
}
546554
}
547555
}
556+
ScenarioStep::AddNode { config } => match config {
557+
NodeTestingConfig::Rust(config) => {
558+
self.add_rust_node(config);
559+
true
560+
}
561+
NodeTestingConfig::Ocaml(config) => {
562+
// before starting ocaml node, read and save secret
563+
// keys from daemon.json.
564+
let mut json_owned = None;
565+
let json = match &config.daemon_json {
566+
DaemonJson::Custom(path) => {
567+
let bytes = tokio::fs::read(path).await.map_err(|err| {
568+
anyhow::anyhow!(
569+
"error reading daemon.json from path({path}): {err}"
570+
)
571+
})?;
572+
let json = serde_json::from_slice(&bytes).map_err(|err| {
573+
anyhow::anyhow!(
574+
"failed to parse damon.json from path({path}): {err}"
575+
)
576+
})?;
577+
json_owned.insert(json)
578+
}
579+
DaemonJson::InMem(json) => json,
580+
};
581+
let accounts = json["ledger"]["accounts"].as_array().ok_or_else(|| {
582+
anyhow::anyhow!("daemon.json `.ledger.accounts` is not array")
583+
})?;
584+
585+
accounts
586+
.iter()
587+
.filter_map(|account| account["sk"].as_str())
588+
.filter_map(|sk| sk.parse().ok())
589+
.for_each(|sk| self.add_account_sec_key(sk));
590+
591+
self.add_ocaml_node(config);
592+
true
593+
}
594+
},
548595
ScenarioStep::ConnectNodes { dialer, listener } => {
549596
let listener_addr = match listener {
550597
ListenerNode::Rust(listener) => {

node/testing/src/node/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use super::ocaml::{
55
};
66
pub use super::rust::{RustNodeBlockProducerTestingConfig, RustNodeTestingConfig, TestPeerId};
77

8-
#[derive(Serialize, Deserialize, Debug, Clone)]
8+
#[derive(Serialize, Deserialize, derive_more::From, Debug, Clone)]
99
#[serde(tag = "kind")]
1010
pub enum NodeTestingConfig {
1111
Rust(RustNodeTestingConfig),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ impl DaemonJson {
211211
let sec_key = AccountSecretKey::rand();
212212
let pub_key = sec_key.public_key();
213213
let account = serde_json::json!({
214+
"sk": sec_key.to_string(),
214215
"pk": pub_key.to_string(),
215216
"balance": format!("{balance}.000000000"),
216217
"delegate": pub_key.to_string(),

node/testing/src/scenario/step.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use node::{event_source::Event, p2p::connection::outgoing::P2pConnectionOutgoing
22
use serde::{Deserialize, Serialize};
33

44
use crate::cluster::{ClusterNodeId, ClusterOcamlNodeId};
5-
use crate::node::OcamlStep;
5+
use crate::node::{NodeTestingConfig, OcamlStep};
66

77
#[derive(Serialize, Deserialize, Debug, Clone)]
88
#[serde(tag = "kind")]
@@ -19,6 +19,10 @@ pub enum ScenarioStep {
1919
node_id: ClusterNodeId,
2020
event: String,
2121
},
22+
/// Create a new node, start it and add it to the cluster.
23+
AddNode {
24+
config: NodeTestingConfig,
25+
},
2226
ConnectNodes {
2327
dialer: ClusterNodeId,
2428
listener: ListenerNode,

node/testing/src/scenarios/cluster_runner.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
cluster::{Cluster, ClusterNodeId, ClusterOcamlNodeId},
1515
network_debugger::Debugger,
1616
node::{
17-
DaemonJson, DaemonJsonGenConfig, Node, OcamlNode, OcamlNodeTestingConfig,
18-
RustNodeTestingConfig,
17+
DaemonJson, DaemonJsonGenConfig, Node, NodeTestingConfig, OcamlNode,
18+
OcamlNodeTestingConfig, RustNodeTestingConfig,
1919
},
2020
scenario::ScenarioStep,
2121
service::{DynEffects, NodeTestingService, PendingEventId},
@@ -67,6 +67,10 @@ impl<'a> ClusterRunner<'a> {
6767
self.cluster.nodes_iter()
6868
}
6969

70+
pub fn ocaml_nodes_iter(&self) -> impl Iterator<Item = (ClusterOcamlNodeId, &OcamlNode)> {
71+
self.cluster.ocaml_nodes_iter()
72+
}
73+
7074
pub fn daemon_json_gen(
7175
&mut self,
7276
genesis_timestamp: &str,
@@ -98,11 +102,33 @@ impl<'a> ClusterRunner<'a> {
98102
}
99103

100104
pub fn add_rust_node(&mut self, testing_config: RustNodeTestingConfig) -> ClusterNodeId {
101-
self.cluster.add_rust_node(testing_config)
105+
let step = ScenarioStep::AddNode {
106+
config: testing_config.into(),
107+
};
108+
(self.add_step)(&step);
109+
let ScenarioStep::AddNode {
110+
config: NodeTestingConfig::Rust(config),
111+
} = step
112+
else {
113+
unreachable!()
114+
};
115+
116+
self.cluster.add_rust_node(config)
102117
}
103118

104119
pub fn add_ocaml_node(&mut self, testing_config: OcamlNodeTestingConfig) -> ClusterOcamlNodeId {
105-
self.cluster.add_ocaml_node(testing_config)
120+
let step = ScenarioStep::AddNode {
121+
config: testing_config.into(),
122+
};
123+
(self.add_step)(&step);
124+
let ScenarioStep::AddNode {
125+
config: NodeTestingConfig::Ocaml(config),
126+
} = step
127+
else {
128+
unreachable!()
129+
};
130+
131+
self.cluster.add_ocaml_node(config)
106132
}
107133

108134
pub async fn exec_step(&mut self, step: ScenarioStep) -> anyhow::Result<bool> {

0 commit comments

Comments
 (0)