Skip to content

Commit

Permalink
optimize claim message
Browse files Browse the repository at this point in the history
  • Loading branch information
zkbenny committed Mar 29, 2024
1 parent 09c7ffc commit 2e8927d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
48 changes: 40 additions & 8 deletions contracts/Arbitrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ contract Arbitrator is IArbitrator, OwnableUpgradeable, UUPSUpgradeable, Reentra
mapping(IL1Gateway => DoubleEndedQueueUpgradeable.Bytes32Deque) public secondaryChainMessageHashQueues;
/// @notice List of permitted relayers
mapping(address relayerAddress => bool isRelayer) public relayers;
/// @dev The msg value and adapter params are used to forward a l2 message from source chain to target chain
bool private claiming;
uint256 private claimMsgValue;
bytes private claimAdapterParams;
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[50] private __gap;
uint256[47] private __gap;

/// @notice Primary chain gateway init
event InitPrimaryChain(IL1Gateway indexed gateway);
Expand All @@ -44,8 +48,6 @@ contract Arbitrator is IArbitrator, OwnableUpgradeable, UUPSUpgradeable, Reentra
event ValidatorStatusUpdate(IL1Gateway indexed gateway, address validatorAddress, bool isActive);
/// @notice Fee params for L1->L2 transactions changed
event NewFeeParams(IL1Gateway indexed gateway, FeeParams newFeeParams);
/// @notice Emit when receive message from l1 gateway
event MessageReceived(uint256 value, bytes callData);
/// @notice Emit when forward message to l1 gateway
event MessageForwarded(IL1Gateway indexed gateway, uint256 value, bytes callData);

Expand Down Expand Up @@ -137,18 +139,26 @@ contract Arbitrator is IArbitrator, OwnableUpgradeable, UUPSUpgradeable, Reentra
emit NewFeeParams(_gateway, _newFeeParams);
}

/// @dev This function is called within the `claimMessageCallback` of L1 gateway
function receiveMessage(uint256 _value, bytes calldata _callData) external payable {
// `claiming`, `claimMsgValue` and `claimAdapterParams` are transient values and set in `claimMessage`
// Ensure claim start from call `claimMessage`
require(claiming, "Invalid claim");
require(msg.value == _value, "Invalid msg value");
// store message hash for forwarding
bytes32 finalizeMessageHash = keccak256(abi.encode(_value, _callData));
IL1Gateway gateway = IL1Gateway(msg.sender);
// Ensure the caller is L1 gateway
if (gateway == primaryChainGateway) {
primaryChainMessageHashQueue.pushBack(finalizeMessageHash);
// Unpack destination chain and final callData
(IL1Gateway secondaryChainGateway, bytes memory finalCallData) = abi.decode(_callData, (IL1Gateway, bytes));
require(secondaryChainGateways[secondaryChainGateway], "Invalid secondary chain gateway");
// Forward fee to send message
secondaryChainGateway.sendMessage{value: claimMsgValue + _value}(_value, finalCallData, claimAdapterParams);
} else {
require(secondaryChainGateways[gateway], "Not secondary chain gateway");
secondaryChainMessageHashQueues[gateway].pushBack(finalizeMessageHash);
// Forward fee to send message
primaryChainGateway.sendMessage{value: claimMsgValue + _value}(_value, _callData, claimAdapterParams);
}
emit MessageReceived(_value, _callData);
emit MessageForwarded(gateway, _value, _callData);
}

function forwardMessage(
Expand Down Expand Up @@ -176,4 +186,26 @@ contract Arbitrator is IArbitrator, OwnableUpgradeable, UUPSUpgradeable, Reentra
}
emit MessageForwarded(_gateway, _value, _callData);
}

function claimMessage(
address _sourceChainCanonicalMessageService,
bytes calldata _sourceChainClaimCallData,
bytes memory _targetChainAdapterParams
) external payable nonReentrant onlyRelayer {
// The `claiming`, `claimMsgValue` and `claimAdapterParams` will be cleared after tx executed
assembly {
tstore(claiming.slot, true)
tstore(claimMsgValue.slot, callvalue())
tstore(claimAdapterParams.slot, _targetChainAdapterParams)
}
// Call the claim interface of source chain message service
// And it will inner call the `claimCallback` interface of source chain L1Gateway
(bool success, bytes memory returnData) = _sourceChainCanonicalMessageService.call(_sourceChainClaimCallData);
if (!success) {
// Propagate an error if the call fails.
assembly {
revert(add(returnData, 0x20), mload(returnData))
}
}
}
}
8 changes: 8 additions & 0 deletions contracts/dev-contracts/DummyArbitrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ contract DummyArbitrator is IArbitrator, OwnableUpgradeable, UUPSUpgradeable, Re
// Forward fee to send message
_gateway.sendMessage{value: msg.value + _value}(_value, _callData, _adapterParams);
}

function claimMessage(
address,
bytes calldata,
bytes memory
) external payable {
// do nothing
}
}
10 changes: 10 additions & 0 deletions contracts/interfaces/IArbitrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ interface IArbitrator {
bytes calldata _callData,
bytes calldata _adapterParams
) external payable;

/// @notice Claim a message of source chain and deliver it to the target chain
/// @param _sourceChainCanonicalMessageService The message service to claim message
/// @param _sourceChainClaimCallData The call data that need to claim message from source chain
/// @param _targetChainAdapterParams Some params need to call canonical message service of target chain
function claimMessage(
address _sourceChainCanonicalMessageService,
bytes calldata _sourceChainClaimCallData,
bytes memory _targetChainAdapterParams
) external payable;
}
13 changes: 13 additions & 0 deletions hardhat.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ const hardhatUserConfig = {
},
},
],
overrides: {
"contracts/Arbitrator.sol": {
version: "0.8.25",
settings: {
viaIR: true,
optimizer: {
enabled: true,
runs: 200,
},
evmVersion: "cancun",
},
}
}
},
networks: {
hardhat: {
Expand Down

0 comments on commit 2e8927d

Please sign in to comment.