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

Chain id issue #1595

Open
greged93 opened this issue Mar 1, 2024 · 5 comments · Fixed by #2204
Open

Chain id issue #1595

greged93 opened this issue Mar 1, 2024 · 5 comments · Fixed by #2204
Labels
bug Something isn't working katana This issue is related to Katana

Comments

@greged93
Copy link
Collaborator

greged93 commented Mar 1, 2024

Describe the bug
The loaded chain id using --chain-id flag is not the same value as returned when using the get_tx_info syscall.

To Reproduce
Steps to reproduce the behavior (the issue is due to the use of the parse_cairo_short_string from Starknet-rs):

use std::str::FromStr as _;
use starknet::core::{types::FieldElement, utils::parse_cairo_short_string};

fn main() {
    let f = FieldElement::from_str("0xc72dd9d5e883e").unwrap();
    println!("{:?}", f);

    // Used in Katana to convert the chain id to a String, as required by the chain id field in the BlockContext type.
    let s = parse_cairo_short_string(&f).unwrap();

    // How Blockifier converts the string reprensentation of the chain id back to a FieldElement.
    let f = FieldElement::from_byte_slice_be(s.as_bytes()).unwrap();
    println!("{:?}", f);
}

Expected behavior
Both chain id's should be the same.

@greged93 greged93 added the bug Something isn't working label Mar 1, 2024
@kariy kariy added the katana This issue is related to Katana label Mar 1, 2024
@greged93
Copy link
Collaborator Author

greged93 commented Mar 1, 2024

I think a way to produce the correct result is by using the unsafe method from_utf8_unchecked, which stores values even if they aren't valid utf8 values. The issue from the above example is that the byte representation of the field element "0xc72dd9d5e883e" contains the byte 136 (hex 88) which isn't a valid utf8 value. This causes the parse_cairo_short_string to add a value in the String to make it a valid utf8 char. I don't think adding from_utf8_unchecked is a solution here because of course, I'm from the start violating the invariants lol. Below is a working example

use std::str::FromStr as _;

use starknet::core::types::FieldElement;

fn main() {
    let f_old = FieldElement::from_str("0xc72dd9d5e883e").unwrap();

    let binding = f_old.to_bytes_be();
    let s = match String::from_utf8(binding.to_vec()) {
        Ok(str) => str,
        Err(_) => unsafe { String::from_utf8_unchecked(binding.to_vec()) },
    };

    let f_new = FieldElement::from_byte_slice_be(s.as_bytes()).unwrap();
    assert_eq!(f_old, f_new);
}

@greged93
Copy link
Collaborator Author

hey @kariy, is it possible at all to fix this or does it require too much changes on Blockifier,...?

@glihm glihm changed the title [BUG] chain id issue Chain id issue Jun 19, 2024
@glihm
Copy link
Collaborator

glihm commented Jul 17, 2024

@greged93 I've just run some tests on the update of blockifier pushed by @kariy in #2180. The issue seems to be resolved:

# Katana run with --chain-id ABCD

# Output from contract get_tx_info().unbox().chain_id in Katana prints
1094861636 # 0x41424344

# Starkli
0x41424344 (ABCD)

@kariy
Copy link
Member

kariy commented Jul 23, 2024

@greged93 i believe this issue has been fixed in the latest blockifier (v0.8.0-dev.2). i've added a test for this in #2204

@greged93
Copy link
Collaborator Author

greged93 commented Aug 8, 2024

hey @kariy @glihm just provided a failing example for the chain id in the latest linked PR, I don't think this can be fixed without changes to blockifier, but let me know :)

@greged93 greged93 reopened this Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working katana This issue is related to Katana
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants