Skip to content

Commit 2530181

Browse files
committed
wip
1 parent e8a87f2 commit 2530181

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

crates/rpc/rpc/src/starknet/forking.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use katana_rpc_types::event::{EventFilter, GetEventsResponse};
1111
use katana_rpc_types::receipt::{ReceiptBlockInfo, TxReceiptWithBlockInfo};
1212
use katana_rpc_types::state_update::StateUpdate;
1313
use katana_rpc_types::transaction::RpcTxWithHash;
14-
use katana_rpc_types::{BlockHashAndNumberResponse, TxStatus};
14+
use katana_rpc_types::{BlockHashAndNumberResponse, BlockNumberResponse, TxStatus};
1515

1616
#[derive(Debug, thiserror::Error)]
1717
pub enum Error {
@@ -49,6 +49,10 @@ impl ForkedClient {
4949
}
5050

5151
impl ForkedClient {
52+
pub async fn block_number(&self) -> Result<BlockNumber, Error> {
53+
Ok(self.client.block_number().await?.block_number)
54+
}
55+
5256
pub async fn block_hash_and_number(&self) -> Result<BlockHashAndNumberResponse, Error> {
5357
Ok(self.client.block_hash_and_number().await?)
5458
}

crates/rpc/rpc/src/starknet/mod.rs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ where
933933
(0, 0)
934934
};
935935

936+
dbg!(transactions.len());
936937
for (tx_idx, (tx, result)) in transactions.iter().enumerate() {
937938
// Skip transactions before the continuation token
938939
if tx_idx < start_txn_idx {
@@ -943,7 +944,6 @@ where
943944
if events_buffer.len() >= chunk_size as usize {
944945
break;
945946
}
946-
947947
// Only process successful executions
948948
if let katana_executor::ExecutionResult::Success { receipt, .. } = result {
949949
for (event_idx, event) in receipt.events().iter().enumerate() {
@@ -1006,7 +1006,21 @@ where
10061006
}
10071007
}
10081008
}
1009+
1010+
// if we already exhaust all the optimistic transactions then we return a continuation token pointing to the next optimistic transaction
1011+
return Ok(Some(katana_primitives::event::ContinuationToken {
1012+
block_n: 0, // Not used for optimistic transactions
1013+
txn_n: transactions.len() as u64,
1014+
event_n: transactions
1015+
.last()
1016+
.and_then(|(.., result)| {
1017+
result.receipt().map(|receipt| receipt.events().len() as u64)
1018+
})
1019+
.unwrap_or(0),
1020+
transaction_hash: transactions.last().map(|(tx, ..)| tx.hash),
1021+
}));
10091022
}
1023+
10101024
Ok(None)
10111025
}
10121026

@@ -1020,16 +1034,13 @@ where
10201034
continuation_token: Option<MaybeForkedContinuationToken>,
10211035
chunk_size: u64,
10221036
) -> StarknetApiResult<GetEventsResponse> {
1023-
let provider = self.inner.backend.blockchain.provider();
1024-
10251037
let from = self.resolve_event_block_id_if_forked(from_block)?;
10261038
let to = self.resolve_event_block_id_if_forked(to_block)?;
10271039

10281040
// reserved buffer to fill up with events to avoid reallocations
10291041
let mut events = Vec::with_capacity(chunk_size as usize);
1030-
// let filter = utils::events::Filter { address, keys: keys.clone() };
10311042

1032-
match (from, to) {
1043+
match dbg!((from, to)) {
10331044
(EventBlockId::Num(from), EventBlockId::Num(to)) => {
10341045
// Check if continuation token is a native (non-forked) token
10351046
let is_native_token = continuation_token
@@ -1063,34 +1074,24 @@ where
10631074
}
10641075
}
10651076

1066-
// Fetch events from optimistic state transactions
1067-
// Extract native token if present
1068-
let native_token = continuation_token.as_ref().and_then(|t| match t {
1069-
MaybeForkedContinuationToken::Token(token) => Some(token),
1070-
_ => None,
1077+
return Ok(GetEventsResponse {
1078+
events,
1079+
continuation_token: continuation_token.map(|t| t.to_string()),
10711080
});
1072-
let opt_token = self.fetch_optimistic_events(
1073-
address,
1074-
&keys,
1075-
&mut events,
1076-
chunk_size,
1077-
native_token,
1078-
)?;
1079-
1080-
let continuation_token =
1081-
opt_token.map(|t| MaybeForkedContinuationToken::Token(t).to_string());
1082-
Ok(GetEventsResponse { events, continuation_token })
10831081
}
10841082

10851083
(EventBlockId::Num(from), EventBlockId::Pending) => {
10861084
// Check if continuation token is a native (non-forked) token
1087-
let is_native_token = continuation_token
1085+
let fetch_from_fork = continuation_token
10881086
.as_ref()
1089-
.map_or(false, |t| matches!(t, MaybeForkedContinuationToken::Token(_)));
1087+
// if not token is supplied then we need to fetch from forked client, or
1088+
// if token is a forked token
1089+
.map_or(true, |t| matches!(t, MaybeForkedContinuationToken::Forked(_)));
10901090

10911091
// Only fetch from forked client if we don't have a native continuation token
1092-
if !is_native_token {
1092+
if dbg!(fetch_from_fork) {
10931093
let client = &self.inner.forked_client.as_ref().unwrap();
1094+
10941095
// Extract forked token if present
10951096
let forked_token = continuation_token.as_ref().and_then(|t| match t {
10961097
MaybeForkedContinuationToken::Forked(token) => Some(token.clone()),
@@ -1109,12 +1110,15 @@ where
11091110
events.extend(forked_result.events);
11101111

11111112
// Return early if there's a continuation token from forked network
1113+
11121114
if let Some(token) = forked_result.continuation_token {
1113-
let token = MaybeForkedContinuationToken::Forked(token);
1114-
return Ok(GetEventsResponse {
1115-
events,
1116-
continuation_token: Some(token.to_string()),
1117-
});
1115+
if dbg!(events.len() as u64 >= chunk_size) {
1116+
let token = MaybeForkedContinuationToken::Forked(token);
1117+
return Ok(GetEventsResponse {
1118+
events,
1119+
continuation_token: Some(token.to_string()),
1120+
});
1121+
}
11181122
}
11191123
}
11201124

@@ -1125,6 +1129,8 @@ where
11251129
MaybeForkedContinuationToken::Token(token) => Some(token),
11261130
_ => None,
11271131
});
1132+
1133+
println!("fetching optimistic events");
11281134
let opt_token = self.fetch_optimistic_events(
11291135
address,
11301136
&keys,
@@ -1133,12 +1139,16 @@ where
11331139
native_token,
11341140
)?;
11351141

1142+
dbg!(&opt_token);
1143+
11361144
let continuation_token =
11371145
opt_token.map(|t| MaybeForkedContinuationToken::Token(t).to_string());
11381146
Ok(GetEventsResponse { events, continuation_token })
11391147
}
11401148

11411149
(EventBlockId::Pending, EventBlockId::Pending) => {
1150+
println!("fetching optimistic events - pending - pending");
1151+
11421152
// Fetch events from optimistic state transactions (which represent pending
11431153
// transactions)
11441154
// Extract native token if present
@@ -1172,25 +1182,32 @@ where
11721182
&self,
11731183
id: BlockIdOrTag,
11741184
) -> StarknetApiResult<EventBlockId> {
1175-
let provider = &self.inner.storage_provider.provider();
1176-
11771185
let id = match id {
11781186
BlockIdOrTag::L1Accepted => EventBlockId::Pending,
11791187
BlockIdOrTag::PreConfirmed => EventBlockId::Pending,
11801188
BlockIdOrTag::Number(num) => EventBlockId::Num(num),
11811189

11821190
BlockIdOrTag::Latest => {
1183-
let num = provider.convert_block_id(id)?;
1184-
EventBlockId::Num(num.ok_or(StarknetApiError::BlockNotFound)?)
1191+
// let num = provider.convert_block_id(id)?;
1192+
// EventBlockId::Num(num.ok_or(StarknetApiError::BlockNotFound)?)
1193+
if let Some(client) = self.forked_client() {
1194+
let num = futures::executor::block_on(client.block_number())?;
1195+
EventBlockId::Num(num)
1196+
}
1197+
// Otherwise the block hash is not found.
1198+
else {
1199+
return Err(StarknetApiError::BlockNotFound);
1200+
}
11851201
}
11861202

11871203
BlockIdOrTag::Hash(hash) => {
1188-
// Check first if the block hash belongs to a local block.
1189-
if let Some(num) = provider.convert_block_id(id)? {
1190-
EventBlockId::Num(num)
1191-
}
1204+
// // Check first if the block hash belongs to a local block.
1205+
// if let Some(num) = provider.convert_block_id(id)? {
1206+
// EventBlockId::Num(num)
1207+
// }
11921208
// If not, check if the block hash belongs to a forked block.
1193-
else if let Some(client) = self.forked_client() {
1209+
// else
1210+
if let Some(client) = self.forked_client() {
11941211
let num = futures::executor::block_on(client.get_block_number_by_hash(hash))?;
11951212
EventBlockId::Num(num)
11961213
}

0 commit comments

Comments
 (0)