-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add CrosschainRemoteExecutor and GovernorCrosschain #6272
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
Changes from 15 commits
d3dbb62
2786db2
5003f4e
68384a5
415f5d8
4d79c70
a727e82
cef9e44
088a054
e4d3bb9
143da1d
9e21544
9b8f660
3438517
7e1f23d
268daab
fd2a401
6c9793e
1ed3b79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': minor | ||
| --- | ||
|
|
||
| `GovernorCrosschain`: Governor module that facilitates the execution of crosschain operations through CrosschainRemoteExecutors and ERC-7786 gateways. |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a side note, I think this remote executor could allow multichain accounts similar to Hyperlane's Interchain Accounts.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Executor is indeed designed to be the "remote identity" of any "account". Can be used by a governor, but also by a timelock, a multisig, or any account |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.27; | ||
|
|
||
| import {IERC7786GatewaySource} from "../interfaces/draft-IERC7786.sol"; | ||
| import {ERC7786Recipient} from "./ERC7786Recipient.sol"; | ||
| import {ERC7579Utils, Mode, CallType, ExecType} from "../account/utils/draft-ERC7579Utils.sol"; | ||
| import {Bytes} from "../utils/Bytes.sol"; | ||
|
|
||
| /** | ||
| * @dev Helper contract used to relay transactions received from a controller through an ERC-7786 gateway. This is | ||
| * used by the {GovernorCrosschain} governance module for the execution of cross-chain actions. | ||
| * | ||
| * A {CrosschainRemoteExecutor} address can be seen as the local identity of a remote executor on another chain. It | ||
| * holds assets and permissions for the sake of its controller. | ||
| */ | ||
| contract CrosschainRemoteExecutor is ERC7786Recipient { | ||
| using Bytes for bytes; | ||
| using ERC7579Utils for *; | ||
|
|
||
| /// @dev Gateway used by the remote controller to relay instructions to this executor. | ||
| address private _gateway; | ||
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// @dev InteroperableAddress of the remote controller that is allowed to relay instructions to this executor. | ||
| bytes private _controller; | ||
|
|
||
| /// @dev Emitted when the gateway or controller of this remote executor is updated. | ||
| event CrosschainControllerSet(address gateway, bytes controller); | ||
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// @dev Reverted when a non-controller tries to relay instructions to this executor. | ||
| error AccessRestricted(); | ||
|
|
||
| constructor(address initialGateway, bytes memory initialController) { | ||
| _setup(initialGateway, initialController); | ||
| } | ||
|
|
||
| /// @dev Accessor that returns the address of the gateway used by this remote executor. | ||
| function gateway() public view virtual returns (address) { | ||
| return _gateway; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Accessor that returns the interoperable address of the controller allowed to relay instructions to this | ||
| * remote executor. | ||
| */ | ||
| function controller() public view virtual returns (bytes memory) { | ||
| return _controller; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Endpoint allowing the controller to reconfigure the executor. This must be called by the executor itself | ||
| * following an instruction from the controller. | ||
| */ | ||
| function reconfigure(address newGateway, bytes memory newController) public virtual { | ||
| require(msg.sender == address(this), AccessRestricted()); | ||
| _setup(newGateway, newController); | ||
| } | ||
|
|
||
| /// @dev Internal setter to reconfigure the gateway and controller. | ||
| function _setup(address gateway_, bytes memory controller_) internal virtual { | ||
| // Sanity check, this should revert if gateway is not an ERC-7786 implementation. Note that since | ||
| // supportsAttribute returns data, an EOA would fail that test (nothing returned). | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| IERC7786GatewaySource(gateway_).supportsAttribute(bytes4(0)); | ||
|
|
||
| _gateway = gateway_; | ||
| _controller = controller_; | ||
|
|
||
| emit CrosschainControllerSet(gateway_, controller_); | ||
| } | ||
|
|
||
| /// @inheritdoc ERC7786Recipient | ||
| function _isAuthorizedGateway( | ||
| address instance, | ||
| bytes calldata sender | ||
| ) internal view virtual override returns (bool) { | ||
| return gateway() == instance && controller().equal(sender); | ||
| } | ||
|
|
||
| /// @inheritdoc ERC7786Recipient | ||
| function _processMessage( | ||
| address /*gateway*/, | ||
| bytes32 /*receiveId*/, | ||
| bytes calldata /*sender*/, | ||
| bytes calldata payload | ||
| ) internal virtual override { | ||
| // split payload | ||
| (CallType callType, ExecType execType, , ) = Mode.wrap(bytes32(payload[0x00:0x20])).decodeMode(); | ||
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bytes calldata executionCalldata = payload[0x20:]; | ||
|
|
||
| if (callType == ERC7579Utils.CALLTYPE_SINGLE) { | ||
| executionCalldata.execSingle(execType); | ||
| } else if (callType == ERC7579Utils.CALLTYPE_BATCH) { | ||
| executionCalldata.execBatch(execType); | ||
| } else if (callType == ERC7579Utils.CALLTYPE_DELEGATECALL) { | ||
| executionCalldata.execDelegateCall(execType); | ||
| } else revert ERC7579Utils.ERC7579UnsupportedCallType(callType); | ||
|
Comment on lines
+90
to
+96
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern is shared with AccountERC7579. Maybe we should add a function to the ERC7579Utils:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No strong opinion here. I'm not sure we really need to have this helper, but I also don't see any issues in adding it. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.26; | ||
|
|
||
| import {Governor} from "../Governor.sol"; | ||
| import {Mode} from "../../account/utils/draft-ERC7579Utils.sol"; | ||
| import {ERC7786Recipient} from "../../crosschain/ERC7786Recipient.sol"; | ||
| import {IERC7786GatewaySource} from "../../interfaces/draft-IERC7786.sol"; | ||
|
|
||
| /// @dev Extension of {Governor} for cross-chain governance through ERC-7786 gateways and {CrosschainRemoteExecutors}. | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| abstract contract GovernorCrosschain is Governor { | ||
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// @dev Send crosschain instruction to an arbitrary remote executor via an arbitrary ERC-7786 gateway. | ||
| function relayCrosschain( | ||
| address gateway, | ||
| bytes memory executor, | ||
| Mode mode, | ||
| bytes memory executionCalldata | ||
| ) public virtual onlyGovernance { | ||
| _crosschainExecute(gateway, executor, mode, executionCalldata); | ||
| } | ||
|
|
||
| /// @dev Send crosschain instruction to an arbitrary remote executor via an arbitrary ERC-7786 gateway. | ||
| function _crosschainExecute( | ||
| address gateway, | ||
| bytes memory executor, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your reasoning to allow specifying the gateway explicitly? It would be easy to batch together
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking that in some cases you may want to use a custom target. Maybe there are two executor (a main one and a secondary) and you want to target one without reconfiguring. Adding/updating the default executor target is a storage operation that is expensive and that some people may want to skip. |
||
| Mode mode, | ||
| bytes memory executionCalldata | ||
| ) internal virtual { | ||
| IERC7786GatewaySource(gateway).sendMessage(executor, abi.encodePacked(mode, executionCalldata), new bytes[](0)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.24; | ||
|
|
||
| import {Governor} from "../../governance/Governor.sol"; | ||
| import {GovernorSettings} from "../../governance/extensions/GovernorSettings.sol"; | ||
| import {GovernorCountingSimple} from "../../governance/extensions/GovernorCountingSimple.sol"; | ||
| import {GovernorVotesQuorumFraction} from "../../governance/extensions/GovernorVotesQuorumFraction.sol"; | ||
| import {GovernorCrosschain} from "../../governance/extensions/GovernorCrosschain.sol"; | ||
|
|
||
| abstract contract GovernorCrosschainMock is | ||
| GovernorSettings, | ||
| GovernorVotesQuorumFraction, | ||
| GovernorCountingSimple, | ||
| GovernorCrosschain | ||
| { | ||
| function proposalThreshold() public view override(Governor, GovernorSettings) returns (uint256) { | ||
| return super.proposalThreshold(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { expect } = require('chai'); | ||
| const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
|
|
||
| const { getLocalChain } = require('../helpers/chains'); | ||
| const { | ||
| CALL_TYPE_SINGLE, | ||
| CALL_TYPE_BATCH, | ||
| CALL_TYPE_DELEGATE, | ||
| encodeMode, | ||
| encodeSingle, | ||
| encodeBatch, | ||
| encodeDelegate, | ||
| } = require('../helpers/erc7579'); | ||
|
|
||
| async function fixture() { | ||
| const chain = await getLocalChain(); | ||
| const [admin, other] = await ethers.getSigners(); | ||
|
|
||
| const gateway = await ethers.deployContract('$ERC7786GatewayMock'); | ||
| const target = await ethers.deployContract('CallReceiverMock'); | ||
|
|
||
| // Deploy executor | ||
| const executor = await ethers.deployContract('$CrosschainRemoteExecutor', [gateway, chain.toErc7930(admin)]); | ||
|
|
||
| const remoteExecute = (from, target, mode, data) => | ||
| gateway.connect(from).sendMessage(target, ethers.concat([mode, data]), []); | ||
|
|
||
| return { chain, gateway, target, executor, admin, other, remoteExecute }; | ||
| } | ||
|
|
||
| describe('CrosschainRemoteController & CrosschainRemoteExecutor', function () { | ||
| beforeEach(async function () { | ||
| Object.assign(this, await loadFixture(fixture)); | ||
| }); | ||
|
|
||
| it('setup', async function () { | ||
| await expect(this.executor.gateway()).to.eventually.equal(this.gateway); | ||
| await expect(this.executor.controller()).to.eventually.equal(this.chain.toErc7930(this.admin)); | ||
| }); | ||
|
|
||
| describe('crosschain operation', function () { | ||
| it('support single mode', async function () { | ||
| const mode = encodeMode({ callType: CALL_TYPE_SINGLE }); | ||
| const data = encodeSingle(this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra')); | ||
|
|
||
| await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data)) | ||
| .to.emit(this.target, 'MockFunctionCalledExtra') | ||
| .withArgs(this.executor, 0n); | ||
| }); | ||
|
|
||
| it('support batch mode', async function () { | ||
| const mode = encodeMode({ callType: CALL_TYPE_BATCH }); | ||
| const data = encodeBatch( | ||
| [this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionWithArgs', [42, '0x1234'])], | ||
| [this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra')], | ||
| ); | ||
|
|
||
| await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data)) | ||
| .to.emit(this.target, 'MockFunctionCalledWithArgs') | ||
| .withArgs(42, '0x1234') | ||
| .to.emit(this.target, 'MockFunctionCalledExtra') | ||
| .withArgs(this.executor, 0n); | ||
| }); | ||
|
|
||
| it('support delegate mode', async function () { | ||
| const mode = encodeMode({ callType: CALL_TYPE_DELEGATE }); | ||
| const data = encodeDelegate(this.target, this.target.interface.encodeFunctionData('mockFunctionExtra')); | ||
|
|
||
| await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data)) | ||
| .to.emit(this.target.attach(this.executor.target), 'MockFunctionCalledExtra') | ||
| .withArgs(this.gateway, 0n); | ||
| }); | ||
|
|
||
| it('revert when mode is invalid', async function () { | ||
| const mode = encodeMode({ callType: '0x42' }); | ||
| const data = '0x'; | ||
|
|
||
| await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data)) | ||
| .to.be.revertedWithCustomError(this.executor, 'ERC7579UnsupportedCallType') | ||
| .withArgs('0x42'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reconfigure', function () { | ||
| beforeEach(async function () { | ||
| this.newGateway = await ethers.deployContract('$ERC7786GatewayMock'); | ||
| }); | ||
|
|
||
| it('through a crosschain call: success', async function () { | ||
| const mode = encodeMode({ callType: CALL_TYPE_SINGLE }); | ||
| const data = encodeSingle( | ||
| this.executor, | ||
| 0n, | ||
| this.executor.interface.encodeFunctionData('reconfigure', [ | ||
| this.newGateway.target, | ||
| this.chain.toErc7930(this.other), | ||
| ]), | ||
| ); | ||
|
|
||
| await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data)) | ||
| .to.emit(this.executor, 'CrosschainControllerSet') | ||
| .withArgs(this.newGateway, this.chain.toErc7930(this.other)); | ||
|
|
||
| await expect(this.executor.gateway()).to.eventually.equal(this.newGateway); | ||
| await expect(this.executor.controller()).to.eventually.equal(this.chain.toErc7930(this.other)); | ||
| }); | ||
|
|
||
| it('through the internal setter: success', async function () { | ||
| await expect(this.executor.$_setup(this.newGateway, this.chain.toErc7930(this.other))) | ||
| .to.emit(this.executor, 'CrosschainControllerSet') | ||
| .withArgs(this.newGateway, this.chain.toErc7930(this.other)); | ||
|
|
||
| await expect(this.executor.gateway()).to.eventually.equal(this.newGateway); | ||
| await expect(this.executor.controller()).to.eventually.equal(this.chain.toErc7930(this.other)); | ||
| }); | ||
|
|
||
| it('with an invalid new gateway: revert', async function () { | ||
| // directly using the internal setter | ||
| await expect(this.executor.$_setup(this.other, this.chain.toErc7930(this.other))).to.be.reverted; | ||
|
|
||
| // through a crosschain call | ||
| const mode = encodeMode({ callType: CALL_TYPE_SINGLE }); | ||
| const data = encodeSingle( | ||
| this.executor, | ||
| 0n, | ||
| this.executor.interface.encodeFunctionData('reconfigure', [ | ||
| this.other.address, | ||
| this.chain.toErc7930(this.other), | ||
| ]), | ||
| ); | ||
|
|
||
| await expect( | ||
| this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data), | ||
| ).to.be.revertedWithCustomError(this.executor, 'FailedCall'); | ||
| }); | ||
|
|
||
| it('is access controlled', async function () { | ||
| await expect( | ||
| this.executor.reconfigure(this.newGateway, this.chain.toErc7930(this.other)), | ||
| ).to.be.revertedWithCustomError(this.executor, 'AccessRestricted'); | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.