Skip to content

Conversation

@james-a-morris
Copy link
Contributor

@james-a-morris james-a-morris commented Dec 13, 2023

This PR creates the Chain Adapter for Scroll.

Sepolia Deployments for L1 -> L2 communication:

Deployments:

Network Contract Name Address
Sepolia Scroll Adapter 0x99EC530a761E68a377593888D9504002Bd191717
Sepolia Mock Hubpool (for l1->l2 token transfers) 0xa9F2Ba6333B78375b967d39CFdB38feE28288F2c
Sepolia Hubpool (for SetDepositRoute) 0x14224e63716afAcE30C9a417E0542281869f7d9e
Sepolia Scroll Spoke Pool 0x76f3fe966F91602129cb278043239afBB7B7646A
Type L1 Transaction L2 Txn Confirmation
ERC-20 0xc2a...8d8 0xddd...125
WETH 0xb42...5c1 0xb1c...532
Set Deposit Route 0x4a9...546 0xbba...af0

Closes ACX-1753, ACX-1757, ACX-1765, ACX-1780

@linear
Copy link

linear bot commented Dec 13, 2023

@socket-security
Copy link

socket-security bot commented Dec 13, 2023

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@scroll-tech/contracts 0.1.0 None +0 198 kB turupawn
axios 1.6.2 None +0 1.8 MB jasonsaayman

@james-a-morris james-a-morris force-pushed the james/acx-1757-create-scroll-adapter branch 2 times, most recently from cc2df44 to 4d1ea9e Compare December 15, 2023 15:53
@james-a-morris james-a-morris marked this pull request as ready for review December 15, 2023 15:53
@james-a-morris james-a-morris force-pushed the james/acx-1757-create-scroll-adapter branch from 19dc2de to be030e4 Compare December 15, 2023 16:07
Copy link
Member

@nicholaspai nicholaspai left a comment

Choose a reason for hiding this comment

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

This looks great, how did it perform with integration tests?

@james-a-morris james-a-morris force-pushed the james/acx-1757-create-scroll-adapter branch from c25b598 to 052c15e Compare December 18, 2023 16:44
Copy link
Contributor

@pxrl pxrl left a comment

Choose a reason for hiding this comment

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

Some quick initial comments.

@james-a-morris james-a-morris force-pushed the james/acx-1757-create-scroll-adapter branch from 2f99efd to 2130bf9 Compare December 19, 2023 19:47
@james-a-morris james-a-morris changed the base branch from master to james/rework-deploy-proxy December 19, 2023 19:48
@james-a-morris james-a-morris requested a review from pxrl December 19, 2023 19:57
Base automatically changed from james/rework-deploy-proxy to master December 20, 2023 17:13
james-a-morris and others added 10 commits December 20, 2023 12:22
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Co-authored-by: Paul <108695806+pxrl@users.noreply.github.com>
@james-a-morris james-a-morris force-pushed the james/acx-1757-create-scroll-adapter branch from 8bd2e50 to 7be19cc Compare December 20, 2023 17:22
@mrice32 mrice32 self-requested a review December 21, 2023 15:02
Copy link
Contributor

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

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

Looks great! A few questions

Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
Copy link
Contributor

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

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

Looks great! Just a few minor comments

// and it will not forward any ETH to the target contract on L2. However,
// we need to set the payable value to msg.value to ensure that the Scroll
// Bridge has enough gas to forward the message to L2.
l1ScrollMessenger.sendMessage{ value: _generateRelayerFee() }(target, 0, message, l2GasLimit);
Copy link
Contributor

Choose a reason for hiding this comment

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

If our gas limit is too high and gas is left unused on the destination, does that money get lost or is it refunded somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per the TG group chat with the Scroll team, our ETH may get burned if we overestimate our gas limit. However, we were told in a previous message that 250,000 is an acceptable amount.

@james-a-morris
Copy link
Contributor Author

@nicholaspai @mrice32 @pxrl updated the test txns in this PR's description to the latest txn tests as of 300735d

// the address that sent the message from L1 to L2. If the calling contract
// isn't the Scroll messenger, then the xdomainMessageSender will be the zero
// address and *NOT* cross domain admin.
address _xDomainSender = l2ScrollMessenger.xDomainMessageSender();
Copy link
Member

Choose a reason for hiding this comment

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

Can xDomainMessageSender be set to whatever a malicious contract wants it to or is it always the contract that is the target of the L1 to L2 message?

Copy link
Contributor Author

@james-a-morris james-a-morris Jan 4, 2024

Choose a reason for hiding this comment

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

(I'm going to go through each call in the stack so this might be longform, but tl;dr I believe we should be fine as long as we trust the chain)


Per master on scroll's official github ( link ), the xDomainMessenger is set to be the _from sender (sender of the L1 txn) here. The transaction stack is l2TokenMessenger calls relayMessage -> _executeMessage.

_executeMessage

function _executeMessage(
        address _from,
        address _to,
        uint256 _value,
        bytes memory _message,
        bytes32 _xDomainCalldataHash
    ) internal {
        // @note check more `_to` address to avoid attack in the future when we add more gateways.
        require(_to != messageQueue, "Forbid to call message queue");
        _validateTargetAddress(_to);

        // @note This usually will never happen, just in case.
        require(_from != xDomainMessageSender, "Invalid message sender"); 

the last line of this snippet always set to be the DEFAULT_XDOMAIN_MESSAGE_SENDER outside of this specific case. This is a call to ensure that doesn't happen

        xDomainMessageSender = _from;
        // solhint-disable-next-line avoid-low-level-calls
        (bool success, ) = _to.call{value: _value}(_message);
        // reset value to refund gas.
        xDomainMessageSender = ScrollConstants.DEFAULT_XDOMAIN_MESSAGE_SENDER; <--- Immediately resets
        ...
    }

In the above ^ it's only set to be read within the context of _to.call(...) and then is immediately reset

relayMessage

    function relayMessage(
        address _from,
        address _to,
        uint256 _value,
        uint256 _nonce,
        bytes memory _message
    ) external override whenNotPaused {
        // It is impossible to deploy a contract with the same address, reentrance is prevented in nature.
        require(AddressAliasHelper.undoL1ToL2Alias(_msgSender()) == counterpart, "Caller is not L1ScrollMessenger");

        bytes32 _xDomainCalldataHash = keccak256(_encodeXDomainCalldata(_from, _to, _value, _nonce, _message));

        require(!isL1MessageExecuted[_xDomainCalldataHash], "Message was already successfully executed");

        _executeMessage(_from, _to, _value, _message, _xDomainCalldataHash);
    }

From the above, what we're really looking for is that _msgSender() which is the msg.sender is the counterpart domain of the L1ScrollMessenger. Ergo if we pass that require, it must be the case that the calling contract is the trusted L2ScrollMessenger.

L2ScrollMessenger

We have an implicit trust that the L2ScrollMessenger is going to set things right.

Copy link
Member

@nicholaspai nicholaspai left a comment

Choose a reason for hiding this comment

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

This looks great. LGTM after we get an answer about excess xchain message fees and I also left two additional comments

@linear
Copy link

linear bot commented Jan 4, 2024

@james-a-morris
Copy link
Contributor Author

@nicholaspai @mrice32 @dohaki I've added a second gas limit so that we can differentiate between arbitrary messages & token relays

Copy link
Contributor

@dohaki dohaki left a comment

Choose a reason for hiding this comment

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

🔥 LGTM

* @notice Generates the relayer fee for a message to be sent to L2.
* @dev Function will revert if the contract does not have enough ETH to pay the fee.
*/
function _generateRelayerFeeForMessageRelay() internal view returns (uint256 l2Fee) {
Copy link
Member

Choose a reason for hiding this comment

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

would have implemented this with a single internal function where you pass in uint256 l2GasLimit.

Then, within relayTokens and relayMessage you pass in a different constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done: e9b4443

Copy link
Member

@nicholaspai nicholaspai left a comment

Choose a reason for hiding this comment

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

Looks good to go, I left two comments on making code better

Copy link
Contributor

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

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

LGTM!

james-a-morris and others added 2 commits January 5, 2024 16:02
Co-authored-by: nicholaspai <9457025+nicholaspai@users.noreply.github.com>
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
@james-a-morris james-a-morris merged commit aed996b into master Jan 6, 2024
@james-a-morris james-a-morris deleted the james/acx-1757-create-scroll-adapter branch January 6, 2024 21:33
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