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

refactor(papyrus_p2p_sync): add random_header utility function #2381

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions crates/papyrus_p2p_sync/src/client/header_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use papyrus_protobuf::sync::{
SignedBlockHeader,
};
use papyrus_storage::header::HeaderStorageReader;
use papyrus_test_utils::{get_rng, GetTestInstance};
use papyrus_test_utils::get_rng;
use starknet_api::block::{BlockHeader, BlockHeaderWithoutHash, BlockNumber};
use tokio::time::timeout;

use super::test_utils::{
create_block_hashes_and_signatures,
random_header,
run_test,
setup,
wait_for_marker,
Expand Down Expand Up @@ -197,20 +198,15 @@ async fn sync_sends_new_header_query_if_it_got_partial_responses() {

#[tokio::test]
async fn wrong_block_number() {
let mut rng = get_rng();
run_test(vec![
// We already validate the query content in other tests.
Action::ReceiveQuery(Box::new(|_query| ())),
Action::SendHeader(DataOrFin(Some(SignedBlockHeader {
block_header: BlockHeader {
block_header_without_hash: BlockHeaderWithoutHash {
block_number: BlockNumber(1),
..GetTestInstance::get_test_instance(&mut rng)
},
..GetTestInstance::get_test_instance(&mut rng)
},
..GetTestInstance::get_test_instance(&mut rng)
}))),
Action::SendHeader(DataOrFin(Some(random_header(
&mut get_rng(),
BlockNumber(1),
None,
None,
)))),
Action::ValidateReportSent,
Action::CheckStorage(Box::new(|reader| {
async move {
Expand Down
37 changes: 36 additions & 1 deletion crates/papyrus_p2p_sync/src/client/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ use papyrus_storage::header::HeaderStorageReader;
use papyrus_storage::state::StateStorageReader;
use papyrus_storage::test_utils::get_test_storage;
use papyrus_storage::StorageReader;
use starknet_api::block::{BlockHash, BlockNumber, BlockSignature};
use papyrus_test_utils::GetTestInstance;
use rand::{Rng, RngCore};
use rand_chacha::ChaCha8Rng;
use starknet_api::block::{
BlockHash,
BlockHeader,
BlockHeaderWithoutHash,
BlockNumber,
BlockSignature,
};
use starknet_api::core::ClassHash;
use starknet_api::crypto::utils::Signature;
use starknet_api::hash::StarkHash;
Expand Down Expand Up @@ -179,6 +188,32 @@ pub async fn run_test(actions: Vec<Action>) {
}
}

pub fn random_header(
rng: &mut ChaCha8Rng,
block_number: BlockNumber,
state_diff_length: Option<usize>,
num_transactions: Option<usize>,
) -> SignedBlockHeader {
SignedBlockHeader {
block_header: BlockHeader {
// TODO(shahak): Remove this once get_test_instance puts random values.
block_hash: BlockHash(rng.next_u64().into()),
block_header_without_hash: BlockHeaderWithoutHash {
block_number,
..GetTestInstance::get_test_instance(rng)
},
state_diff_length: Some(state_diff_length.unwrap_or_else(|| rng.gen())),
n_transactions: num_transactions.unwrap_or_else(|| rng.gen()),
..GetTestInstance::get_test_instance(rng)
},
// TODO(shahak): Remove this once get_test_instance puts random values.
signatures: vec![BlockSignature(Signature {
r: rng.next_u64().into(),
s: rng.next_u64().into(),
})],
}
}

pub fn create_block_hashes_and_signatures(n_blocks: u8) -> Vec<(BlockHash, BlockSignature)> {
let mut bytes = [0u8; 32];
(0u8..n_blocks)
Expand Down
Loading