Skip to content

Commit

Permalink
Fix inconsistent class hash (#385)
Browse files Browse the repository at this point in the history
* add test for class_hash recomputation

* class hash test with Pathfinder classes WIP

* class hash test with Pathfinder classes

* fix(hash_tests): fix constructor ABI entry conversion

* fix(hash_tests): entry point offsets use decimal representation in ABI

* chore(pyproject.toml): force use of sympy 1.12

The compiler does not work with sympy 1.13.

* add blocks with cairo0 contracts to CI

* fix: LegacyContractClass decompression

* clean up

* add recompute class hash to CI

* remove unnecesary deps

---------

Co-authored-by: Krisztian Kovacs <[email protected]>
  • Loading branch information
ftheirs and kkovaacs authored Sep 26, 2024
1 parent 92542ec commit 6d85daa
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/prove_blocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ jobs:
PATHFINDER_RPC_URL: ${{ secrets.PATHFINDER_RPC_URL }}
run: |
cargo test --release --package prove_block --test prove_block -- test_prove_selected_blocks --show-output --ignored
- name: Class hashes
env:
PATHFINDER_RPC_URL: ${{ secrets.PATHFINDER_RPC_URL }}
run: |
cargo test --release --package prove_block --test hash_tests -- test_recompute_class_hash --show-output --ignored
37 changes: 37 additions & 0 deletions crates/bin/prove_block/tests/hash_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use rpc_client::RpcClient;
use rstest::rstest;
use starknet::core::types::BlockId;
use starknet::providers::Provider;
use starknet_os_types::compiled_class::GenericCompiledClass;
use starknet_os_types::deprecated_compiled_class::GenericDeprecatedCompiledClass;
use starknet_types_core::felt::Felt;

#[rstest]
// Contract address 0x41a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf
#[case::correct_hash_computation_0("0x07b3e05f48f0c69e4a65ce5e076a66271a527aff2c34ce1083ec6e1526997a69", 78720)]
// Contract address 0x7a3c142b1ef242f093642604c2ac2259da0efa3a0517715c34a722ba2ecd048
#[case::correct_hash_computation_1("0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6", 30000)]
#[ignore = "Requires a running Pathfinder node"]
#[tokio::test(flavor = "multi_thread")]
async fn test_recompute_class_hash(#[case] class_hash_str: String, #[case] block_number: u64) {
let endpoint = std::env::var("PATHFINDER_RPC_URL").expect("Missing PATHFINDER_RPC_URL in env");
let class_hash = Felt::from_hex(&class_hash_str).unwrap();
let block_id = BlockId::Number(block_number);

let rpc_client = RpcClient::new(&endpoint);
let contract_class = rpc_client.starknet_rpc().get_class(block_id, class_hash).await.unwrap();

let compiled_class = if let starknet::core::types::ContractClass::Legacy(legacy_cc) = contract_class {
let compiled_class = GenericDeprecatedCompiledClass::try_from(legacy_cc).unwrap();
GenericCompiledClass::Cairo0(compiled_class)
} else {
panic!("Test intended to test Legacy contracts");
};

let recomputed_class_hash = Felt::from(compiled_class.class_hash().unwrap());

println!("Class hash: {:#x}", class_hash);
println!("Recomputed class hash: {:#x}", recomputed_class_hash);

assert_eq!(class_hash, recomputed_class_hash);
}
2 changes: 2 additions & 0 deletions crates/bin/prove_block/tests/prove_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use rstest::rstest;
#[case::declare_and_deploy_in_same_block(169206)]
#[case::dest_ptr_not_a_relocatable(155140)]
#[case::dest_ptr_not_a_relocatable_2(155830)]
#[case::inconsistent_cairo0_class_hash_0(30000)]
#[case::inconsistent_cairo0_class_hash_1(204936)]
#[ignore = "Requires a running Pathfinder node"]
#[tokio::test(flavor = "multi_thread")]
async fn test_prove_selected_blocks(#[case] block_number: u64) {
Expand Down
25 changes: 14 additions & 11 deletions crates/starknet-os-types/src/starknet_core_addons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::io::Read;

use flate2::read::GzDecoder;
use starknet_core::types::contract::legacy::{
LegacyContractClass, LegacyEntrypointOffset, RawLegacyAbiEntry, RawLegacyEntryPoint, RawLegacyEntryPoints,
RawLegacyEvent, RawLegacyFunction, RawLegacyL1Handler, RawLegacyMember, RawLegacyStruct,
LegacyContractClass, LegacyEntrypointOffset, RawLegacyAbiEntry, RawLegacyConstructor, RawLegacyEntryPoint,
RawLegacyEntryPoints, RawLegacyEvent, RawLegacyFunction, RawLegacyL1Handler, RawLegacyMember, RawLegacyStruct,
};
use starknet_core::types::{
CompressedLegacyContractClass, LegacyContractAbiEntry, LegacyContractEntryPoint, LegacyEntryPointsByType,
Expand All @@ -13,14 +13,17 @@ use starknet_core::types::{

fn raw_abi_entry_from_legacy_function_abi_entry(entry: LegacyFunctionAbiEntry) -> RawLegacyAbiEntry {
match entry.r#type {
LegacyFunctionAbiType::Function | LegacyFunctionAbiType::Constructor => {
RawLegacyAbiEntry::Function(RawLegacyFunction {
inputs: entry.inputs,
name: entry.name,
outputs: entry.outputs,
state_mutability: entry.state_mutability,
})
}
LegacyFunctionAbiType::Function => RawLegacyAbiEntry::Function(RawLegacyFunction {
inputs: entry.inputs,
name: entry.name,
outputs: entry.outputs,
state_mutability: entry.state_mutability,
}),
LegacyFunctionAbiType::Constructor => RawLegacyAbiEntry::Constructor(RawLegacyConstructor {
inputs: entry.inputs,
name: entry.name,
outputs: entry.outputs,
}),
LegacyFunctionAbiType::L1Handler => RawLegacyAbiEntry::L1Handler(RawLegacyL1Handler {
inputs: entry.inputs,
name: entry.name,
Expand Down Expand Up @@ -53,7 +56,7 @@ fn raw_legacy_abi_entry_from_legacy_contract_abi_entry(

fn raw_legacy_entrypoint_from_legacy_entrypoint(legacy_entry_point: LegacyContractEntryPoint) -> RawLegacyEntryPoint {
RawLegacyEntryPoint {
offset: LegacyEntrypointOffset::U64AsHex(legacy_entry_point.offset),
offset: LegacyEntrypointOffset::U64AsInt(legacy_entry_point.offset),
selector: legacy_entry_point.selector,
}
}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
python = "^3.9"
ecdsa = "^0.18.0"
fastecdsa = "^2.3.0"
sympy = "^1.12"
sympy = "~1.12"
cairo-lang = "0.13.1"

[build-system]
Expand Down

0 comments on commit 6d85daa

Please sign in to comment.