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

refactor / optimise get_events #66

Merged
merged 10 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ git # Deoxys Changelog

## Next release

- refactor: optimise get_events RPC
- feat(docker): add dockerfile and docker-compose
- fix: fix implementation `get_storage_at()` for `BlockifierStateAdapter`
- fix(sync): Fix end condition of the l2 sync
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ mc-sync = { path = "crates/client/sync" }
deoxys-runtime = { path = "crates/runtime" }

# TUI Dashboard
deoxys-tui = { path = "crates/deoxys-tui" }
deoxys-tui = { path = "crates/tui" }

# Starknet dependencies
# Cairo Virtual Machine
Expand Down
2 changes: 1 addition & 1 deletion crates/client/rpc/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ where
} else {
(Some(starknet_block.header().hash::<H>().0), Some(starknet_block.header().block_number))
};

let emitted_events = tx_hash_and_events
.into_iter()
.map(|(tx_hash, event)| EmittedEvent {
Expand All @@ -72,7 +73,6 @@ where
transaction_hash: tx_hash.0,
})
.collect();

Ok(emitted_events)
}

Expand Down
57 changes: 14 additions & 43 deletions crates/client/rpc/src/methods/read/get_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,17 @@ where
}

let from_block = continuation_token.block_n;
let mut current_block = from_block;

let mut filtered_events: Vec<EmittedEvent> = Vec::new();

// Iterate on block range: TODO: stop using Cpp-like loops with mutation
while current_block <= to_block {
let emitted_events = if current_block <= latest_block {
for current_block in from_block..=to_block {
let block_filtered_events: Vec<EmittedEvent> = if current_block <= latest_block {
starknet.get_block_events(BlockId::Number(current_block))?
} else {
starknet.get_block_events(BlockId::Tag(BlockTag::Pending))?
};

let block_filtered_events: Vec<EmittedEvent> = filter_events_by_params(emitted_events, from_address, &keys);
}
.into_iter()
.filter(|event| event_match_filter(event, from_address, &keys))
.collect();

if current_block == from_block && (block_filtered_events.len() as u64) < continuation_token.event_n {
return Err(StarknetRpcApiError::InvalidContinuationToken.into());
Expand All @@ -117,45 +115,18 @@ where

return Ok(EventsPage { events: filtered_events, continuation_token: token });
}

current_block += 1;
}

Ok(EventsPage { events: filtered_events, continuation_token: None })
}

/// Helper function to get filter events using address and keys

/// # Arguments
///
/// * `events` - A vector of all events
/// * `address` - Address to use to filter the events
/// * `keys` - Keys to use to filter the events. An event is filtered if any key is present
/// * `max_results` - Optional, indicated the max events that need to be filtered
///
/// # Returns
///
/// * `(block_events: Vec<EventWrapper>, continuation_token: usize)` - A tuple of the filtered
/// events and the first index which still hasn't been processed block_id and an instance of Block
fn filter_events_by_params(
events: Vec<EmittedEvent>,
address: Option<Felt252Wrapper>,
keys: &[Vec<FieldElement>],
) -> Vec<EmittedEvent> {
let mut filtered_events = vec![];

for event in events {
let match_from_address = address.map_or(true, |addr| addr.0 == event.from_address);
let match_keys = keys
.iter()
.enumerate()
.all(|(i, keys)| event.keys.len() > i && (keys.is_empty() || keys.contains(&event.keys[i])));

if match_from_address && match_keys {
filtered_events.push(event);
}
}
filtered_events
#[inline]
fn event_match_filter(event: &EmittedEvent, address: Option<Felt252Wrapper>, keys: &[Vec<FieldElement>]) -> bool {
let match_from_address = address.map_or(true, |addr| addr.0 == event.from_address);
let match_keys = keys
.iter()
.enumerate()
.all(|(i, keys)| event.keys.len() > i && (keys.is_empty() || keys.contains(&event.keys[i])));
match_from_address && match_keys
}

fn block_range<A, BE, G, C, P, H>(
Expand Down
2 changes: 1 addition & 1 deletion crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mp-types = { workspace = true }
try-runtime-cli = { optional = true, git = "https://github.com/massalabs/polkadot-sdk", branch = "release-polkadot-v1.3.0-std" }

#Deoxys
deoxys-tui = { optional = true, path = "../deoxys-tui" }
deoxys-tui = { optional = true, path = "../tui" }
mc-sync = { workspace = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
reqwest = { workspace = true }
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading