Skip to content

Commit 45a060a

Browse files
authored
revert(starknet_class_manager_types, papyrus_p2p_sync): delete MemoryClassManagerClient (#3560)
1 parent fb3eade commit 45a060a

File tree

12 files changed

+102
-156
lines changed

12 files changed

+102
-156
lines changed

Cargo.lock

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

crates/papyrus_p2p_sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ tracing.workspace = true
3636
[dev-dependencies]
3737
assert_matches.workspace = true
3838
lazy_static.workspace = true
39+
mockall.workspace = true
3940
papyrus_network = { workspace = true, features = ["testing"] }
4041
papyrus_protobuf = { workspace = true, features = ["testing"] }
4142
papyrus_storage = { workspace = true, features = ["testing"] }

crates/papyrus_p2p_sync/src/client/class_test.rs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22

33
use futures::FutureExt;
4+
use mockall::predicate::eq;
45
use papyrus_common::pending_classes::ApiContractClass;
56
use papyrus_protobuf::sync::{
67
BlockHashOrNumber,
@@ -16,14 +17,14 @@ use papyrus_test_utils::{get_rng, GetTestInstance};
1617
use rand::{Rng, RngCore};
1718
use rand_chacha::ChaCha8Rng;
1819
use starknet_api::block::BlockNumber;
19-
use starknet_api::contract_class::ContractClass;
2020
use starknet_api::core::{ClassHash, CompiledClassHash, EntryPointSelector};
2121
use starknet_api::deprecated_contract_class::{
2222
ContractClass as DeprecatedContractClass,
2323
EntryPointOffset,
2424
EntryPointV0,
2525
};
2626
use starknet_api::state::SierraContractClass;
27+
use starknet_class_manager_types::MockClassManagerClient;
2728

2829
use super::test_utils::{
2930
random_header,
@@ -39,6 +40,8 @@ use super::test_utils::{
3940
async fn class_basic_flow() {
4041
let mut rng = get_rng();
4142

43+
let mut class_manager_client = MockClassManagerClient::new();
44+
4245
let state_diffs_and_classes_of_blocks = [
4346
vec![
4447
create_random_state_diff_chunk_with_class(&mut rng),
@@ -51,6 +54,30 @@ async fn class_basic_flow() {
5154
],
5255
];
5356

57+
// Fill class manager client with expectations.
58+
for state_diffs_and_classes in &state_diffs_and_classes_of_blocks {
59+
for (state_diff, class) in state_diffs_and_classes {
60+
let class_hash = state_diff.get_class_hash();
61+
match class {
62+
ApiContractClass::ContractClass(class) => {
63+
let compiled_class_hash = state_diff.get_compiled_class_hash();
64+
class_manager_client
65+
.expect_add_class()
66+
.times(1)
67+
.with(eq(class_hash), eq(class.clone()))
68+
.return_once(move |_, _| Ok(compiled_class_hash));
69+
}
70+
ApiContractClass::DeprecatedContractClass(class) => {
71+
class_manager_client
72+
.expect_add_deprecated_class()
73+
.times(1)
74+
.with(eq(class_hash), eq(class.clone()))
75+
.return_once(|_, _| Ok(()));
76+
}
77+
}
78+
}
79+
}
80+
5481
let mut actions = vec![
5582
Action::RunP2pSync,
5683
// We already validate the header query content in other tests.
@@ -99,7 +126,7 @@ async fn class_basic_flow() {
99126
let class_hash = state_diff.get_class_hash();
100127

101128
// Check that before the last class was sent, the classes aren't written.
102-
actions.push(Action::CheckStorage(Box::new(move |(reader, _)| {
129+
actions.push(Action::CheckStorage(Box::new(move |reader| {
103130
async move {
104131
assert_eq!(
105132
u64::try_from(i).unwrap(),
@@ -111,7 +138,7 @@ async fn class_basic_flow() {
111138
actions.push(Action::SendClass(DataOrFin(Some((class.clone(), class_hash)))));
112139
}
113140
// Check that a block's classes are written before the entire query finished.
114-
actions.push(Action::CheckStorage(Box::new(move |(reader, class_manager_client)| {
141+
actions.push(Action::CheckStorage(Box::new(move |reader| {
115142
async move {
116143
let block_number = BlockNumber(i.try_into().unwrap());
117144
wait_for_marker(
@@ -122,22 +149,6 @@ async fn class_basic_flow() {
122149
TIMEOUT_FOR_TEST,
123150
)
124151
.await;
125-
126-
for (state_diff, expected_class) in state_diffs_and_classes {
127-
let class_hash = state_diff.get_class_hash();
128-
match expected_class {
129-
ApiContractClass::ContractClass(expected_class) => {
130-
let actual_class =
131-
class_manager_client.get_sierra(class_hash).await.unwrap();
132-
assert_eq!(actual_class, expected_class.clone());
133-
}
134-
ApiContractClass::DeprecatedContractClass(expected_class) => {
135-
let actual_class =
136-
class_manager_client.get_executable(class_hash).await.unwrap();
137-
assert_eq!(actual_class, ContractClass::V0(expected_class.clone()));
138-
}
139-
}
140-
}
141152
}
142153
.boxed()
143154
})));
@@ -149,6 +160,7 @@ async fn class_basic_flow() {
149160
(DataType::StateDiff, len.try_into().unwrap()),
150161
(DataType::Class, len.try_into().unwrap()),
151162
]),
163+
Some(class_manager_client),
152164
actions,
153165
)
154166
.await;
@@ -158,6 +170,7 @@ async fn class_basic_flow() {
158170
// we need to define this trait because StateDiffChunk is defined in an other crate.
159171
trait GetClassHash {
160172
fn get_class_hash(&self) -> ClassHash;
173+
fn get_compiled_class_hash(&self) -> CompiledClassHash;
161174
}
162175

163176
impl GetClassHash for StateDiffChunk {
@@ -170,6 +183,13 @@ impl GetClassHash for StateDiffChunk {
170183
_ => unreachable!(),
171184
}
172185
}
186+
187+
fn get_compiled_class_hash(&self) -> CompiledClassHash {
188+
match self {
189+
StateDiffChunk::DeclaredClass(declared_class) => declared_class.compiled_class_hash,
190+
_ => unreachable!(),
191+
}
192+
}
173193
}
174194

175195
fn create_random_state_diff_chunk_with_class(
@@ -325,6 +345,7 @@ async fn validate_class_sync_fails(
325345
(DataType::StateDiff, header_state_diff_lengths.len().try_into().unwrap()),
326346
(DataType::Class, header_state_diff_lengths.len().try_into().unwrap()),
327347
]),
348+
None,
328349
actions,
329350
)
330351
.await;

crates/papyrus_p2p_sync/src/client/header_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ async fn sync_sends_new_header_query_if_it_got_partial_responses() {
202202
async fn wrong_block_number() {
203203
run_test(
204204
HashMap::from([(DataType::Header, 1)]),
205+
None,
205206
vec![
206207
Action::RunP2pSync,
207208
// We already validate the query content in other tests.
@@ -213,7 +214,7 @@ async fn wrong_block_number() {
213214
None,
214215
)))),
215216
Action::ValidateReportSent(DataType::Header),
216-
Action::CheckStorage(Box::new(|(reader, _)| {
217+
Action::CheckStorage(Box::new(|reader| {
217218
async move {
218219
assert_eq!(0, reader.begin_ro_txn().unwrap().get_header_marker().unwrap().0);
219220
}

crates/papyrus_p2p_sync/src/client/state_diff_test.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async fn state_diff_basic_flow() {
136136
{
137137
for state_diff_chunk in state_diff_chunks {
138138
// Check that before the last chunk was sent, the state diff isn't written.
139-
actions.push(Action::CheckStorage(Box::new(move |(reader, _)| {
139+
actions.push(Action::CheckStorage(Box::new(move |reader| {
140140
async move {
141141
assert_eq!(
142142
u64::try_from(i).unwrap(),
@@ -149,7 +149,7 @@ async fn state_diff_basic_flow() {
149149
actions.push(Action::SendStateDiff(DataOrFin(Some(state_diff_chunk))));
150150
}
151151
// Check that a block's state diff is written before the entire query finished.
152-
actions.push(Action::CheckStorage(Box::new(move |(reader, _)| {
152+
actions.push(Action::CheckStorage(Box::new(move |reader| {
153153
async move {
154154
let block_number = BlockNumber(i.try_into().unwrap());
155155
wait_for_marker(
@@ -175,6 +175,7 @@ async fn state_diff_basic_flow() {
175175
(DataType::Header, state_diffs_and_chunks.len().try_into().unwrap()),
176176
(DataType::StateDiff, state_diffs_and_chunks.len().try_into().unwrap()),
177177
]),
178+
None,
178179
actions,
179180
)
180181
.await;
@@ -340,6 +341,7 @@ async fn validate_state_diff_fails(
340341
(DataType::Header, header_state_diff_lengths.len().try_into().unwrap()),
341342
(DataType::StateDiff, header_state_diff_lengths.len().try_into().unwrap()),
342343
]),
344+
None,
343345
actions,
344346
)
345347
.await;

crates/papyrus_p2p_sync/src/client/test.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ async fn receive_block_internally() {
3535

3636
run_test(
3737
HashMap::new(),
38+
None,
3839
vec![
3940
Action::SendInternalBlock(sync_block),
4041
Action::RunP2pSync,
41-
Action::CheckStorage(Box::new(move |(reader, _)| {
42+
Action::CheckStorage(Box::new(move |reader| {
4243
async move {
4344
wait_for_marker(
4445
DataType::StateDiff,
@@ -109,11 +110,12 @@ async fn receive_blocks_out_of_order() {
109110

110111
run_test(
111112
HashMap::new(),
113+
None,
112114
vec![
113115
Action::SendInternalBlock(sync_block_1),
114116
Action::SendInternalBlock(sync_block_0),
115117
Action::RunP2pSync,
116-
Action::CheckStorage(Box::new(move |(reader, _)| {
118+
Action::CheckStorage(Box::new(move |reader| {
117119
async move {
118120
wait_for_marker(
119121
DataType::StateDiff,
@@ -183,6 +185,7 @@ async fn receive_blocks_first_externally_and_then_internally() {
183185
let sync_block_1 = create_random_sync_block(BlockNumber(1), 1, rng);
184186
run_test(
185187
HashMap::from([(DataType::Header, 2)]),
188+
None,
186189
vec![
187190
Action::RunP2pSync,
188191
// We already validate the query content in other tests.
@@ -193,7 +196,7 @@ async fn receive_blocks_first_externally_and_then_internally() {
193196
None,
194197
None,
195198
)))),
196-
Action::CheckStorage(Box::new(|(reader, _)| {
199+
Action::CheckStorage(Box::new(|reader| {
197200
async move {
198201
wait_for_marker(
199202
DataType::Header,
@@ -212,7 +215,7 @@ async fn receive_blocks_first_externally_and_then_internally() {
212215
})),
213216
Action::SendInternalBlock(sync_block_0),
214217
Action::SendInternalBlock(sync_block_1),
215-
Action::CheckStorage(Box::new(|(reader, _)| {
218+
Action::CheckStorage(Box::new(|reader| {
216219
async move {
217220
wait_for_marker(
218221
DataType::Header,

crates/papyrus_p2p_sync/src/client/test_utils.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ use starknet_api::core::ClassHash;
4444
use starknet_api::crypto::utils::Signature;
4545
use starknet_api::hash::StarkHash;
4646
use starknet_api::transaction::FullTransaction;
47-
use starknet_class_manager_types::test_utils::MemoryClassManagerClient;
48-
use starknet_class_manager_types::SharedClassManagerClient;
47+
use starknet_class_manager_types::MockClassManagerClient;
4948
use starknet_state_sync_types::state_sync_types::SyncBlock;
5049
use starknet_types_core::felt::Felt;
5150
use tokio::sync::oneshot;
@@ -113,7 +112,7 @@ pub fn setup() -> TestArgs {
113112
transaction_sender,
114113
class_sender,
115114
};
116-
let class_manager_client = Arc::new(MemoryClassManagerClient::new());
115+
let class_manager_client = Arc::new(MockClassManagerClient::new());
117116
let p2p_sync = P2pSyncClient::new(
118117
p2p_sync_config,
119118
storage_reader.clone(),
@@ -161,9 +160,7 @@ pub enum Action {
161160
SendClass(DataOrFin<(ApiContractClass, ClassHash)>),
162161
/// Perform custom validations on the storage. Returns back the storage reader it received as
163162
/// input
164-
CheckStorage(
165-
Box<dyn FnOnce((StorageReader, SharedClassManagerClient)) -> BoxFuture<'static, ()>>,
166-
),
163+
CheckStorage(Box<dyn FnOnce(StorageReader) -> BoxFuture<'static, ()>>),
167164
/// Check that a report was sent on the current header query.
168165
ValidateReportSent(DataType),
169166
/// Sends an internal block to the sync.
@@ -172,7 +169,11 @@ pub enum Action {
172169
}
173170

174171
// TODO(shahak): add support for state diffs, transactions and classes.
175-
pub async fn run_test(max_query_lengths: HashMap<DataType, u64>, actions: Vec<Action>) {
172+
pub async fn run_test(
173+
max_query_lengths: HashMap<DataType, u64>,
174+
class_manager_client: Option<MockClassManagerClient>,
175+
actions: Vec<Action>,
176+
) {
176177
let p2p_sync_config = P2pSyncClientConfig {
177178
num_headers_per_query: max_query_lengths.get(&DataType::Header).cloned().unwrap_or(1),
178179
num_block_state_diffs_per_query: max_query_lengths
@@ -187,6 +188,8 @@ pub async fn run_test(max_query_lengths: HashMap<DataType, u64>, actions: Vec<Ac
187188
wait_period_for_new_data: WAIT_PERIOD_FOR_NEW_DATA,
188189
buffer_size: BUFFER_SIZE,
189190
};
191+
let class_manager_client = class_manager_client.unwrap_or_default();
192+
let class_manager_client = Arc::new(class_manager_client);
190193
let buffer_size = p2p_sync_config.buffer_size;
191194
let ((storage_reader, storage_writer), _temp_dir) = get_test_storage();
192195
let (header_sender, mut mock_header_network) = mock_register_sqmr_protocol_client(buffer_size);
@@ -202,14 +205,13 @@ pub async fn run_test(max_query_lengths: HashMap<DataType, u64>, actions: Vec<Ac
202205
class_sender,
203206
};
204207
let (mut internal_block_sender, internal_block_receiver) = mpsc::channel(buffer_size);
205-
let class_manager_client = Arc::new(MemoryClassManagerClient::new());
206208
let p2p_sync = P2pSyncClient::new(
207209
p2p_sync_config,
208210
storage_reader.clone(),
209211
storage_writer,
210212
p2p_sync_channels,
211213
internal_block_receiver.boxed(),
212-
class_manager_client.clone(),
214+
class_manager_client,
213215
);
214216

215217
let mut headers_current_query_responses_manager = None;
@@ -275,7 +277,7 @@ pub async fn run_test(max_query_lengths: HashMap<DataType, u64>, actions: Vec<Ac
275277
}
276278
Action::CheckStorage(check_storage_fn) => {
277279
// We tried avoiding the clone here but it causes lifetime issues.
278-
check_storage_fn((storage_reader.clone(), class_manager_client.clone())).await;
280+
check_storage_fn(storage_reader.clone()).await;
279281
}
280282
Action::ValidateReportSent(DataType::Header) => {
281283
let responses_manager = headers_current_query_responses_manager.take()

crates/papyrus_p2p_sync/src/client/transaction_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async fn transaction_basic_flow() {
8585
.zip(transaction_outputs.iter().cloned().zip(transaction_hashes.iter().cloned()))
8686
{
8787
// Check that before the last transaction was sent, the transactions aren't written.
88-
actions.push(Action::CheckStorage(Box::new(move |(reader, _)| {
88+
actions.push(Action::CheckStorage(Box::new(move |reader| {
8989
async move {
9090
assert_eq!(i, reader.begin_ro_txn().unwrap().get_body_marker().unwrap().0);
9191
}
@@ -100,7 +100,7 @@ async fn transaction_basic_flow() {
100100
}
101101

102102
// Check that a block's transactions are written before the entire query finished.
103-
actions.push(Action::CheckStorage(Box::new(move |(reader, _)| {
103+
actions.push(Action::CheckStorage(Box::new(move |reader| {
104104
async move {
105105
let block_number = BlockNumber(i);
106106
wait_for_marker(
@@ -140,6 +140,7 @@ async fn transaction_basic_flow() {
140140
(DataType::Header, NUM_BLOCKS),
141141
(DataType::Transaction, TRANSACTION_QUERY_LENGTH),
142142
]),
143+
None,
143144
actions,
144145
)
145146
.await;

0 commit comments

Comments
 (0)