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

Issue #1502 add a name field in GenesisClassJson (Unfinished) #1552

Closed
wants to merge 3 commits into from

Conversation

Yogalholic
Copy link
Contributor

@Yogalholic Yogalholic commented Feb 18, 2024

resolves #1502
resolves DOJ-130

Copy link
Member

@kariy kariy left a comment

Choose a reason for hiding this comment

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

First of all, thanks for the contribution!

I believe we can do this with a simpler approach. During this phase, where we compute the class hashes of all the classes, at the same time we can also build up a mapping of class name to its respective class hash. This mapping can later then be used throughout the method for resolving any class names to its class hash.

crates/katana/primitives/src/genesis/mod.rs Outdated Show resolved Hide resolved
@kariy
Copy link
Member

kariy commented Feb 19, 2024

We also need to modify all instances of this class field (basically here, here, and here) so that it can hold either a class name or the class hash, and perform the resolution accordingly.

@glihm
Copy link
Collaborator

glihm commented Mar 6, 2024

@Yogalholic how are you feeling about this? Do you need any help / additional information?

@Yogalholic
Copy link
Contributor Author

As you can see in me last PR, I am struggling with adding new GenesisJsonError and how to handle FieldElement.

@kariy
Copy link
Member

kariy commented Mar 8, 2024

As you can see in me last PR, I am struggling with adding new GenesisJsonError and how to handle FieldElement.

What trouble are you having? Can you point out where in the code that you have trouble with?

@Yogalholic
Copy link
Contributor Author

As you can see in me last PR, I am struggling with adding new GenesisJsonError and how to handle FieldElement.

What trouble are you having? Can you point out where in the code that you have trouble with?

Firstly, could you check please if I'm on the right direction ? Is creating an enum ClassOrHash was a good idea?

@Yogalholic
Copy link
Contributor Author

Any feedback please?

Copy link
Collaborator

@glihm glihm 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 the work being done here.
Some first comments.

Also, do not hesitate to run the scripts/rust_fmt.sh to ensure you've the correct formatting. It helps for the review. :)

crates/katana/primitives/src/genesis/json.rs Outdated Show resolved Hide resolved
crates/katana/primitives/src/genesis/json.rs Outdated Show resolved Hide resolved
crates/katana/primitives/src/genesis/json.rs Outdated Show resolved Hide resolved
crates/katana/primitives/src/genesis/json.rs Outdated Show resolved Hide resolved
crates/katana/primitives/src/genesis/json.rs Outdated Show resolved Hide resolved
// resolves the class paths, if any
genesis.resolve_class_artifacts(path)?;
genesis.resolve_class_artifacts(path, &class_name_map)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
genesis.resolve_class_artifacts(path, &class_name_map)?;
genesis.resolve_class_artifacts(path)?;

This functions resolves the artifacts, which consist on loading the class JSON into memory to be available for genesis operation. The name here shouldn't be relevant as we are expecting to use the names into other entries, not the classes themselves.

crates/katana/primitives/src/genesis/json.rs Outdated Show resolved Hide resolved
@glihm
Copy link
Collaborator

glihm commented Mar 20, 2024

Hey @Yogalholic, do you need any support or info to tackle this? Don't hesitate to reach out if yes. 👍

@glihm glihm added katana This issue is related to Katana contributor labels Mar 20, 2024
@Yogalholic
Copy link
Contributor Author

In my last PR I have implemented your suggestions. I still don't know how to add a new GenesisJsonError

@glihm
Copy link
Collaborator

glihm commented Mar 21, 2024

In my last PR I have implemented your suggestions. I still don't know how to add a new GenesisJsonError

This one you mean?

pub enum GenesisJsonError {

@Yogalholic
Copy link
Contributor Author

Any feedback please? I have run the rust_fmt script

Copy link
Collaborator

@glihm glihm 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 the progress here @Yogalholic!
I've left some comment. Also, if you have more doubt don't hesitate to reach out on the discord we would be happy to help!

You can also start to add the name for testing once you're confident with the changes. 👍

Ok((class_hash, GenesisClass { compiled_class_hash, sierra, casm }))
})
.collect::<Result<_, GenesisJsonError>>()?;

// Populate the classes and name_to_class_hash
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Populate the classes and name_to_class_hash

Some(NameOrHash::ClassHash(class_hash)) => class_hash,
Some(NameOrHash::ClassName(class_name)) => *name_to_class_hash
.get(&class_name)
.ok_or_else(|| value_out_of_range_error::ValueOutOfRangeError)?,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here we want to return a new error something like:

#[error("Class name not found in the genesis classes: {0}")]
InvalidClassName(String)

You should add this to the GenesisJsonError.

This applies to all conversions from name that fails.

}
};

// Populate the classes and name_to_class_hash
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we have this code two times? Did you mean to replace it?

// Deserialize JSON
let mut genesis_json: GenesisJson = serde_json::from_slice(&decoded)?;

// Populate name field
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you want to achieve here? The name of the class is supposed to be given by the user into the genesis json file. :) We don't need to retrieve it from the artifact file itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. I have removed the for loop

(class_hash, class_hash, None, Arc::new(CompiledContractClass::V0(casm)))
}
};

// Add the class name and class hash to the mapping
let _ = name.map(|name| {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't want the user to define 2 classes with the same name. Could we add a check to ensure that the names are unique for only ONE class?

@glihm
Copy link
Collaborator

glihm commented Mar 30, 2024

I have run the rust_fmt script

Thanks for that. You could also run the clippy script to prepare your code for the CI. 👍

@Yogalholic
Copy link
Contributor Author

I have implemented every suggestion, the code is compiling fine. I updated the first unit test (deserialize_from_json() ) and modified the test-genesis.json to replace the "0x08" classhash with the random name "MyErc20". The test is successful and now I will update the rest of them.

@glihm
Copy link
Collaborator

glihm commented Apr 15, 2024

I have implemented every suggestion, the code is compiling fine. I updated the first unit test (deserialize_from_json() ) and modified the test-genesis.json to replace the "0x08" classhash with the random name "MyErc20". The test is successful and now I will update the rest of them.

Awesome, thanks for the heads up @Yogalholic and the work done here. A recent work was merged on Katana for the allocations to compute the total supply automatically for the fee token. You have a rebase here. Don't hesitate to ping us if you have any question/difficulty while merging.

@glihm
Copy link
Collaborator

glihm commented Apr 22, 2024

@Yogalholic don't forget to rebase on main to ensure tests can be run on the CI. 👍

@Yogalholic Yogalholic reopened this May 12, 2024
@Yogalholic
Copy link
Contributor Author

Yogalholic commented May 12, 2024

I have finished working on the issue, the code compiles and tests are successful but clippy is failing
I had to restart from scratch because of the rebase

@Yogalholic Yogalholic marked this pull request as ready for review May 12, 2024 20:18
@Yogalholic Yogalholic closed this May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor katana This issue is related to Katana
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Katana] Allow choosing the class in the genesis file without affecting the actual class hash
3 participants