Skip to content

Commit

Permalink
test(sync): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Nov 21, 2024
1 parent 50978f7 commit b28315d
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions crates/pathfinder/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl LatestStream {

#[cfg(test)]
mod tests {
use std::collections::VecDeque;
use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;

use fake::{Fake, Faker};
Expand All @@ -445,7 +445,14 @@ mod tests {
use pathfinder_common::event::Event;
use pathfinder_common::state_update::StateUpdateData;
use pathfinder_common::transaction::Transaction;
use pathfinder_common::{BlockHeader, BlockId, ClassHash, SignedBlockHeader, TransactionHash};
use pathfinder_common::{
BlockHeader,
BlockId,
ClassHash,
SierraHash,
SignedBlockHeader,
TransactionHash,
};
use pathfinder_crypto::signature::ecdsa_sign;
use pathfinder_crypto::Felt;
use pathfinder_ethereum::EthereumClient;
Expand Down Expand Up @@ -498,8 +505,10 @@ mod tests {
let (public_key, blocks) = generate_fake_blocks(CHECKPOINT_BLOCKS + TRACK_BLOCKS);
let last_header = &blocks.last().unwrap().header.header;
let mid_header = &blocks[CHECKPOINT_BLOCKS - 1].header.header;
let storage = StorageBuilder::in_tempdir().unwrap();

let sync = Sync {
storage: StorageBuilder::in_tempdir().unwrap(),
storage: storage.clone(),
p2p: FakeP2PClient {
blocks: blocks.clone(),
},
Expand All @@ -520,10 +529,67 @@ mod tests {
block_hash_db: None,
};

tokio::time::timeout(Duration::from_secs(10), sync.run_inner(false))
tokio::time::timeout(Duration::from_secs(5), sync.run())
.await
.expect("test timed out")
.expect("No sync errors");
.expect_err("Sync should have finished by now");

let mut db = storage.connection().unwrap();
let db = db.transaction().unwrap();
for mut expected in blocks {
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();
let transaction_data = db.transaction_data_for_block(block_id).unwrap().unwrap();
let state_update_data: StateUpdateData =
db.state_update(block_id).unwrap().unwrap().into();
let declared = db.declared_classes_at(block_id).unwrap().unwrap();

let mut cairo_defs = HashMap::new();
let mut sierra_defs = HashMap::new();

for class_hash in declared {
let class = db.class_definition(class_hash).unwrap().unwrap();
match db.casm_hash(class_hash).unwrap() {
Some(casm_hash) => {
let casm = db.casm_definition(class_hash).unwrap().unwrap();
sierra_defs.insert(SierraHash(class_hash.0), (class, casm));
}
None => {
cairo_defs.insert(class_hash, class);
}
}
}

pretty_assertions_sorted::assert_eq!(header, expected.header.header);
pretty_assertions_sorted::assert_eq!(signature, expected.header.signature);
pretty_assertions_sorted::assert_eq!(
header.state_diff_commitment,
expected.header.header.state_diff_commitment
);
pretty_assertions_sorted::assert_eq!(
header.state_diff_length,
expected.header.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.unwrap().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<_, _>>()
);
}

// TODO
// 2 cases here:
Expand Down

0 comments on commit b28315d

Please sign in to comment.