Skip to content

Commit

Permalink
test(sync/track): remove redundant test
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Nov 22, 2024
1 parent 4b1a297 commit 191498f
Showing 1 changed file with 0 additions and 238 deletions.
238 changes: 0 additions & 238 deletions crates/pathfinder/src/sync/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,241 +886,3 @@ impl ProcessStage for StoreBlock {
result
}
}

#[cfg(test)]
mod tests {
use futures::{stream, Stream, StreamExt};
use p2p::client::types::{
ClassDefinition,
ClassDefinitionsError,
EventsResponseStreamFailure,
Receipt as P2PReceipt,
StateDiffsError,
};
use pathfinder_storage::fake::{self, Block, Config};
use starknet_gateway_types::error::SequencerError;

use super::*;
use crate::sync::tests::generate_fake_blocks;

#[tokio::test]
async fn happy_path() {
let (public_key, blocks) = generate_fake_blocks(10);

let BlockHeader { hash, number, .. } = blocks.last().unwrap().header.header;
let latest = (number, hash);

let p2p: FakeP2PClient = FakeP2PClient {
blocks: blocks.clone(),
};

let storage = pathfinder_storage::StorageBuilder::in_tempdir().unwrap();

let sync = Sync {
latest: futures::stream::iter(vec![latest]),
p2p,
storage: storage.clone(),
chain_id: ChainId::SEPOLIA_TESTNET,
public_key,
block_hash_db: None,
verify_tree_hashes: true,
};

let mut start_number = BlockNumber::GENESIS;
let mut parent_hash = BlockHash::default();

sync.run(&mut start_number, &mut parent_hash, FakeFgw)
.await
.unwrap();

assert_eq!(start_number, number + 1);
assert_eq!(parent_hash, hash);

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<_, _>>()
);
}
}

#[derive(Clone)]
struct FakeP2PClient {
pub blocks: Vec<Block>,
}

impl HeaderStream for FakeP2PClient {
fn header_stream(
self,
start: BlockNumber,
stop: BlockNumber,
reverse: bool,
) -> impl Stream<Item = PeerData<SignedBlockHeader>> + Send {
assert!(!reverse);
assert_eq!(start, self.blocks.first().unwrap().header.header.number);
assert_eq!(stop, self.blocks.last().unwrap().header.header.number);

stream::iter(
self.blocks
.into_iter()
.map(|block| PeerData::for_tests(block.header)),
)
}
}

impl BlockClient for FakeP2PClient {
async fn transactions_for_block(
self,
block: BlockNumber,
) -> Option<(
PeerId,
impl Stream<Item = anyhow::Result<(Transaction, P2PReceipt)>> + Send,
)> {
let tr = self
.blocks
.iter()
.find(|b| b.header.header.number == block)
.unwrap()
.transaction_data
.iter()
.map(|(t, r, e)| Ok((t.clone(), P2PReceipt::from(r.clone()))))
.collect::<Vec<anyhow::Result<(Transaction, P2PReceipt)>>>();

Some((PeerId::random(), stream::iter(tr)))
}

async fn state_diff_for_block(
self,
block: BlockNumber,
state_diff_length: u64,
) -> Result<Option<(PeerId, StateUpdateData)>, StateDiffsError> {
let sd: StateUpdateData = self
.blocks
.iter()
.find(|b| b.header.header.number == block)
.unwrap()
.state_update
.clone()
.unwrap()
.into();

assert_eq!(sd.state_diff_length() as u64, state_diff_length);

Ok(Some((PeerId::random(), sd)))
}

async fn class_definitions_for_block(
self,
block: BlockNumber,
declared_classes_count: u64,
) -> Result<Option<(PeerId, Vec<ClassDefinition>)>, ClassDefinitionsError> {
let b = self
.blocks
.iter()
.find(|b| b.header.header.number == block)
.unwrap();
let defs = b
.cairo_defs
.iter()
.map(|(h, x)| ClassDefinition::Cairo {
block_number: block,
definition: x.clone(),
hash: *h,
})
.chain(
b.sierra_defs
.iter()
.map(|(h, x, _)| ClassDefinition::Sierra {
block_number: block,
sierra_definition: x.clone(),
hash: *h,
}),
)
.collect::<Vec<ClassDefinition>>();

Ok(Some((PeerId::random(), defs)))
}

async fn events_for_block(
self,
block: BlockNumber,
) -> Option<(
PeerId,
impl Stream<Item = Result<(TransactionHash, Event), EventsResponseStreamFailure>> + Send,
)> {
let e = self
.blocks
.iter()
.find(|b| b.header.header.number == block)
.unwrap()
.transaction_data
.iter()
.flat_map(|(t, _, e)| e.iter().map(move |e| (t.hash, e.clone())))
.map(Ok)
.collect::<Vec<_>>();

Some((PeerId::random(), stream::iter(e)))
}
}

#[derive(Clone)]
struct FakeFgw;

#[async_trait::async_trait]
impl GatewayApi for FakeFgw {
async fn pending_casm_by_hash(&self, _: ClassHash) -> Result<bytes::Bytes, SequencerError> {
Ok(bytes::Bytes::from_static(b"I'm from the fgw!"))
}
}
}

0 comments on commit 191498f

Please sign in to comment.