-
Notifications
You must be signed in to change notification settings - Fork 4k
Add mocked message passing contracts #229
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
packages/contracts/contracts/optimistic-ethereum/bridge/CrossDomainMessenger.interface.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| pragma solidity ^0.5.0; | ||
|
|
||
| contract ICrossDomainMessenger { | ||
| address public crossDomainMsgSender; | ||
|
|
||
| /** | ||
| * Relays a message to a given target contract. | ||
| * @param _target Address of the target contract. | ||
| * @param _sender Address of the message sender. | ||
| * @param _message Calldata to relay. | ||
| */ | ||
| function relayMessage( | ||
| address _target, | ||
| address _sender, | ||
| bytes memory _message | ||
| ) public; | ||
|
|
||
| /** | ||
| * Sends a message to another cross domain messenger to be relayed. | ||
| * @param _target Address of the target contract. | ||
| * @param _message Calldata to relay. | ||
| */ | ||
| function sendMessage( | ||
| address _target, | ||
| bytes memory _message | ||
| ) public; | ||
| } |
70 changes: 70 additions & 0 deletions
70
packages/contracts/contracts/optimistic-ethereum/bridge/MockCrossDomainMessenger.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| pragma solidity ^0.5.0; | ||
|
|
||
| /* Interface Imports */ | ||
| import { ICrossDomainMessenger } from "./CrossDomainMessenger.interface.sol"; | ||
|
|
||
| /** | ||
| * @title MockCrossDomainMessenger | ||
| */ | ||
| contract MockCrossDomainMessenger is ICrossDomainMessenger { | ||
| /* | ||
| * Contract Variables | ||
| */ | ||
|
|
||
| ICrossDomainMessenger targetMessenger; | ||
| address public crossDomainMsgSender; | ||
|
|
||
| /* | ||
| * Public Functions | ||
| */ | ||
|
|
||
| /** | ||
| * Relays a message to a target contract. | ||
| * .inheritdoc ICrossDomainMessenger | ||
| */ | ||
| function relayMessage( | ||
| address _target, | ||
| address _sender, | ||
| bytes memory _message | ||
| ) | ||
| public | ||
| { | ||
| crossDomainMsgSender = _sender; | ||
| (bool success,) = _target.call(_message); | ||
| require(success, "Received message reverted during execution."); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a message to the target messenger. | ||
| * .inheritdoc ICrossDomainMessenger | ||
| */ | ||
| function sendMessage( | ||
| address _target, | ||
| bytes memory _message | ||
| ) | ||
| public | ||
| { | ||
| require( | ||
| address(targetMessenger) != address(0), | ||
| "Cannot send a message without setting the target messenger." | ||
| ); | ||
|
|
||
| targetMessenger.relayMessage( | ||
| _target, | ||
| msg.sender, | ||
| _message | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the target messenger. | ||
| * @param _messenger Target messenger address. | ||
| */ | ||
| function setTargetMessenger( | ||
| address _messenger | ||
| ) | ||
| public | ||
| { | ||
| targetMessenger = ICrossDomainMessenger(_messenger); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
packages/contracts/contracts/test-helpers/CrossDomainSimpleStorage.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| pragma solidity ^0.5.0; | ||
|
|
||
| import { ICrossDomainMessenger} from "../optimistic-ethereum/bridge/CrossDomainMessenger.interface.sol"; | ||
| import { SimpleStorage } from "./SimpleStorage.sol"; | ||
|
|
||
| contract CrossDomainSimpleStorage is SimpleStorage { | ||
| ICrossDomainMessenger crossDomainMessenger; | ||
| address public crossDomainMsgSender; | ||
|
|
||
| function setMessenger(address _crossDomainMessengerAddress) public { | ||
| crossDomainMessenger = ICrossDomainMessenger(_crossDomainMessengerAddress); | ||
| } | ||
|
|
||
| function crossDomainSetStorage(bytes32 key, bytes32 value) public { | ||
| crossDomainMsgSender = crossDomainMessenger.crossDomainMsgSender(); | ||
| setStorage(key, value); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/contracts/test/contracts/bridge/MockCrossDomainMessenger.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { expect } from '../../setup' | ||
|
|
||
| /* External Imports */ | ||
| import { ethers } from '@nomiclabs/buidler' | ||
| import { ContractFactory, Contract, Signer } from 'ethers' | ||
| import { NULL_ADDRESS } from '@eth-optimism/core-utils' | ||
|
|
||
| describe('MockCrossDomainMessenger', () => { | ||
| let wallet: Signer | ||
| before(async () => { | ||
| ;[wallet] = await ethers.getSigners() | ||
| }) | ||
|
|
||
| let MockCrossDomainMessengerFactory: ContractFactory | ||
| let CrossDomainSimpleStorageFactory: ContractFactory | ||
| before(async () => { | ||
| MockCrossDomainMessengerFactory = await ethers.getContractFactory( | ||
| 'MockCrossDomainMessenger' | ||
| ) | ||
| CrossDomainSimpleStorageFactory = await ethers.getContractFactory( | ||
| 'CrossDomainSimpleStorage' | ||
| ) | ||
| }) | ||
|
|
||
| let L1MockCrossDomainMessenger: Contract | ||
| let L2MockCrossDomainMessenger: Contract | ||
| beforeEach(async () => { | ||
| L1MockCrossDomainMessenger = await MockCrossDomainMessengerFactory.deploy() | ||
| L2MockCrossDomainMessenger = await MockCrossDomainMessengerFactory.deploy() | ||
|
|
||
| await L1MockCrossDomainMessenger.setTargetMessenger( | ||
| L2MockCrossDomainMessenger.address | ||
| ) | ||
| await L2MockCrossDomainMessenger.setTargetMessenger( | ||
| L1MockCrossDomainMessenger.address | ||
| ) | ||
| }) | ||
|
|
||
| let L2SimpleStorage: Contract | ||
| beforeEach(async () => { | ||
| L2SimpleStorage = await CrossDomainSimpleStorageFactory.deploy() | ||
|
|
||
| await L2SimpleStorage.setMessenger(L2MockCrossDomainMessenger.address) | ||
| }) | ||
| describe('relayMessage', () => { | ||
| it('should successfully relay a message to the target receiver', async () => { | ||
| const expectedStorageKey = ethers.utils.keccak256('0x1234') | ||
| const expectedStorageValue = ethers.utils.keccak256('0x5678') | ||
|
|
||
| const calldata = L2SimpleStorage.interface.encodeFunctionData( | ||
| 'crossDomainSetStorage', | ||
| [expectedStorageKey, expectedStorageValue] | ||
| ) | ||
|
|
||
| const expectedMessage = [await wallet.getAddress(), calldata] | ||
|
|
||
| await L2MockCrossDomainMessenger.relayMessage( | ||
| L2SimpleStorage.address, | ||
| ...expectedMessage | ||
| ) | ||
|
|
||
| const actualStorageValue = await L2SimpleStorage.getStorage( | ||
| expectedStorageKey | ||
| ) | ||
| expect(actualStorageValue).to.equal(expectedStorageValue) | ||
| expect(await L2SimpleStorage.crossDomainMsgSender()).to.equal( | ||
| await wallet.getAddress() | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('sendMessage', () => { | ||
| it('should successfully send a message to another messenger', async () => { | ||
| const expectedStorageKey = ethers.utils.keccak256('0x1234') | ||
| const expectedStorageValue = ethers.utils.keccak256('0x5678') | ||
|
|
||
| const calldata = L2SimpleStorage.interface.encodeFunctionData( | ||
| 'crossDomainSetStorage', | ||
| [expectedStorageKey, expectedStorageValue] | ||
| ) | ||
|
|
||
| await L1MockCrossDomainMessenger.sendMessage( | ||
| L2SimpleStorage.address, | ||
| calldata, | ||
| { | ||
| from: await wallet.getAddress(), | ||
| } | ||
| ) | ||
|
|
||
| const currentBlock = await ethers.provider.getBlock('latest') | ||
| const expectedMessage = [await wallet.getAddress(), calldata] | ||
|
|
||
| const actualStorageValue = await L2SimpleStorage.getStorage( | ||
| expectedStorageKey | ||
| ) | ||
| expect(actualStorageValue).to.equal(expectedStorageValue) | ||
| expect(await L2SimpleStorage.crossDomainMsgSender()).to.equal( | ||
| await wallet.getAddress() | ||
| ) | ||
| }) | ||
|
|
||
| it('should revert if its target messenger is not set', async () => { | ||
| const expectedStorageKey = ethers.utils.keccak256('0x1234') | ||
| const expectedStorageValue = ethers.utils.keccak256('0x5678') | ||
|
|
||
| const calldata = L2SimpleStorage.interface.encodeFunctionData( | ||
| 'setStorage', | ||
| [expectedStorageKey, expectedStorageValue] | ||
| ) | ||
|
|
||
| await L1MockCrossDomainMessenger.setTargetMessenger(NULL_ADDRESS) | ||
|
|
||
| await expect( | ||
| L1MockCrossDomainMessenger.sendMessage( | ||
| L2SimpleStorage.address, | ||
| calldata, | ||
| { | ||
| from: await wallet.getAddress(), | ||
| } | ||
| ) | ||
| ).to.be.revertedWith( | ||
| 'Cannot send a message without setting the target messenger.' | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this needed for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's useful for being able to import the contracts into other packages