Skip to content

Commit 8c67a8b

Browse files
committed
wip
1 parent 0e1b78d commit 8c67a8b

File tree

11 files changed

+131
-15
lines changed

11 files changed

+131
-15
lines changed

crates/cli/src/full.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use std::path::PathBuf;
22

33
use anyhow::{Context, Result};
44
pub use clap::Parser;
5+
use katana_node::config::db::DbConfig;
56
use katana_node::config::metrics::MetricsConfig;
67
use katana_node::config::rpc::RpcConfig;
78
use katana_node::full;
8-
use katana_node::{config::db::DbConfig, full::Network};
9+
use katana_node::full::Network;
910
use serde::{Deserialize, Serialize};
1011
use tracing::info;
1112

crates/gateway/src/server/handlers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use axum::extract::{Query, State};
22
use axum::http::StatusCode;
33
use axum::response::{IntoResponse, Json, Response};
4-
use katana_executor::implementation::blockifier::BlockifierFactory;
54
use katana_pool_api::TransactionPool;
65
use katana_primitives::block::{BlockHash, BlockIdOrTag, BlockNumber};
76
use katana_primitives::class::{ClassHash, CompiledClass, ContractClassCompilationError};

crates/gateway/src/server/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::time::Duration;
44

55
use axum::routing::get;
66
use axum::Router;
7-
use katana_executor::implementation::blockifier::BlockifierFactory;
87
use katana_pool_api::TransactionPool;
98
use katana_rpc::cors::Cors;
109
use katana_rpc::starknet::StarknetApi;

crates/node/src/full/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Experimental full node implementation.
22
33
mod exit;
4+
mod pending;
45
mod pool;
56
mod tip_watcher;
67

crates/node/src/full/pending.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use katana_gateway::types::{ErrorCode, GatewayError};
2+
use katana_primitives::block::BlockNumber;
3+
use katana_primitives::class::{ClassHash, CompiledClassHash, ContractClass};
4+
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
5+
use katana_primitives::state::StateUpdates;
6+
use katana_primitives::Felt;
7+
use katana_provider::api::contract::ContractClassProvider;
8+
use katana_provider::api::state::{StateProofProvider, StateProvider, StateRootProvider};
9+
use katana_provider::{ProviderError, ProviderResult};
10+
use katana_trie::MultiProof;
11+
use tokio::runtime;
12+
13+
pub struct PendingStateProvider {
14+
base: Box<dyn StateProvider>,
15+
pending_block_id: BlockNumber,
16+
pending_state_updates: StateUpdates,
17+
gateway: katana_gateway::client::Client,
18+
}
19+
20+
impl StateProvider for PendingStateProvider {
21+
fn nonce(&self, address: ContractAddress) -> ProviderResult<Option<Nonce>> {
22+
if let Some(nonce) = self.pending_state_updates.nonce_updates.get(&address) {
23+
return Ok(Some(*nonce));
24+
}
25+
26+
self.base.nonce(address)
27+
}
28+
29+
fn storage(
30+
&self,
31+
address: ContractAddress,
32+
storage_key: StorageKey,
33+
) -> ProviderResult<Option<StorageValue>> {
34+
if let Some(contract_storage) = self.pending_state_updates.storage_updates.get(&address) {
35+
if let Some(value) = contract_storage.get(&storage_key) {
36+
return Ok(Some(*value));
37+
}
38+
}
39+
40+
self.base.storage(address, storage_key)
41+
}
42+
43+
fn class_hash_of_contract(
44+
&self,
45+
address: ContractAddress,
46+
) -> ProviderResult<Option<ClassHash>> {
47+
if let Some(class_hash) = self.pending_state_updates.replaced_classes.get(&address) {
48+
return Ok(Some(*class_hash));
49+
}
50+
51+
if let Some(class_hash) = self.pending_state_updates.deployed_contracts.get(&address) {
52+
return Ok(Some(*class_hash));
53+
}
54+
55+
self.base.class_hash_of_contract(address)
56+
}
57+
}
58+
59+
impl ContractClassProvider for PendingStateProvider {
60+
fn class(&self, hash: ClassHash) -> ProviderResult<Option<ContractClass>> {
61+
if let Some(class) = self.base.class(hash)? {
62+
return Ok(Some(class));
63+
}
64+
65+
let result = runtime::Builder::new_current_thread()
66+
.build()
67+
.unwrap()
68+
.block_on(self.gateway.get_class(hash, block_id));
69+
70+
match result {
71+
Ok(class) => {
72+
let class = class.try_into().map_err(|e| ProviderError::Other(e.to_string()))?;
73+
Ok(Some(class))
74+
}
75+
76+
Err(error) => {
77+
if let katana_gateway::client::Error::Sequencer(GatewayError {
78+
code: ErrorCode::UndeclaredClass,
79+
..
80+
}) = error
81+
{
82+
Ok(None)
83+
} else {
84+
Err(ProviderError::Other(error.to_string()))
85+
}
86+
}
87+
}
88+
}
89+
90+
fn compiled_class_hash_of_class_hash(
91+
&self,
92+
hash: ClassHash,
93+
) -> ProviderResult<Option<CompiledClassHash>> {
94+
if let Some(compiled_hash) = self.pending_state_updates.declared_classes.get(&hash) {
95+
return Ok(Some(*compiled_hash));
96+
}
97+
98+
// Fallback to the base provider
99+
self.base.compiled_class_hash_of_class_hash(hash)
100+
}
101+
}

crates/pool/pool-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub enum PoolError {
3030
pub type PoolResult<T> = Result<T, PoolError>;
3131

3232
/// Represents a complete transaction pool.
33-
pub trait TransactionPool: Send + Sync + 'static {
33+
pub trait TransactionPool: Send + Sync {
3434
/// The pool's transaction type.
3535
type Transaction: PoolTransaction;
3636

crates/rpc/rpc/src/starknet/blockifier.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use katana_chain_spec::ChainSpec;
4+
use katana_executor::implementation::blockifier::blockifier::context::BlockContext;
45
use katana_executor::implementation::blockifier::cache::ClassCache;
56
use katana_executor::implementation::blockifier::call::execute_call;
67
use katana_executor::implementation::blockifier::state::CachedState;
@@ -15,6 +16,12 @@ use katana_rpc_types::{FeeEstimate, FunctionCall};
1516

1617
use crate::starknet::StarknetApiResult;
1718

19+
pub struct ExecutionCtx {
20+
state: Box<dyn StateProvider>,
21+
block_context: Arc<BlockContext>,
22+
simulation_flags: ExecutionFlags,
23+
}
24+
1825
#[tracing::instrument(level = "trace", target = "rpc", skip_all, fields(total_txs = transactions.len()))]
1926
pub fn simulate(
2027
chain_spec: &ChainSpec,

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ struct StarknetApiInner<P: TransactionPool> {
9090
config: StarknetApiConfig,
9191
}
9292

93-
impl<P: TransactionPool> StarknetApi<P> {
93+
impl<P> StarknetApi<P>
94+
where
95+
P: TransactionPool + 'static,
96+
{
9497
pub fn pool(&self) -> &P {
9598
&self.inner.pool
9699
}
@@ -112,7 +115,10 @@ impl<P: TransactionPool> StarknetApi<P> {
112115
}
113116
}
114117

115-
impl<P: TransactionPool> StarknetApi<P> {
118+
impl<P> StarknetApi<P>
119+
where
120+
P: TransactionPool + 'static,
121+
{
116122
pub fn new(
117123
chain_spec: Arc<ChainSpec>,
118124
storage: BlockchainProvider<Box<dyn Database>>,
@@ -1026,7 +1032,10 @@ impl<P: TransactionPool> StarknetApi<P> {
10261032
// `StarknetApiExt` Implementations
10271033
/////////////////////////////////////////////////////
10281034

1029-
impl<P: TransactionPool> StarknetApi<P> {
1035+
impl<P> StarknetApi<P>
1036+
where
1037+
P: TransactionPool + 'static,
1038+
{
10301039
async fn blocks(&self, request: GetBlocksRequest) -> StarknetApiResult<GetBlocksResponse> {
10311040
self.on_io_blocking_task(move |this| {
10321041
let provider = &this.inner.storage;

crates/rpc/rpc/src/starknet/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::cartridge;
4040
#[async_trait]
4141
impl<P> StarknetApiServer for StarknetApi<P>
4242
where
43-
P: TransactionPool + Send + Sync + 'static,
43+
P: TransactionPool + 'static,
4444
{
4545
async fn chain_id(&self) -> RpcResult<Felt> {
4646
Ok(self.inner.chain_spec.id().id())

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ pub trait PendingBlockProvider: Send + Sync {
1313

1414
fn block_env(&self) -> ProviderResult<Option<BlockEnv>>;
1515

16-
fn transaction(&self, hash: TxHash) -> ProviderResult<Option<TxWithHash>>;
17-
18-
fn receipt(&self, hash: TxHash) -> ProviderResult<Option<Receipt>>;
16+
fn transactions(&self) -> ProviderResult<Vec<(TxWithHash, Option<Receipt>)>>;
1917

2018
fn state_update(&self) -> ProviderResult<Option<StateUpdates>>;
2119
}

0 commit comments

Comments
 (0)