Skip to content

Commit

Permalink
Merge pull request #63 from KasarLabs/fix/state_update
Browse files Browse the repository at this point in the history
Fix/state update
  • Loading branch information
antiyro authored Jun 17, 2024
2 parents a2e0a92 + bfd11f9 commit 79fabe4
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions unit_tests/tests/test_get_class_hash_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,20 @@ async fn fail_non_existing_contract(clients: HashMap<String, JsonRpcClient<HttpT
///
#[rstest]
#[tokio::test]
#[ignore = "slash this ignore when Deoxys node is fully synced"]
async fn work_block_latest(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let class_hash_deoxys = deoxys
.get_class_hash_at(
BlockId::Tag(BlockTag::Latest),
BlockId::Number(200000),
FieldElement::from_hex_be(STARKGATE_ETH_CONTRACT_ADDR).unwrap(),
)
.await
.expect("Error waiting for response from Deoxys node");
let class_hash_pathfinder = pathfinder
.get_class_hash_at(
BlockId::Tag(BlockTag::Latest),
BlockId::Number(200000),
FieldElement::from_hex_be(STARKGATE_ETH_CONTRACT_ADDR).unwrap(),
)
.await
Expand Down
171 changes: 168 additions & 3 deletions unit_tests/tests/test_get_state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@
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;

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
// * `block_id` - The block id to get the state update from
Expand Down Expand Up @@ -41,14 +67,142 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
}
}

#[rstest]
#[tokio::test]
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(0);

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 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 block_number = BlockId::Number(250000);

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_loop_existing_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];
let juno = &clients[JUNO];

for i in 0..5 {
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]
#[ignore = "Pending data is not supported yet"]
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");

let block_number = BlockId::Number(100);
// 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_latest(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];
let juno = &clients[JUNO];

let block_number = BlockId::Tag(BlockTag::Latest);
let response_deoxys = deoxys
.get_state_update(block_number)
.await
Expand All @@ -57,6 +211,17 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
.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!(response_deoxys, response_pathfinder);
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");
}

0 comments on commit 79fabe4

Please sign in to comment.