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

perf(get events) #378

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ starknet-e2e-test/contracts/build
# rpc output
output*

# test output
test_output*

tmp/

*.info
Expand Down
27 changes: 26 additions & 1 deletion crates/client/db/src/block_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use mp_block::{
MadaraMaybePendingBlockInfo, MadaraPendingBlock, MadaraPendingBlockInfo,
};
use mp_state_update::StateDiff;
use rocksdb::WriteOptions;
use rocksdb::{Direction, IteratorMode, WriteOptions};
use starknet_api::core::ChainId;
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -370,6 +370,31 @@ impl MadaraBackend {
Ok(Some(MadaraMaybePendingBlock { info, inner }))
}

#[tracing::instrument(skip(self), fields(module = "BlockDB"))]
pub fn get_block_stream(&self, block_n: u64) -> Result<impl Iterator<Item = MadaraBlock> + '_> {
let handle_block_info = self.db.get_column(Column::BlockNToBlockInfo);
let handle_block_innr = self.db.get_column(Column::BlockNToBlockInner);

let key = bincode::serialize(&block_n)?;
let iter_mode = IteratorMode::From(&key, Direction::Forward);

let iter_block_info = self.db.iterator_cf(&handle_block_info, iter_mode.clone());
let iter_block_innr = self.db.iterator_cf(&handle_block_innr, iter_mode);

let iter = iter_block_info.zip(iter_block_innr).map_while(move |kvs| {
if let (Ok((_, bytes_info)), Ok((_, bytes_innr))) = kvs {
let block_info = bincode::deserialize::<MadaraBlockInfo>(&bytes_info).ok();
let block_innr = bincode::deserialize::<MadaraBlockInner>(&bytes_innr).ok();

block_info.zip(block_innr).map(|(info, inner)| MadaraBlock { info, inner })
} else {
None
}
});

Ok(iter)
}

// Tx hashes and tx status

/// Returns the index of the tx.
Expand Down
2 changes: 1 addition & 1 deletion crates/client/rpc/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Maximum number of filter keys that can be passed to the `get_events` RPC.
pub const MAX_EVENTS_KEYS: usize = 100;
/// Maximum number of events that can be fetched in a single chunk for the `get_events` RPC.
pub const MAX_EVENTS_CHUNK_SIZE: usize = 1000;
pub const MAX_EVENTS_CHUNK_SIZE: u64 = 1000;
8 changes: 7 additions & 1 deletion crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! It uses the madara client and backend in order to answer queries.

#![feature(iter_collect_into)]

mod constants;
mod errors;
pub mod providers;
Expand All @@ -17,7 +19,7 @@ use std::sync::Arc;

use mc_db::db_block_id::DbBlockIdResolvable;
use mc_db::MadaraBackend;
use mp_block::{MadaraMaybePendingBlock, MadaraMaybePendingBlockInfo};
use mp_block::{MadaraBlock, MadaraMaybePendingBlock, MadaraMaybePendingBlockInfo};
use mp_chain_config::{ChainConfig, RpcVersion};
use mp_convert::ToFelt;

Expand Down Expand Up @@ -69,6 +71,10 @@ impl Starknet {
.ok_or(StarknetRpcApiError::BlockNotFound)
}

pub fn get_block_stream(&self, block_n: u64) -> StarknetRpcResult<impl Iterator<Item = MadaraBlock> + '_> {
self.backend.get_block_stream(block_n).map_err(|_| StarknetRpcApiError::BlockNotFound)
}

pub fn chain_id(&self) -> Felt {
self.backend.chain_config().chain_id.clone().to_felt()
}
Expand Down
Loading