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 contract class unmarshall for modern cairo compilers #656

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

Conversation

baitcode
Copy link

@baitcode baitcode commented Jan 17, 2025

Fixes: #655

Added workaround for abi data that is not string, but json object.

  • added contract casm compiled by scarn 2.7.0 compiler
  • added tests to unmarshall contract class from new casm json format

Fix ClassHash calculation for casm compiled classes for modern cairo compilers

  • Added bytecode_segment_lengths field to CasmClass, allowing for backward compatibility
  • Added NestedUInts type to handle deserialisation of CasmClass
  • Added test to check that ClassHash now respects bytecode_segment_lengths and calculates correct value

Here is the snippet for hash values generation with starknet.py:

https://gist.github.com/baitcode/5adec0f74121d0ae3a09aaf573ff8665

* added contract casm compiled by scarn 2.7.0 compiler
* added tests to unmarshall contract class from new casm json format
…compilers

* Added bytecode_segment_lengths field to CasmClass, allowing for backward compatibility
* Added NestedUInts type to handle deserialisation of CasmClass
* Added test to check that ClassHash now respects bytecode_segment_lengths and calculates correct value
… of Declare transation

* Simplify nested string
Copy link
Collaborator

@thiagodeev thiagodeev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your help, @baitcode!
I left some comments, mostly about testing

.tool-versions Outdated Show resolved Hide resolved
return hash.CalculateTransactionHashCommon(
PREFIX_DECLARE,
txnVersionFelt,
txn.SenderAddress,
&felt.Zero,
utils.Uint64ToFelt(0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make tests to check the accuracy of this hash, taking examples and results from trusted sources

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand. I can assure that I created contract myself, compiled it, calculated hash using starknet.py and it didn't match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was that we needed to test this change, but I had forgotten about existing tests like 'TestTransactionHashDeclare'. Sorry.

But about this change, the felt.Zero indeed is equal to 0 as felt. I ran 'TestTransactionHashDeclare' here and it worked the same for both '&felt.Zero' and 'utils.Uint64ToFelt(0)'.
What could have happened is that you changed the value of felt.Zero at some point. felt.Zero is just a pointer to a felt var containing the zero value, and it's used across the entire code as a reference. If you need a zero felt just do new(felt.Felt) instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the func getByteCodeSegmentHasher, at line 198, you change the value of felt.Zero. Change it to new(felt.Felt) instead

// Returns:
//
// none
func TestUnmarshalContractClassWithNonStringAbi(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to a new test, let's just include this test case in the "TestUnmarshalContractClass" test and leave a comment saying it's a "...contract class made with compiler >= 2.7.0 version..."

hash/hash_test.go Outdated Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to test outputs from the new implementation of the CompiledClassHash. Let's take examples from trusted sources
Ex: starknet.py, cairo-lang

Or we could make new contracts and use these libraries to get the hash, and then compare them with the ones generated by starknet.go

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used starknet.py on a contract I've created myself. I can use argent contracts for example.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. No need for the argent contract then, please remove it.
Please send here evidence of your test with starknet.py (screenshot, the code itself,...etc)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baitcode
Copy link
Author

baitcode commented Feb 3, 2025

@thiagodeev Fixed!

Added argent account contract v0.4.0 to hash tests.

@baitcode baitcode requested a review from thiagodeev February 3, 2025 16:54
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.

Support for modern CASM format
2 participants