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

feat: MsgToL1 hash computation #476

Merged
merged 6 commits into from
Nov 4, 2023
Merged

Conversation

glihm
Copy link
Contributor

@glihm glihm commented Sep 28, 2023

Related to #448.

As suggested in the issue, this is the responsibility of L1HandlerTransaction to expose it's message that can be parsed from it's content.

The use of EthAddress solve the issue concern about a proper type to handle the from_address field.

Also, this PR includes the computation of MsgToL1, which is very practical in some scenarios when testing auto-consume of messages on L1.

@glihm glihm changed the title feat: add L1 to L2 message support for L1HandlerTransaction feat: add MsgToL2 and MsgToL1 hash computation Sep 28, 2023
@glihm
Copy link
Contributor Author

glihm commented Oct 6, 2023

@xJonathanLEI could you try to re-run the action? Or could I do that on my end without adding a new commit?

@xJonathanLEI
Copy link
Owner

Rebased and squashed PR.

@xJonathanLEI
Copy link
Owner

I don't like how this impl uses BigUint to represent a Keccak hash. Keccak hashes are always 256 bits. You shouldn't need to resort to allocating for this.

@xJonathanLEI
Copy link
Owner

Also this impl uses the hasher inefficiently. You shouldn't need to build a buffer to feed the hasher at the end. The hasher takes slices.

pub fn parse_msg_to_l2(&self) -> MsgToL2 {
// TODO: is that necessary? As the sequencer
// itself is the one firing this kind of transaction?
assert!(!self.calldata.is_empty());
Copy link
Owner

Choose a reason for hiding this comment

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

This assert! is completely unacceptable. You're assuming that whatever self instances always come from a trusted source, which is not true. As a library, you cannot decide this for your users. Only invariants can be checked with panic in a library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it, thank you for pointing this out.

@xJonathanLEI
Copy link
Owner

The MsgToL2 part has been superseded by #495. For the MsgToL1 part though, I'm unable to confirm whether the to_address field should be padded to 32 bytes or not.

Based on the official docs, it's not padded (no wrapping wiith uint256() on ToAddress), unlike the L1->L2 counterpart. However, Starkscan shows hashes that are apparently computed with the 32-byte padding.

docs_screenshot

I'm holding off adding the L2->L1 hashing until this is resolved. Maybe @amanusk can shed some light on this.

@glihm
Copy link
Contributor Author

glihm commented Nov 3, 2023

The MsgToL2 part has been superseded by #495. For the MsgToL1 part though, I'm unable to confirm whether the to_address field should be padded to 32 bytes or not.

Based on the official docs, it's not padded (no wrapping wiith uint256() on ToAddress), unlike the L1->L2 counterpart. However, Starkscan shows hashes that are apparently computed with the 32-byte padding.

docs_screenshot

I'm holding off adding the L2->L1 hashing until this is resolved. Maybe @amanusk can shed some light on this.

If we take the code from Starknet Core Contract, the ToAddress must be padded as it's casted in u256 here. Maybe a missing detail in the documentation that can help having it clear without reading the L1 code.

    function consumeMessageFromL2(uint256 fromAddress, uint256[] calldata payload)
        external
        override
        returns (bytes32)
    {
        bytes32 msgHash = keccak256(
            abi.encodePacked(fromAddress, uint256(msg.sender), payload.length, payload)
        );

        require(l2ToL1Messages()[msgHash] > 0, "INVALID_MESSAGE_TO_CONSUME");
        emit ConsumedMessageToL1(fromAddress, msg.sender, payload);
        l2ToL1Messages()[msgHash] -= 1;
        return msgHash;
    }

@glihm
Copy link
Contributor Author

glihm commented Nov 3, 2023

I don't like how this impl uses BigUint to represent a Keccak hash. Keccak hashes are always 256 bits. You shouldn't need to resort to allocating for this.

It was a suggestion to perhaps have user not bothering for conversion. But as you mentioned, as a library, being as agnostic as we can is far better.

@glihm glihm changed the title feat: add MsgToL2 and MsgToL1 hash computation feat: MsgToL1 hash computation Nov 3, 2023
@xJonathanLEI
Copy link
Owner

xJonathanLEI commented Nov 4, 2023

Please rebase instead of merging.

@xJonathanLEI
Copy link
Owner

It was a suggestion to perhaps have user not bothering for conversion.

How do you know the user wants BigUint? Not to mention that BigUint requires an allocation.

Copy link
Owner

@xJonathanLEI xJonathanLEI left a comment

Choose a reason for hiding this comment

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

LGTM

@xJonathanLEI xJonathanLEI merged commit 94c3b6f into xJonathanLEI:master Nov 4, 2023
23 checks passed
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