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

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

 * expose the genesis and clique_private_key fields in GethInstance
 * add tests which initialize geth with clique_private_key, making sure
   the genesis and clique_private_key fields are exposed on the
   GethInstance

* add clique p2p port test

* refactor tests to clean up properly
This commit is contained in:
Dan Cline 2023-01-30 14:57:34 -05:00 committed by GitHub
parent 08514ea986
commit 9ebc5b378c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 83 additions and 3 deletions

View File

@ -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 {
@ -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
@ -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 {
@ -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,
}
}
}
@ -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());
}
}