Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: LogQuery panics on error #2695

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions ethers-providers/src/toolbox/log_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum LogQueryState<'a> {
LoadLastBlock(PinBoxFut<'a, U64>),
LoadLogs(PinBoxFut<'a, Vec<Log>>),
Consume,
End,
}

impl<'a, P> LogQuery<'a, P>
Expand Down Expand Up @@ -60,6 +61,14 @@ macro_rules! rewake_with_new_state {
};
}

macro_rules! return_error {
($ctx:ident, $this:ident, $error:expr) => {
$this.state = LogQueryState::End;
$ctx.waker().wake_by_ref();
return Poll::Ready(Some(Err($error)))
};
}

/// Errors while querying for logs
#[derive(Error, Debug)]
pub enum LogQueryError<E> {
Expand Down Expand Up @@ -111,15 +120,19 @@ where
let fut = Box::pin(async move { provider.get_logs(&filter).await });
rewake_with_new_state!(ctx, self, LogQueryState::LoadLogs(fut));
}
Err(err) => Poll::Ready(Some(Err(LogQueryError::LoadLastBlockError(err)))),
Err(err) => {
return_error!(ctx, self, LogQueryError::LoadLastBlockError(err));
}
}
}
LogQueryState::LoadLogs(fut) => match futures_util::ready!(fut.as_mut().poll(ctx)) {
Ok(logs) => {
self.current_logs = VecDeque::from(logs);
rewake_with_new_state!(ctx, self, LogQueryState::Consume);
}
Err(err) => Poll::Ready(Some(Err(LogQueryError::LoadLogsError(err)))),
Err(err) => {
return_error!(ctx, self, LogQueryError::LoadLogsError(err));
}
},
LogQueryState::Consume => {
let log = self.current_logs.pop_front();
Expand All @@ -136,7 +149,7 @@ where
// no more pages to load, and everything is consumed
// can safely assume this will always be set in this state
if from_block > self.last_block.unwrap() {
return Poll::Ready(None)
return Poll::Ready(None);
}
// load next page
self.from_block = Some(to_block + 1);
Expand All @@ -151,6 +164,30 @@ where
Poll::Ready(log.map(Ok))
}
}
LogQueryState::End => Poll::Ready(None),
}
}
}

#[cfg(test)]
mod test {
use ethers_core::types::Filter;
use futures_util::StreamExt;

use crate::{JsonRpcError, LogQuery, MockResponse, Provider};

#[tokio::test]
async fn return_error() {
let (provider, mock) = Provider::mocked();
mock.push_response(MockResponse::Error(JsonRpcError {code: -32000, message: "One of the blocks specified in filter (fromBlock, toBlock or blockHash) cannot be found.".to_string(), data: None}));

let filter =
Filter::new().from_block(1).to_block(11).event("Transfer(address,address,uint256)");

let log_query = LogQuery::new(&provider, &filter).with_page_size(5);

let logs: Vec<_> = log_query.collect().await;

assert!(logs[0].is_err());
}
}
Loading