feat: make geth executable configurable (#1947)

* feat: make geth executable configurable

* Update ethers-core/src/utils/geth.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

* Update ethers-core/src/utils/geth.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>
This commit is contained in:
Matthias Seitz 2022-12-18 12:33:40 +01:00 committed by GitHub
parent 279aea6323
commit e86183e935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 2 deletions

View File

@ -164,6 +164,7 @@ impl Default for PrivateNetOptions {
/// ``` /// ```
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Geth { pub struct Geth {
program: Option<PathBuf>,
port: Option<u16>, port: Option<u16>,
authrpc_port: Option<u16>, authrpc_port: Option<u16>,
ipc_path: Option<PathBuf>, ipc_path: Option<PathBuf>,
@ -180,6 +181,32 @@ impl Geth {
Self::default() Self::default()
} }
/// Creates a Geth builder which will execute `geth` at the given path.
///
/// # Example
///
/// ```
/// use ethers_core::utils::Geth;
/// # fn a() {
/// let geth = Geth::at("../go-ethereum/build/bin/geth").spawn();
///
/// println!("Geth running at `{}`", geth.endpoint());
/// # }
/// ```
pub fn at(path: impl Into<PathBuf>) -> Self {
Self::new().path(path)
}
/// Sets the `path` to the `geth` executable
///
/// By default, it's expected that `geth` is in `$PATH`, see also
/// [`std::process::Command::new()`]
#[must_use]
pub fn path<T: Into<PathBuf>>(mut self, path: T) -> Self {
self.program = Some(path.into());
self
}
/// Sets the port which will be used when the `geth-cli` instance is launched. /// Sets the port which will be used when the `geth-cli` instance is launched.
#[must_use] #[must_use]
pub fn port<T: Into<u16>>(mut self, port: T) -> Self { pub fn port<T: Into<u16>>(mut self, port: T) -> Self {
@ -274,9 +301,10 @@ impl Geth {
/// Consumes the builder and spawns `geth` with stdout redirected /// Consumes the builder and spawns `geth` with stdout redirected
/// to /dev/null. /// to /dev/null.
pub fn spawn(self) -> GethInstance { pub fn spawn(self) -> GethInstance {
let mut cmd = Command::new(GETH); let mut cmd =
if let Some(ref prg) = self.program { Command::new(prg) } else { Command::new(GETH) };
// geth uses stderr for its logs // geth uses stderr for its logs
cmd.stderr(std::process::Stdio::piped()); cmd.stderr(Stdio::piped());
let port = if let Some(port) = self.port { port } else { unused_port() }; let port = if let Some(port) = self.port { port } else { unused_port() };
let authrpc_port = if let Some(port) = self.authrpc_port { port } else { unused_port() }; let authrpc_port = if let Some(port) = self.authrpc_port { port } else { unused_port() };