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

feat(katana): forked provider fetch remote non-state data #1700

Closed
wants to merge 10 commits into from
5 changes: 5 additions & 0 deletions crates/katana/primitives/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use core::fmt;
use std::num::ParseIntError;

pub type ChunkSize = u64;
pub type EventContinuationToken = Option<String>;
pub type EventFilter = starknet::core::types::EventFilter;
pub type EventsPage = starknet::core::types::EventsPage;

#[derive(PartialEq, Eq, Debug, Default)]
pub struct ContinuationToken {
pub block_n: u64,
Expand Down
1 change: 1 addition & 0 deletions crates/katana/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{utils, FieldElement};
pub type TxHash = FieldElement;
/// The sequential number for all the transactions..
pub type TxNumber = u64;
pub type Transaction = starknet::core::types::Transaction;

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
179 changes: 178 additions & 1 deletion crates/katana/storage/provider/src/providers/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use futures::future::BoxFuture;
use futures::stream::Stream;
use futures::{Future, FutureExt};
use katana_primitives::block::BlockHashOrNumber;
use katana_primitives::block::{BlockHashOrNumber, BlockIdOrTag};
use katana_primitives::contract::{
ClassHash, CompiledClassHash, CompiledContractClass, ContractAddress, FlattenedSierraClass,
GenericContractInfo, Nonce, StorageKey, StorageValue,
Expand All @@ -18,6 +18,10 @@
compiled_class_hash_from_flattened_sierra_class, flattened_sierra_to_compiled_class,
legacy_rpc_to_inner_compiled_class,
};
use katana_primitives::event::{
ChunkSize, EventContinuationToken, EventFilter, EventsPage,
};
use katana_primitives::transaction::{Transaction, TxHash, TxNumber};
use katana_primitives::FieldElement;
use parking_lot::Mutex;
use starknet::core::types::{BlockId, ContractClass, StarknetError};
Expand All @@ -35,6 +39,14 @@
type GetStorageResult = Result<StorageValue, ForkedBackendError>;
type GetClassHashAtResult = Result<ClassHash, ForkedBackendError>;
type GetClassAtResult = Result<starknet::core::types::ContractClass, ForkedBackendError>;
type GetEventResult = Result<EventsPage, ForkedBackendError>;
type GetBlockWithTxHashesResult =
Result<starknet::core::types::MaybePendingBlockWithTxHashes, ForkedBackendError>;
type GetBlockWithTxsResult =
Result<starknet::core::types::MaybePendingBlockWithTxs, ForkedBackendError>;
type GetTransactionResult = Result<Transaction, ForkedBackendError>;
type GetTransactionReceiptResult =
Result<starknet::core::types::MaybePendingTransactionReceipt, ForkedBackendError>;

#[derive(Debug, thiserror::Error)]
pub enum ForkedBackendError {
Expand All @@ -60,6 +72,12 @@
GetNonce(ContractAddress, OneshotSender<GetNonceResult>),
GetClassHashAt(ContractAddress, OneshotSender<GetClassHashAtResult>),
GetStorage(ContractAddress, StorageKey, OneshotSender<GetStorageResult>),
GetEvents(EventFilter, EventContinuationToken, ChunkSize, OneshotSender<GetEventResult>),
GetBlockWithTxHash(BlockIdOrTag, OneshotSender<GetBlockWithTxHashesResult>),
GetBlockWithTxs(BlockIdOrTag, OneshotSender<GetBlockWithTxsResult>),
GetTransactionByBlockIdAndIndex(BlockIdOrTag, TxNumber, OneshotSender<GetTransactionResult>),
GetTransactionByHash(TxHash, OneshotSender<GetTransactionResult>),
GetTransactionReceipt(TxHash, OneshotSender<GetTransactionReceiptResult>),
}

type BackendRequestFuture = BoxFuture<'static, ()>;
Expand Down Expand Up @@ -142,6 +160,84 @@

self.pending_requests.push(fut);
}

BackendRequest::GetEvents(filter, continuation_token, chunks_size, sender) => {
let fut = Box::pin(async move {
let res = provider
.get_events(filter, continuation_token, chunks_size)
.await
.map_err(ForkedBackendError::StarknetProvider);

sender.send(res).expect("failed to send events result")
});

self.pending_requests.push(fut);
}

Check warning on line 175 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L164-L175

Added lines #L164 - L175 were not covered by tests

BackendRequest::GetBlockWithTxHash(block_id, sender) => {
let fut = Box::pin(async move {
let res = provider
.get_block_with_tx_hashes(block_id)
.await
.map_err(ForkedBackendError::StarknetProvider);

sender.send(res).expect("failed to send block result")
});

self.pending_requests.push(fut);
}

Check warning on line 188 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L177-L188

Added lines #L177 - L188 were not covered by tests

BackendRequest::GetBlockWithTxs(block_id, sender) => {
let fut = Box::pin(async move {
let res = provider
.get_block_with_txs(block_id)
.await
.map_err(ForkedBackendError::StarknetProvider);

sender.send(res).expect("failed to send block result")
});

self.pending_requests.push(fut);
}

Check warning on line 201 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L190-L201

Added lines #L190 - L201 were not covered by tests

BackendRequest::GetTransactionByBlockIdAndIndex(block_id, index, sender) => {
let fut = Box::pin(async move {
let res = provider
.get_transaction_by_block_id_and_index(block_id, index)
.await
.map_err(ForkedBackendError::StarknetProvider);

sender.send(res).expect("failed to send transaction result")
});

self.pending_requests.push(fut);
}

Check warning on line 214 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L203-L214

Added lines #L203 - L214 were not covered by tests

BackendRequest::GetTransactionByHash(transaction_hash, sender) => {
let fut = Box::pin(async move {
let res = provider
.get_transaction_by_hash(transaction_hash)
.await
.map_err(ForkedBackendError::StarknetProvider);

sender.send(res).expect("failed to send transaction result")
});

self.pending_requests.push(fut);
}

Check warning on line 227 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L216-L227

Added lines #L216 - L227 were not covered by tests

BackendRequest::GetTransactionReceipt(transaction_hash, sender) => {
let fut = Box::pin(async move {
let res = provider
.get_transaction_receipt(transaction_hash)
.await
.map_err(ForkedBackendError::StarknetProvider);

sender.send(res).expect("failed to send transaction result")
});

self.pending_requests.push(fut);
}

Check warning on line 240 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L229-L240

Added lines #L229 - L240 were not covered by tests
}
}
}
Expand Down Expand Up @@ -313,6 +409,87 @@
}
}
}

pub fn do_get_events(
&self,
filter: EventFilter,
continuation_token: Option<String>,
chunks_size: ChunkSize,
) -> Result<EventsPage, ForkedBackendError> {
trace!(target: "forked_backend", "requesting evetns at filter{filter:#?}, continuation_token {continuation_token:#?}, and chunks_size {chunks_size:#?} ");
let (sender, rx) = oneshot();
self.0
.lock()
.try_send(BackendRequest::GetEvents(filter, continuation_token, chunks_size, sender))
.map_err(|e| e.into_send_error())?;
rx.recv()?
}

Check warning on line 426 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L413-L426

Added lines #L413 - L426 were not covered by tests

pub fn do_get_block_with_tx_hashes(
&self,
block_id: BlockIdOrTag,
) -> Result<starknet::core::types::MaybePendingBlockWithTxHashes, ForkedBackendError> {
trace!(target: "forked_backend", "requesting block with tx_hashes at block {block_id:#?} ");
let (sender, rx) = oneshot();
self.0
.lock()
.try_send(BackendRequest::GetBlockWithTxHash(block_id, sender))
.map_err(|e| e.into_send_error())?;
rx.recv()?
}

Check warning on line 439 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L428-L439

Added lines #L428 - L439 were not covered by tests

pub fn do_get_block_with_txs(
&self,
block_id: BlockIdOrTag,
) -> Result<starknet::core::types::MaybePendingBlockWithTxs, ForkedBackendError> {
trace!(target: "forked_backend", "requesting block with txs at block {block_id:#?} ");
let (sender, rx) = oneshot();
self.0
.lock()
.try_send(BackendRequest::GetBlockWithTxs(block_id, sender))
.map_err(|e| e.into_send_error())?;
rx.recv()?
}

Check warning on line 452 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L441-L452

Added lines #L441 - L452 were not covered by tests

pub fn do_get_transaction_by_block_id_and_index(
&self,
block_id: BlockIdOrTag,
index: TxNumber,
) -> Result<Transaction, ForkedBackendError> {
trace!(target: "forked_backend", "requesting transaction at block {block_id:#?}, index {index:#?}");
let (sender, rx) = oneshot();
self.0
.lock()
.try_send(BackendRequest::GetTransactionByBlockIdAndIndex(block_id, index, sender))
.map_err(|e| e.into_send_error())?;
rx.recv()?
}

Check warning on line 466 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L454-L466

Added lines #L454 - L466 were not covered by tests

pub fn do_get_transaction_by_hash(
&self,
transaction_hash: TxHash
) -> Result<Transaction, ForkedBackendError> {
trace!(target: "forked_backend", "requesting transaction at trasanction hash {transaction_hash:#?} ");
let (sender, rx) = oneshot();
self.0
.lock()
.try_send(BackendRequest::GetTransactionByHash(transaction_hash, sender))
.map_err(|e| e.into_send_error())?;
rx.recv()?
}

Check warning on line 479 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L468-L479

Added lines #L468 - L479 were not covered by tests

pub fn do_get_transaction_receipt(
&self,
transaction_hash: TxHash
) -> Result<starknet::core::types::MaybePendingTransactionReceipt, ForkedBackendError> {
trace!(target: "forked_backend", "requesting transaction receipt at trasanction hash {transaction_hash:#?} ");
let (sender, rx) = oneshot();
self.0
.lock()
.try_send(BackendRequest::GetTransactionReceipt(transaction_hash, sender))
.map_err(|e| e.into_send_error())?;
rx.recv()?
}

Check warning on line 492 in crates/katana/storage/provider/src/providers/fork/backend.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/fork/backend.rs#L481-L492

Added lines #L481 - L492 were not covered by tests
}

/// A shared cache that stores data fetched from the forked network.
Expand Down
Loading