Skip to content

Commit

Permalink
Merge branch 'SC-452-refactor-forwards-receivers' into SC-437-fix-cov…
Browse files Browse the repository at this point in the history
…erage
  • Loading branch information
hexonaut committed Jun 8, 2024
2 parents 46ee62c + 7eb7d7a commit bc5911a
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
13 changes: 6 additions & 7 deletions src/receivers/AMBReceiver.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.0;

import { Address } from "lib/openzeppelin-contracts/contracts/utils/Address.sol";

interface IArbitraryMessagingBridge {
function messageSender() external view returns (address);
function messageSourceChainId() external view returns (bytes32);
Expand All @@ -12,6 +14,8 @@ interface IArbitraryMessagingBridge {
*/
contract AMBReceiver {

using Address for address;

address public immutable amb;
bytes32 public immutable sourceChainId;
address public immutable sourceAuthority;
Expand All @@ -29,17 +33,12 @@ contract AMBReceiver {
target = _target;
}

function forward(bytes memory message) external {
fallback(bytes calldata message) external returns (bytes memory) {
require(msg.sender == amb, "AMBReceiver/invalid-sender");
require(IArbitraryMessagingBridge(amb).messageSourceChainId() == sourceChainId, "AMBReceiver/invalid-sourceChainId");
require(IArbitraryMessagingBridge(amb).messageSender() == sourceAuthority, "AMBReceiver/invalid-sourceAuthority");

(bool success, bytes memory ret) = target.call(message);
if (!success) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
return target.functionCall(message);
}

}
13 changes: 6 additions & 7 deletions src/receivers/ArbitrumReceiver.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.0;

import { Address } from "lib/openzeppelin-contracts/contracts/utils/Address.sol";

/**
* @title ArbitrumReceiver
* @notice Receive messages to an Arbitrum-style chain.
*/
contract ArbitrumReceiver {

using Address for address;

address public immutable l1Authority;
address public immutable target;

Expand All @@ -24,15 +28,10 @@ contract ArbitrumReceiver {
}
}

function forward(bytes memory message) external {
fallback(bytes calldata message) external returns (bytes memory) {
require(_getL1MessageSender() == l1Authority, "ArbitrumReceiver/invalid-l1Authority");

(bool success, bytes memory ret) = target.call(message);
if (!success) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
return target.functionCall(message);
}

}
11 changes: 5 additions & 6 deletions src/receivers/CCTPReceiver.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.0;

import { Address } from "lib/openzeppelin-contracts/contracts/utils/Address.sol";

/**
* @title CCTPReceiver
* @notice Receive messages from CCTP-style bridge.
*/
contract CCTPReceiver {

using Address for address;

address public immutable destinationMessenger;
uint32 public immutable sourceDomainId;
bytes32 public immutable sourceAuthority;
Expand All @@ -33,12 +37,7 @@ contract CCTPReceiver {
require(sourceDomainId == sourceDomain, "CCTPReceiver/invalid-sourceDomain");
require(sender == sourceAuthority, "CCTPReceiver/invalid-sourceAuthority");

(bool success, bytes memory ret) = target.call(messageBody);
if (!success) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
target.functionCall(messageBody);

return true;
}
Expand Down
13 changes: 6 additions & 7 deletions src/receivers/OptimismReceiver.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.0;

import { Address } from "lib/openzeppelin-contracts/contracts/utils/Address.sol";

interface ICrossDomainOptimism {
function xDomainMessageSender() external view returns (address);
}
Expand All @@ -11,6 +13,8 @@ interface ICrossDomainOptimism {
*/
contract OptimismReceiver {

using Address for address;

ICrossDomainOptimism public constant l2CrossDomain = ICrossDomainOptimism(0x4200000000000000000000000000000000000007);

address public immutable l1Authority;
Expand All @@ -24,16 +28,11 @@ contract OptimismReceiver {
target = _target;
}

function forward(bytes memory message) external {
fallback(bytes calldata message) external returns (bytes memory) {
require(msg.sender == address(l2CrossDomain), "OptimismReceiver/invalid-sender");
require(l2CrossDomain.xDomainMessageSender() == l1Authority, "OptimismReceiver/invalid-l1Authority");

(bool success, bytes memory ret) = target.call(message);
if (!success) {
assembly {
revert(add(ret, 0x20), mload(ret))
}
}
return target.functionCall(message);
}

}
2 changes: 1 addition & 1 deletion test/ArbitrumIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract ArbitrumIntegrationTest is IntegrationBaseTest {
ArbitrumForwarder.sendMessageL1toL2(
bridge.sourceCrossChainMessenger,
destinationReceiver,
abi.encodeCall(ArbitrumReceiver.forward, (message)),
message,
100000,
1 gwei,
block.basefee + 10 gwei
Expand Down
6 changes: 3 additions & 3 deletions test/GnosisIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract GnosisIntegrationTest is IntegrationBaseTest {

vm.prank(randomAddress);
vm.expectRevert("AMBReceiver/invalid-sender");
AMBReceiver(destinationReceiver).forward(abi.encodeCall(MessageOrdering.push, (1)));
MessageOrdering(destinationReceiver).push(1);
}

function test_invalidSourceChainId() public {
Expand Down Expand Up @@ -76,15 +76,15 @@ contract GnosisIntegrationTest is IntegrationBaseTest {
function queueSourceToDestination(bytes memory message) internal override {
AMBForwarder.sendMessageEthereumToGnosisChain(
destinationReceiver,
abi.encodeCall(AMBReceiver.forward, (message)),
message,
100000
);
}

function queueDestinationToSource(bytes memory message) internal override {
AMBForwarder.sendMessageGnosisChainToEthereum(
sourceReceiver,
abi.encodeCall(AMBReceiver.forward, (message)),
message,
100000
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/OptimismIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract OptimismIntegrationTest is IntegrationBaseTest {

vm.prank(randomAddress);
vm.expectRevert("OptimismReceiver/invalid-sender");
OptimismReceiver(destinationReceiver).forward(abi.encodeCall(MessageOrdering.push, (1)));
MessageOrdering(destinationReceiver).push(1);
}

function test_optimism() public {
Expand All @@ -63,7 +63,7 @@ contract OptimismIntegrationTest is IntegrationBaseTest {
OptimismForwarder.sendMessageL1toL2(
bridge.sourceCrossChainMessenger,
destinationReceiver,
abi.encodeCall(OptimismReceiver.forward, (message)),
message,
100000
);
}
Expand Down

0 comments on commit bc5911a

Please sign in to comment.