alloy_node_bindings/nodes/
geth.rs1use crate::{
4 utils::{extract_endpoint, extract_value, unused_port},
5 NodeError, NODE_DIAL_LOOP_TIMEOUT, NODE_STARTUP_TIMEOUT,
6};
7use alloy_genesis::{CliqueConfig, Genesis};
8use alloy_primitives::Address;
9use k256::ecdsa::SigningKey;
10use std::{
11 ffi::OsString,
12 fs::{create_dir, File},
13 io::{BufRead, BufReader},
14 path::PathBuf,
15 process::{Child, ChildStderr, Command, Stdio},
16 time::Instant,
17};
18use tempfile::tempdir;
19use url::Url;
20
21const API: &str = "eth,net,web3,txpool,admin,personal,miner,debug";
23
24const GETH: &str = "geth";
26
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum NodeMode {
30 Dev(DevOptions),
32 NonDev(PrivateNetOptions),
34}
35
36impl Default for NodeMode {
37 fn default() -> Self {
38 Self::Dev(Default::default())
39 }
40}
41
42#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
44pub struct DevOptions {
45 pub block_time: Option<u64>,
47}
48
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub struct PrivateNetOptions {
52 pub p2p_port: Option<u16>,
54
55 pub discovery: bool,
57}
58
59impl Default for PrivateNetOptions {
60 fn default() -> Self {
61 Self { p2p_port: None, discovery: true }
62 }
63}
64
65#[derive(Debug)]
69pub struct GethInstance {
70 pid: Child,
71 host: String,
72 port: u16,
73 p2p_port: Option<u16>,
74 auth_port: Option<u16>,
75 ipc: Option<PathBuf>,
76 data_dir: Option<PathBuf>,
77 genesis: Option<Genesis>,
78 clique_private_key: Option<SigningKey>,
79}
80
81impl GethInstance {
82 pub fn host(&self) -> &str {
84 &self.host
85 }
86
87 pub const fn port(&self) -> u16 {
89 self.port
90 }
91
92 pub const fn p2p_port(&self) -> Option<u16> {
94 self.p2p_port
95 }
96
97 pub const fn auth_port(&self) -> Option<u16> {
99 self.auth_port
100 }
101
102 #[doc(alias = "http_endpoint")]
104 pub fn endpoint(&self) -> String {
105 format!("http://{}:{}", self.host, self.port)
106 }
107
108 pub fn ws_endpoint(&self) -> String {
110 format!("ws://{}:{}", self.host, self.port)
111 }
112
113 pub fn ipc_endpoint(&self) -> String {
115 self.ipc.clone().map_or_else(|| "geth.ipc".to_string(), |ipc| ipc.display().to_string())
116 }
117
118 #[doc(alias = "http_endpoint_url")]
120 pub fn endpoint_url(&self) -> Url {
121 Url::parse(&self.endpoint()).unwrap()
122 }
123
124 pub fn ws_endpoint_url(&self) -> Url {
126 Url::parse(&self.ws_endpoint()).unwrap()
127 }
128
129 pub const fn data_dir(&self) -> Option<&PathBuf> {
131 self.data_dir.as_ref()
132 }
133
134 pub const fn genesis(&self) -> Option<&Genesis> {
136 self.genesis.as_ref()
137 }
138
139 #[deprecated = "clique support was removed in geth >=1.14"]
141 pub const fn clique_private_key(&self) -> Option<&SigningKey> {
142 self.clique_private_key.as_ref()
143 }
144
145 pub fn stderr(&mut self) -> Result<ChildStderr, NodeError> {
150 self.pid.stderr.take().ok_or(NodeError::NoStderr)
151 }
152
153 pub fn wait_to_add_peer(&mut self, id: &str) -> Result<(), NodeError> {
157 let mut stderr = self.pid.stderr.as_mut().ok_or(NodeError::NoStderr)?;
158 let mut err_reader = BufReader::new(&mut stderr);
159 let mut line = String::new();
160 let start = Instant::now();
161
162 while start.elapsed() < NODE_DIAL_LOOP_TIMEOUT {
163 line.clear();
164 err_reader.read_line(&mut line).map_err(NodeError::ReadLineError)?;
165
166 let truncated_id = if id.len() > 16 { &id[..16] } else { id };
168 if line.contains("Adding p2p peer") && line.contains(truncated_id) {
169 return Ok(());
170 }
171 }
172 Err(NodeError::Timeout)
173 }
174}
175
176impl Drop for GethInstance {
177 fn drop(&mut self) {
178 self.pid.kill().expect("could not kill geth");
179 }
180}
181
182#[derive(Clone, Debug, Default)]
201#[must_use = "This Builder struct does nothing unless it is `spawn`ed"]
202pub struct Geth {
203 program: Option<PathBuf>,
204 host: Option<String>,
205 port: Option<u16>,
206 authrpc_port: Option<u16>,
207 ipc_path: Option<PathBuf>,
208 ipc_enabled: bool,
209 data_dir: Option<PathBuf>,
210 chain_id: Option<u64>,
211 insecure_unlock: bool,
212 keep_err: bool,
213 genesis: Option<Genesis>,
214 mode: NodeMode,
215 clique_private_key: Option<SigningKey>,
216 args: Vec<OsString>,
217}
218
219impl Geth {
220 pub fn new() -> Self {
222 Self::default()
223 }
224
225 pub fn at(path: impl Into<PathBuf>) -> Self {
238 Self::new().path(path)
239 }
240
241 pub fn path<T: Into<PathBuf>>(mut self, path: T) -> Self {
246 self.program = Some(path.into());
247 self
248 }
249
250 pub fn dev(mut self) -> Self {
252 self.mode = NodeMode::Dev(Default::default());
253 self
254 }
255
256 pub const fn is_clique(&self) -> bool {
258 self.clique_private_key.is_some()
259 }
260
261 pub fn clique_address(&self) -> Option<Address> {
263 self.clique_private_key.as_ref().map(|pk| Address::from_public_key(pk.verifying_key()))
264 }
265
266 #[deprecated = "clique support was removed in geth >=1.14"]
272 pub fn set_clique_private_key<T: Into<SigningKey>>(mut self, private_key: T) -> Self {
273 self.clique_private_key = Some(private_key.into());
274 self
275 }
276
277 pub fn port<T: Into<u16>>(mut self, port: T) -> Self {
282 self.port = Some(port.into());
283 self
284 }
285
286 pub fn host<T: Into<String>>(mut self, host: T) -> Self {
290 self.host = Some(host.into());
291 self
292 }
293
294 pub fn p2p_port(mut self, port: u16) -> Self {
299 match &mut self.mode {
300 NodeMode::Dev(_) => {
301 self.mode = NodeMode::NonDev(PrivateNetOptions {
302 p2p_port: Some(port),
303 ..Default::default()
304 })
305 }
306 NodeMode::NonDev(opts) => opts.p2p_port = Some(port),
307 }
308 self
309 }
310
311 pub const fn block_time(mut self, block_time: u64) -> Self {
316 self.mode = NodeMode::Dev(DevOptions { block_time: Some(block_time) });
317 self
318 }
319
320 pub const fn chain_id(mut self, chain_id: u64) -> Self {
322 self.chain_id = Some(chain_id);
323 self
324 }
325
326 pub const fn insecure_unlock(mut self) -> Self {
328 self.insecure_unlock = true;
329 self
330 }
331
332 pub const fn enable_ipc(mut self) -> Self {
334 self.ipc_enabled = true;
335 self
336 }
337
338 pub fn disable_discovery(mut self) -> Self {
343 self.inner_disable_discovery();
344 self
345 }
346
347 fn inner_disable_discovery(&mut self) {
348 match &mut self.mode {
349 NodeMode::Dev(_) => {
350 self.mode =
351 NodeMode::NonDev(PrivateNetOptions { discovery: false, ..Default::default() })
352 }
353 NodeMode::NonDev(opts) => opts.discovery = false,
354 }
355 }
356
357 pub fn ipc_path<T: Into<PathBuf>>(mut self, path: T) -> Self {
359 self.ipc_path = Some(path.into());
360 self
361 }
362
363 pub fn data_dir<T: Into<PathBuf>>(mut self, path: T) -> Self {
365 self.data_dir = Some(path.into());
366 self
367 }
368
369 pub fn genesis(mut self, genesis: Genesis) -> Self {
376 self.genesis = Some(genesis);
377 self
378 }
379
380 pub const fn authrpc_port(mut self, port: u16) -> Self {
382 self.authrpc_port = Some(port);
383 self
384 }
385
386 pub const fn keep_stderr(mut self) -> Self {
390 self.keep_err = true;
391 self
392 }
393
394 pub fn push_arg<T: Into<OsString>>(&mut self, arg: T) {
396 self.args.push(arg.into());
397 }
398
399 pub fn extend_args<I, S>(&mut self, args: I)
401 where
402 I: IntoIterator<Item = S>,
403 S: Into<OsString>,
404 {
405 for arg in args {
406 self.push_arg(arg);
407 }
408 }
409
410 pub fn arg<T: Into<OsString>>(mut self, arg: T) -> Self {
414 self.args.push(arg.into());
415 self
416 }
417
418 pub fn args<I, S>(mut self, args: I) -> Self
422 where
423 I: IntoIterator<Item = S>,
424 S: Into<OsString>,
425 {
426 for arg in args {
427 self = self.arg(arg);
428 }
429 self
430 }
431
432 #[track_caller]
438 pub fn spawn(self) -> GethInstance {
439 self.try_spawn().unwrap()
440 }
441
442 pub fn try_spawn(mut self) -> Result<GethInstance, NodeError> {
444 let bin_path = self
445 .program
446 .as_ref()
447 .map_or_else(|| GETH.as_ref(), |bin| bin.as_os_str())
448 .to_os_string();
449 let mut cmd = Command::new(&bin_path);
450 cmd.stderr(Stdio::piped());
452
453 let mut port = self.port.unwrap_or(0);
455 let port_s = port.to_string();
456
457 if !self.ipc_enabled {
459 cmd.arg("--ipcdisable");
460 }
461
462 cmd.arg("--http");
464 cmd.arg("--http.port").arg(&port_s);
465 cmd.arg("--http.api").arg(API);
466
467 if let Some(ref host) = self.host {
468 cmd.arg("--http.addr").arg(host);
469 }
470
471 cmd.arg("--ws");
473 cmd.arg("--ws.port").arg(port_s);
474 cmd.arg("--ws.api").arg(API);
475
476 if let Some(ref host) = self.host {
477 cmd.arg("--ws.addr").arg(host);
478 }
479
480 let is_clique = self.is_clique();
482 if self.insecure_unlock || is_clique {
483 cmd.arg("--allow-insecure-unlock");
484 }
485
486 if is_clique {
487 self.inner_disable_discovery();
488 }
489
490 let authrpc_port = self.authrpc_port.unwrap_or_else(&mut unused_port);
492 cmd.arg("--authrpc.port").arg(authrpc_port.to_string());
493
494 if is_clique {
496 let clique_addr = self.clique_address();
497 if let Some(genesis) = &mut self.genesis {
498 let clique_config = CliqueConfig { period: Some(0), epoch: Some(8) };
500 genesis.config.clique = Some(clique_config);
501
502 let clique_addr = clique_addr.ok_or_else(|| {
503 NodeError::CliqueAddressError(
504 "could not calculates the address of the Clique consensus address."
505 .to_string(),
506 )
507 })?;
508
509 let extra_data_bytes =
511 [&[0u8; 32][..], clique_addr.as_ref(), &[0u8; 65][..]].concat();
512 genesis.extra_data = extra_data_bytes.into();
513 }
514
515 let clique_addr = self.clique_address().ok_or_else(|| {
516 NodeError::CliqueAddressError(
517 "could not calculates the address of the Clique consensus address.".to_string(),
518 )
519 })?;
520
521 self.genesis = Some(Genesis::clique_genesis(
522 self.chain_id.ok_or(NodeError::ChainIdNotSet)?,
523 clique_addr,
524 ));
525
526 cmd.arg("--miner.etherbase").arg(format!("{clique_addr:?}"));
530 }
531
532 if let Some(genesis) = &self.genesis {
533 let temp_genesis_dir_path = tempdir().map_err(NodeError::CreateDirError)?.keep();
535
536 let temp_genesis_path = temp_genesis_dir_path.join("genesis.json");
538
539 let mut file = File::create(&temp_genesis_path).map_err(|_| {
541 NodeError::GenesisError("could not create genesis file".to_string())
542 })?;
543
544 serde_json::to_writer_pretty(&mut file, &genesis).map_err(|_| {
546 NodeError::GenesisError("could not write genesis to file".to_string())
547 })?;
548
549 let mut init_cmd = Command::new(bin_path);
550 if let Some(data_dir) = &self.data_dir {
551 init_cmd.arg("--datadir").arg(data_dir);
552 }
553
554 init_cmd.stderr(Stdio::null());
556
557 init_cmd.arg("init").arg(temp_genesis_path);
558 let res = init_cmd
559 .spawn()
560 .map_err(NodeError::SpawnError)?
561 .wait()
562 .map_err(NodeError::WaitError)?;
563 if !res.success() {
565 return Err(NodeError::InitError);
566 }
567
568 std::fs::remove_dir_all(temp_genesis_dir_path).map_err(|_| {
570 NodeError::GenesisError("could not remove genesis temp dir".to_string())
571 })?;
572 }
573
574 if let Some(data_dir) = &self.data_dir {
575 cmd.arg("--datadir").arg(data_dir);
576
577 if !data_dir.exists() {
579 create_dir(data_dir).map_err(NodeError::CreateDirError)?;
580 }
581 }
582
583 let mut p2p_port = match self.mode {
585 NodeMode::Dev(DevOptions { block_time }) => {
586 cmd.arg("--dev");
587 if let Some(block_time) = block_time {
588 cmd.arg("--dev.period").arg(block_time.to_string());
589 }
590 None
591 }
592 NodeMode::NonDev(PrivateNetOptions { p2p_port, discovery }) => {
593 let port = p2p_port.unwrap_or(0);
595 cmd.arg("--port").arg(port.to_string());
596
597 if !discovery {
599 cmd.arg("--nodiscover");
600 }
601 Some(port)
602 }
603 };
604
605 if let Some(chain_id) = self.chain_id {
606 cmd.arg("--networkid").arg(chain_id.to_string());
607 }
608
609 cmd.arg("--verbosity").arg("4");
611
612 if let Some(ipc) = &self.ipc_path {
613 cmd.arg("--ipcpath").arg(ipc);
614 }
615
616 cmd.args(self.args);
617
618 let mut child = cmd.spawn().map_err(NodeError::SpawnError)?;
619
620 let stderr = child.stderr.take().ok_or(NodeError::NoStderr)?;
621
622 let start = Instant::now();
623 let mut reader = BufReader::new(stderr);
624
625 let mut p2p_started = matches!(self.mode, NodeMode::Dev(_));
628 let mut ports_started = false;
629
630 loop {
631 if start + NODE_STARTUP_TIMEOUT <= Instant::now() {
632 let _ = child.kill();
633 return Err(NodeError::Timeout);
634 }
635
636 let mut line = String::with_capacity(120);
637 reader.read_line(&mut line).map_err(NodeError::ReadLineError)?;
638
639 if matches!(self.mode, NodeMode::NonDev(_)) && line.contains("Started P2P networking") {
640 p2p_started = true;
641 }
642
643 if !matches!(self.mode, NodeMode::Dev(_)) {
644 if line.contains("New local node record") {
646 if let Some(port) = extract_value("tcp=", &line) {
647 p2p_port = port.parse::<u16>().ok();
648 }
649 }
650 }
651
652 if line.contains("HTTP endpoint opened")
655 || (line.contains("HTTP server started") && !line.contains("auth=true"))
656 {
657 if let Some(addr) = extract_endpoint("endpoint=", &line) {
659 port = addr.port();
661 }
662
663 ports_started = true;
664 }
665
666 if line.contains("Fatal:") {
669 let _ = child.kill();
670 return Err(NodeError::Fatal(line));
671 }
672
673 if ports_started && p2p_started {
675 break;
676 }
677 }
678
679 if self.keep_err {
680 child.stderr = Some(reader.into_inner());
682 } else {
683 std::thread::spawn(move || {
687 let mut buf = String::new();
688 loop {
689 let _ = reader.read_line(&mut buf);
690 }
691 });
692 }
693
694 Ok(GethInstance {
695 pid: child,
696 host: self.host.unwrap_or_else(|| "localhost".to_string()),
697 port,
698 ipc: self.ipc_path,
699 data_dir: self.data_dir,
700 p2p_port,
701 auth_port: self.authrpc_port,
702 genesis: self.genesis,
703 clique_private_key: self.clique_private_key,
704 })
705 }
706}
707
708#[cfg(test)]
709mod tests {
710 use super::*;
711
712 #[test]
713 fn can_set_host() {
714 let geth = Geth::new().host("0.0.0.0").dev().try_spawn();
715 if let Ok(geth) = geth {
716 assert_eq!(geth.host(), "0.0.0.0");
717 assert!(geth.endpoint().starts_with("http://0.0.0.0:"));
718 assert!(geth.ws_endpoint().starts_with("ws://0.0.0.0:"));
719 }
720 }
721
722 #[test]
723 fn default_host_is_localhost() {
724 let geth = Geth::new().dev().try_spawn();
725 if let Ok(geth) = geth {
726 assert_eq!(geth.host(), "localhost");
727 assert!(geth.endpoint().starts_with("http://localhost:"));
728 assert!(geth.ws_endpoint().starts_with("ws://localhost:"));
729 }
730 }
731}