diff --git a/arbiter-core/Cargo.toml b/arbiter-core/Cargo.toml index 83983fe3..912b62dd 100644 --- a/arbiter-core/Cargo.toml +++ b/arbiter-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "arbiter-core" -version = "0.3.1" +version = "0.3.2" edition = "2021" # Dependencies for the release build @@ -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" \ No newline at end of file +test-log = "0.2.12" +futures = "0.3.28" +assert_matches = "1.5" diff --git a/arbiter-core/src/middleware.rs b/arbiter-core/src/middleware.rs index fe50179a..d9b9b245 100644 --- a/arbiter-core/src/middleware.rs +++ b/arbiter-core/src/middleware.rs @@ -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) = diff --git a/arbiter-core/src/tests/interaction.rs b/arbiter-core/src/tests/interaction.rs index 73bcae5f..a31cb446 100644 --- a/arbiter-core/src/tests/interaction.rs +++ b/arbiter-core/src/tests/interaction.rs @@ -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; @@ -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(); @@ -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