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

refactor: gateway compiler handle declare tx #439

Merged

Conversation

ArniStarkware
Copy link
Contributor

@ArniStarkware ArniStarkware commented Jul 11, 2024

This change is Reviewable

Copy link
Contributor Author

ArniStarkware commented Jul 11, 2024

@codecov-commenter
Copy link

codecov-commenter commented Jul 11, 2024

Codecov Report

Attention: Patch coverage is 81.25000% with 9 lines in your changes missing coverage. Please review.

Project coverage is 83.64%. Comparing base (d09a690) to head (d6075c4).

Files Patch % Lines
crates/gateway/src/compilation.rs 81.39% 5 Missing and 3 partials ⚠️
crates/gateway/src/gateway.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #439      +/-   ##
==========================================
+ Coverage   83.59%   83.64%   +0.04%     
==========================================
  Files          37       37              
  Lines        1780     1785       +5     
  Branches     1780     1785       +5     
==========================================
+ Hits         1488     1493       +5     
  Misses        215      215              
  Partials       77       77              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/add_to_app_state branch from 9152413 to 6265f87 Compare July 11, 2024 08:26
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from c22f909 to e853a8a Compare July 11, 2024 08:26
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/add_to_app_state branch from 6265f87 to e470743 Compare July 11, 2024 08:43
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from e853a8a to ba27f39 Compare July 11, 2024 08:43
@ArniStarkware ArniStarkware marked this pull request as ready for review July 11, 2024 08:47
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from ba27f39 to 4d7678f Compare July 11, 2024 08:58
Copy link
Contributor

@Yael-Starkware Yael-Starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 6 of 6 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ArniStarkware and @yair-starkware)


crates/gateway/src/compilation.rs line 46 at r1 (raw file):

        self.validate_casm_class(&casm_contract_class)?;

        build_result_class_info(

Suggestion:

into_class_info

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 4d7678f to f8e4a28 Compare July 11, 2024 11:10
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/add_to_app_state branch from e470743 to 87825c1 Compare July 11, 2024 11:13
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch 2 times, most recently from 57fc04f to 9b3a9ac Compare July 11, 2024 11:32
Copy link
Contributor Author

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 2 of 7 files reviewed, 1 unresolved discussion (waiting on @Yael-Starkware and @yair-starkware)


crates/gateway/src/compilation.rs line 46 at r1 (raw file):

        self.validate_casm_class(&casm_contract_class)?;

        build_result_class_info(

Done.

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 9b3a9ac to 69da6de Compare July 11, 2024 13:37
Copy link
Contributor

@Yael-Starkware Yael-Starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 11 files reviewed, 5 unresolved discussions (waiting on @ArniStarkware and @yair-starkware)


crates/gateway/src/compilation.rs line 118 at r3 (raw file):

fn validate_compiled_class_hash(
    casm_contract_class: &CasmContractClass,
    suppled_compiled_class_hash: CompiledClassHash,

Suggestion:

supplied_compiled_class_hash

crates/gateway/src/compilation_test.rs line 25 at r3 (raw file):

#[rstest]
fn test_compile_contract_class_compiled_class_hash_missmatch(

Suggestion:

mismatch

crates/gateway/src/compilation_test.rs line 37 at r3 (raw file):

        GatewayError::CompiledClassHashMismatch { supplied, hash_result }
        if supplied == supplied_hash && hash_result == expected_hash_result
    );

you can use assert_eq here

Code quote:

    assert_matches!(
        result.unwrap_err(),
        GatewayError::CompiledClassHashMismatch { supplied, hash_result }
        if supplied == supplied_hash && hash_result == expected_hash_result
    );

crates/gateway/src/compilation_test.rs line 55 at r3 (raw file):

            AllowedLibfuncsError::SierraProgramError
        ))
    )

same

Code quote:

    assert_matches!(
        result.unwrap_err(),
        GatewayError::CompilationError(CompilationUtilError::AllowedLibfuncsError(
            AllowedLibfuncsError::SierraProgramError
        ))
    )

crates/gateway/src/gateway.rs line 131 at r3 (raw file):

    let optional_class_info = match &tx {
        RPCTransaction::Declare(declare_tx) => {
            Some(gateway_compiler.handle_declare_tx(declare_tx)?)

not sure about the name, it is a bit too general
declare_tx_is handled also in the stateful and stateless validator.

how about process_declare_tx / calculate_class_info ?

Code quote:

handle_declare_tx

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/add_to_app_state branch from 87825c1 to e59df3a Compare July 14, 2024 07:59
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 69da6de to 19028b3 Compare July 14, 2024 07:59
Copy link
Contributor Author

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 11 files reviewed, 5 unresolved discussions (waiting on @Yael-Starkware and @yair-starkware)


crates/gateway/src/compilation.rs line 118 at r3 (raw file):

fn validate_compiled_class_hash(
    casm_contract_class: &CasmContractClass,
    suppled_compiled_class_hash: CompiledClassHash,

Done.


crates/gateway/src/compilation_test.rs line 25 at r3 (raw file):

#[rstest]
fn test_compile_contract_class_compiled_class_hash_missmatch(

Done.


crates/gateway/src/compilation_test.rs line 37 at r3 (raw file):

Previously, Yael-Starkware (YaelD) wrote…

you can use assert_eq here

The enum GatewayError can not derive PartialEq (it contains many variants that do not support it, like JoinError).


crates/gateway/src/compilation_test.rs line 55 at r3 (raw file):

Previously, Yael-Starkware (YaelD) wrote…

same

Same.


crates/gateway/src/gateway.rs line 131 at r3 (raw file):

Previously, Yael-Starkware (YaelD) wrote…

not sure about the name, it is a bit too general
declare_tx_is handled also in the stateful and stateless validator.

how about process_declare_tx / calculate_class_info ?

  1. Done.
  2. I think part of the issue is that the struct is called gateway_compiler. Do you have a better suggestion for a name for it?
  3. We look for a verb that says "doing this component's job on a declare tx".

Copy link
Contributor

@Yael-Starkware Yael-Starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 11 files reviewed, all discussions resolved (waiting on @yair-starkware)


crates/gateway/src/gateway.rs line 131 at r3 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…
  1. Done.
  2. I think part of the issue is that the struct is called gateway_compiler. Do you have a better suggestion for a name for it?
  3. We look for a verb that says "doing this component's job on a declare tx".

All it does is compilation and validation of the related fields , right?
so maybe Compiler is fine.
why is the word gateway needed? since it uses gateway size limits?
sounds ok to me.

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/add_to_app_state branch from e59df3a to 33dbd4d Compare July 14, 2024 11:13
@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch 2 times, most recently from b5f3c66 to 620444a Compare July 14, 2024 11:18
Copy link
Contributor

@yair-starkware yair-starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: 7 of 11 files reviewed, all discussions resolved (waiting on @dafnamatsry)

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 71fdc0a to 81954d0 Compare July 17, 2024 07:34
Copy link
Collaborator

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 6 files at r4, 2 of 4 files at r6, 1 of 5 files at r7, all commit messages.
Reviewable status: 7 of 11 files reviewed, 9 unresolved discussions (waiting on @ArniStarkware and @yair-starkware)


crates/gateway/src/compilation.rs line 56 at r7 (raw file):

    fn compile(
        &self,
        cairo_lang_contract_class: CairoLangContractClass,

Suggestion:

cairo_lang_contract_class: &CairoLangContractClass

crates/gateway/src/compilation.rs line 66 at r7 (raw file):

                Err(GatewayError::CompilationError(CompilationUtilError::CompilationPanic))
            }
        }

Please use map_err.

Code quote:

        match catch_unwind_result {
            Ok(compilation_result) => Ok(compilation_result?),
            Err(_) => {
                // TODO(Arni): Log the panic.
                Err(GatewayError::CompilationError(CompilationUtilError::CompilationPanic))
            }
        }

crates/gateway/src/compilation.rs line 104 at r7 (raw file):

/// Validates that the compiled class hash of the compiled contract class matches the supplied
/// compiled class hash.
fn validate_compiled_class_hash(

Should it be also a method of the Compiler like validate_casm_class?

Code quote:

validate_compiled_class_hash

crates/gateway/src/compilation.rs line 106 at r7 (raw file):

fn validate_compiled_class_hash(
    casm_contract_class: &CasmContractClass,
    supplied_compiled_class_hash: CompiledClassHash,

Suggestion:

&CompiledClassHash

crates/gateway/src/compilation_test.rs line 30 at r7 (raw file):

) {
    let supplied_hash = CompiledClassHash::default();
    let expected_hash_result = compiled_class_hash;

Suggestion:

    let wrong_supplied_hash = CompiledClassHash::default();
    let expected_hash = compiled_class_hash;

crates/gateway/src/compilation_test.rs line 32 at r7 (raw file):

    let expected_hash_result = compiled_class_hash;

    let result = validate_compiled_class_hash(&casm_contract_class, supplied_hash);

Please call process_declare_tx which is the public API of the compiler, and the one we want to test.

Code quote:

let result = validate_compiled_class_hash(&casm_contract_class, supplied_hash);

crates/gateway/src/compilation_test.rs line 45 at r7 (raw file):

    mut contract_class: RpcContractClass,
) {
    // Create a currupted contract class.

Suggestion:

corrupted

crates/gateway/src/compilation_test.rs line 49 at r7 (raw file):

    let cairo_lang_contract_class = into_contract_class_for_compilation(&contract_class);
    let result = gateway_compiler.compile(cairo_lang_contract_class);

Same here. call process_declare_tx.

Code quote:

gateway_compiler.compile

crates/gateway/src/compilation_test.rs line 59 at r7 (raw file):

#[rstest]
fn test_handle_declare_tx(gateway_compiler: GatewayCompiler) {

Suggestion:

test_process_declare_tx_success

Copy link
Collaborator

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 6 files at r1, 2 of 9 files at r3, 3 of 5 files at r7.
Reviewable status: 10 of 11 files reviewed, 9 unresolved discussions (waiting on @ArniStarkware and @yair-starkware)

Copy link
Contributor Author

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 10 of 11 files reviewed, 9 unresolved discussions (waiting on @dafnamatsry, @dorimedini-starkware, and @yair-starkware)


crates/gateway/src/compilation.rs line 56 at r7 (raw file):

    fn compile(
        &self,
        cairo_lang_contract_class: CairoLangContractClass,

The function: CasmContractClass::from_contract_class (from the cairo-lang crate) takes ownership of the CairoLangContractClass


crates/gateway/src/compilation.rs line 66 at r7 (raw file):

Previously, dafnamatsry wrote…

Please use map_err.

Done.
(Thank @dorimedini-starkware - for making us not overthink stuff).


crates/gateway/src/compilation.rs line 104 at r7 (raw file):

Previously, dafnamatsry wrote…

Should it be also a method of the Compiler like validate_casm_class?

I did not make it a method as it does not use self. I have no objection.


crates/gateway/src/compilation.rs line 106 at r7 (raw file):

fn validate_compiled_class_hash(
    casm_contract_class: &CasmContractClass,
    supplied_compiled_class_hash: CompiledClassHash,

Done.
Does not make much of a difference as this struct implements Copy.


crates/gateway/src/compilation_test.rs line 30 at r7 (raw file):

) {
    let supplied_hash = CompiledClassHash::default();
    let expected_hash_result = compiled_class_hash;

Done.


crates/gateway/src/compilation_test.rs line 32 at r7 (raw file):

Previously, dafnamatsry wrote…

Please call process_declare_tx which is the public API of the compiler, and the one we want to test.

I disagree. This is a unit test for the validate_compiled_class_hash function.
(the declare tx is irrelevant).

This was the original intent for this refactor, per @yair-starkware 's suggestion.


crates/gateway/src/compilation_test.rs line 45 at r7 (raw file):

    mut contract_class: RpcContractClass,
) {
    // Create a currupted contract class.

Done.


crates/gateway/src/compilation_test.rs line 49 at r7 (raw file):

Previously, dafnamatsry wrote…

Same here. call process_declare_tx.

This is a unit test for compile.

(the declare tx is irrelevant).


crates/gateway/src/compilation_test.rs line 59 at r7 (raw file):

#[rstest]
fn test_handle_declare_tx(gateway_compiler: GatewayCompiler) {

Done.

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 81954d0 to 989780d Compare July 18, 2024 12:48
Copy link

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 8 of 11 files reviewed, 9 unresolved discussions (waiting on @dafnamatsry and @yair-starkware)


crates/gateway/src/compilation.rs line 66 at r7 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

Done.
(Thank @dorimedini-starkware - for making us not overthink stuff).

🫡

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 989780d to 00a1079 Compare July 18, 2024 13:28
Copy link
Collaborator

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 5 files at r7, 1 of 2 files at r8, 1 of 1 files at r9, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @ArniStarkware and @yair-starkware)


crates/gateway/src/compilation.rs line 104 at r7 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

I did not make it a method as it does not use self. I have no objection.

The other validate_* method gets self, but does not use it, so just make them consistent with each other.


crates/gateway/src/compilation_test.rs line 32 at r7 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

I disagree. This is a unit test for the validate_compiled_class_hash function.
(the declare tx is irrelevant).

This was the original intent for this refactor, per @yair-starkware 's suggestion.

Discussed f2f.


crates/gateway/src/compilation_test.rs line 49 at r7 (raw file):

Previously, ArniStarkware (Arnon Hod) wrote…

This is a unit test for compile.

(the declare tx is irrelevant).

Discussed f2f.

Copy link
Contributor Author

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 of 11 files reviewed, 3 unresolved discussions (waiting on @dafnamatsry and @yair-starkware)


crates/gateway/src/compilation.rs line 104 at r7 (raw file):

Previously, dafnamatsry wrote…

The other validate_* method gets self, but does not use it, so just make them consistent with each other.

Done*.
The method, compile still keeps its self. In the following PR: #348 compile uses self.


crates/gateway/src/compilation_test.rs line 32 at r7 (raw file):

Previously, dafnamatsry wrote…

Discussed f2f.

Done.


crates/gateway/src/compilation_test.rs line 49 at r7 (raw file):

Previously, dafnamatsry wrote…

Discussed f2f.

Done.

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch 2 times, most recently from 4ab9474 to 33642fb Compare July 22, 2024 11:14
Copy link
Collaborator

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

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

Reviewed 7 of 7 files at r10, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @ArniStarkware and @yair-starkware)


crates/gateway/src/compilation.rs line 54 at r10 (raw file):

    // TODO(Arni): Pass the compilation args from the config.
    // TODO(Arni): Pass the compiler with dependancy injection.

Note that the compiler should be injected to process_declare_tx and not here, which is the actual compile implementation.

Code quote:

// TODO(Arni): Pass the compiler with dependancy injection.

crates/gateway/src/compilation_test.rs line 29 at r10 (raw file):

    tx.compiled_class_hash = supplied_hash;
    let declare_tx = RPCDeclareTransaction::V3(tx);

Suggestion:

    let expected_hash = tx.compiled_class_hash;
    let wrong_supplied_hash = CompiledClassHash::default();
    tx.compiled_class_hash = wrong_supplied_hash;

    let declare_tx = RPCDeclareTransaction::V3(tx);

crates/mempool_test_utils/src/starknet_api_test_utils.rs line 95 at r10 (raw file):

/// Get the contract class used for testing.
#[fixture]

Not used as a fixture anymore I think. Also compiled_class_hash()

Code quote:

#[fixture]

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 33642fb to 7644b4d Compare July 23, 2024 10:47
Copy link
Contributor Author

@ArniStarkware ArniStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 8 of 11 files reviewed, 3 unresolved discussions (waiting on @dafnamatsry and @yair-starkware)


crates/gateway/src/compilation.rs line 54 at r10 (raw file):

Previously, dafnamatsry wrote…

Note that the compiler should be injected to process_declare_tx and not here, which is the actual compile implementation.

Moved the todo to a more relevant place.


crates/gateway/src/compilation_test.rs line 29 at r10 (raw file):

    tx.compiled_class_hash = supplied_hash;
    let declare_tx = RPCDeclareTransaction::V3(tx);

Done.


crates/mempool_test_utils/src/starknet_api_test_utils.rs line 95 at r10 (raw file):

Previously, dafnamatsry wrote…

Not used as a fixture anymore I think. Also compiled_class_hash()

Done.

Copy link
Collaborator

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 3 of 3 files at r11, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yair-starkware)

@ArniStarkware ArniStarkware force-pushed the arni/declare/compilation/restruct_gateway_compiler branch from 7644b4d to d6075c4 Compare July 23, 2024 11:10
Copy link
Collaborator

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r12, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @yair-starkware)

@ArniStarkware ArniStarkware merged commit fc5813a into main Jul 23, 2024
8 checks passed
@ArniStarkware ArniStarkware deleted the arni/declare/compilation/restruct_gateway_compiler branch August 21, 2024 07:17
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.

6 participants