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(disassemble): PC should start from 0 instead of 1 #570

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chen4903
Copy link
Contributor

Motivation

The same problem as #559, but now it has turned to disassemble. We should also start from zero, not one.
Here is a test:

#[cfg(test)]
mod tests {
    use super::super::DisassemblerArgsBuilder;
    use super::disassemble;
    use tokio::test;

    #[test]
    async fn test_disassemble() -> Result<(), Box<dyn std::error::Error>> {
        let args = DisassemblerArgsBuilder::new()
            .target("0x9f00c43700bc0000Ff91bE00841F8e04c0495000".to_string())
            .rpc_url("https://eth.llamarpc.com".to_string())
            .build()
            .unwrap();

        let result = disassemble(args).await.unwrap();

        println!("Disassembled contract: {:#?}", result);

        Ok(())
    }
}

Old result:

Disassembled contract: "000001 PUSH1 80\n000003 PUSH1 40\n000004 MSTORE \n000005 CALLVALUE \n000006 DUP1...

After update the offset:

Disassembled contract: "000000 PUSH1 80\n000002 PUSH1 40\n000003 MSTORE \n000004 CALLVALUE \n000005 DUP1...

Solution

Simply subtract 1 from the offset:

let offset = program_counter - 1;
        asm.push_str(
            format!(
                "{} {} {}\n",
                if args.decimal_counter {
                    offset.to_string()
                } else {
                    format!("{:06x}", offset)
                },
                opcode_name(opcode),
                pushed_bytes
            )
            .as_str(),
        );

@chen4903 chen4903 requested a review from Jon-Becker as a code owner February 26, 2025 15:01
@chen4903
Copy link
Contributor Author

hi @Jon-Becker . Thanks for merging if there are no more problems. (Not sure why the test fails)

@Jon-Becker
Copy link
Owner

hey! can you just run cargo fmt and recommit?

@chen4903
Copy link
Contributor Author

hey! can you just run cargo fmt and recommit?

done

@Jon-Becker Jon-Becker changed the title disassemble: offset start from 0 instead of 1 fix(disassemble): offset start from 0 instead of 1 Feb 28, 2025
@Jon-Becker Jon-Becker changed the title fix(disassemble): offset start from 0 instead of 1 fix(disassemble): PC should start from 0 instead of 1 Feb 28, 2025
@Jon-Becker
Copy link
Owner

It seems like some of the disassemble tests expect 0, and some expect 1. I will investigate and fix / review tonight :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants