2020-06-22 13:42:34 +00:00
|
|
|
use crate::types::PrivateKey;
|
2020-06-10 12:21:16 +00:00
|
|
|
use std::{
|
2020-06-22 13:42:34 +00:00
|
|
|
io::{BufRead, BufReader},
|
|
|
|
net::TcpListener,
|
2020-06-10 12:21:16 +00:00
|
|
|
process::{Child, Command},
|
2020-06-22 13:42:34 +00:00
|
|
|
time::{Duration, Instant},
|
2020-06-10 12:21:16 +00:00
|
|
|
};
|
|
|
|
|
2020-06-22 13:42:34 +00:00
|
|
|
/// How long we will wait for ganache to indicate that it is ready.
|
|
|
|
const GANACHE_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
|
2020-05-30 14:11:51 +00:00
|
|
|
|
|
|
|
/// A ganache CLI instance. Will close the instance when dropped.
|
2020-06-10 07:33:51 +00:00
|
|
|
///
|
2020-06-20 13:55:07 +00:00
|
|
|
/// Construct this using [`Ganache`](crate::utils::Ganache)
|
2020-06-22 13:42:34 +00:00
|
|
|
pub struct GanacheInstance {
|
|
|
|
pid: Child,
|
|
|
|
private_keys: Vec<PrivateKey>,
|
|
|
|
port: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GanacheInstance {
|
|
|
|
/// Returns the private keys used to instantiate this instance
|
|
|
|
pub fn keys(&self) -> &[PrivateKey] {
|
|
|
|
&self.private_keys
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the port of this instance
|
|
|
|
pub fn port(&self) -> u16 {
|
|
|
|
self.port
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the HTTP endpoint of this instance
|
|
|
|
pub fn endpoint(&self) -> String {
|
|
|
|
format!("http://localhost:{}", self.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Websocket endpoint of this instance
|
|
|
|
pub fn ws_endpoint(&self) -> String {
|
|
|
|
format!("ws://localhost:{}", self.port)
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 14:11:51 +00:00
|
|
|
|
2020-06-10 07:33:51 +00:00
|
|
|
impl Drop for GanacheInstance {
|
2020-05-30 14:11:51 +00:00
|
|
|
fn drop(&mut self) {
|
2020-06-22 13:42:34 +00:00
|
|
|
let _ = self.pid.kill().expect("could not kill ganache");
|
2020-05-30 14:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder for launching `ganache-cli`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If `spawn` is called without `ganache-cli` being available in the user's $PATH
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2020-06-10 07:33:51 +00:00
|
|
|
/// ```no_run
|
2020-06-17 08:02:03 +00:00
|
|
|
/// use ethers::utils::Ganache;
|
2020-05-31 15:31:13 +00:00
|
|
|
///
|
2020-06-22 13:42:34 +00:00
|
|
|
/// let port = 8545u16;
|
2020-05-30 14:11:51 +00:00
|
|
|
/// let url = format!("http://localhost:{}", port).to_string();
|
|
|
|
///
|
2020-06-10 07:33:51 +00:00
|
|
|
/// let ganache = Ganache::new()
|
2020-05-30 14:11:51 +00:00
|
|
|
/// .port(port)
|
|
|
|
/// .mnemonic("abstract vacuum mammal awkward pudding scene penalty purchase dinner depart evoke puzzle")
|
|
|
|
/// .spawn();
|
|
|
|
///
|
|
|
|
/// drop(ganache); // this will kill the instance
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone, Default)]
|
2020-06-10 07:33:51 +00:00
|
|
|
pub struct Ganache {
|
2020-06-22 13:42:34 +00:00
|
|
|
port: Option<u16>,
|
2020-06-17 08:02:03 +00:00
|
|
|
block_time: Option<u64>,
|
2020-05-30 14:11:51 +00:00
|
|
|
mnemonic: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-06-10 07:33:51 +00:00
|
|
|
impl Ganache {
|
2020-05-30 14:11:51 +00:00
|
|
|
/// Creates an empty Ganache builder.
|
|
|
|
/// The default port is 8545. The mnemonic is chosen randomly.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the port which will be used when the `ganache-cli` instance is launched.
|
2020-06-22 13:42:34 +00:00
|
|
|
pub fn port<T: Into<u16>>(mut self, port: T) -> Self {
|
2020-05-30 14:11:51 +00:00
|
|
|
self.port = Some(port.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the mnemonic which will be used when the `ganache-cli` instance is launched.
|
|
|
|
pub fn mnemonic<T: Into<String>>(mut self, mnemonic: T) -> Self {
|
|
|
|
self.mnemonic = Some(mnemonic.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-06-17 08:02:03 +00:00
|
|
|
/// Sets the block-time which will be used when the `ganache-cli` instance is launched.
|
|
|
|
pub fn block_time<T: Into<u64>>(mut self, block_time: T) -> Self {
|
|
|
|
self.block_time = Some(block_time.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:11:51 +00:00
|
|
|
/// Consumes the builder and spawns `ganache-cli` with stdout redirected
|
|
|
|
/// to /dev/null. This takes ~2 seconds to execute as it blocks while
|
|
|
|
/// waiting for `ganache-cli` to launch.
|
2020-06-10 07:33:51 +00:00
|
|
|
pub fn spawn(self) -> GanacheInstance {
|
2020-05-30 14:11:51 +00:00
|
|
|
let mut cmd = Command::new("ganache-cli");
|
2020-06-22 13:42:34 +00:00
|
|
|
cmd.stdout(std::process::Stdio::piped());
|
|
|
|
let port = if let Some(port) = self.port {
|
|
|
|
port
|
|
|
|
} else {
|
|
|
|
unused_port()
|
|
|
|
};
|
|
|
|
cmd.arg("-p").arg(port.to_string());
|
2020-05-30 14:11:51 +00:00
|
|
|
|
|
|
|
if let Some(mnemonic) = self.mnemonic {
|
|
|
|
cmd.arg("-m").arg(mnemonic);
|
|
|
|
}
|
|
|
|
|
2020-06-17 08:02:03 +00:00
|
|
|
if let Some(block_time) = self.block_time {
|
|
|
|
cmd.arg("-b").arg(block_time.to_string());
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:42:34 +00:00
|
|
|
let mut child = cmd.spawn().expect("couldnt start ganache-cli");
|
|
|
|
|
|
|
|
let stdout = child
|
|
|
|
.stdout
|
|
|
|
.expect("Unable to get stdout for ganache child process");
|
|
|
|
|
|
|
|
let start = Instant::now();
|
|
|
|
let mut reader = BufReader::new(stdout);
|
|
|
|
|
|
|
|
let mut private_keys = Vec::new();
|
|
|
|
let mut is_private_key = false;
|
|
|
|
loop {
|
|
|
|
if start + Duration::from_millis(GANACHE_STARTUP_TIMEOUT_MILLIS) <= Instant::now() {
|
|
|
|
panic!("Timed out waiting for ganache to start. Is ganache-cli installed?")
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut line = String::new();
|
|
|
|
reader
|
|
|
|
.read_line(&mut line)
|
|
|
|
.expect("Failed to read line from ganache process");
|
|
|
|
if line.starts_with("Listening on") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if line.starts_with("Private Keys") {
|
|
|
|
is_private_key = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_private_key && line.starts_with('(') {
|
|
|
|
let key_str = &line[6..line.len() - 1];
|
|
|
|
let key: PrivateKey = key_str.parse().expect("did not get private key");
|
|
|
|
private_keys.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
child.stdout = Some(reader.into_inner());
|
2020-05-30 14:11:51 +00:00
|
|
|
|
2020-06-22 13:42:34 +00:00
|
|
|
GanacheInstance {
|
|
|
|
pid: child,
|
|
|
|
private_keys,
|
|
|
|
port,
|
|
|
|
}
|
2020-05-30 14:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 13:42:34 +00:00
|
|
|
|
|
|
|
/// A bit of hack to find an unused TCP port.
|
|
|
|
///
|
|
|
|
/// Does not guarantee that the given port is unused after the function exists, just that it was
|
|
|
|
/// unused before the function started (i.e., it does not reserve a port).
|
|
|
|
pub fn unused_port() -> u16 {
|
|
|
|
let listener = TcpListener::bind("127.0.0.1:0")
|
|
|
|
.expect("Failed to create TCP listener to find unused port");
|
|
|
|
|
|
|
|
let local_addr = listener
|
|
|
|
.local_addr()
|
|
|
|
.expect("Failed to read TCP listener local_addr to find unused port");
|
|
|
|
local_addr.port()
|
|
|
|
}
|