Skip to content

Commit

Permalink
fix(node-bindings): reset child.stdout in AnvilInstance (#1920)
Browse files Browse the repository at this point in the history
* feat(`node-bindings`): expose `stdout` reader

* remove reader and reset child.stdout

* Update crates/node-bindings/src/nodes/anvil.rs

---------

Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
yash-atreya and DaniPopes authored Jan 15, 2025
1 parent 8a07df5 commit 70df381
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion crates/node-bindings/src/nodes/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Anvil {

let mut child = cmd.spawn().map_err(NodeError::SpawnError)?;

let stdout = child.stdout.take().ok_or(NodeError::NoStderr)?;
let stdout = child.stdout.take().ok_or(NodeError::NoStdout)?;

let start = Instant::now();
let mut reader = BufReader::new(stdout);
Expand Down Expand Up @@ -334,6 +334,8 @@ impl Anvil {
}
}

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

Ok(AnvilInstance {
child,
private_keys,
Expand Down
28 changes: 27 additions & 1 deletion crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ impl<N: Network> Provider<N> for RootProvider<N> {

#[cfg(test)]
mod tests {
use std::{str::FromStr, time::Duration};
use std::{io::Read, str::FromStr, time::Duration};

use super::*;
use crate::{builder, ProviderBuilder, WalletProvider};
Expand Down Expand Up @@ -1867,4 +1867,30 @@ mod tests {
let err = provider.send_transaction(tx).await.unwrap_err().to_string();
assert!(err.contains("missing properties: [(\"NonceManager\", [\"from\"])]"));
}

#[tokio::test]
async fn capture_anvil_logs() {
let mut anvil = Anvil::new().spawn();

let provider = ProviderBuilder::new().on_http(anvil.endpoint_url());

let tx = TransactionRequest::default()
.with_from(address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"))
.with_to(address!("70997970C51812dc3A010C7d01b50e0d17dc79C8"))
.value(U256::from(100));

let _ = provider.send_transaction(tx).await.unwrap().get_receipt().await.unwrap();

anvil.child_mut().kill().unwrap();

let mut output = String::new();
anvil.child_mut().stdout.take().unwrap().read_to_string(&mut output).unwrap();

assert_eq!(anvil.chain_id(), 31337);
assert_eq!(anvil.addresses().len(), 10);
assert_eq!(anvil.keys().len(), 10);

assert!(output.contains("eth_sendTransaction"));
assert!(output.contains("Block Number: 1"))
}
}

0 comments on commit 70df381

Please sign in to comment.