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

Fix/state update #63

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
sorted state updates
  • Loading branch information
antiyro committed Jun 17, 2024
commit 7a6e4c3d42711fcb56fed213237a98f8233b2638
141 changes: 125 additions & 16 deletions unit_tests/tests/test_get_state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateUpdate> {
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
Expand Down Expand Up @@ -43,12 +69,12 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran

#[rstest]
#[tokio::test]
async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
async fn work_genesis_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
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)
Expand All @@ -63,24 +89,107 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
.await
.expect("RPC : Error while getting the state update");

let raw_deoxys = format!("{:?}", response_deoxys);
let raw_pathfinder = format!("{:?}", response_pathfinder);
let raw_juno = format!("{:?}", response_juno);
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_existing_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];
let juno = &clients[JUNO];

let mut sorted_deoxys: Vec<char> = 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<char> = 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<char> = 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<String, JsonRpcClient<HttpTransport>>) {
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<String, JsonRpcClient<HttpTransport>>) {
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");
}
Loading