Skip to content

Commit

Permalink
fix(anvil): only include historic logs if from field set (#1861)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Jun 7, 2022
1 parent 25241a6 commit 07b3520
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,13 @@ impl EthApi {
/// Handler for ETH RPC call: `eth_newFilter`
pub async fn new_filter(&self, filter: Filter) -> Result<String> {
node_info!("eth_newFilter");
// all logs that are already available that match the filter
let historic = self.backend.logs(filter.clone()).await?;
// all logs that are already available that match the filter if the filter's block range is
// in the past
let historic = if filter.from_block.is_some() {
self.backend.logs(filter.clone()).await?
} else {
vec![]
};
let filter = EthFilter::Logs(Box::new(LogsFilter {
blocks: self.new_block_notifications(),
storage: self.storage_info(),
Expand Down
49 changes: 49 additions & 0 deletions anvil/tests/it/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ethers::{
prelude::{BlockNumber, Filter, FilterKind, Middleware, Signer, H256},
types::Log,
};
use futures::StreamExt;
use std::sync::Arc;

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -124,3 +125,51 @@ async fn can_install_filter() {

assert_eq!(all_logs.len(), num_logs + 1);
}

#[tokio::test(flavor = "multi_thread")]
async fn watch_events() {
let (_api, handle) = spawn(NodeConfig::test().with_port(next_port())).await;
let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(handle.http_provider(), wallet));

let contract = SimpleStorage::deploy(Arc::clone(&client), "initial value".to_string())
.unwrap()
.send()
.await
.unwrap();

// We spawn the event listener:
let event = contract.event::<ValueChanged>();
let mut stream = event.stream().await.unwrap();

// Also set up a subscription for the same thing
let ws = Arc::new(handle.ws_provider().await);
let contract2 = SimpleStorage::new(contract.address(), ws);
let event2 = contract2.event::<ValueChanged>();
let mut subscription = event2.subscribe().await.unwrap();

let mut subscription_meta = event2.subscribe().await.unwrap().with_meta();

let num_calls = 3u64;

// and we make a few calls
let num = client.get_block_number().await.unwrap();
for i in 0..num_calls {
let call = contract.method::<_, H256>("setValue", i.to_string()).unwrap().legacy();
let pending_tx = call.send().await.unwrap();
let _receipt = pending_tx.await.unwrap();
}

for i in 0..num_calls {
// unwrap the option of the stream, then unwrap the decoding result
let log = stream.next().await.unwrap().unwrap();
let log2 = subscription.next().await.unwrap().unwrap();
let (log3, meta) = subscription_meta.next().await.unwrap().unwrap();
assert_eq!(log.new_value, log3.new_value);
assert_eq!(log.new_value, log2.new_value);
assert_eq!(log.new_value, i.to_string());
assert_eq!(meta.block_number, num + i + 1);
let hash = client.get_block(num + i + 1).await.unwrap().unwrap().hash.unwrap();
assert_eq!(meta.block_hash, hash);
}
}

0 comments on commit 07b3520

Please sign in to comment.