Skip to content

Commit

Permalink
Merge pull request #443 from primitivefinance/bug/filter_test_termina…
Browse files Browse the repository at this point in the history
…tion

Bug/filter test termination
  • Loading branch information
Autoparallel authored Aug 17, 2023
2 parents bb4b7af + d000d2c commit 8c0cee1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 111 deletions.
6 changes: 4 additions & 2 deletions arbiter-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arbiter-core"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

# Dependencies for the release build
Expand Down Expand Up @@ -38,4 +38,6 @@ log = "0.4.19"
hex = { version = "0.4.3", default-features = false }
anyhow = "1.0.71"
env_logger = "0.10.0"
test-log = "0.2.12"
test-log = "0.2.12"
futures = "0.3.28"
assert_matches = "1.5"
2 changes: 1 addition & 1 deletion arbiter-core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl Middleware for RevmMiddleware {
};
let filter = args.clone();
let mut hasher = Sha256::new();
hasher.update(serde_json::to_string(&args).map_err(|e| RevmMiddlewareError::Json(e))?);
hasher.update(serde_json::to_string(&args).map_err(RevmMiddlewareError::Json)?);
let hash = hasher.finalize();
let id = ethers::types::U256::from(ethers::types::H256::from_slice(&hash).as_bytes());
let (event_sender, event_receiver) =
Expand Down
221 changes: 113 additions & 108 deletions arbiter-core/src/tests/interaction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
use std::{
pin::Pin,
task::{Context, Poll},
};

use anyhow::Ok;
use assert_matches::assert_matches;
use futures::stream::Stream;

use super::*;
use crate::bindings::arbiter_math::ArbiterMath;

Expand Down Expand Up @@ -57,6 +66,17 @@ async fn transact() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn filter_id() -> Result<()> {
let (arbiter_token, _environment, client) = deploy_and_start().await.unwrap();
let filter_watcher_1 = client.watch(&Filter::default()).await?;
let filter_watcher_2 = client
.watch(&Filter::new().address(arbiter_token.address()))
.await?;
assert_ne!(filter_watcher_1.id, filter_watcher_2.id);
Ok(())
}

#[tokio::test]
async fn filter_watcher() -> Result<()> {
let (arbiter_token, _environment, client) = deploy_and_start().await.unwrap();
Expand Down Expand Up @@ -101,121 +121,106 @@ async fn filter_watcher() -> Result<()> {
Ok(())
}

// #[tokio::test]
// async fn filter_address() -> Result<()> {
// let (arbiter_token, _environment, client) =
// deploy_and_start().await.unwrap(); let mut default_watcher =
// client.watch(&Filter::default()).await?; let mut address_watcher = client
// .watch(&Filter::new().address(arbiter_token.address()))
// .await?;
#[tokio::test]
async fn filter_address() -> Result<()> {
let (arbiter_token, _environment, client) = deploy_and_start().await.unwrap();

let mut default_watcher = client.watch(&Filter::default()).await?;
let mut address_watcher = client
.watch(&Filter::new().address(arbiter_token.address()))
.await?;

// // Check that both watchers get this event
// let approval = arbiter_token.approve(
// client.default_sender().unwrap(),
// ethers::types::U256::from(TEST_APPROVAL_AMOUNT),
// );
// approval.send().await?.await?;
// let default_watcher_event = default_watcher.next().await.unwrap();
// let address_watcher_event = address_watcher.next().await.unwrap();
// assert!(!default_watcher_event.data.is_empty());
// assert!(!address_watcher_event.data.is_empty());
// assert_eq!(default_watcher_event, address_watcher_event);
// Check that both watchers get this event
let approval = arbiter_token.approve(
client.default_sender().unwrap(),
ethers::types::U256::from(TEST_APPROVAL_AMOUNT),
);
approval.send().await?.await?;
let default_watcher_event = default_watcher.next().await.unwrap();
let address_watcher_event = address_watcher.next().await.unwrap();
assert!(!default_watcher_event.data.is_empty());
assert!(!address_watcher_event.data.is_empty());
assert_eq!(default_watcher_event, address_watcher_event);

// Create a new token contract to check that the address watcher only gets
// events from the correct contract Check that only the default watcher gets
// this event
let arbiter_token2 = ArbiterToken::deploy(
client.clone(),
(
format!("new_{}", TEST_ARG_NAME),
format!("new_{}", TEST_ARG_SYMBOL),
TEST_ARG_DECIMALS,
),
)?
.send()
.await?;

// Sanity check that tokens have different addresses
assert_ne!(arbiter_token.address(), arbiter_token2.address());

let approval = arbiter_token2.approve(
client.default_sender().unwrap(),
ethers::types::U256::from(TEST_APPROVAL_AMOUNT),
);
approval.send().await?.await?;

// // Create a new token contract to check that the address watcher only
// gets // events from the correct contract Check that only the default
// watcher gets // this event
// let arbiter_token2 = ArbiterToken::deploy(
// client.clone(),
// (
// format!("new_{}", TEST_ARG_NAME),
// format!("new_{}", TEST_ARG_SYMBOL),
// TEST_ARG_DECIMALS,
// ),
// )?
// .send()
// .await?;
// let mint2 = arbiter_token2.mint(
// ethers::types::H160::from_str(TEST_MINT_TO)?,
// ethers::types::U256::from(TEST_MINT_AMOUNT),
// );
// mint2.send().await?.await?;
// let default_watcher_event = default_watcher.next().await.unwrap();
// assert!(!default_watcher_event.data.is_empty());
// println!("default_watcher_event: {:#?}", default_watcher_event);
// get the next event with the default_watcher
let event_two = default_watcher.next().await.unwrap();
assert!(!event_two.data.is_empty());

// // Use tokio::time::timeout to await the approval_watcher for a specific
// // duration The timeout is needed because the approval_watcher is a
// stream // that will never end when the test is passing
// let timeout_duration = tokio::time::Duration::from_secs(1); // Adjust the
// duration as needed let timeout = tokio::time::timeout(timeout_duration,
// address_watcher.next()); match timeout.await {
// Result::Ok(Some(_)) => {
// // Event received
// panic!("This means the test is failing! The filter did not
// work."); }
// Result::Ok(None) => {
// // Timeout occurred, no event received
// println!("Expected result. The filter worked.")
// }
// Err(_) => {
// // Timer error (shouldn't happen in normal conditions)
// panic!("Timer error!")
// }
// }
// Ok(())
// }
// check that the address_watcher has not received any events
let mut ctx = Context::from_waker(futures::task::noop_waker_ref());
let poll_result = Pin::new(&mut address_watcher).poll_next(&mut ctx);
match poll_result {
Poll::Ready(Some(_event)) => panic!("Event received unexpectedly!"),
Poll::Ready(None) => println!("Stream completed!"),
Poll::Pending => println!("No event ready yet, as expected."),
}
assert_matches!(poll_result, Poll::Pending);
Ok(())
}

// #[tokio::test]
// async fn filter_topics() -> Result<()> {
// let (arbiter_token, _environment, client) =
// deploy_and_start().await.unwrap(); let mut default_watcher =
// client.watch(&Filter::default()).await?; let mut approval_watcher =
// client .watch(&arbiter_token.approval_filter().filter)
// .await?;
#[tokio::test]
async fn filter_topics() -> Result<()> {
let (arbiter_token, _environment, client) = deploy_and_start().await.unwrap();
let mut default_watcher = client.watch(&Filter::default()).await?;
let mut approval_watcher = client
.watch(&arbiter_token.approval_filter().filter)
.await?;

// // Check that both watchers get this event
// let approval = arbiter_token.approve(
// client.default_sender().unwrap(),
// ethers::types::U256::from(TEST_APPROVAL_AMOUNT),
// );
// approval.send().await?.await?;
// let default_watcher_event = default_watcher.next().await.unwrap();
// let approval_watcher_event = approval_watcher.next().await.unwrap();
// assert!(!default_watcher_event.data.is_empty());
// assert!(!approval_watcher_event.data.is_empty());
// assert_eq!(default_watcher_event, approval_watcher_event);
// Check that both watchers get this event
let approval = arbiter_token.approve(
client.default_sender().unwrap(),
ethers::types::U256::from(TEST_APPROVAL_AMOUNT),
);
approval.send().await?.await?;
let default_watcher_event = default_watcher.next().await.unwrap();
let approval_watcher_event = approval_watcher.next().await.unwrap();
assert!(!default_watcher_event.data.is_empty());
assert!(!approval_watcher_event.data.is_empty());
assert_eq!(default_watcher_event, approval_watcher_event);

// // Check that only the default watcher gets this event
// let mint = arbiter_token.mint(
// ethers::types::H160::from_str(TEST_MINT_TO)?,
// ethers::types::U256::from(TEST_MINT_AMOUNT),
// );
// mint.send().await?.await?;
// let default_watcher_event = default_watcher.next().await.unwrap();
// assert!(!default_watcher_event.data.is_empty());
// println!("default_watcher_event: {:#?}", default_watcher_event);
// Check that only the default watcher gets this event
let mint = arbiter_token.mint(
ethers::types::H160::from_str(TEST_MINT_TO)?,
ethers::types::U256::from(TEST_MINT_AMOUNT),
);
mint.send().await?.await?;
let default_watcher_event = default_watcher.next().await.unwrap();
assert!(!default_watcher_event.data.is_empty());

// // Use tokio::time::timeout to await the approval_watcher for a specific
// // duration The timeout is needed because the approval_watcher is a
// stream // that will never end when the test is passing
// let timeout_duration = tokio::time::Duration::from_secs(5); // Adjust the
// duration as needed let timeout = tokio::time::timeout(timeout_duration,
// approval_watcher.next()); match timeout.await {
// Result::Ok(Some(_)) => {
// // Event received
// panic!("This means the test is failing! The filter did not
// work."); }
// Result::Ok(None) => {
// // Timeout occurred, no event received
// println!("Expected result. The filter worked.")
// }
// Err(_) => {
// // Timer error (shouldn't happen in normal conditions)
// panic!("Timer error!")
// }
// }
// Ok(())
// }
// check that the address_watcher has not received any events
let mut ctx = Context::from_waker(futures::task::noop_waker_ref());
let poll_result = Pin::new(&mut approval_watcher).poll_next(&mut ctx);
match poll_result {
Poll::Ready(Some(_event)) => panic!("Event received unexpectedly!"),
Poll::Ready(None) => println!("Stream completed!"),
Poll::Pending => println!("No event ready yet, as expected."),
}
assert_matches!(poll_result, Poll::Pending);
Ok(())
}

// This test has two parts
// 1 check that the expected number of transactions per block is the actual
Expand Down

0 comments on commit 8c0cee1

Please sign in to comment.