Skip to content

Commit

Permalink
test: fix get_headers proptest and track sync test assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Jul 9, 2024
1 parent 2c9a344 commit 54e596b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
17 changes: 15 additions & 2 deletions crates/pathfinder/src/p2p_network/sync_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use p2p_proto::state::{
};
use p2p_proto::transaction::{TransactionWithReceipt, TransactionsRequest, TransactionsResponse};
use pathfinder_common::{class_definition, BlockHash, BlockNumber};
use pathfinder_crypto::Felt;
use pathfinder_storage::{Storage, Transaction};
use tokio::sync::mpsc;

use crate::state::block_hash::calculate_receipt_commitment;

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -140,6 +141,18 @@ fn get_header(
if let Some((state_diff_commitment, state_diff_len)) = state_diff_cl {
tracing::trace!(?header, "Sending block header");

// TODO this is a temporary solution until receipt commitment is stored in the
// database
let receipts = db_tx
.transaction_data_for_block(block_number.into())
.context("Getting receipts")?
.context("No receipts found for block")?
.into_iter()
.map(|(_, r, _)| r)
.collect::<Vec<_>>();
let receipt_commitment = calculate_receipt_commitment(&receipts)
.context("Calculating receipt commitment")?;

let txn_count = header
.transaction_count
.try_into()
Expand Down Expand Up @@ -167,7 +180,7 @@ fn get_header(
.context("invalid event count")?,
root: Hash(header.event_commitment.0),
},
receipts: Hash(Felt::ZERO), // TODO
receipts: Hash(receipt_commitment.0),
protocol_version: header.starknet_version.to_string(),
gas_price_wei: header.eth_l1_gas_price.0,
gas_price_fri: header.strk_l1_gas_price.0,
Expand Down
15 changes: 12 additions & 3 deletions crates/pathfinder/src/p2p_network/sync_handlers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,12 @@ mod prop {

/// Fixtures for prop tests
mod fixtures {
use pathfinder_storage::fake::{with_n_blocks_and_rng, Block};
use pathfinder_storage::fake::init::Config;
use pathfinder_storage::fake::{with_n_blocks_rng_and_config, Block};
use pathfinder_storage::{Storage, StorageBuilder};

use crate::p2p_network::sync_handlers::MAX_COUNT_IN_TESTS;
use crate::state::block_hash::calculate_receipt_commitment;

pub const MAX_NUM_BLOCKS: u64 = MAX_COUNT_IN_TESTS * 2;

Expand All @@ -510,8 +512,15 @@ mod prop {
let storage = StorageBuilder::in_memory().unwrap();
// Explicitly choose RNG to make sure seeded storage is always reproducible
let mut rng = rand_chacha::ChaCha12Rng::seed_from_u64(seed);
let initializer =
with_n_blocks_and_rng(&storage, num_blocks.try_into().unwrap(), &mut rng);
let initializer = with_n_blocks_rng_and_config(
&storage,
num_blocks.try_into().unwrap(),
&mut rng,
Config {
calculate_receipt_commitment: Box::new(calculate_receipt_commitment),
..Default::default()
},
);
(storage, initializer)
}
}
Expand Down
59 changes: 30 additions & 29 deletions crates/pathfinder/src/sync/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,14 +848,14 @@ mod tests {
const N: usize = 10;
let blocks = fake::init::with_n_blocks_and_config(
N,
Config::new(
|sbh: &SignedBlockHeader, rc: ReceiptCommitment| {
Config {
calculate_block_hash: Box::new(|sbh: &SignedBlockHeader, rc: ReceiptCommitment| {
compute_final_hash(&BlockHeaderData::from_signed_header(sbh, rc))
},
calculate_transaction_commitment,
calculate_receipt_commitment,
calculate_event_commitment,
),
}),
calculate_transaction_commitment: Box::new(calculate_transaction_commitment),
calculate_receipt_commitment: Box::new(calculate_receipt_commitment),
calculate_event_commitment: Box::new(calculate_event_commitment),
},
);

let BlockHeader { hash, number, .. } = blocks.last().unwrap().header.header;
Expand All @@ -882,12 +882,12 @@ mod tests {

let mut db = storage.connection().unwrap();
let db = db.transaction().unwrap();
for mut block in blocks {
for mut expected in blocks {
// TODO p2p sync does not update class and storage tries yet
block.header.header.class_commitment = ClassCommitment::ZERO;
block.header.header.storage_commitment = StorageCommitment::ZERO;
expected.header.header.class_commitment = ClassCommitment::ZERO;
expected.header.header.storage_commitment = StorageCommitment::ZERO;

let block_number = block.header.header.number;
let block_number = expected.header.header.number;
let block_id = block_number.into();
let header = db.block_header(block_id).unwrap().unwrap();
let signature = db.signature(block_id).unwrap().unwrap();
Expand Down Expand Up @@ -916,30 +916,31 @@ mod tests {
}
}

pretty_assertions_sorted::assert_eq!(header, block.header.header);
pretty_assertions_sorted::assert_eq!(signature, block.header.signature);
pretty_assertions_sorted::assert_eq!(header, expected.header.header);
pretty_assertions_sorted::assert_eq!(signature, expected.header.signature);
pretty_assertions_sorted::assert_eq!(
state_diff_commitment,
block.header.state_diff_commitment
expected.header.state_diff_commitment
);
pretty_assertions_sorted::assert_eq!(
state_diff_length as u64,
block.header.state_diff_length
expected.header.state_diff_length
);
pretty_assertions_sorted::assert_eq!(transaction_data, expected.transaction_data);
pretty_assertions_sorted::assert_eq!(state_update_data, expected.state_update.into());
pretty_assertions_sorted::assert_eq!(
cairo_defs,
expected.cairo_defs.into_iter().collect::<HashMap<_, _>>()
);
pretty_assertions_sorted::assert_eq!(
sierra_defs,
expected
.sierra_defs
.into_iter()
// All sierra fixtures are not compile-able
.map(|(h, s, _)| (h, (s, b"I'm from the fgw!".to_vec())))
.collect::<HashMap<_, _>>()
);
pretty_assertions_sorted::assert_eq!(transaction_data, block.transaction_data);
pretty_assertions_sorted::assert_eq!(state_update_data, block.state_update.into());
// pretty_assertions_sorted::assert_eq!(
// cairo_defs,
// block.cairo_defs.into_iter().collect::<HashMap<_, _>>()
// );
// pretty_assertions_sorted::assert_eq!(
// sierra_defs,
// block
// .sierra_defs
// .into_iter()
// .map(|(h, s, c)| (h, (s, c)))
// .collect::<HashMap<_, _>>()
// );
}
}

Expand Down
44 changes: 17 additions & 27 deletions crates/storage/src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ pub fn with_n_blocks_and_rng<R: Rng>(storage: &Storage, n: usize, rng: &mut R) -
blocks
}

/// Same as [`with_n_blocks`] except caller can specify the rng and additional
/// configuration
pub fn with_n_blocks_rng_and_config<R: Rng>(
storage: &Storage,
n: usize,
rng: &mut R,
config: init::Config,
) -> Vec<Block> {
let blocks = init::with_n_blocks_rng_and_config(n, rng, config);
fill(storage, &blocks);
blocks
}

/// Raw _fake state initializers_
pub mod init {
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -146,33 +159,10 @@ pub mod init {
>;

pub struct Config {
calculate_block_hash: BlockHashFn,
calculate_transaction_commitment: TransactionCommitmentFn,
calculate_receipt_commitment: ReceiptCommitmentFn,
calculate_event_commitment: EventCommitmentFn,
}

impl Config {
pub fn new(
calculate_block_hash: impl Fn(&SignedBlockHeader, ReceiptCommitment) -> anyhow::Result<BlockHash>
+ 'static,
calculate_transaction_commitment: impl Fn(&[Transaction], StarknetVersion) -> anyhow::Result<TransactionCommitment>
+ 'static,
calculate_receipt_commitment: impl Fn(&[Receipt]) -> anyhow::Result<ReceiptCommitment>
+ 'static,
calculate_event_commitment: impl Fn(
&[(TransactionHash, &[Event])],
StarknetVersion,
) -> anyhow::Result<EventCommitment>
+ 'static,
) -> Self {
Self {
calculate_block_hash: Box::new(calculate_block_hash),
calculate_transaction_commitment: Box::new(calculate_transaction_commitment),
calculate_receipt_commitment: Box::new(calculate_receipt_commitment),
calculate_event_commitment: Box::new(calculate_event_commitment),
}
}
pub calculate_block_hash: BlockHashFn,
pub calculate_transaction_commitment: TransactionCommitmentFn,
pub calculate_receipt_commitment: ReceiptCommitmentFn,
pub calculate_event_commitment: EventCommitmentFn,
}

impl Default for Config {
Expand Down

0 comments on commit 54e596b

Please sign in to comment.