Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat(core): expose genesis and private key in Geth #2091

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 83 additions & 3 deletions ethers-core/src/utils/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct GethInstance {
ipc: Option<PathBuf>,
data_dir: Option<PathBuf>,
p2p_port: Option<u16>,
genesis: Option<Genesis>,
clique_private_key: Option<SigningKey>,
}

impl GethInstance {
Expand Down Expand Up @@ -81,6 +83,16 @@ impl GethInstance {
&self.data_dir
}

/// Returns the genesis configuration used to conifugre this instance
pub fn genesis(&self) -> &Option<Genesis> {
&self.genesis
}

/// Returns the private key used to configure clique on this instance
pub fn clique_private_key(&self) -> &Option<SigningKey> {
&self.clique_private_key
}

/// Takes the stderr contained in the child process.
///
/// This leaves a `None` in its place, so calling methods that require a stderr to be present
Expand Down Expand Up @@ -186,9 +198,9 @@ pub struct Geth {
data_dir: Option<PathBuf>,
chain_id: Option<u64>,
insecure_unlock: bool,
pub genesis: Option<Genesis>,
genesis: Option<Genesis>,
mode: GethMode,
pub clique_private_key: Option<SigningKey>,
clique_private_key: Option<SigningKey>,
}

impl Geth {
Expand Down Expand Up @@ -515,7 +527,15 @@ impl Geth {

child.stderr = Some(reader.into_inner());

GethInstance { pid: child, port, ipc: self.ipc_path, data_dir: self.data_dir, p2p_port }
GethInstance {
pid: child,
port,
ipc: self.ipc_path,
data_dir: self.data_dir,
p2p_port,
genesis: self.genesis,
clique_private_key: self.clique_private_key,
}
}
}

Expand Down Expand Up @@ -569,4 +589,64 @@ mod tests {

assert!(p2p_port.is_none());
}

#[test]
fn clique_private_key_configured() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_path_buf();

let private_key = SigningKey::random(&mut rand::thread_rng());
let geth = Geth::new()
.set_clique_private_key(private_key)
.chain_id(1337u64)
.data_dir(temp_dir_path)
.spawn();

let clique_private_key = geth.clique_private_key().clone();

drop(geth);
temp_dir.close().unwrap();

assert!(clique_private_key.is_some());
}

#[test]
fn clique_genesis_configured() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_path_buf();

let private_key = SigningKey::random(&mut rand::thread_rng());
let geth = Geth::new()
.set_clique_private_key(private_key)
.chain_id(1337u64)
.data_dir(temp_dir_path)
.spawn();

let genesis = geth.genesis().clone();

drop(geth);
temp_dir.close().unwrap();

assert!(genesis.is_some());
}

#[test]
fn clique_p2p_configured() {
let temp_dir = tempfile::tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_path_buf();

let private_key = SigningKey::random(&mut rand::thread_rng());
let geth = Geth::new()
.set_clique_private_key(private_key)
.chain_id(1337u64)
.data_dir(temp_dir_path)
.spawn();

let p2p_port = geth.p2p_port();

drop(geth);
temp_dir.close().unwrap();

assert!(p2p_port.is_some());
}
}