From 7a6e4c3d42711fcb56fed213237a98f8233b2638 Mon Sep 17 00:00:00 2001 From: antiyro Date: Mon, 17 Jun 2024 14:52:49 +0200 Subject: [PATCH] sorted state updates --- unit_tests/tests/test_get_state_update.rs | 141 +++++++++++++++++++--- 1 file changed, 125 insertions(+), 16 deletions(-) diff --git a/unit_tests/tests/test_get_state_update.rs b/unit_tests/tests/test_get_state_update.rs index 14aeec0..aae9445 100644 --- a/unit_tests/tests/test_get_state_update.rs +++ b/unit_tests/tests/test_get_state_update.rs @@ -3,9 +3,35 @@ mod common; use common::*; -use starknet_core::types::{BlockId, StarknetError}; +use starknet_core::types::{BlockId, BlockTag, MaybePendingStateUpdate, StarknetError, StateUpdate}; use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; -use std::{collections::HashMap, os::macos::raw}; +use std::collections::HashMap; + +pub fn extract_and_sort_state_update(maybe_update: MaybePendingStateUpdate) -> Option { + match maybe_update { + MaybePendingStateUpdate::Update(state_update) => Some(sort_state_update(state_update)), + MaybePendingStateUpdate::PendingUpdate(_) => None, // or handle pending update if necessary + } +} + +pub fn sort_state_update(state_update: StateUpdate) -> StateUpdate { + let mut sorted_state_update = state_update.clone(); + let state_diff = &mut sorted_state_update.state_diff; + let storage_diffs = &mut state_diff.storage_diffs; + + for storage_diff in storage_diffs.iter_mut() { + storage_diff.storage_entries.sort_by_key(|x| x.key); + } + + storage_diffs.sort_by_key(|x| x.address); + state_diff.deprecated_declared_classes.sort(); + state_diff.declared_classes.sort_by_key(|x| x.class_hash); + state_diff.deployed_contracts.sort_by_key(|x| x.address); + state_diff.replaced_classes.sort_by_key(|x| x.contract_address); + state_diff.nonces.sort_by_key(|x| x.contract_address); + + sorted_state_update +} /// Test for the `get_state_update` Deoxys RPC method /// # Arguments @@ -43,12 +69,12 @@ async fn fail_non_existing_block(clients: HashMap>) { +async fn work_genesis_block(clients: HashMap>) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER]; let juno = &clients[JUNO]; - let block_number = BlockId::Number(10000); + let block_number = BlockId::Number(0); let response_deoxys = deoxys .get_state_update(block_number) @@ -63,24 +89,107 @@ async fn work_existing_block(clients: HashMap>) { + let deoxys = &clients[DEOXYS]; + let pathfinder = &clients[PATHFINDER]; + let juno = &clients[JUNO]; - let mut sorted_deoxys: Vec = raw_deoxys.chars().collect(); - sorted_deoxys.sort(); // Use sort instead of sort_unstable - let sorted_deoxys: String = sorted_deoxys.into_iter().collect(); + let block_number = BlockId::Number(250000); - let mut sorted_pathfinder: Vec = raw_pathfinder.chars().collect(); - sorted_pathfinder.sort(); // Use sort instead of sort_unstable - let sorted_pathfinder: String = sorted_pathfinder.into_iter().collect(); + let response_deoxys = deoxys + .get_state_update(block_number) + .await + .expect("Deoxys : Error while getting the state update"); + let response_pathfinder = pathfinder + .get_state_update(block_number) + .await + .expect("RPC : Error while getting the state update"); + let response_juno = juno + .get_state_update(block_number) + .await + .expect("RPC : Error while getting the state update"); - let mut sorted_juno: Vec = raw_juno.chars().collect(); - sorted_juno.sort(); // Use sort instead of sort_unstable - let sorted_juno: String = sorted_juno.into_iter().collect(); + // Extract and sort the updates + let sorted_deoxys = extract_and_sort_state_update(response_deoxys); + let sorted_pathfinder = extract_and_sort_state_update(response_pathfinder); + let sorted_juno = extract_and_sort_state_update(response_juno); assert_eq!(sorted_deoxys, sorted_pathfinder, "The sorted responses do not match"); assert_eq!(sorted_deoxys, sorted_juno, "The sorted responses do not match"); assert_eq!(sorted_juno, sorted_pathfinder, "The sorted responses do not match"); } +#[rstest] +#[tokio::test] +async fn work_loop_existing_block(clients: HashMap>) { + let deoxys = &clients[DEOXYS]; + let pathfinder = &clients[PATHFINDER]; + let juno = &clients[JUNO]; + + for i in 0..5q { + let block_number = BlockId::Number(i * 10000); + let response_deoxys = deoxys + .get_state_update(block_number) + .await + .expect("Deoxys : Error while getting the state update"); + let response_pathfinder = pathfinder + .get_state_update(block_number) + .await + .expect("RPC : Error while getting the state update"); + let response_juno = juno + .get_state_update(block_number) + .await + .expect("RPC : Error while getting the state update"); + + // Extract and sort the updates + let sorted_deoxys = extract_and_sort_state_update(response_deoxys); + let sorted_pathfinder = extract_and_sort_state_update(response_pathfinder); + let sorted_juno = extract_and_sort_state_update(response_juno); + + assert_eq!(sorted_deoxys, sorted_pathfinder, "The sorted responses do not match"); + assert_eq!(sorted_deoxys, sorted_juno, "The sorted responses do not match"); + assert_eq!(sorted_juno, sorted_pathfinder, "The sorted responses do not match"); + } +} + +#[rstest] +#[tokio::test] +async fn work_block_pending(clients: HashMap>) { + let deoxys = &clients[DEOXYS]; + let pathfinder = &clients[PATHFINDER]; + let juno = &clients[JUNO]; + + let block_number = BlockId::Tag(BlockTag::Pending); + let response_deoxys = deoxys + .get_state_update(block_number) + .await + .expect("Deoxys : Error while getting the state update"); + let response_pathfinder = pathfinder + .get_state_update(block_number) + .await + .expect("RPC : Error while getting the state update"); + let response_juno = juno + .get_state_update(block_number) + .await + .expect("RPC : Error while getting the state update"); + + // Extract and sort the updates + let sorted_deoxys = extract_and_sort_state_update(response_deoxys); + let sorted_pathfinder = extract_and_sort_state_update(response_pathfinder); + let sorted_juno = extract_and_sort_state_update(response_juno); + + assert_eq!(sorted_deoxys, sorted_pathfinder, "The sorted responses do not match"); + assert_eq!(sorted_deoxys, sorted_juno, "The sorted responses do not match"); + assert_eq!(sorted_juno, sorted_pathfinder, "The sorted responses do not match"); +}