Skip to content

Commit 6716819

Browse files
committed
Use the sdout as a source for chainspec file
1 parent 636ea38 commit 6716819

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

crates/node/src/kitchensink.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
2-
fs,
32
fs::create_dir_all,
4-
io::{BufRead, Read},
3+
io::{BufRead},
54
path::PathBuf,
65
process::{Child, Command, Stdio},
76
sync::atomic::{AtomicU32, Ordering},
@@ -56,26 +55,25 @@ impl KitchensinkNode {
5655

5756
const PROXY_LOG_ENV: &'static str = "info,eth-rpc=debug";
5857

59-
fn init(&mut self, genesis_path: &str) -> anyhow::Result<&mut Self> {
58+
fn init(&mut self, genesis: &str) -> anyhow::Result<&mut Self> {
6059
create_dir_all(&self.base_directory)?;
6160

6261
let template_chainspec_path = self.base_directory.join(Self::CHAIN_SPEC_JSON_FILE);
6362

64-
let status = Command::new(&self.substrate_binary)
63+
let output = Command::new(&self.substrate_binary)
6564
.arg("export-chain-spec")
6665
.arg("--chain")
6766
.arg("dev")
68-
.arg("--output")
69-
.arg(&template_chainspec_path)
70-
.status()?;
67+
.output()?;
7168

72-
if !status.success() {
73-
anyhow::bail!("substrate-node export-chain-spec failed");
69+
if !output.status.success() {
70+
anyhow::bail!(
71+
"substrate-node export-chain-spec failed: {}",
72+
String::from_utf8_lossy(&output.stderr)
73+
);
7474
}
7575

76-
let mut file = std::fs::File::open(&template_chainspec_path)?;
77-
let mut content = String::new();
78-
file.read_to_string(&mut content)?;
76+
let content = String::from_utf8(output.stdout)?;
7977
let mut chainspec_json: JsonValue = serde_json::from_str(&content)?;
8078

8179
let existing_chainspec_balances =
@@ -97,7 +95,7 @@ impl KitchensinkNode {
9795
None
9896
})
9997
.collect();
100-
let mut eth_balances = self.extract_balance_from_genesis_file(genesis_path)?;
98+
let mut eth_balances = self.extract_balance_from_genesis_file(genesis)?;
10199
merged_balances.append(&mut eth_balances);
102100

103101
chainspec_json["genesis"]["runtimeGenesis"]["patch"]["balances"]["balances"] =
@@ -170,9 +168,8 @@ impl KitchensinkNode {
170168

171169
fn extract_balance_from_genesis_file(
172170
&self,
173-
genesis_path: &str,
171+
genesis_str: &str,
174172
) -> anyhow::Result<Vec<(String, u128)>> {
175-
let genesis_str = fs::read_to_string(genesis_path)?;
176173
let genesis_json: JsonValue = serde_json::from_str(&genesis_str)?;
177174
let alloc = genesis_json
178175
.get("alloc")
@@ -364,7 +361,6 @@ mod tests {
364361
use temp_dir::TempDir;
365362

366363
use std::fs;
367-
use tempfile::tempdir;
368364

369365
use super::KitchensinkNode;
370366
use crate::{GENESIS_JSON, Node};
@@ -383,8 +379,6 @@ mod tests {
383379

384380
#[test]
385381
fn test_init_generates_chainspec_with_balances() {
386-
// Setup: Write a minimal Geth-style genesis.json
387-
let test_genesis_path = std::env::temp_dir().join("test_genesis_file.json");
388382
let genesis_content = r#"
389383
{
390384
"alloc": {
@@ -397,12 +391,12 @@ mod tests {
397391
}
398392
}
399393
"#;
400-
fs::write(&test_genesis_path, genesis_content).expect("Failed to write test genesis");
394+
401395
let mut dummy_node = KitchensinkNode::new(&test_config().0);
402396

403397
// Call `init()`
404398
dummy_node
405-
.init(test_genesis_path.to_str().unwrap())
399+
.init(genesis_content)
406400
.expect("init failed");
407401

408402
// Check that the patched chainspec file was generated
@@ -433,8 +427,6 @@ mod tests {
433427

434428
#[test]
435429
fn test_parse_genesis_alloc() {
436-
let temp_dir = tempdir().unwrap();
437-
let genesis_path = temp_dir.path().join("test_genesis_file_eth.json");
438430

439431
// Create test genesis file
440432
let genesis_json = r#"
@@ -447,12 +439,11 @@ mod tests {
447439
}
448440
"#;
449441

450-
fs::write(&genesis_path, genesis_json).unwrap();
451442

452443
let node = KitchensinkNode::new(&test_config().0);
453444

454445
let result = node
455-
.extract_balance_from_genesis_file(genesis_path.to_str().unwrap())
446+
.extract_balance_from_genesis_file(genesis_json)
456447
.unwrap();
457448

458449
let result_map: std::collections::HashMap<_, _> = result.into_iter().collect();
@@ -526,12 +517,8 @@ mod tests {
526517
fn spawn_works() {
527518
let (config, _temp_dir) = test_config();
528519

529-
// Write GENESIS_JSON to a temp file
530-
let genesis_path = std::env::temp_dir().join("test_genesis_eth.json");
531-
std::fs::write(&genesis_path, GENESIS_JSON).unwrap();
532-
533520
let mut node = KitchensinkNode::new(&config);
534-
node.spawn(genesis_path.to_str().unwrap().to_string())
521+
node.spawn(GENESIS_JSON.to_string())
535522
.unwrap();
536523
}
537524

0 commit comments

Comments
 (0)