diff --git a/script/DeploySignedProposer.s.sol b/script/DeploySignedProposer.s.sol index 443a53c..b207f55 100644 --- a/script/DeploySignedProposer.s.sol +++ b/script/DeploySignedProposer.s.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.27; +import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; import {Script} from "forge-std/Script.sol"; import {console} from "forge-std/console.sol"; import {ISignatureTransfer} from "permit2/src/interfaces/ISignatureTransfer.sol"; @@ -9,7 +10,7 @@ import {SignedProposer} from "../src/optimistic-oracle-v2/implementation/SignedP /** * @title Deployment script for SignedProposer - * @notice Deploys SignedProposer with configurable Permit2 and admin addresses + * @notice Deploys a UUPS SignedProposer implementation and atomically initialized ERC1967 proxy * * Environment variables: * - MNEMONIC: Required. The mnemonic phrase for the deployer wallet @@ -33,12 +34,21 @@ contract DeploySignedProposer is Script { vm.startBroadcast(deployerPrivateKey); - signedProposer = new SignedProposer(ISignatureTransfer(permit2Address), admin); + SignedProposer implementation = new SignedProposer(); + signedProposer = SignedProposer( + address( + new ERC1967Proxy( + address(implementation), + abi.encodeCall(SignedProposer.initialize, (ISignatureTransfer(permit2Address), admin)) + ) + ) + ); vm.stopBroadcast(); console.log("\n=== Deployment Summary ==="); - console.log("SignedProposer:", address(signedProposer)); + console.log("SignedProposer proxy:", address(signedProposer)); + console.log("SignedProposer implementation:", address(implementation)); console.log("Chain ID:", block.chainid); console.log("Deployer:", deployer); console.log("Permit2:", permit2Address); diff --git a/script/README.md b/script/README.md index 1950621..a96e1b6 100644 --- a/script/README.md +++ b/script/README.md @@ -307,7 +307,7 @@ The `ManagedOptimisticOracleV2` contract: ## SignedProposer Deployment -The `DeploySignedProposer.s.sol` script deploys `SignedProposer` using the canonical Permit2 deployment by default. +The `DeploySignedProposer.s.sol` script deploys a UUPS `SignedProposer` implementation and an ERC1967 proxy, atomically calling `initialize(permit2, admin)` during proxy construction. It uses the canonical Permit2 deployment by default and logs both addresses. The implementation disables initialization. ### Environment Variables @@ -315,7 +315,7 @@ The `DeploySignedProposer.s.sol` script deploys `SignedProposer` using the canon |----------|----------|-------------| | `MNEMONIC` | Yes | The mnemonic phrase for the deployer wallet (uses 0 index address) | | `PERMIT2_ADDRESS` | No | Permit2 address; defaults to `0x000000000022D473030F116dDEE9F6B43aC78BA3` | -| `SIGNED_PROPOSER_ADMIN` | No | Address receiving the default admin role; defaults to the deployer | +| `SIGNED_PROPOSER_ADMIN` | No | Address receiving the default admin role, including upgrade authority; defaults to the deployer | ### Usage @@ -323,7 +323,21 @@ The `DeploySignedProposer.s.sol` script deploys `SignedProposer` using the canon forge script script/DeploySignedProposer.s.sol --rpc-url "YOUR_RPC_URL" --broadcast ``` -The script only deploys the contract. The admin configures delegated proposers and any required whitelist ownership separately. +The script deploys and initializes the proxy. The admin configures delegated proposers and any required whitelist ownership separately. Use the **proxy address** for all relay calls, the Permit2 spender in signatures, whitelist membership, and whitelist ownership. + +### Upgradeability + +SignedProposer uses the same OpenZeppelin UUPS/ERC1967 mechanism as Managed OO and OOReporter. Its existing `DEFAULT_ADMIN_ROLE` authorizes `upgradeToAndCall(newImplementation, data)` on the proxy; no separate owner, proxy admin contract, or upgrade role is added. Delegated proposers and whitelist admins cannot upgrade unless they also hold the default admin role. Use empty `data` when no migration is needed; do not call `initialize` again. + +The Permit2 address is initialized in proxy storage. Roles, retained token balances, whitelist ownership, and the Permit2 spender address survive compatible implementation upgrades. Existing signatures remain usable subject to their original deadlines, nonce usage, and proposal validity. The default admin is trusted to install implementations that can change relay behavior, including how outstanding signatures are handled. + +Future upgrades must preserve the storage layout, including the inherited `TryMulticall` batch lock and `permit2`, and retain the UUPS authorization hook. Validate storage compatibility against the deployed build before upgrading. This deployment change does not retrofit upgradeability onto any previously deployed direct SignedProposer instance; that requires a new proxy deployment and updating integrations to its address. + +The integrated layout stores the batch lock in the ERC-7201 namespace `uma.storage.TryMulticall` and `permit2` at +slot 0, offset 0. This is the layout for the initial proxy deployment. The earlier UUPS proposal with a sequential batch +lock placed `permit2` at slot 0, offset 1; it is not storage-compatible with this version. Do not upgrade a proxy using +that earlier layout directly to this implementation. Such a deployment requires a separately reviewed migration or a +new proxy. The integration does not include a storage migration. ### Partial-success proposal batches @@ -339,7 +353,7 @@ batch reverts and rolls back earlier successes. Polygon Bor rejects transactions larger than 128 KiB. With the maximum valid OOv2 ancillary data of 8,139 non-zero bytes, each encoded `propose` child is 8,804 bytes: 14 children produce 124,612 -bytes of outer calldata and execute within the 53,902,641 gas limit at pinned block `81,683,818`, +bytes of outer calldata; the proxy-based capacity test uses the 53,902,641 gas limit at pinned block `81,683,818`, while 15 children produce 133,508 bytes before the signed transaction envelope and are rejected. Smaller ancillary data permits larger batches, subject to the same transaction-size and gas bounds. @@ -373,8 +387,10 @@ reachable functions must retain their own authorization because `multicall` does ### Verification +Verify the implementation with no constructor arguments. Verify the ERC1967 proxy separately using its implementation address and encoded `initialize(permit2, admin)` calldata as constructor arguments. + ```bash -forge verify-contract src/optimistic-oracle-v2/implementation/SignedProposer.sol:SignedProposer --chain-id --constructor-args $(cast abi-encode "constructor(address,address)" ) --etherscan-api-key +forge verify-contract src/optimistic-oracle-v2/implementation/SignedProposer.sol:SignedProposer --chain-id --etherscan-api-key ``` ## ManagedOptimisticOracleV2 Upgrade diff --git a/src/optimistic-oracle-v2/implementation/SignedProposer.sol b/src/optimistic-oracle-v2/implementation/SignedProposer.sol index 7721b5f..1e8184c 100644 --- a/src/optimistic-oracle-v2/implementation/SignedProposer.sol +++ b/src/optimistic-oracle-v2/implementation/SignedProposer.sol @@ -3,14 +3,15 @@ pragma solidity ^0.8.27; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; -import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol"; -import {Multicall} from "@openzeppelin/contracts/utils/Multicall.sol"; -import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; +import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol"; +import {MulticallUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol"; +import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol"; import {OptimisticOracleV2Interface} from "../interfaces/OptimisticOracleV2Interface.sol"; import {SignedProposerOracleInterface} from "../interfaces/SignedProposerOracleInterface.sol"; import {ISignatureTransfer} from "permit2/src/interfaces/ISignatureTransfer.sol"; import {AddressWhitelist} from "../../common/implementation/AddressWhitelist.sol"; import {AddressWhitelistInterface} from "../../common/interfaces/AddressWhitelistInterface.sol"; +import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; import {TryMulticall} from "../../common/implementation/TryMulticall.sol"; /** @@ -32,7 +33,11 @@ import {TryMulticall} from "../../common/implementation/TryMulticall.sol"; * spender and the `proposePriceFor` call target, so executing an unintended oracle can spend up to * the signer-approved Permit2 amount for that proposal. * + * Deploy behind an ERC1967Proxy and initialize atomically. The default admin authorizes UUPS + * upgrades; the proxy remains the Permit2 spender and whitelist owner across upgrades. + * * The contract is permissioned: + * - `DEFAULT_ADMIN_ROLE` — manages roles, payments, whitelist ownership, and upgrades. * - `DELEGATED_PROPOSER_ROLE` — may call `propose` and `tryMulticall`. * - `WHITELIST_ADMIN_ROLE` — may directly add/remove entries on whitelists owned by this contract. * @@ -42,7 +47,13 @@ import {TryMulticall} from "../../common/implementation/TryMulticall.sol"; * apply their own. `tryMulticall` instead restricts the caller to delegated proposers, accepts only `propose`, and rejects * nested partial-success batches. These restrictions do not apply to the atomic `multicall` entry point. */ -contract SignedProposer is AccessControl, Multicall, ReentrancyGuard, TryMulticall { +contract SignedProposer is + AccessControlUpgradeable, + MulticallUpgradeable, + ReentrancyGuardUpgradeable, + UUPSUpgradeable, + TryMulticall +{ using SafeERC20 for IERC20; // ─── Structs ────────────────────────────────────────────────────────────────── @@ -73,9 +84,9 @@ contract SignedProposer is AccessControl, Multicall, ReentrancyGuard, TryMultica string public constant WITNESS_TYPE_STRING = "Proposal witness)Proposal(address oracle,address requester,bytes32 identifier,uint256 timestamp,bytes ancillaryData,int256 proposedPrice,uint256 maxPayment)TokenPermissions(address token,uint256 amount)"; - // ─── Immutables ─────────────────────────────────────────────────────────────── + // ─── Storage ─────────────────────────────────────────────────────────────── - ISignatureTransfer public immutable permit2; + ISignatureTransfer public permit2; // ─── Events ─────────────────────────────────────────────────────────────────── @@ -92,18 +103,34 @@ contract SignedProposer is AccessControl, Multicall, ReentrancyGuard, TryMultica event PaymentWithdrawn(address indexed token, address indexed to, uint256 amount); + error ZeroAddress(); error PaymentExceedsMaxPayment(); error PermitTransferAmountMismatch(uint256 expectedAmount, uint256 receivedAmount); error PermitTokenMismatch(address requestCurrency, address permitToken); error CannotRemoveSelfFromWhitelist(); error NewOwnerNotWhitelisted(address newOwner); + // ─── Constructor ────────────────────────────────────────────────────────────── - constructor(ISignatureTransfer _permit2, address admin) { + /// @custom:oz-upgrades-unsafe-allow constructor + constructor() { + _disableInitializers(); + } + + /// @notice Initializes the proxy with its Permit2 contract and role/upgrade administrator. + function initialize(ISignatureTransfer _permit2, address admin) external initializer { + if (address(_permit2) == address(0) || admin == address(0)) revert ZeroAddress(); + __AccessControl_init(); + __Multicall_init(); + __ReentrancyGuard_init(); + __UUPSUpgradeable_init(); permit2 = _permit2; _grantRole(DEFAULT_ADMIN_ROLE, admin); } + /// @dev Reuses the existing admin role rather than introducing a separate upgrade authority. + function _authorizeUpgrade(address) internal override onlyRole(DEFAULT_ADMIN_ROLE) {} + // ─── Propose ────────────────────────────────────────────────────────────────── /** diff --git a/test/SignedProposer.t.sol b/test/SignedProposer.t.sol index c84be55..31b0d01 100644 --- a/test/SignedProposer.t.sol +++ b/test/SignedProposer.t.sol @@ -20,12 +20,14 @@ import {MockOracle} from "./mocks/MockOracle.sol"; import {MockPermit2} from "./mocks/MockPermit2.sol"; import {MaliciousSignedProposerOracle} from "./mocks/MaliciousSignedProposerOracle.sol"; +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; +import {ERC1967Utils} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol"; import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol"; import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {ERC20Mock} from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol"; import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; -import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; +import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol"; contract ShortTransferERC20Mock is ERC20Mock { uint256 internal constant TRANSFER_FEE_BPS = 1_000; @@ -164,7 +166,14 @@ contract SignedProposerTest is Test { moo.addRequestManager(requestManager); // Deploy SignedProposer with admin, then grant relayer the delegated proposer role. - signedProposer = new SignedProposer(ISignatureTransfer(address(mockPermit2)), admin); + signedProposer = SignedProposer( + address( + new ERC1967Proxy( + address(new SignedProposer()), + abi.encodeCall(SignedProposer.initialize, (ISignatureTransfer(address(mockPermit2)), admin)) + ) + ) + ); vm.prank(admin); signedProposer.addDelegatedProposer(relayer); @@ -176,6 +185,84 @@ contract SignedProposerTest is Test { requesterWhitelist.addToWhitelist(requester); } + function test_revert_initialize_implementationLockedAndProxyCannotReinitialize() public { + SignedProposer implementation = new SignedProposer(); + vm.expectRevert(Initializable.InvalidInitialization.selector); + implementation.initialize(ISignatureTransfer(address(mockPermit2)), admin); + + vm.expectRevert(Initializable.InvalidInitialization.selector); + signedProposer.initialize(ISignatureTransfer(address(mockPermit2)), admin); + } + + function test_revert_initialize_zeroAddresses() public { + SignedProposer implementation = new SignedProposer(); + vm.expectRevert(SignedProposer.ZeroAddress.selector); + new ERC1967Proxy( + address(implementation), abi.encodeCall(SignedProposer.initialize, (ISignatureTransfer(address(0)), admin)) + ); + vm.expectRevert(SignedProposer.ZeroAddress.selector); + new ERC1967Proxy( + address(implementation), + abi.encodeCall(SignedProposer.initialize, (ISignatureTransfer(address(mockPermit2)), address(0))) + ); + } + + function test_revert_upgrade_onlyDefaultAdmin() public { + SignedProposer implementation = new SignedProposer(); + address whitelistAdmin = makeAddr("whitelistAdmin"); + vm.prank(admin); + signedProposer.addWhitelistAdmin(whitelistAdmin); + address[3] memory unauthorized = [relayer, whitelistAdmin, makeAddr("outsider")]; + for (uint256 i; i < unauthorized.length; ++i) { + vm.expectRevert( + abi.encodeWithSelector( + IAccessControl.AccessControlUnauthorizedAccount.selector, unauthorized[i], DEFAULT_ADMIN_ROLE + ) + ); + vm.prank(unauthorized[i]); + signedProposer.upgradeToAndCall(address(implementation), ""); + } + } + + function test_revert_upgrade_nonUUPSImplementation() public { + vm.expectRevert( + abi.encodeWithSelector(ERC1967Utils.ERC1967InvalidImplementation.selector, address(mockPermit2)) + ); + vm.prank(admin); + signedProposer.upgradeToAndCall(address(mockPermit2), ""); + } + + function test_upgrade_preservesStateAndBatching() public { + vm.prank(admin); + signedProposer.addWhitelistAdmin(relayer); + defaultProposerWhitelist.transferOwnership(address(signedProposer)); + test_tryMulticall_allSuccess_preservesProposalEventsAndAccounting(); + uint256 retainedPayment = currency.balanceOf(address(signedProposer)); + assertGt(retainedPayment, 0); + + SignedProposer implementation = new SignedProposer(); + vm.prank(admin); + signedProposer.upgradeToAndCall(address(implementation), ""); + + assertEq( + address(uint160(uint256(vm.load(address(signedProposer), ERC1967Utils.IMPLEMENTATION_SLOT)))), + address(implementation) + ); + assertEq(address(signedProposer.permit2()), address(mockPermit2)); + assertTrue(signedProposer.hasRole(DEFAULT_ADMIN_ROLE, admin)); + assertTrue(signedProposer.hasRole(DELEGATED_PROPOSER_ROLE, relayer)); + assertTrue(signedProposer.hasRole(WHITELIST_ADMIN_ROLE, relayer)); + assertEq(defaultProposerWhitelist.owner(), address(signedProposer)); + assertTrue(defaultProposerWhitelist.isOnWhitelist(address(signedProposer))); + assertEq(currency.balanceOf(address(signedProposer)), retainedPayment); + vm.prank(admin); + signedProposer.withdrawPayments(IERC20(address(currency)), admin, retainedPayment); + assertEq(currency.balanceOf(admin), retainedPayment); + + // Exercises self-delegatecall, the batch lock, proposal reentrancy guard, and accounting after upgrading. + test_tryMulticall_allSuccess_preservesProposalEventsAndAccounting(); + } + // ─── Helpers ────────────────────────────────────────────────────────────────── function _makeRequest(uint256 timestamp, uint256 reward) internal returns (uint256 totalBond) { @@ -459,7 +546,7 @@ contract SignedProposerTest is Test { assertFalse(callbackRequester.reentrantProposeSucceeded()); assertEq( callbackRequester.reentrantRevertData(), - abi.encodeWithSelector(ReentrancyGuard.ReentrancyGuardReentrantCall.selector) + abi.encodeWithSelector(ReentrancyGuardUpgradeable.ReentrancyGuardReentrantCall.selector) ); } @@ -900,7 +987,8 @@ contract SignedProposerTest is Test { vm.expectEmit(true, true, false, true, address(signedProposer)); emit ProposalCallFailed(1, keccak256(calls[1]), bytes4(0x01020304), keccak256(expectedPrefix)); - (bool outerSuccess, bool[] memory successes) = _tryMulticallWithGas(calls, 2_000_000); + // The self-delegatecall also traverses the proxy, which must copy the child's revert payload before the cap. + (bool outerSuccess, bool[] memory successes) = _tryMulticallWithGas(calls, 2_800_000); assertTrue(outerSuccess); assertTrue(successes[0]); @@ -1271,7 +1359,16 @@ contract SignedProposerTest is Test { defaultProposerWhitelist.removeFromWhitelist(proposer); address replacementAdmin = makeAddr("replacementAdmin"); - SignedProposer replacement = new SignedProposer(ISignatureTransfer(address(mockPermit2)), replacementAdmin); + SignedProposer replacement = SignedProposer( + address( + new ERC1967Proxy( + address(new SignedProposer()), + abi.encodeCall( + SignedProposer.initialize, (ISignatureTransfer(address(mockPermit2)), replacementAdmin) + ) + ) + ) + ); defaultProposerWhitelist.addToWhitelist(address(replacement)); defaultProposerWhitelist.transferOwnership(address(signedProposer)); diff --git a/test/SignedProposerPermit2.t.sol b/test/SignedProposerPermit2.t.sol index b3831c7..377fca1 100644 --- a/test/SignedProposerPermit2.t.sol +++ b/test/SignedProposerPermit2.t.sol @@ -143,7 +143,14 @@ contract SignedProposerPermit2Test is Test, DeployPermit2 { permit2Address = deployPermit2(); vm.label(permit2Address, "PERMIT2"); - signedProposer = new SignedProposer(ISignatureTransfer(permit2Address), admin); + signedProposer = SignedProposer( + address( + new ERC1967Proxy( + address(new SignedProposer()), + abi.encodeCall(SignedProposer.initialize, (ISignatureTransfer(permit2Address), admin)) + ) + ) + ); vm.prank(admin); signedProposer.addDelegatedProposer(relayer); @@ -422,6 +429,14 @@ contract SignedProposerPermit2Test is Test, DeployPermit2 { } function test_tryMulticall_realPermit2DifferentProposersAndNonces() public { + _testRealPermit2Batch(false); + } + + function test_upgrade_preservesPreviouslySignedPermit2Batch() public { + _testRealPermit2Batch(true); + } + + function _testRealPermit2Batch(bool upgradeBeforeRelay) internal { vm.warp(block.timestamp + 2); uint256 firstTimestamp = block.timestamp - 1; uint256 secondTimestamp = block.timestamp; @@ -471,6 +486,12 @@ contract SignedProposerPermit2Test is Test, DeployPermit2 { ) ); + if (upgradeBeforeRelay) { + SignedProposer implementation = new SignedProposer(); + vm.prank(admin); + signedProposer.upgradeToAndCall(address(implementation), ""); + } + vm.prank(relayer); bool[] memory successes = signedProposer.tryMulticall(calls); diff --git a/test/SignedProposerTryMulticall.Fork.t.sol b/test/SignedProposerTryMulticall.Fork.t.sol index 6ef3289..2c0c9e5 100644 --- a/test/SignedProposerTryMulticall.Fork.t.sol +++ b/test/SignedProposerTryMulticall.Fork.t.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.27; +import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; import {Test} from "forge-std/Test.sol"; import {IEIP712} from "permit2/src/interfaces/IEIP712.sol"; @@ -58,7 +59,14 @@ contract SignedProposerTryMulticallForkTest is Test { assertNotEq(firstProposer, secondProposer, "proposers must differ"); currency = new ERC20Mock(); - signedProposer = new SignedProposer(ISignatureTransfer(POLYGON_PERMIT2), admin); + signedProposer = SignedProposer( + address( + new ERC1967Proxy( + address(new SignedProposer()), + abi.encodeCall(SignedProposer.initialize, (ISignatureTransfer(POLYGON_PERMIT2), admin)) + ) + ) + ); oracle = new MaliciousSignedProposerOracle(currency, address(signedProposer), address(0)); oracle.setBondAmount(BOND);