Skip to content

Commit

Permalink
chore: simplify log filter to_block
Browse files Browse the repository at this point in the history
it was an Option, but inconditionally set as Some
  • Loading branch information
marcospb19-cw committed Aug 2, 2024
1 parent 78b709b commit 51339c9
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/eth/primitives/log_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::ext::not;
#[cfg_attr(test, derive(Default))]
pub struct LogFilter {
pub from_block: BlockNumber,
pub to_block: Option<BlockNumber>,
pub to_block: BlockNumber,
pub addresses: Vec<Address>,

/// Original payload received via RPC.
Expand All @@ -22,10 +22,7 @@ impl LogFilter {
/// Checks if a log matches the filter.
pub fn matches(&self, log: &LogMined) -> bool {
// filter block range
if log.block_number < self.from_block {
return false;
}
if self.to_block.as_ref().is_some_and(|to_block| log.block_number > *to_block) {
if log.block_number < self.from_block || self.to_block < log.block_number {
return false;
}

Expand Down
5 changes: 2 additions & 3 deletions src/eth/primitives/log_filter_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ impl LogFilterInput {
StoragePointInTime::MinedPast(number) => number,
};
let to = match to {
StoragePointInTime::Pending => None,
StoragePointInTime::Mined => None,
StoragePointInTime::MinedPast(number) => Some(number),
StoragePointInTime::MinedPast(number) => number,
StoragePointInTime::Mined | StoragePointInTime::Pending => storage.read_mined_block_number()?,
};

Ok(LogFilter {
Expand Down
10 changes: 3 additions & 7 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,19 +673,15 @@ fn eth_get_logs(params: Params<'_>, ctx: Arc<RpcContext>, ext: Extensions) -> Re
// parse params
reject_unknown_client(ext.rpc_client())?;
let (_, filter_input) = next_rpc_param_or_default::<LogFilterInput>(params.sequence())?;
let mut filter = filter_input.parse(&ctx.storage)?;
let filter = filter_input.parse(&ctx.storage)?;

// for this operation, the filter always need the end block specified to calculate the difference
if filter.to_block.is_none() {
filter.to_block = Some(ctx.storage.read_mined_block_number()?);
}
let blocks_in_range = filter.from_block.count_to(&filter.to_block.unwrap());
let blocks_in_range = filter.from_block.count_to(&filter.to_block);

// track
Span::with(|s| {
s.rec_str("filter", &to_json_string(&filter));
s.rec_str("filter_from", &filter.from_block);
s.rec_str("filter_to", &filter.to_block.unwrap());
s.rec_str("filter_to", &filter.to_block);
s.rec_str("filter_range", &blocks_in_range);
});
tracing::info!(?filter, "reading logs");
Expand Down
5 changes: 1 addition & 4 deletions src/eth/storage/inmemory/inmemory_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ impl PermanentStorage for InMemoryPermanentStorage {
.logs
.iter()
.skip_while(|log| log.block_number < filter.from_block)
.take_while(|log| match filter.to_block {
Some(to_block) => log.block_number <= to_block,
None => true,
})
.take_while(|log| log.block_number <= filter.to_block)
.filter(|log| filter.matches(log))
.cloned()
.collect();
Expand Down
2 changes: 0 additions & 2 deletions src/eth/storage/permanent_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ pub trait PermanentStorage: Send + Sync + 'static {
fn read_transaction(&self, hash: &Hash) -> anyhow::Result<Option<TransactionMined>>;

/// Retrieves logs from the storage.
///
/// TODO: `filter.to_block` is always populated, need to change the type to reflect that.
fn read_logs(&self, filter: &LogFilter) -> anyhow::Result<Vec<LogMined>>;

// -------------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions src/eth/storage/redis/redis_permanent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ impl PermanentStorage for RedisPermanentStorage {
fn read_logs(&self, filter: &LogFilter) -> anyhow::Result<Vec<LogMined>> {
// prepare keys
let from_block = filter.from_block.as_u64();
let to_block = match filter.to_block {
Some(number) => number.as_u64(),
None => self.read_mined_block_number()?.as_u64(),
};
let to_block = filter.to_block.as_u64();
let block_keys = (from_block..=to_block).map(key_block_by_number).collect_vec();

// exit if no keys
Expand Down
7 changes: 1 addition & 6 deletions src/eth/storage/rocks/rocks_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,6 @@ impl RocksStorageState {
pub fn read_logs(&self, filter: &LogFilter) -> Result<Vec<LogMined>> {
let addresses: HashSet<AddressRocksdb> = filter.addresses.iter().map(|&address| AddressRocksdb::from(address)).collect();

let end_block_range_filter = |number: BlockNumber| match filter.to_block.as_ref() {
Some(&last_block) => number <= last_block,
None => true,
};

let iter = self
.blocks_by_number
.iter_from(BlockNumberRocksdb::from(filter.from_block), Direction::Forward)?;
Expand All @@ -265,7 +260,7 @@ impl RocksStorageState {
for next in iter {
let (number, block) = next?;

if !end_block_range_filter(number.into()) {
if number > filter.to_block.into() {
break;
}
let transactions_with_matching_addresses = block
Expand Down

0 comments on commit 51339c9

Please sign in to comment.