Skip to content

Commit

Permalink
shahak/client refactor/move reader client to reader mod (#922)
Browse files Browse the repository at this point in the history
* refactor(starknet_client)!: move FGW code to reader module

* refactor(starknet_client)!: fix imports and rename client structs

* refactor(starknet_client)!: rename starknet_reader_client to starknet_client

* refactor(starknet_client)!: fix references to starknet_client in other crates
  • Loading branch information
ShahakShama authored Jul 27, 2023
1 parent fa68afc commit b96cedb
Show file tree
Hide file tree
Showing 55 changed files with 385 additions and 324 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- run: >
cargo run -p papyrus_node --bin starknet_client_integration_test;
cargo run -p papyrus_node --bin starknet_feeder_gateway_client_integration_test;
cargo run -p papyrus_node --bin central_source_integration_test
rustfmt:
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
"crates/papyrus_proc_macros",
"crates/papyrus_storage",
"crates/papyrus_sync",
"crates/starknet_reader_client",
"crates/starknet_client",
"crates/test_utils",
]

Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ papyrus_common = { path = "../papyrus_common"}
papyrus_config = { path = "../papyrus_config" }
papyrus_proc_macros = { path = "../papyrus_proc_macros"}
papyrus_storage = { path = "../papyrus_storage", version = "0.0.3" }
starknet_reader_client = { path = "../starknet_reader_client" }
starknet_client = { path = "../starknet_client" }
regex = {workspace = true}
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand All @@ -42,5 +42,5 @@ prometheus-parse.workspace = true
rand_chacha.workspace = true
test_utils = { path = "../test_utils" }
starknet_api = { workspace = true, features = ["testing"] }
starknet_reader_client = { path = "../starknet_reader_client", features = ["testing"] }
starknet_client = { path = "../starknet_client", features = ["testing"] }
indexmap = { workspace = true, features = ["serde"] }
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::transaction::{
Calldata, ContractAddressSalt, Fee, TransactionSignature, TransactionVersion,
};
use starknet_reader_client::writer::objects::transaction::DeprecatedContractClass;
use starknet_client::writer::objects::transaction::DeprecatedContractClass;

use super::state::ContractClass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starknet_api::state::{EntryPoint, EntryPointType};
use starknet_api::transaction::{
Calldata, ContractAddressSalt, Fee, TransactionSignature, TransactionVersion,
};
use starknet_reader_client::writer::objects::transaction::{
use starknet_client::writer::objects::transaction::{
DeprecatedContractClass, DeprecatedContractClassAbiEntry,
};
use test_utils::{auto_impl_get_test_instance, get_rng, GetTestInstance};
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ reqwest = { workspace = true, features = ["json", "blocking"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
starknet_api.workspace = true
starknet_reader_client = { path = "../starknet_reader_client" }
starknet_client = { path = "../starknet_client" }
thiserror.workspace = true
tokio = { workspace = true, features = ["full", "sync"] }
tokio-stream.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use papyrus_node::version::VERSION_FULL;
use starknet_api::block::BlockNumber;
use starknet_api::core::ClassHash;
use starknet_api::hash::StarkHash;
use starknet_reader_client::{StarknetClient, StarknetClientTrait};
use starknet_client::reader::{StarknetFeederGatewayClient, StarknetReader};

#[tokio::main]
async fn main() {
Expand All @@ -12,9 +12,13 @@ async fn main() {
"--central.url=https://alpha4.starknet.io/".to_owned(),
])
.expect("Load config");
let starknet_client =
StarknetClient::new(&config.central.url, None, VERSION_FULL, config.central.retry_config)
.expect("Create new client");
let starknet_client = StarknetFeederGatewayClient::new(
&config.central.url,
None,
VERSION_FULL,
config.central.retry_config,
)
.expect("Create new client");

// Get the last block.
// Last block.
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use papyrus_sync::{CentralSourceConfig, SyncConfig};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use starknet_api::core::ChainId;
use starknet_reader_client::RetryConfig;
use starknet_client::RetryConfig;

use crate::version::VERSION_FULL;

Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ reqwest = { workspace = true, features = ["json", "blocking"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
starknet_api.workspace = true
starknet_reader_client = { path = "../starknet_reader_client" }
starknet_client = { path = "../starknet_client" }
thiserror.workspace = true
tokio = { workspace = true, features = ["full", "sync"] }
tokio-stream.workspace = true
Expand All @@ -35,5 +35,5 @@ assert_matches.workspace = true
mockall.workspace = true
papyrus_storage = { path = "../papyrus_storage", features = ["testing"] }
pretty_assertions.workspace = true
starknet_reader_client = { path = "../starknet_reader_client", features = ["testing"] }
starknet_client = { path = "../starknet_client", features = ["testing"] }
starknet_api = { workspace = true, features = ["testing"] }
16 changes: 7 additions & 9 deletions crates/papyrus_sync/src/sources/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ use starknet_api::core::{ClassHash, CompiledClassHash};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_api::state::{ContractClass, StateDiff};
use starknet_api::StarknetApiError;
use starknet_reader_client::{
ClientCreationError, ClientError, GenericContractClass, RetryConfig, StarknetClient,
StarknetClientTrait,
};
use starknet_client::reader::{GenericContractClass, StarknetFeederGatewayClient, StarknetReader};
use starknet_client::{ClientCreationError, ClientError, RetryConfig};
use tracing::{debug, trace};

use self::state_update_stream::StateUpdateStream;
Expand Down Expand Up @@ -78,7 +76,7 @@ impl SerializeConfig for CentralSourceConfig {
}
}

pub struct GenericCentralSource<TStarknetClient: StarknetClientTrait + Send + Sync> {
pub struct GenericCentralSource<TStarknetClient: StarknetReader + Send + Sync> {
pub concurrent_requests: usize,
pub starknet_client: Arc<TStarknetClient>,
pub storage_reader: StorageReader,
Expand Down Expand Up @@ -175,7 +173,7 @@ type CentralCompiledClass = (ClassHash, CompiledClassHash, CasmContractClass);
pub(crate) type CompiledClassesStream<'a> = BoxStream<'a, CentralResult<CentralCompiledClass>>;

#[async_trait]
impl<TStarknetClient: StarknetClientTrait + Send + Sync + 'static> CentralSourceTrait
impl<TStarknetClient: StarknetReader + Send + Sync + 'static> CentralSourceTrait
for GenericCentralSource<TStarknetClient>
{
// Returns the block number of the latest block from the central source.
Expand Down Expand Up @@ -308,7 +306,7 @@ impl<TStarknetClient: StarknetClientTrait + Send + Sync + 'static> CentralSource

fn client_to_central_block(
current_block_number: BlockNumber,
maybe_client_block: Result<Option<starknet_reader_client::Block>, ClientError>,
maybe_client_block: Result<Option<starknet_client::reader::Block>, ClientError>,
) -> CentralResult<(Block, StarknetVersion)> {
let res = match maybe_client_block {
Ok(Some(block)) => {
Expand All @@ -330,15 +328,15 @@ fn client_to_central_block(
}
}

pub type CentralSource = GenericCentralSource<StarknetClient>;
pub type CentralSource = GenericCentralSource<StarknetFeederGatewayClient>;

impl CentralSource {
pub fn new(
config: CentralSourceConfig,
node_version: &'static str,
storage_reader: StorageReader,
) -> Result<CentralSource, ClientCreationError> {
let starknet_client = StarknetClient::new(
let starknet_client = StarknetFeederGatewayClient::new(
&config.url,
config.http_headers,
node_version,
Expand Down
15 changes: 7 additions & 8 deletions crates/papyrus_sync/src/sources/central/state_update_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use papyrus_storage::StorageReader;
use starknet_api::block::BlockNumber;
use starknet_api::core::ClassHash;
use starknet_api::state::{StateDiff, StateNumber};
use starknet_reader_client::{ClientResult, StarknetClientTrait, StateUpdate};
use starknet_client::reader::{StarknetReader, StateUpdate};
use starknet_client::ClientResult;
use tracing::log::trace;
use tracing::{debug, instrument};

Expand All @@ -27,7 +28,7 @@ const CLASSES_INITIAL_CAPACITY: usize = MAX_STATE_UPDATES_TO_STORE_IN_MEMORY * 5
type TasksQueue<T> = FuturesOrdered<Pin<Box<dyn Future<Output = T> + Send>>>;
type NumberOfClasses = usize;

pub(crate) struct StateUpdateStream<TStarknetClient: StarknetClientTrait + Send + 'static> {
pub(crate) struct StateUpdateStream<TStarknetClient: StarknetReader + Send + 'static> {
initial_block_number: BlockNumber,
up_to_block_number: BlockNumber,
starknet_client: Arc<TStarknetClient>,
Expand All @@ -40,7 +41,7 @@ pub(crate) struct StateUpdateStream<TStarknetClient: StarknetClientTrait + Send
downloaded_classes: VecDeque<ApiContractClass>,
}

impl<TStarknetClient: StarknetClientTrait + Send + Sync + 'static> Stream
impl<TStarknetClient: StarknetReader + Send + Sync + 'static> Stream
for StateUpdateStream<TStarknetClient>
{
type Item = CentralResult<CentralStateUpdate>;
Expand Down Expand Up @@ -80,9 +81,7 @@ impl<TStarknetClient: StarknetClientTrait + Send + Sync + 'static> Stream
}
}

impl<TStarknetClient: StarknetClientTrait + Send + Sync + 'static>
StateUpdateStream<TStarknetClient>
{
impl<TStarknetClient: StarknetReader + Send + Sync + 'static> StateUpdateStream<TStarknetClient> {
pub fn new(
initial_block_number: BlockNumber,
up_to_block_number: BlockNumber,
Expand Down Expand Up @@ -237,7 +236,7 @@ fn client_to_central_state_update(
match maybe_client_state_update {
Ok((state_update, mut declared_classes)) => {
// Destruct the state diff to avoid partial move.
let starknet_reader_client::StateDiff {
let starknet_client::reader::StateDiff {
storage_diffs,
deployed_contracts,
declared_classes: declared_class_hashes,
Expand Down Expand Up @@ -321,7 +320,7 @@ fn client_to_central_state_update(
// First tries to retrieve the class from the storage.
// If not found in the storage, the class is downloaded.
#[instrument(skip(starknet_client, storage_reader), level = "debug", err)]
async fn download_class_if_necessary<TStarknetClient: StarknetClientTrait>(
async fn download_class_if_necessary<TStarknetClient: StarknetReader>(
class_hash: ClassHash,
starknet_client: Arc<TStarknetClient>,
storage_reader: StorageReader,
Expand Down
24 changes: 12 additions & 12 deletions crates/papyrus_sync/src/sources/central_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContract
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::{ContractClass as sn_api_ContractClass, StateDiff, StorageKey};
use starknet_api::{patricia_key, stark_felt};
use starknet_reader_client::{
Block, ClientError, ContractClass, DeclaredClassHashEntry, DeployedContract,
GenericContractClass, GlobalRoot, MockStarknetClientTrait, ReplacedClass, StateUpdate,
StorageEntry,
use starknet_client::reader::{
Block, ContractClass, DeclaredClassHashEntry, DeployedContract, GenericContractClass,
GlobalRoot, MockStarknetReader, ReplacedClass, StateUpdate, StorageEntry,
};
use starknet_client::ClientError;
use tokio_stream::StreamExt;

use crate::sources::central::{CentralError, CentralSourceTrait, GenericCentralSource};
Expand All @@ -28,7 +28,7 @@ const TEST_CONCURRENT_REQUESTS: usize = 300;

#[tokio::test]
async fn last_block_number() {
let mut mock = MockStarknetClientTrait::new();
let mut mock = MockStarknetReader::new();

// We need to perform all the mocks before moving the mock into central_source.
const EXPECTED_LAST_BLOCK_NUMBER: BlockNumber = BlockNumber(9);
Expand All @@ -49,7 +49,7 @@ async fn last_block_number() {
async fn stream_block_headers() {
const START_BLOCK_NUMBER: u64 = 5;
const END_BLOCK_NUMBER: u64 = 9;
let mut mock = MockStarknetClientTrait::new();
let mut mock = MockStarknetReader::new();

// We need to perform all the mocks before moving the mock into central_source.
for i in START_BLOCK_NUMBER..END_BLOCK_NUMBER {
Expand Down Expand Up @@ -81,7 +81,7 @@ async fn stream_block_headers_some_are_missing() {
const START_BLOCK_NUMBER: u64 = 5;
const END_BLOCK_NUMBER: u64 = 13;
const MISSING_BLOCK_NUMBER: u64 = 9;
let mut mock = MockStarknetClientTrait::new();
let mut mock = MockStarknetReader::new();

// We need to perform all the mocks before moving the mock into central_source.
for i in START_BLOCK_NUMBER..MISSING_BLOCK_NUMBER {
Expand Down Expand Up @@ -126,7 +126,7 @@ async fn stream_block_headers_error() {
const START_BLOCK_NUMBER: u64 = 5;
const END_BLOCK_NUMBER: u64 = 13;
const ERROR_BLOCK_NUMBER: u64 = 9;
let mut mock = MockStarknetClientTrait::new();
let mut mock = MockStarknetReader::new();
const CODE: StatusCode = StatusCode::NOT_FOUND;
const MESSAGE: &str = "msg";

Expand Down Expand Up @@ -213,7 +213,7 @@ async fn stream_state_updates() {
compiled_class_hash: compiled_class_hash2,
};

let client_state_diff1 = starknet_reader_client::StateDiff {
let client_state_diff1 = starknet_client::reader::StateDiff {
storage_diffs: IndexMap::from([(contract_address1, vec![StorageEntry { key, value }])]),
deployed_contracts: vec![
DeployedContract { address: contract_address1, class_hash: class_hash2 },
Expand All @@ -227,7 +227,7 @@ async fn stream_state_updates() {
class_hash: class_hash4,
}],
};
let client_state_diff2 = starknet_reader_client::StateDiff::default();
let client_state_diff2 = starknet_client::reader::StateDiff::default();

let block_state_update1 = StateUpdate {
block_hash: block_hash1,
Expand All @@ -242,7 +242,7 @@ async fn stream_state_updates() {
state_diff: client_state_diff2,
};

let mut mock = MockStarknetClientTrait::new();
let mut mock = MockStarknetReader::new();
let block_state_update1_clone = block_state_update1.clone();
mock.expect_state_update()
.with(predicate::eq(BlockNumber(START_BLOCK_NUMBER)))
Expand Down Expand Up @@ -379,7 +379,7 @@ async fn stream_compiled_classes() {
).unwrap().commit().unwrap();

let felts: Vec<_> = (0..4).map(|i| stark_felt!(format!("0x{i}").as_str())).collect();
let mut mock = MockStarknetClientTrait::new();
let mut mock = MockStarknetReader::new();
for felt in felts.clone() {
mock.expect_compiled_class_by_hash()
.with(predicate::eq(ClassHash(felt)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "starknet_reader_client"
name = "starknet_client"
version.workspace = true
edition.workspace = true
repository.workspace = true
Expand Down
Loading

0 comments on commit b96cedb

Please sign in to comment.