Skip to content

Commit 8711d99

Browse files
committed
trace working
1 parent ebf337e commit 8711d99

File tree

8 files changed

+85
-26
lines changed

8 files changed

+85
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rpc/rpc-server/src/starknet/trace.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use katana_pool::TransactionPool;
44
use katana_primitives::block::{BlockHashOrNumber, BlockIdOrTag, ConfirmedBlockIdOrTag};
55
use katana_primitives::execution::TypedTransactionExecutionInfo;
66
use katana_primitives::transaction::{ExecutableTx, ExecutableTxWithHash, TxHash};
7-
use katana_provider::api::block::{BlockNumberProvider, BlockProvider};
8-
use katana_provider::api::transaction::{TransactionTraceProvider, TransactionsProviderExt};
7+
use katana_provider::api::block::BlockNumberProvider;
8+
use katana_provider::api::transaction::TransactionTraceProvider;
99
use katana_rpc_api::error::starknet::StarknetApiError;
1010
use katana_rpc_api::starknet::StarknetTraceApiServer;
1111
use katana_rpc_types::broadcasted::BroadcastedTx;
@@ -97,7 +97,6 @@ where
9797
block_id: ConfirmedBlockIdOrTag,
9898
) -> Result<Vec<TxTraceWithHash>, StarknetApiError> {
9999
use StarknetApiError::BlockNotFound;
100-
101100
let provider = &self.inner.storage;
102101

103102
let block_id: BlockHashOrNumber = match block_id {
@@ -109,19 +108,14 @@ where
109108
ConfirmedBlockIdOrTag::Hash(hash) => hash.into(),
110109
};
111110

112-
let indices = provider.block_body_indices(block_id)?.ok_or(BlockNotFound)?;
113-
let tx_hashes = provider.transaction_hashes_in_range(indices.into())?;
114-
115-
let traces = provider.transaction_executions_by_block(block_id)?.ok_or(BlockNotFound)?;
116-
let traces = traces.into_iter().map(TxTrace::from);
117-
118-
let result = tx_hashes
119-
.into_iter()
120-
.zip(traces)
121-
.map(|(h, r)| TxTraceWithHash { transaction_hash: h, trace_root: r })
122-
.collect::<Vec<_>>();
111+
let traces = self
112+
.inner
113+
.storage
114+
.inner()
115+
.transaction_executions_by_block(block_id)?
116+
.ok_or(BlockNotFound)?;
123117

124-
Ok(result)
118+
Ok(traces)
125119
}
126120

127121
fn trace(&self, tx_hash: TxHash) -> Result<TxTrace, StarknetApiError> {

crates/storage/fork/src/lib.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use katana_rpc_client::starknet::{
3131
};
3232
use katana_rpc_types::class::Class;
3333
use katana_rpc_types::{
34-
ContractStorageKeys, GetBlockWithReceiptsResponse, GetStorageProofResponse, StateUpdate,
35-
TxReceiptWithBlockInfo,
34+
ConfirmedBlockIdOrTag, ContractStorageKeys, GetBlockWithReceiptsResponse, GetStorageProofResponse, StateUpdate, TraceBlockTransactionsResponse, TxReceiptWithBlockInfo
3635
};
3736
use parking_lot::Mutex;
3837
use tracing::{error, trace};
@@ -349,6 +348,24 @@ impl Backend {
349348
}
350349
}
351350

351+
pub fn get_block_transactions_traces(
352+
&self,
353+
block_id: BlockHashOrNumber,
354+
) -> Result<Option<TraceBlockTransactionsResponse>, BackendClientError> {
355+
trace!( block = %block_id, "Requesting trace block.");
356+
let (req, rx) = BackendRequest::trace(block_id);
357+
self.request(req)?;
358+
match rx.recv()? {
359+
BackendResponse::TraceBlockTransactions(res) => {
360+
if let Some(res) = handle_not_found_err(res)? {
361+
Ok(Some(res))
362+
} else {
363+
Ok(None)
364+
}
365+
}
366+
response => Err(BackendClientError::UnexpectedResponse(anyhow!("{response:?}"))),
367+
}
368+
}
352369
/// Send a request to the backend thread.
353370
fn request(&self, req: BackendRequest) -> Result<(), BackendClientError> {
354371
self.sender.lock().try_send(req).map_err(|e| e.into_send_error())?;
@@ -383,6 +400,7 @@ enum BackendResponse {
383400
StorageRoot(BackendResult<Felt>),
384401
GlobalRoots(BackendResult<GetStorageProofResponse>),
385402
Proofs(BackendResult<GetStorageProofResponse>),
403+
TraceBlockTransactions(BackendResult<TraceBlockTransactionsResponse>),
386404
}
387405

388406
/// Errors that can occur when interacting with the backend.
@@ -423,6 +441,7 @@ enum BackendRequest {
423441
Nonce(Request<(ContractAddress, BlockNumber)>),
424442
ClassHash(Request<(ContractAddress, BlockNumber)>),
425443
Storage(Request<((ContractAddress, StorageKey), BlockNumber)>),
444+
Trace(Request<BlockHashOrNumber>),
426445
}
427446

428447
impl BackendRequest {
@@ -520,6 +539,11 @@ impl BackendRequest {
520539
let (sender, receiver) = oneshot();
521540
(BackendRequest::StorageRoot(Request { payload: (contract, block_id), sender }), receiver)
522541
}
542+
543+
fn trace(block_id: BlockHashOrNumber) -> (BackendRequest, OneshotReceiver<BackendResponse>) {
544+
let (sender, receiver) = oneshot();
545+
(BackendRequest::Trace(Request { payload: block_id, sender }), receiver)
546+
}
523547
}
524548

525549
type BackendRequestFuture = BoxFuture<'static, BackendResponse>;
@@ -538,6 +562,7 @@ enum BackendRequestIdentifier {
538562
Class(ClassHash, BlockNumber),
539563
ClassHash(ContractAddress, BlockNumber),
540564
Storage((ContractAddress, StorageKey), BlockNumber),
565+
Trace(BlockHashOrNumber),
541566
}
542567

543568
/// Metrics for the forking backend.
@@ -793,6 +818,25 @@ impl BackendWorker {
793818
}),
794819
);
795820
}
821+
BackendRequest::Trace(Request { payload: block_id, sender }) => {
822+
let req_key = BackendRequestIdentifier::Trace(block_id);
823+
let block_id = match block_id {
824+
BlockHashOrNumber::Hash(h) => ConfirmedBlockIdOrTag::Hash(h),
825+
BlockHashOrNumber::Num(n) => ConfirmedBlockIdOrTag::Number(n),
826+
};
827+
828+
self.dedup_request(
829+
req_key,
830+
sender,
831+
Box::pin(async move {
832+
let result = provider
833+
.trace_block_transactions(block_id)
834+
.await
835+
.map_err(|e| BackendError::StarknetProvider(Arc::new(e)));
836+
BackendResponse::TraceBlockTransactions(result)
837+
}),
838+
);
839+
}
796840
}
797841
}
798842

crates/storage/provider/provider-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ version.workspace = true
88
katana-db = { workspace = true, features = [ "test-utils" ] }
99
katana-primitives.workspace = true
1010
katana-trie.workspace = true
11+
katana-rpc-types.workspace = true
1112

1213
auto_impl.workspace = true
1314
starknet.workspace = true

crates/storage/provider/provider-api/src/transaction.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use katana_primitives::block::{BlockHash, BlockHashOrNumber, BlockNumber, Finali
44
use katana_primitives::execution::TypedTransactionExecutionInfo;
55
use katana_primitives::receipt::Receipt;
66
use katana_primitives::transaction::{TxHash, TxNumber, TxWithHash};
7+
use katana_rpc_types::TxTraceWithHash;
78

89
use crate::ProviderResult;
910

@@ -68,7 +69,7 @@ pub trait TransactionTraceProvider: Send + Sync {
6869
fn transaction_executions_by_block(
6970
&self,
7071
block_id: BlockHashOrNumber,
71-
) -> ProviderResult<Option<Vec<TypedTransactionExecutionInfo>>>;
72+
) -> ProviderResult<Option<Vec<TxTraceWithHash>>>;
7273

7374
/// Retrieves the execution traces for the given range of tx numbers.
7475
fn transaction_executions_in_range(

crates/storage/provider/provider/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use katana_provider_api::transaction::{
3030
};
3131
use katana_provider_api::trie::TrieWriter;
3232
pub use katana_provider_api::{ProviderError, ProviderResult};
33+
use katana_rpc_types::TxTraceWithHash;
3334

3435
pub mod providers;
3536
#[cfg(any(test, feature = "test-utils"))]
@@ -217,7 +218,7 @@ where
217218
fn transaction_executions_by_block(
218219
&self,
219220
block_id: BlockHashOrNumber,
220-
) -> ProviderResult<Option<Vec<TypedTransactionExecutionInfo>>> {
221+
) -> ProviderResult<Option<Vec<TxTraceWithHash>>> {
221222
TransactionTraceProvider::transaction_executions_by_block(&self.provider, block_id)
222223
}
223224

crates/storage/provider/provider/src/providers/db/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use katana_provider_api::transaction::{
4343
TransactionsProviderExt,
4444
};
4545
use katana_provider_api::ProviderError;
46+
use katana_rpc_types::{TxTrace, TxTraceWithHash};
4647

4748
use crate::ProviderResult;
4849

@@ -619,9 +620,19 @@ impl<Db: Database> TransactionTraceProvider for DbProvider<Db> {
619620
fn transaction_executions_by_block(
620621
&self,
621622
block_id: BlockHashOrNumber,
622-
) -> ProviderResult<Option<Vec<TypedTransactionExecutionInfo>>> {
623+
) -> ProviderResult<Option<Vec<TxTraceWithHash>>> {
623624
if let Some(index) = self.block_body_indices(block_id)? {
624-
let traces = self.transaction_executions_in_range(index.into())?;
625+
let traces = self.transaction_executions_in_range(index.clone().into())?;
626+
let traces = traces.into_iter().map(TxTrace::from);
627+
628+
let tx_hashes = self.transaction_hashes_in_range(index.into())?;
629+
630+
let traces = tx_hashes
631+
.into_iter()
632+
.zip(traces)
633+
.map(|(h, r)| TxTraceWithHash { transaction_hash: h, trace_root: r })
634+
.collect::<Vec<_>>();
635+
625636
Ok(Some(traces))
626637
} else {
627638
Ok(None)

crates/storage/provider/provider/src/providers/fork/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use katana_provider_api::transaction::{
2929
};
3030
use katana_provider_api::ProviderError;
3131
use katana_rpc_client::starknet::Client as StarknetClient;
32-
use katana_rpc_types::{GetBlockWithReceiptsResponse, RpcTxWithReceipt, StateUpdate};
32+
use katana_rpc_types::{GetBlockWithReceiptsResponse, RpcTxWithReceipt, StateUpdate, TxTraceWithHash};
3333

3434
use super::db::{self, DbProvider};
3535
use crate::ProviderResult;
@@ -564,14 +564,20 @@ impl<Db: Database> TransactionTraceProvider for ForkedProvider<Db> {
564564
fn transaction_executions_by_block(
565565
&self,
566566
block_id: BlockHashOrNumber,
567-
) -> ProviderResult<Option<Vec<TypedTransactionExecutionInfo>>> {
567+
) -> ProviderResult<Option<Vec<TxTraceWithHash>>> {
568568
if let Some(result) = self.local_db.transaction_executions_by_block(block_id)? {
569569
return Ok(Some(result));
570570
}
571571

572-
// TODO: fetch from remote
573-
574-
Ok(None)
572+
if let Some(value) = self.fork_db.db.transaction_executions_by_block(block_id)? {
573+
return Ok(Some(value));
574+
}
575+
if let Some(value) = self.fork_db.backend.get_block_transactions_traces(block_id)? {
576+
let traces = value.traces;
577+
return Ok(Some(traces));
578+
} else {
579+
Ok(None)
580+
}
575581
}
576582

577583
fn transaction_executions_in_range(

0 commit comments

Comments
 (0)