gear_node_wrapper/
node.rs1use crate::{Log, NodeInstance, utils};
6use anyhow::Result;
7use std::{
8 env,
9 path::Path,
10 process::{Command, Stdio},
11};
12
13const GEAR_BINARY: &str = "gear";
14const DEFAULT_ARGS: [&str; 4] = ["--dev", "--tmp", "--no-hardware-benchmarks", "--rpc-port"];
15
16pub struct Node {
18 command: Command,
20 port: Option<u16>,
22 logs: Option<usize>,
24}
25
26impl Node {
27 pub fn new() -> Result<Self> {
30 Self::from_path(which::which(GEAR_BINARY)?)
31 }
32
33 pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
35 Ok(Self {
36 command: Command::new(path.as_ref()),
37 port: None,
38 logs: None,
39 })
40 }
41
42 pub fn arg(&mut self, arg: &str) -> &mut Self {
46 self.command.arg(arg);
47 self
48 }
49
50 pub fn args(&mut self, args: &[&str]) -> &mut Self {
55 self.command.args(args);
56 self
57 }
58
59 pub fn rpc_port(&mut self, port: u16) -> &mut Self {
61 self.port = Some(port);
62 self
63 }
64
65 pub fn logs(&mut self, limit: usize) -> &mut Self {
69 self.logs = Some(limit);
70 self
71 }
72
73 pub fn spawn(&mut self) -> Result<NodeInstance> {
75 let port = self.port.unwrap_or(utils::pick()).to_string();
76 let mut args = DEFAULT_ARGS.to_vec();
77 args.push(&port);
78
79 let mut process = self
80 .command
81 .env(
82 "RUST_LOG",
83 env::var_os("GEAR_NODE_WRAPPER_LOG")
84 .or_else(|| env::var_os("RUST_LOG"))
85 .unwrap_or_default(),
86 )
87 .args(args)
88 .stderr(Stdio::piped())
89 .stdout(Stdio::piped())
90 .spawn()?;
91
92 let address = format!("{}:{port}", utils::LOCALHOST).parse()?;
93 let mut log = Log::new(self.logs);
94 log.spawn(&mut process)?;
95 Ok(NodeInstance {
96 address,
97 log,
98 process,
99 })
100 }
101}