From 09a5bd0bb208a987953a5777477d2d6931611624 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Wed, 21 Oct 2026 16:53:06 +0200 Subject: [PATCH 01/19] fix(contracts): Require a timelock canceller in the governance deploy Once a queued operation's delay has passed anyone may execute it, and OZ Governor lets only the proposer cancel before voting starts. Without a canceller nobody could veto a passed proposal during the delay, so the script now refuses to deploy without TIMELOCK_CANCELLER. The Base Sepolia governance rehearsal passes its guardian as canceller and checks the role. --- contracts/.env.example | 7 ++++--- contracts/script/DeployGovernance.s.sol | 15 +++++++++------ contracts/test/PENGovernor.t.sol | 2 +- testing/src/phase5b-governance.mjs | 3 ++- testing/src/rehearsal-deploy.mjs | 3 ++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index c3ed4e0d2..7382281cb 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -68,7 +68,8 @@ QUORUM_FRACTION=2 # so quorum is reachable soon after launch, or no proposal (an unpause # included) can pass. QUORUM_FLOOR= -# Strongly recommended: a multisig granted CANCELLER_ROLE on the timelock, so a -# queued operation can still be vetoed during the 48h delay (OZ Governor alone -# lets only the proposer cancel, and only before voting starts). +# Required: a multisig granted CANCELLER_ROLE on the timelock, so a queued +# operation can still be vetoed during the 48h delay (OZ Governor alone lets +# only the proposer cancel, and only before voting starts). The governance +# deploy script refuses to run without it. TIMELOCK_CANCELLER= diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index 6e88e23bd..14c6c2437 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -28,13 +28,15 @@ import {IVotes} from "@openzeppelin/contracts/governance/utils/IVotes.sol"; /// QUORUM_FLOOR absolute quorum lower bound, 18-decimal token /// units; keeps early proposals from being trivially /// cheap while circulating supply is still small -/// TIMELOCK_CANCELLER optional: an address (the guardian/council Safe) +/// TIMELOCK_CANCELLER required: an address (the guardian/council Safe) /// that can cancel a QUEUED operation during the /// timelock delay. Without it only the Governor holds /// CANCELLER_ROLE, and OZ Governor lets only the /// proposer cancel, only before voting starts — so a /// hostile proposal that passed could not be stopped -/// during the 48h delay by anyone (review round 9) +/// during the 48h delay by anyone (review round 9). +/// Once the delay has passed anyone may execute, so +/// the script refuses to deploy without it. contract DeployGovernance is Script { function run() external { address token = vm.envAddress("PEN_TOKEN"); @@ -45,6 +47,7 @@ contract DeployGovernance is Script { uint256 quorumFraction = vm.envUint("QUORUM_FRACTION"); uint256 quorumFloor = vm.envUint("QUORUM_FLOOR"); address canceller = vm.envOr("TIMELOCK_CANCELLER", address(0)); + require(canceller != address(0), "TIMELOCK_CANCELLER is required: no veto during the timelock delay otherwise"); vm.startBroadcast(); @@ -58,12 +61,12 @@ contract DeployGovernance is Script { ); // Only the Governor proposes; anyone may execute after the delay. The - // Governor cancels its own operations, and an optional human canceller - // can veto a queued operation during the delay — the reaction window - // the timelock exists to provide. + // Governor cancels its own operations, and the human canceller can + // veto a queued operation during the delay — the reaction window the + // timelock exists to provide. timelock.grantRole(timelock.PROPOSER_ROLE(), address(governor)); timelock.grantRole(timelock.CANCELLER_ROLE(), address(governor)); - if (canceller != address(0)) timelock.grantRole(timelock.CANCELLER_ROLE(), canceller); + timelock.grantRole(timelock.CANCELLER_ROLE(), canceller); timelock.grantRole(timelock.EXECUTOR_ROLE(), address(0)); // Leave the timelock self-administered: changes require a proposal. timelock.renounceRole(timelock.DEFAULT_ADMIN_ROLE(), msg.sender); diff --git a/contracts/test/PENGovernor.t.sol b/contracts/test/PENGovernor.t.sol index f939c9ccc..fa741e5a7 100644 --- a/contracts/test/PENGovernor.t.sol +++ b/contracts/test/PENGovernor.t.sol @@ -40,7 +40,7 @@ contract PENGovernorTest is Test { timelock.grantRole(timelock.PROPOSER_ROLE(), address(governor)); timelock.grantRole(timelock.CANCELLER_ROLE(), address(governor)); // The human veto during the timelock delay (DeployGovernance's - // optional TIMELOCK_CANCELLER): the guardian Safe. + // required TIMELOCK_CANCELLER): the guardian Safe. timelock.grantRole(timelock.CANCELLER_ROLE(), guardian); timelock.grantRole(timelock.EXECUTOR_ROLE(), address(0)); timelock.renounceRole(timelock.DEFAULT_ADMIN_ROLE(), address(this)); diff --git a/testing/src/phase5b-governance.mjs b/testing/src/phase5b-governance.mjs index 790b8f024..a080fcb45 100644 --- a/testing/src/phase5b-governance.mjs +++ b/testing/src/phase5b-governance.mjs @@ -123,7 +123,7 @@ async function main() { // --- governance deployment ---------------------------------------------- section("Governance deployment (first execution of DeployGovernance.s.sol)"); - const { timelock, governor, params } = deployGovernanceToSepolia({ env, pen, log }); + const { timelock, governor, params } = deployGovernanceToSepolia({ env, pen, canceller: ctx.roles.guardian.address, log }); log(`timelock ${timelock}\n governor ${governor}`); const G = { address: governor, abi: governorAbi }; const T = { address: timelock, abi: timelockAbi }; @@ -138,6 +138,7 @@ async function main() { await eventually(async () => { assertEq(await read(T, "hasRole", [proposerRole, governor]), true, "governor is proposer"); assertEq(await read(T, "hasRole", [cancellerRole, governor]), true, "governor is canceller"); + assertEq(await read(T, "hasRole", [cancellerRole, ctx.roles.guardian.address]), true, "guardian is canceller"); assertEq(await read(T, "hasRole", [executorRole, "0x0000000000000000000000000000000000000000"]), true, "execution open to anyone"); assertEq(await read(T, "hasRole", [adminRole, ctx.roles.deployer.address]), false, "deployer admin renounced"); assertEq(await read(T, "hasRole", [adminRole, timelock]), true, "timelock self-administered"); diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index 1fc0bd822..c504df3ef 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -91,7 +91,7 @@ export function governanceDrillParams(env) { } /** Runs the real DeployGovernance.s.sol — its first execution anywhere. */ -export function deployGovernanceToSepolia({ env, pen, log }) { +export function deployGovernanceToSepolia({ env, pen, canceller, log }) { const params = governanceDrillParams(env); const scriptEnv = { ...process.env, @@ -102,6 +102,7 @@ export function deployGovernanceToSepolia({ env, pen, log }) { PROPOSAL_THRESHOLD: params.proposalThreshold.toString(), QUORUM_FRACTION: String(params.quorumFraction), QUORUM_FLOOR: params.quorumFloor.toString(), + TIMELOCK_CANCELLER: canceller, }; log(`deploying governance (timelock ${params.timelockDelay}s, voting ${params.votingDelay}s+${params.votingPeriod}s, quorum ${params.quorumFraction}% of circulating, floor ${params.quorumFloor / PEN_18} PEN) ...`); execFileSync( From 50971171a4a0a6845f1af9a9b6a5ec071c317836 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 09:54:39 +0200 Subject: [PATCH 02/19] fix(contracts): Check the permanent values in the migration deploy script MAX_ISSUANCE and EARLIEST_SWEEP_TS cannot be changed once the token and vault exist. The script now refuses an issuance other than 150M PEN in 18-decimal units and a sweep floor that is not a future unix time in seconds within the next two years, so a units slip fails before anything is broadcast. It also broadcasts explicitly as msg.sender, the address it already hands the interim admin role to, and returns the deployed contracts. That leaves forge script behaviour unchanged and lets a Foundry test run the script itself: the new test covers each refusal and the happy path. --- contracts/.env.example | 12 +++--- contracts/script/Deploy.s.sol | 28 ++++++++++--- contracts/test/DeployScripts.t.sol | 63 ++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 contracts/test/DeployScripts.t.sol diff --git a/contracts/.env.example b/contracts/.env.example index 7382281cb..fadeb4bb2 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -21,7 +21,8 @@ ATTESTOR_2= ATTESTOR_3= ATTESTOR_4= -# PERMANENT. Max issuance in 18-decimal units. Decision D3: 150M PEN. +# PERMANENT. Max issuance in 18-decimal units. Decision D3: 150M PEN. The +# script refuses any other value (Deploy.s.sol EXPECTED_MAX_ISSUANCE). MAX_ISSUANCE=150000000000000000000000000 # Caps in 18-decimal units. Set equal to each other on purpose: a release @@ -37,10 +38,11 @@ MAX_ISSUANCE=150000000000000000000000000 PER_RELEASE_CAP=1000000000000000000000000 DAILY_CAP=1000000000000000000000000 -# PERMANENT. Unix timestamp before which the remainder CANNOT be swept. This is -# a floor on the earliest a sweep could happen, not a deadline for holders: -# closing the window is a separate governance decision and can be deferred -# indefinitely. +# PERMANENT. Unix timestamp in SECONDS before which the remainder CANNOT be +# swept; the script refuses a value in the past or more than two years ahead +# (a milliseconds value lands far beyond that). This is a floor on the +# earliest a sweep could happen, not a deadline for holders: closing the +# window is a separate governance decision and can be deferred indefinitely. # # Deliberately set beyond the ~3-month window we intend to advertise: a later # floor costs nothing operationally (the remainder simply waits in the vault, diff --git a/contracts/script/Deploy.s.sol b/contracts/script/Deploy.s.sol index 27a9a2034..f22c125d7 100644 --- a/contracts/script/Deploy.s.sol +++ b/contracts/script/Deploy.s.sol @@ -17,22 +17,36 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// ADMIN_SAFE bootstrap Safe that becomes vault admin /// GUARDIAN_SAFE fast pause guardian /// ATTESTOR_1..4 attestor transaction-sender addresses (D4: 3-of-4) -/// MAX_ISSUANCE max issuance in 18-decimal units (decision D3) +/// MAX_ISSUANCE max issuance in 18-decimal units (decision D3); +/// must equal EXPECTED_MAX_ISSUANCE /// PER_RELEASE_CAP initial per-release cap, 18-decimal units /// DAILY_CAP initial daily cap, 18-decimal units -/// EARLIEST_SWEEP_TS unix timestamp before which no remainder sweep (D5) +/// EARLIEST_SWEEP_TS unix timestamp in SECONDS before which no remainder +/// sweep (D5); must lie in the next MAX_SWEEP_FLOOR_AHEAD contract Deploy is Script { // 12-decimal pallet amounts -> 18-decimal token amounts (decision D2). uint256 internal constant CONVERSION_FACTOR = 1e6; uint256 internal constant THRESHOLD = 3; + // MAX_ISSUANCE and EARLIEST_SWEEP_TS are permanent once deployed, so the + // script checks them instead of trusting the environment. + // Decision D3: 150M PEN. + uint256 internal constant EXPECTED_MAX_ISSUANCE = 150_000_000e18; + // A sweep floor further out than this is a units slip (milliseconds land + // ~1000x further) or a typo, not a decision. + uint256 internal constant MAX_SWEEP_FLOOR_AHEAD = 2 * 365 days; - function run() external { + function run() external returns (PEN pen, MigrationVault vault) { address adminSafe = vm.envAddress("ADMIN_SAFE"); address guardianSafe = vm.envAddress("GUARDIAN_SAFE"); uint256 maxIssuance = vm.envUint("MAX_ISSUANCE"); uint256 perReleaseCap = vm.envUint("PER_RELEASE_CAP"); uint256 dailyCap = vm.envUint("DAILY_CAP"); uint256 earliestSweepTs = vm.envUint("EARLIEST_SWEEP_TS"); + require(maxIssuance == EXPECTED_MAX_ISSUANCE, "MAX_ISSUANCE must be 150M PEN in 18-decimal units"); + require( + earliestSweepTs > block.timestamp && earliestSweepTs <= block.timestamp + MAX_SWEEP_FLOOR_AHEAD, + "EARLIEST_SWEEP_TS must be a future unix time in seconds, at most two years ahead" + ); address[] memory attestors = new address[](4); attestors[0] = vm.envAddress("ATTESTOR_1"); @@ -40,9 +54,11 @@ contract Deploy is Script { attestors[2] = vm.envAddress("ATTESTOR_3"); attestors[3] = vm.envAddress("ATTESTOR_4"); - vm.startBroadcast(); + // Broadcast as msg.sender (forge sets it to --sender or the single + // signer), the address the interim admin role below is given to. + vm.startBroadcast(msg.sender); - MigrationVault vault = new MigrationVault( + vault = new MigrationVault( msg.sender, // interim admin for setToken; handed over below guardianSafe, attestors, @@ -52,7 +68,7 @@ contract Deploy is Script { dailyCap, earliestSweepTs ); - PEN pen = new PEN(address(vault), maxIssuance); + pen = new PEN(address(vault), maxIssuance); vault.setToken(IERC20(address(pen))); vault.transferAdmin(adminSafe); diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol new file mode 100644 index 000000000..9b9a23ff1 --- /dev/null +++ b/contracts/test/DeployScripts.t.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity 0.8.26; + +import {Test} from "forge-std/Test.sol"; +import {PEN} from "../src/PEN.sol"; +import {MigrationVault} from "../src/MigrationVault.sol"; +import {Deploy} from "../script/Deploy.s.sol"; + +// These run the real scripts against their environment. The environment is +// process-wide and forge runs test functions in parallel, so each script's +// cases share one test function instead of racing on the same variables. +// The scripts broadcast as their caller, so this test contract is the deployer. + +contract DeployScriptTest is Test { + uint256 internal constant SWEEP_FLOOR = 1803859200; // 2027-03-01 00:00 UTC + + Deploy internal script; + address internal adminSafe = makeAddr("adminSafe"); + + function setUp() public { + script = new Deploy(); + vm.warp(1792000000); // October 2026 + } + + function _setValidEnv() internal { + vm.setEnv("ADMIN_SAFE", vm.toString(adminSafe)); + vm.setEnv("GUARDIAN_SAFE", vm.toString(makeAddr("guardianSafe"))); + vm.setEnv("ATTESTOR_1", vm.toString(makeAddr("attestor1"))); + vm.setEnv("ATTESTOR_2", vm.toString(makeAddr("attestor2"))); + vm.setEnv("ATTESTOR_3", vm.toString(makeAddr("attestor3"))); + vm.setEnv("ATTESTOR_4", vm.toString(makeAddr("attestor4"))); + vm.setEnv("MAX_ISSUANCE", "150000000000000000000000000"); + vm.setEnv("PER_RELEASE_CAP", "1000000000000000000000000"); + vm.setEnv("DAILY_CAP", "1000000000000000000000000"); + vm.setEnv("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR)); + } + + function _expectRefused(string memory key, string memory value, string memory reason) internal { + _setValidEnv(); + vm.setEnv(key, value); + vm.expectRevert(bytes(reason)); + script.run(); + } + + function test_RunRefusesBadPermanentValuesAndDeploys() public { + string memory issuance = "MAX_ISSUANCE must be 150M PEN in 18-decimal units"; + _expectRefused("MAX_ISSUANCE", "15000000000000000000000000", issuance); // one zero short + _expectRefused("MAX_ISSUANCE", "150000000", issuance); // whole PEN + + string memory sweep = "EARLIEST_SWEEP_TS must be a future unix time in seconds, at most two years ahead"; + _expectRefused("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR * 1000), sweep); // milliseconds + _expectRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp), sweep); + + _setValidEnv(); + (PEN pen, MigrationVault vault) = script.run(); + assertEq(address(vault.token()), address(pen)); + assertEq(pen.totalSupply(), 150_000_000e18); + assertEq(pen.balanceOf(address(vault)), 150_000_000e18); + assertEq(vault.earliestSweepTimestamp(), SWEEP_FLOOR); + assertEq(vault.admin(), address(this), "interim admin until the Safe accepts"); + assertEq(vault.pendingAdmin(), adminSafe); + } +} From 62f3d66c039daf7c45a9327130372a965b9b455a Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 09:55:03 +0200 Subject: [PATCH 03/19] fix(contracts): Validate every governance deploy parameter Most governance parameters are fixed at deploy or can only be changed by a proposal through the governor being deployed, so the script now checks each one before broadcasting: - PEN_TOKEN must be the token of the new VAULT_ADDRESS - TIMELOCK_DELAY is 48h to 30 days; VOTING_DELAY and VOTING_PERIOD are at most 30 days, checked before narrowing to the governor's types - PROPOSAL_THRESHOLD and QUORUM_FLOOR are 18-decimal units between 1 PEN and the total supply, so a whole-PEN figure is refused - TIMELOCK_CANCELLER must not be the deployer and must have code TEST_DEPLOY=true admits the shorter delay, a codeless canceller and a zero floor that testnet drills use, and is refused on Base mainnet. The Base Sepolia rehearsal sets it and passes the vault address. The script broadcasts as msg.sender and returns the deployed contracts, so a Foundry test now runs it: each refusal, the drill override, and the role wiring (the deployer keeps no timelock role, the canceller holds CANCELLER_ROLE). --- contracts/.env.example | 36 +++++--- contracts/script/DeployGovernance.s.sol | 107 ++++++++++++++++------ contracts/test/DeployScripts.t.sol | 112 ++++++++++++++++++++++++ testing/src/phase5b-governance.mjs | 2 +- testing/src/rehearsal-deploy.mjs | 8 +- 5 files changed, 224 insertions(+), 41 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index fadeb4bb2..0840a3297 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -56,22 +56,32 @@ DAILY_CAP=1000000000000000000000000 EARLIEST_SWEEP_TS=1803859200 # --- DeployGovernance.s.sol (phase 5) --- -PEN_TOKEN= -TIMELOCK_DELAY=172800 # 48h (PRD V5) -VOTING_DELAY=86400 # 1 day, seconds (timestamp clock) -VOTING_PERIOD=432000 # 5 days -PROPOSAL_THRESHOLD= # token units required to propose +# The script checks every value below before broadcasting. +PEN_TOKEN= # must be the token of VAULT_ADDRESS +VAULT_ADDRESS= # the MigrationVault deployed above +TIMELOCK_DELAY=172800 # 48h (PRD V5); seconds, 48h to 30 days +VOTING_DELAY=86400 # 1 day, seconds (timestamp clock), max 30 days +VOTING_PERIOD=432000 # 5 days, seconds, max 30 days +# 18-decimal token units required to propose, NOT whole PEN: the script +# refuses anything below 1 PEN (1e18) or above the total supply. +PROPOSAL_THRESHOLD= # Percent of CIRCULATING supply (total minus the vault's unmigrated balance, # which setToken parks at the vote sink), counting For+Abstain # votes actually cast. Bounded below by QUORUM_FLOOR. QUORUM_FRACTION=2 -# Absolute quorum floor, 18-decimal token units. Decide this before phase 5 — -# there is no safe default and the script refuses to run without it. Size it -# so quorum is reachable soon after launch, or no proposal (an unpause -# included) can pass. +# PERMANENT (immutable in the governor). Absolute quorum floor, 18-decimal +# token units, NOT whole PEN: the script refuses anything below 1 PEN (1e18) +# or above the total supply. Decide this before phase 5 — there is no safe +# default and the script refuses to run without it. Size it so quorum is +# reachable soon after launch, or no proposal (an unpause included) can pass. QUORUM_FLOOR= -# Required: a multisig granted CANCELLER_ROLE on the timelock, so a queued -# operation can still be vetoed during the 48h delay (OZ Governor alone lets -# only the proposer cancel, and only before voting starts). The governance -# deploy script refuses to run without it. +# Required: a dedicated canceller Safe, separate from the guardian, granted +# CANCELLER_ROLE on the timelock so a queued operation can still be vetoed +# during the 48h delay (OZ Governor alone lets only the proposer cancel, and +# only before voting starts). The script refuses to run without it, with the +# deployer, or with an address that has no code on this chain. TIMELOCK_CANCELLER= +# Testnet drills only: TEST_DEPLOY=true admits a shorter TIMELOCK_DELAY, a +# codeless TIMELOCK_CANCELLER and QUORUM_FLOOR=0. Refused on Base mainnet; +# leave it unset for production. +# TEST_DEPLOY= diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index 14c6c2437..ce792d51c 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -5,6 +5,8 @@ import {Script, console} from "forge-std/Script.sol"; import {TimelockController} from "@openzeppelin/contracts/governance/TimelockController.sol"; import {PENGovernor} from "../src/PENGovernor.sol"; import {IVotes} from "@openzeppelin/contracts/governance/utils/IVotes.sol"; +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import {MigrationVault} from "../src/MigrationVault.sol"; /// @notice Deploys the on-chain governance stack (PRD rollout phase 5): /// TimelockController + PENGovernor, wired so that only the Governor @@ -16,48 +18,103 @@ import {IVotes} from "@openzeppelin/contracts/governance/utils/IVotes.sol"; /// (bootstrap Safe calls vault.transferAdmin(timelock), then a /// governance proposal calls vault.acceptAdmin()). /// -/// Environment: -/// PEN_TOKEN deployed PEN address -/// TIMELOCK_DELAY seconds (PRD V5: >= 48h = 172800) -/// VOTING_DELAY seconds before voting starts (timestamp clock) -/// VOTING_PERIOD seconds of voting -/// PROPOSAL_THRESHOLD token units needed to propose +/// Environment (each value is checked before anything is broadcast: most are +/// fixed at deploy or changeable only by a proposal through this governor, so +/// a wrong one could not be corrected afterwards): +/// PEN_TOKEN deployed PEN address; must be VAULT_ADDRESS's token +/// VAULT_ADDRESS deployed MigrationVault (its setToken verified that +/// PEN_TOKEN minted the whole supply into it) +/// TIMELOCK_DELAY seconds, 48h (172800) to 30 days (PRD V5) +/// VOTING_DELAY seconds before voting starts (timestamp clock), +/// at most 30 days +/// VOTING_PERIOD seconds of voting, at most 30 days +/// PROPOSAL_THRESHOLD 18-decimal token units needed to propose, 1 PEN +/// (1e18) to total supply; a whole-PEN figure such as +/// 50000 is refused /// QUORUM_FRACTION percent of CIRCULATING supply (total minus the /// vault's unmigrated balance parked at the vote /// sink, see PRD G1 as revised in review round 8) /// QUORUM_FLOOR absolute quorum lower bound, 18-decimal token -/// units; keeps early proposals from being trivially -/// cheap while circulating supply is still small -/// TIMELOCK_CANCELLER required: an address (the guardian/council Safe) -/// that can cancel a QUEUED operation during the -/// timelock delay. Without it only the Governor holds -/// CANCELLER_ROLE, and OZ Governor lets only the -/// proposer cancel, only before voting starts — so a -/// hostile proposal that passed could not be stopped -/// during the 48h delay by anyone (review round 9). +/// units, 1 PEN (1e18) to total supply; PERMANENT. +/// Keeps early proposals from being trivially cheap +/// while circulating supply is still small +/// TIMELOCK_CANCELLER required: a dedicated canceller Safe, separate +/// from the guardian, that can cancel a QUEUED +/// operation during the timelock delay. Without it +/// only the Governor holds CANCELLER_ROLE, and OZ +/// Governor lets only the proposer cancel, only before +/// voting starts, so a hostile proposal that passed +/// could not be stopped during the delay by anyone. /// Once the delay has passed anyone may execute, so -/// the script refuses to deploy without it. +/// the script refuses to deploy without it. It must +/// have code on this chain and must not be the +/// deployer. +/// TEST_DEPLOY optional, testnet drills only: allows a shorter +/// TIMELOCK_DELAY, a codeless TIMELOCK_CANCELLER and +/// QUORUM_FLOOR=0. Refused on Base mainnet. contract DeployGovernance is Script { - function run() external { + uint256 internal constant BASE_MAINNET = 8453; + uint256 internal constant MIN_TIMELOCK_DELAY = 2 days; + // Longer periods are a units slip (milliseconds land ~1000x further), and + // a governor whose votes never end could not vote to shorten them. + uint256 internal constant MAX_PERIOD = 30 days; + + function run() external returns (TimelockController timelock, PENGovernor governor) { + bool testDeploy = vm.envOr("TEST_DEPLOY", false); + require(!testDeploy || block.chainid != BASE_MAINNET, "TEST_DEPLOY is refused on Base mainnet"); + address token = vm.envAddress("PEN_TOKEN"); + require( + address(MigrationVault(vm.envAddress("VAULT_ADDRESS")).token()) == token, + "PEN_TOKEN is not the migration vault's token" + ); + uint256 supply = IERC20(token).totalSupply(); + uint256 timelockDelay = vm.envUint("TIMELOCK_DELAY"); - uint48 votingDelay = uint48(vm.envUint("VOTING_DELAY")); - uint32 votingPeriod = uint32(vm.envUint("VOTING_PERIOD")); + require(timelockDelay >= MIN_TIMELOCK_DELAY || testDeploy, "TIMELOCK_DELAY is below 48h"); + require(timelockDelay <= MAX_PERIOD, "TIMELOCK_DELAY is above 30 days (seconds expected)"); + uint256 votingDelay = vm.envUint("VOTING_DELAY"); + require(votingDelay <= MAX_PERIOD, "VOTING_DELAY is above 30 days (seconds expected)"); + uint256 votingPeriod = vm.envUint("VOTING_PERIOD"); + require(votingPeriod <= MAX_PERIOD, "VOTING_PERIOD is above 30 days (seconds expected)"); + + // 18-decimal units: below one PEN is a whole-PEN figure entered as + // wei, above the supply could never be met. uint256 proposalThreshold = vm.envUint("PROPOSAL_THRESHOLD"); + require( + proposalThreshold >= 1e18 && proposalThreshold <= supply, + "PROPOSAL_THRESHOLD must be 18-decimal units, 1 PEN to total supply" + ); uint256 quorumFraction = vm.envUint("QUORUM_FRACTION"); uint256 quorumFloor = vm.envUint("QUORUM_FLOOR"); + require( + (quorumFloor >= 1e18 || (testDeploy && quorumFloor == 0)) && quorumFloor <= supply, + "QUORUM_FLOOR must be 18-decimal units, 1 PEN to total supply" + ); + address canceller = vm.envOr("TIMELOCK_CANCELLER", address(0)); require(canceller != address(0), "TIMELOCK_CANCELLER is required: no veto during the timelock delay otherwise"); + require(canceller != msg.sender, "TIMELOCK_CANCELLER must not be the deployer"); + require( + canceller.code.length > 0 || testDeploy, "TIMELOCK_CANCELLER has no code on this chain (expected a Safe)" + ); - vm.startBroadcast(); + // Broadcast as msg.sender (forge sets it to --sender or the single + // signer), the deployer that is temporary admin so the roles below + // can be wired. + vm.startBroadcast(msg.sender); - // Deployer is temporary admin so the roles below can be wired. address[] memory empty = new address[](0); - TimelockController timelock = - new TimelockController(timelockDelay, empty, empty, msg.sender); + timelock = new TimelockController(timelockDelay, empty, empty, msg.sender); - PENGovernor governor = new PENGovernor( - IVotes(token), timelock, votingDelay, votingPeriod, proposalThreshold, quorumFraction, quorumFloor + governor = new PENGovernor( + IVotes(token), + timelock, + uint48(votingDelay), // bounded by MAX_PERIOD above + uint32(votingPeriod), + proposalThreshold, + quorumFraction, + quorumFloor ); // Only the Governor proposes; anyone may execute after the delay. The diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 9b9a23ff1..0fc715855 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -2,9 +2,13 @@ pragma solidity 0.8.26; import {Test} from "forge-std/Test.sol"; +import {TimelockController} from "@openzeppelin/contracts/governance/TimelockController.sol"; +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {PEN} from "../src/PEN.sol"; import {MigrationVault} from "../src/MigrationVault.sol"; +import {PENGovernor} from "../src/PENGovernor.sol"; import {Deploy} from "../script/Deploy.s.sol"; +import {DeployGovernance} from "../script/DeployGovernance.s.sol"; // These run the real scripts against their environment. The environment is // process-wide and forge runs test functions in parallel, so each script's @@ -61,3 +65,111 @@ contract DeployScriptTest is Test { assertEq(vault.pendingAdmin(), adminSafe); } } + +contract DeployGovernanceScriptTest is Test { + uint256 internal constant SUPPLY = 150_000_000e18; + + DeployGovernance internal script; + PEN internal pen; + MigrationVault internal vault; + // The dedicated canceller Safe, separate from the vault's guardian. + address internal canceller = makeAddr("cancellerSafe"); + + function setUp() public { + script = new DeployGovernance(); + address[] memory attestors = new address[](3); + attestors[0] = makeAddr("a0"); + attestors[1] = makeAddr("a1"); + attestors[2] = makeAddr("a2"); + vault = new MigrationVault( + address(this), makeAddr("guardian"), attestors, 2, 1e6, 1e24, 1e24, block.timestamp + 365 days + ); + pen = new PEN(address(vault), SUPPLY); + vault.setToken(IERC20(address(pen))); + vm.etch(canceller, hex"00"); // stands in for the Safe's code + } + + function _setValidEnv() internal { + vm.setEnv("TEST_DEPLOY", "false"); + vm.setEnv("PEN_TOKEN", vm.toString(address(pen))); + vm.setEnv("VAULT_ADDRESS", vm.toString(address(vault))); + vm.setEnv("TIMELOCK_DELAY", "172800"); + vm.setEnv("VOTING_DELAY", "86400"); + vm.setEnv("VOTING_PERIOD", "432000"); + vm.setEnv("PROPOSAL_THRESHOLD", "1000000000000000000000"); // 1,000 PEN + vm.setEnv("QUORUM_FRACTION", "2"); + vm.setEnv("QUORUM_FLOOR", "100000000000000000000000"); // 100,000 PEN + vm.setEnv("TIMELOCK_CANCELLER", vm.toString(canceller)); + } + + function _expectRefused(string memory key, string memory value, string memory reason) internal { + _setValidEnv(); + vm.setEnv(key, value); + vm.expectRevert(bytes(reason)); + script.run(); + } + + function test_RunValidatesEveryParameterAndWiresRoles() public { + string memory notToken = "PEN_TOKEN is not the migration vault's token"; + _expectRefused("PEN_TOKEN", vm.toString(address(vault)), notToken); + _expectRefused("PEN_TOKEN", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notToken); + + _expectRefused("TIMELOCK_DELAY", "0", "TIMELOCK_DELAY is below 48h"); + _expectRefused("TIMELOCK_DELAY", "172799", "TIMELOCK_DELAY is below 48h"); + _expectRefused("TIMELOCK_DELAY", "172800000", "TIMELOCK_DELAY is above 30 days (seconds expected)"); + _expectRefused("VOTING_DELAY", "86400000", "VOTING_DELAY is above 30 days (seconds expected)"); + // Would have been narrowed to 432000 by a bare uint32 cast. + _expectRefused("VOTING_PERIOD", "4295399296", "VOTING_PERIOD is above 30 days (seconds expected)"); + + string memory threshold = "PROPOSAL_THRESHOLD must be 18-decimal units, 1 PEN to total supply"; + _expectRefused("PROPOSAL_THRESHOLD", "1000", threshold); // whole PEN + _expectRefused("PROPOSAL_THRESHOLD", vm.toString(SUPPLY + 1), threshold); + string memory floor = "QUORUM_FLOOR must be 18-decimal units, 1 PEN to total supply"; + _expectRefused("QUORUM_FLOOR", "100000", floor); // whole PEN + _expectRefused("QUORUM_FLOOR", "0", floor); + _expectRefused("QUORUM_FLOOR", vm.toString(SUPPLY + 1), floor); + + _expectRefused( + "TIMELOCK_CANCELLER", "", "TIMELOCK_CANCELLER is required: no veto during the timelock delay otherwise" + ); + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(this)), "TIMELOCK_CANCELLER must not be the deployer"); + _expectRefused( + "TIMELOCK_CANCELLER", + vm.toString(makeAddr("codeless")), + "TIMELOCK_CANCELLER has no code on this chain (expected a Safe)" + ); + + vm.chainId(8453); + _expectRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); + vm.chainId(31337); + + _setValidEnv(); + (TimelockController timelock, PENGovernor governor) = script.run(); + bytes32[4] memory roles = [ + timelock.DEFAULT_ADMIN_ROLE(), timelock.PROPOSER_ROLE(), timelock.CANCELLER_ROLE(), timelock.EXECUTOR_ROLE() + ]; + for (uint256 i = 0; i < roles.length; i++) { + assertFalse(timelock.hasRole(roles[i], address(this)), "deployer keeps no timelock role"); + } + assertTrue(timelock.hasRole(timelock.CANCELLER_ROLE(), canceller)); + assertTrue(timelock.hasRole(timelock.CANCELLER_ROLE(), address(governor))); + assertTrue(timelock.hasRole(timelock.PROPOSER_ROLE(), address(governor))); + assertTrue(timelock.hasRole(timelock.EXECUTOR_ROLE(), address(0))); + assertTrue(timelock.hasRole(timelock.DEFAULT_ADMIN_ROLE(), address(timelock))); + assertEq(timelock.getMinDelay(), 2 days); + assertEq(address(governor.token()), address(pen)); + assertEq(governor.votingDelay(), 1 days); + assertEq(governor.votingPeriod(), 5 days); + assertEq(governor.proposalThreshold(), 1_000e18); + assertEq(governor.quorumFloor(), 100_000e18); + + // Testnet drills: a short delay, a codeless canceller and no floor. + _setValidEnv(); + vm.setEnv("TEST_DEPLOY", "true"); + vm.setEnv("TIMELOCK_DELAY", "180"); + vm.setEnv("QUORUM_FLOOR", "0"); + vm.setEnv("TIMELOCK_CANCELLER", vm.toString(makeAddr("codeless"))); + (timelock,) = script.run(); + assertEq(timelock.getMinDelay(), 180); + } +} diff --git a/testing/src/phase5b-governance.mjs b/testing/src/phase5b-governance.mjs index a080fcb45..ef7de71f8 100644 --- a/testing/src/phase5b-governance.mjs +++ b/testing/src/phase5b-governance.mjs @@ -123,7 +123,7 @@ async function main() { // --- governance deployment ---------------------------------------------- section("Governance deployment (first execution of DeployGovernance.s.sol)"); - const { timelock, governor, params } = deployGovernanceToSepolia({ env, pen, canceller: ctx.roles.guardian.address, log }); + const { timelock, governor, params } = deployGovernanceToSepolia({ env, pen, vault, canceller: ctx.roles.guardian.address, log }); log(`timelock ${timelock}\n governor ${governor}`); const G = { address: governor, abi: governorAbi }; const T = { address: timelock, abi: timelockAbi }; diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index c504df3ef..238ec19ff 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -90,12 +90,16 @@ export function governanceDrillParams(env) { }; } -/** Runs the real DeployGovernance.s.sol — its first execution anywhere. */ -export function deployGovernanceToSepolia({ env, pen, canceller, log }) { +/** Runs the real DeployGovernance.s.sol — its first execution anywhere. + * TEST_DEPLOY admits the drill-scale delay, the zero floor and an EOA + * canceller, which the script refuses for a production deploy. */ +export function deployGovernanceToSepolia({ env, pen, vault, canceller, log }) { const params = governanceDrillParams(env); const scriptEnv = { ...process.env, + TEST_DEPLOY: "true", PEN_TOKEN: pen, + VAULT_ADDRESS: vault, TIMELOCK_DELAY: String(params.timelockDelay), VOTING_DELAY: String(params.votingDelay), VOTING_PERIOD: String(params.votingPeriod), From 886f27015846f488d0293187cf00a9a4547f7326 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 09:55:10 +0200 Subject: [PATCH 04/19] test(contracts): Give the timelock canceller its own address in the governor tests The canceller is meant to be a dedicated Safe, separate from the pause guardian, so the governor test fixture now grants CANCELLER_ROLE to its own address instead of the guardian's. The Base Sepolia drill keeps reusing its guardian key for the role and now says so. --- contracts/test/PENGovernor.t.sol | 8 +++++--- testing/src/phase5b-governance.mjs | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/contracts/test/PENGovernor.t.sol b/contracts/test/PENGovernor.t.sol index fa741e5a7..5fbfadbfb 100644 --- a/contracts/test/PENGovernor.t.sol +++ b/contracts/test/PENGovernor.t.sol @@ -24,6 +24,7 @@ contract PENGovernorTest is Test { address internal alice = makeAddr("alice"); address internal guardian = makeAddr("guardian"); + address internal canceller = makeAddr("canceller"); function setUp() public { // Token held by alice directly so she has voting power without going @@ -40,8 +41,9 @@ contract PENGovernorTest is Test { timelock.grantRole(timelock.PROPOSER_ROLE(), address(governor)); timelock.grantRole(timelock.CANCELLER_ROLE(), address(governor)); // The human veto during the timelock delay (DeployGovernance's - // required TIMELOCK_CANCELLER): the guardian Safe. - timelock.grantRole(timelock.CANCELLER_ROLE(), guardian); + // required TIMELOCK_CANCELLER): a dedicated canceller Safe, separate + // from the guardian. + timelock.grantRole(timelock.CANCELLER_ROLE(), canceller); timelock.grantRole(timelock.EXECUTOR_ROLE(), address(0)); timelock.renounceRole(timelock.DEFAULT_ADMIN_ROLE(), address(this)); @@ -121,7 +123,7 @@ contract PENGovernorTest is Test { // GovernorTimelockControl salts the operation with the governor address. bytes32 salt = bytes20(address(governor)) ^ descriptionHash; bytes32 operationId = timelock.hashOperationBatch(targets, values, calldatas, 0, salt); - vm.prank(guardian); + vm.prank(canceller); timelock.cancel(operationId); assertEq(uint256(governor.state(proposalId)), uint256(IGovernor.ProposalState.Canceled)); diff --git a/testing/src/phase5b-governance.mjs b/testing/src/phase5b-governance.mjs index ef7de71f8..5addb5a6c 100644 --- a/testing/src/phase5b-governance.mjs +++ b/testing/src/phase5b-governance.mjs @@ -123,6 +123,8 @@ async function main() { // --- governance deployment ---------------------------------------------- section("Governance deployment (first execution of DeployGovernance.s.sol)"); + // The drill reuses the guardian key as canceller; production uses a + // dedicated canceller Safe, separate from the guardian. const { timelock, governor, params } = deployGovernanceToSepolia({ env, pen, vault, canceller: ctx.roles.guardian.address, log }); log(`timelock ${timelock}\n governor ${governor}`); const G = { address: governor, abi: governorAbi }; From 89c1a503717a007a04cca9edadaaf4bd6f72f0f8 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 10:13:41 +0200 Subject: [PATCH 05/19] fix(contracts): Require the timelock canceller to be a separate Safe Having code on the chain is true of any contract, so the governance deploy script now asks the canceller for its Safe threshold instead: getThreshold() must succeed and return a non-zero value, and an EIP-7702 delegated EOA is refused whatever it delegates to. The canceller must also not be PEN_TOKEN, VAULT_ADDRESS or the vault's guardian. TEST_DEPLOY keeps admitting a canceller that is not a Safe and the guardian key, which the testnet drills reuse. The deploy test now uses a stub that answers getThreshold() and covers each refusal. --- contracts/.env.example | 10 +++--- contracts/script/DeployGovernance.s.sol | 29 +++++++++++----- contracts/test/DeployScripts.t.sol | 46 +++++++++++++++++++------ testing/src/rehearsal-deploy.mjs | 4 +-- 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index 0840a3297..ed293aaea 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -78,10 +78,12 @@ QUORUM_FLOOR= # Required: a dedicated canceller Safe, separate from the guardian, granted # CANCELLER_ROLE on the timelock so a queued operation can still be vetoed # during the 48h delay (OZ Governor alone lets only the proposer cancel, and -# only before voting starts). The script refuses to run without it, with the -# deployer, or with an address that has no code on this chain. +# only before voting starts). Create the Safe on this chain first: the script +# refuses to run without it, with an address that is not a Safe (getThreshold() +# must return a non-zero threshold), or with the deployer, PEN_TOKEN, +# VAULT_ADDRESS or the vault's guardian. TIMELOCK_CANCELLER= # Testnet drills only: TEST_DEPLOY=true admits a shorter TIMELOCK_DELAY, a -# codeless TIMELOCK_CANCELLER and QUORUM_FLOOR=0. Refused on Base mainnet; -# leave it unset for production. +# TIMELOCK_CANCELLER that is not a Safe or is the guardian, and +# QUORUM_FLOOR=0. Refused on Base mainnet; leave it unset for production. # TEST_DEPLOY= diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index ce792d51c..bbd225439 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -47,11 +47,14 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// could not be stopped during the delay by anyone. /// Once the delay has passed anyone may execute, so /// the script refuses to deploy without it. It must -/// have code on this chain and must not be the -/// deployer. +/// be a Safe on this chain (getThreshold() returns a +/// non-zero threshold, no EIP-7702 delegated EOA) and +/// must not be the deployer, PEN_TOKEN, VAULT_ADDRESS +/// or the vault's guardian. /// TEST_DEPLOY optional, testnet drills only: allows a shorter -/// TIMELOCK_DELAY, a codeless TIMELOCK_CANCELLER and -/// QUORUM_FLOOR=0. Refused on Base mainnet. +/// TIMELOCK_DELAY, a TIMELOCK_CANCELLER that is not a +/// Safe or is the guardian, and QUORUM_FLOOR=0. +/// Refused on Base mainnet. contract DeployGovernance is Script { uint256 internal constant BASE_MAINNET = 8453; uint256 internal constant MIN_TIMELOCK_DELAY = 2 days; @@ -64,10 +67,8 @@ contract DeployGovernance is Script { require(!testDeploy || block.chainid != BASE_MAINNET, "TEST_DEPLOY is refused on Base mainnet"); address token = vm.envAddress("PEN_TOKEN"); - require( - address(MigrationVault(vm.envAddress("VAULT_ADDRESS")).token()) == token, - "PEN_TOKEN is not the migration vault's token" - ); + MigrationVault vault = MigrationVault(vm.envAddress("VAULT_ADDRESS")); + require(address(vault.token()) == token, "PEN_TOKEN is not the migration vault's token"); uint256 supply = IERC20(token).totalSupply(); uint256 timelockDelay = vm.envUint("TIMELOCK_DELAY"); @@ -95,8 +96,10 @@ contract DeployGovernance is Script { address canceller = vm.envOr("TIMELOCK_CANCELLER", address(0)); require(canceller != address(0), "TIMELOCK_CANCELLER is required: no veto during the timelock delay otherwise"); require(canceller != msg.sender, "TIMELOCK_CANCELLER must not be the deployer"); + require(canceller != token && canceller != address(vault), "TIMELOCK_CANCELLER must not be PEN or the vault"); + require(canceller != vault.guardian() || testDeploy, "TIMELOCK_CANCELLER must be separate from the guardian"); require( - canceller.code.length > 0 || testDeploy, "TIMELOCK_CANCELLER has no code on this chain (expected a Safe)" + _isSafe(canceller) || testDeploy, "TIMELOCK_CANCELLER is not a Safe on this chain (getThreshold() failed)" ); // Broadcast as msg.sender (forge sets it to --sender or the single @@ -133,4 +136,12 @@ contract DeployGovernance is Script { console.log("TimelockController: ", address(timelock)); console.log("PENGovernor: ", address(governor)); } + + // Any contract has code; a Safe answers getThreshold() with its non-zero + // threshold. An EIP-7702 delegated EOA stays a single key whatever it + // delegates to. + function _isSafe(address account) internal view returns (bool) { + (bool ok, bytes memory threshold) = account.staticcall(abi.encodeWithSignature("getThreshold()")); + return ok && threshold.length == 32 && abi.decode(threshold, (uint256)) > 0 && bytes3(account.code) != 0xef0100; + } } diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 0fc715855..d14e8a0a5 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -66,14 +66,29 @@ contract DeployScriptTest is Test { } } +// Answers getThreshold() like a Safe. Immutable, so an account delegating to +// it answers the same. +contract SafeStub { + uint256 internal immutable threshold; + + constructor(uint256 threshold_) { + threshold = threshold_; + } + + function getThreshold() external view returns (uint256) { + return threshold; + } +} + contract DeployGovernanceScriptTest is Test { uint256 internal constant SUPPLY = 150_000_000e18; DeployGovernance internal script; PEN internal pen; MigrationVault internal vault; - // The dedicated canceller Safe, separate from the vault's guardian. - address internal canceller = makeAddr("cancellerSafe"); + // The dedicated canceller Safe, separate from the vault's guardian Safe. + address internal canceller = address(new SafeStub(2)); + address internal guardian = address(new SafeStub(2)); function setUp() public { script = new DeployGovernance(); @@ -81,12 +96,9 @@ contract DeployGovernanceScriptTest is Test { attestors[0] = makeAddr("a0"); attestors[1] = makeAddr("a1"); attestors[2] = makeAddr("a2"); - vault = new MigrationVault( - address(this), makeAddr("guardian"), attestors, 2, 1e6, 1e24, 1e24, block.timestamp + 365 days - ); + vault = new MigrationVault(address(this), guardian, attestors, 2, 1e6, 1e24, 1e24, block.timestamp + 365 days); pen = new PEN(address(vault), SUPPLY); vault.setToken(IERC20(address(pen))); - vm.etch(canceller, hex"00"); // stands in for the Safe's code } function _setValidEnv() internal { @@ -133,11 +145,19 @@ contract DeployGovernanceScriptTest is Test { "TIMELOCK_CANCELLER", "", "TIMELOCK_CANCELLER is required: no veto during the timelock delay otherwise" ); _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(this)), "TIMELOCK_CANCELLER must not be the deployer"); + string memory notPenOrVault = "TIMELOCK_CANCELLER must not be PEN or the vault"; + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(pen)), notPenOrVault); + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(vault)), notPenOrVault); _expectRefused( - "TIMELOCK_CANCELLER", - vm.toString(makeAddr("codeless")), - "TIMELOCK_CANCELLER has no code on this chain (expected a Safe)" + "TIMELOCK_CANCELLER", vm.toString(guardian), "TIMELOCK_CANCELLER must be separate from the guardian" ); + string memory notSafe = "TIMELOCK_CANCELLER is not a Safe on this chain (getThreshold() failed)"; + _expectRefused("TIMELOCK_CANCELLER", vm.toString(makeAddr("codeless")), notSafe); + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notSafe); + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new SafeStub(0))), notSafe); + address delegated = makeAddr("delegatedEoa"); + vm.etch(delegated, abi.encodePacked(hex"ef0100", canceller)); // EIP-7702 delegation to a Safe + _expectRefused("TIMELOCK_CANCELLER", vm.toString(delegated), notSafe); vm.chainId(8453); _expectRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); @@ -163,13 +183,17 @@ contract DeployGovernanceScriptTest is Test { assertEq(governor.proposalThreshold(), 1_000e18); assertEq(governor.quorumFloor(), 100_000e18); - // Testnet drills: a short delay, a codeless canceller and no floor. + // Testnet drills: a short delay, no floor and the guardian key, not a + // Safe, as canceller. + address guardianKey = makeAddr("guardianKey"); + vault.setGuardian(guardianKey); _setValidEnv(); vm.setEnv("TEST_DEPLOY", "true"); vm.setEnv("TIMELOCK_DELAY", "180"); vm.setEnv("QUORUM_FLOOR", "0"); - vm.setEnv("TIMELOCK_CANCELLER", vm.toString(makeAddr("codeless"))); + vm.setEnv("TIMELOCK_CANCELLER", vm.toString(guardianKey)); (timelock,) = script.run(); assertEq(timelock.getMinDelay(), 180); + assertTrue(timelock.hasRole(timelock.CANCELLER_ROLE(), guardianKey)); } } diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index 238ec19ff..2640e3402 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -91,8 +91,8 @@ export function governanceDrillParams(env) { } /** Runs the real DeployGovernance.s.sol — its first execution anywhere. - * TEST_DEPLOY admits the drill-scale delay, the zero floor and an EOA - * canceller, which the script refuses for a production deploy. */ + * TEST_DEPLOY admits the drill-scale delay, the zero floor and the guardian + * EOA as canceller, which the script refuses for a production deploy. */ export function deployGovernanceToSepolia({ env, pen, vault, canceller, log }) { const params = governanceDrillParams(env); const scriptEnv = { From 3ceaf191f1fdd045d37d7ef9a9f87314160594aa Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 10:14:31 +0200 Subject: [PATCH 06/19] fix(contracts): Bound the governance voting windows from below VOTING_DELAY and VOTING_PERIOD only had an upper bound, so days entered as seconds produced a governor with a vote of a few seconds. The deploy script now requires a voting delay of at least one hour and a voting period of at least one day. TEST_DEPLOY admits the shorter drill windows, which the deploy test now runs. --- contracts/.env.example | 11 ++++++----- contracts/script/DeployGovernance.s.sol | 17 ++++++++++++----- contracts/test/DeployScripts.t.sol | 13 ++++++++++--- testing/src/rehearsal-deploy.mjs | 5 +++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index ed293aaea..a0610aac6 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -60,8 +60,8 @@ EARLIEST_SWEEP_TS=1803859200 PEN_TOKEN= # must be the token of VAULT_ADDRESS VAULT_ADDRESS= # the MigrationVault deployed above TIMELOCK_DELAY=172800 # 48h (PRD V5); seconds, 48h to 30 days -VOTING_DELAY=86400 # 1 day, seconds (timestamp clock), max 30 days -VOTING_PERIOD=432000 # 5 days, seconds, max 30 days +VOTING_DELAY=86400 # 1 day, seconds (timestamp clock), 1h to 30 days +VOTING_PERIOD=432000 # 5 days, seconds, 1 to 30 days # 18-decimal token units required to propose, NOT whole PEN: the script # refuses anything below 1 PEN (1e18) or above the total supply. PROPOSAL_THRESHOLD= @@ -83,7 +83,8 @@ QUORUM_FLOOR= # must return a non-zero threshold), or with the deployer, PEN_TOKEN, # VAULT_ADDRESS or the vault's guardian. TIMELOCK_CANCELLER= -# Testnet drills only: TEST_DEPLOY=true admits a shorter TIMELOCK_DELAY, a -# TIMELOCK_CANCELLER that is not a Safe or is the guardian, and -# QUORUM_FLOOR=0. Refused on Base mainnet; leave it unset for production. +# Testnet drills only: TEST_DEPLOY=true admits a shorter TIMELOCK_DELAY, +# VOTING_DELAY and VOTING_PERIOD, a TIMELOCK_CANCELLER that is not a Safe or +# is the guardian, and QUORUM_FLOOR=0. Refused on Base mainnet; leave it +# unset for production. # TEST_DEPLOY= diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index bbd225439..7c3cad301 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -26,8 +26,8 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// PEN_TOKEN minted the whole supply into it) /// TIMELOCK_DELAY seconds, 48h (172800) to 30 days (PRD V5) /// VOTING_DELAY seconds before voting starts (timestamp clock), -/// at most 30 days -/// VOTING_PERIOD seconds of voting, at most 30 days +/// 1 hour to 30 days +/// VOTING_PERIOD seconds of voting, 1 day to 30 days /// PROPOSAL_THRESHOLD 18-decimal token units needed to propose, 1 PEN /// (1e18) to total supply; a whole-PEN figure such as /// 50000 is refused @@ -52,12 +52,17 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// must not be the deployer, PEN_TOKEN, VAULT_ADDRESS /// or the vault's guardian. /// TEST_DEPLOY optional, testnet drills only: allows a shorter -/// TIMELOCK_DELAY, a TIMELOCK_CANCELLER that is not a -/// Safe or is the guardian, and QUORUM_FLOOR=0. -/// Refused on Base mainnet. +/// TIMELOCK_DELAY, VOTING_DELAY and VOTING_PERIOD, a +/// TIMELOCK_CANCELLER that is not a Safe or is the +/// guardian, and QUORUM_FLOOR=0. Refused on Base +/// mainnet. contract DeployGovernance is Script { uint256 internal constant BASE_MAINNET = 8453; uint256 internal constant MIN_TIMELOCK_DELAY = 2 days; + // Shorter windows are a units slip (days entered as seconds), and a vote + // too short for holders to take part in could not be undone by one. + uint256 internal constant MIN_VOTING_DELAY = 1 hours; + uint256 internal constant MIN_VOTING_PERIOD = 1 days; // Longer periods are a units slip (milliseconds land ~1000x further), and // a governor whose votes never end could not vote to shorten them. uint256 internal constant MAX_PERIOD = 30 days; @@ -75,8 +80,10 @@ contract DeployGovernance is Script { require(timelockDelay >= MIN_TIMELOCK_DELAY || testDeploy, "TIMELOCK_DELAY is below 48h"); require(timelockDelay <= MAX_PERIOD, "TIMELOCK_DELAY is above 30 days (seconds expected)"); uint256 votingDelay = vm.envUint("VOTING_DELAY"); + require(votingDelay >= MIN_VOTING_DELAY || testDeploy, "VOTING_DELAY is below 1h (seconds expected)"); require(votingDelay <= MAX_PERIOD, "VOTING_DELAY is above 30 days (seconds expected)"); uint256 votingPeriod = vm.envUint("VOTING_PERIOD"); + require(votingPeriod >= MIN_VOTING_PERIOD || testDeploy, "VOTING_PERIOD is below 1 day (seconds expected)"); require(votingPeriod <= MAX_PERIOD, "VOTING_PERIOD is above 30 days (seconds expected)"); // 18-decimal units: below one PEN is a whole-PEN figure entered as diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index d14e8a0a5..75a27f0d7 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -129,7 +129,11 @@ contract DeployGovernanceScriptTest is Test { _expectRefused("TIMELOCK_DELAY", "0", "TIMELOCK_DELAY is below 48h"); _expectRefused("TIMELOCK_DELAY", "172799", "TIMELOCK_DELAY is below 48h"); _expectRefused("TIMELOCK_DELAY", "172800000", "TIMELOCK_DELAY is above 30 days (seconds expected)"); + _expectRefused("VOTING_DELAY", "1", "VOTING_DELAY is below 1h (seconds expected)"); // days + _expectRefused("VOTING_DELAY", "3599", "VOTING_DELAY is below 1h (seconds expected)"); _expectRefused("VOTING_DELAY", "86400000", "VOTING_DELAY is above 30 days (seconds expected)"); + _expectRefused("VOTING_PERIOD", "5", "VOTING_PERIOD is below 1 day (seconds expected)"); // days + _expectRefused("VOTING_PERIOD", "86399", "VOTING_PERIOD is below 1 day (seconds expected)"); // Would have been narrowed to 432000 by a bare uint32 cast. _expectRefused("VOTING_PERIOD", "4295399296", "VOTING_PERIOD is above 30 days (seconds expected)"); @@ -183,17 +187,20 @@ contract DeployGovernanceScriptTest is Test { assertEq(governor.proposalThreshold(), 1_000e18); assertEq(governor.quorumFloor(), 100_000e18); - // Testnet drills: a short delay, no floor and the guardian key, not a - // Safe, as canceller. + // Testnet drills: short delays and voting, no floor and the guardian + // key, not a Safe, as canceller. address guardianKey = makeAddr("guardianKey"); vault.setGuardian(guardianKey); _setValidEnv(); vm.setEnv("TEST_DEPLOY", "true"); vm.setEnv("TIMELOCK_DELAY", "180"); + vm.setEnv("VOTING_DELAY", "60"); + vm.setEnv("VOTING_PERIOD", "240"); vm.setEnv("QUORUM_FLOOR", "0"); vm.setEnv("TIMELOCK_CANCELLER", vm.toString(guardianKey)); - (timelock,) = script.run(); + (timelock, governor) = script.run(); assertEq(timelock.getMinDelay(), 180); + assertEq(governor.votingPeriod(), 240); assertTrue(timelock.hasRole(timelock.CANCELLER_ROLE(), guardianKey)); } } diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index 2640e3402..5f10c238e 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -91,8 +91,9 @@ export function governanceDrillParams(env) { } /** Runs the real DeployGovernance.s.sol — its first execution anywhere. - * TEST_DEPLOY admits the drill-scale delay, the zero floor and the guardian - * EOA as canceller, which the script refuses for a production deploy. */ + * TEST_DEPLOY admits the drill-scale delay and voting, the zero floor and + * the guardian EOA as canceller, which the script refuses for a production + * deploy. */ export function deployGovernanceToSepolia({ env, pen, vault, canceller, log }) { const params = governanceDrillParams(env); const scriptEnv = { From 56b8157671b0b9b80a27df7dc8b8c68a15183679 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 10:14:39 +0200 Subject: [PATCH 07/19] docs(contracts): Point the quorum note at the PRD section --- contracts/script/DeployGovernance.s.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index 7c3cad301..a9e35356a 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -33,7 +33,7 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// 50000 is refused /// QUORUM_FRACTION percent of CIRCULATING supply (total minus the /// vault's unmigrated balance parked at the vote -/// sink, see PRD G1 as revised in review round 8) +/// sink, see PRD G1) /// QUORUM_FLOOR absolute quorum lower bound, 18-decimal token /// units, 1 PEN (1e18) to total supply; PERMANENT. /// Keeps early proposals from being trivially cheap From 2ae34730d251b06d76ba6b7a66e6884334553b27 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:37:23 +0200 Subject: [PATCH 08/19] fix(contracts): Name the reason a timelock canceller is not a Safe The canceller check in the governance deploy refused every non-Safe with the same message, including a Safe with threshold 0 and an EIP-7702 delegated EOA, where getThreshold() had answered. It now names the reason: no code on this chain, a delegated EOA, getThreshold() failing or answering anything but one word, or a threshold of 0. The deploy test covers each refusal, including short and long getThreshold() answers. --- contracts/script/DeployGovernance.s.sol | 18 ++++++------ contracts/test/DeployScripts.t.sol | 37 +++++++++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index a9e35356a..308936d53 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -105,9 +105,7 @@ contract DeployGovernance is Script { require(canceller != msg.sender, "TIMELOCK_CANCELLER must not be the deployer"); require(canceller != token && canceller != address(vault), "TIMELOCK_CANCELLER must not be PEN or the vault"); require(canceller != vault.guardian() || testDeploy, "TIMELOCK_CANCELLER must be separate from the guardian"); - require( - _isSafe(canceller) || testDeploy, "TIMELOCK_CANCELLER is not a Safe on this chain (getThreshold() failed)" - ); + if (!testDeploy) _safeThreshold(canceller); // Broadcast as msg.sender (forge sets it to --sender or the single // signer), the deployer that is temporary admin so the roles below @@ -144,11 +142,15 @@ contract DeployGovernance is Script { console.log("PENGovernor: ", address(governor)); } - // Any contract has code; a Safe answers getThreshold() with its non-zero - // threshold. An EIP-7702 delegated EOA stays a single key whatever it - // delegates to. - function _isSafe(address account) internal view returns (bool) { + // A Safe answers getThreshold() with its non-zero threshold. An EIP-7702 + // delegated EOA stays a single key whatever it delegates to. + function _safeThreshold(address account) internal view returns (uint256) { + require(account.code.length > 0, "TIMELOCK_CANCELLER has no code on this chain (create the Safe first)"); + require(bytes3(account.code) != 0xef0100, "TIMELOCK_CANCELLER is an EIP-7702 delegated EOA, not a Safe"); (bool ok, bytes memory threshold) = account.staticcall(abi.encodeWithSignature("getThreshold()")); - return ok && threshold.length == 32 && abi.decode(threshold, (uint256)) > 0 && bytes3(account.code) != 0xef0100; + require(ok && threshold.length == 32, "TIMELOCK_CANCELLER is not a Safe (getThreshold() failed)"); + uint256 value = abi.decode(threshold, (uint256)); + require(value > 0, "TIMELOCK_CANCELLER Safe has threshold 0 (not set up)"); + return value; } } diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 75a27f0d7..a50b649b7 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -80,6 +80,19 @@ contract SafeStub { } } +// Answers every call with the given raw bytes. +contract RawAnswerStub { + bytes internal answer; + + constructor(bytes memory answer_) { + answer = answer_; + } + + fallback(bytes calldata) external returns (bytes memory) { + return answer; + } +} + contract DeployGovernanceScriptTest is Test { uint256 internal constant SUPPLY = 150_000_000e18; @@ -155,13 +168,27 @@ contract DeployGovernanceScriptTest is Test { _expectRefused( "TIMELOCK_CANCELLER", vm.toString(guardian), "TIMELOCK_CANCELLER must be separate from the guardian" ); - string memory notSafe = "TIMELOCK_CANCELLER is not a Safe on this chain (getThreshold() failed)"; - _expectRefused("TIMELOCK_CANCELLER", vm.toString(makeAddr("codeless")), notSafe); - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notSafe); - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new SafeStub(0))), notSafe); + _expectRefused( + "TIMELOCK_CANCELLER", + vm.toString(makeAddr("codeless")), + "TIMELOCK_CANCELLER has no code on this chain (create the Safe first)" + ); address delegated = makeAddr("delegatedEoa"); vm.etch(delegated, abi.encodePacked(hex"ef0100", canceller)); // EIP-7702 delegation to a Safe - _expectRefused("TIMELOCK_CANCELLER", vm.toString(delegated), notSafe); + _expectRefused( + "TIMELOCK_CANCELLER", vm.toString(delegated), "TIMELOCK_CANCELLER is an EIP-7702 delegated EOA, not a Safe" + ); + string memory notSafe = "TIMELOCK_CANCELLER is not a Safe (getThreshold() failed)"; + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notSafe); + // Answers that are not exactly one word, whatever their first bytes say. + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new RawAnswerStub(hex"03"))), notSafe); + bytes memory twoWords = abi.encode(uint256(3), uint256(3)); + _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new RawAnswerStub(twoWords))), notSafe); + _expectRefused( + "TIMELOCK_CANCELLER", + vm.toString(address(new SafeStub(0))), + "TIMELOCK_CANCELLER Safe has threshold 0 (not set up)" + ); vm.chainId(8453); _expectRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); From 60c2502e0863e4d24b9ebd38afc36c67cb016ef9 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:37:55 +0200 Subject: [PATCH 09/19] fix(contracts): Require a timelock canceller Safe with at least two signers A 1-of-n canceller Safe is a single key holding a veto over every queued proposal. The governance deploy now refuses a canceller whose Safe threshold is below 2 unless TEST_DEPLOY is set. --- contracts/.env.example | 6 +++--- contracts/script/DeployGovernance.s.sol | 15 ++++++++++----- contracts/test/DeployScripts.t.sol | 6 +++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index a0610aac6..f7ae8f7c6 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -79,9 +79,9 @@ QUORUM_FLOOR= # CANCELLER_ROLE on the timelock so a queued operation can still be vetoed # during the 48h delay (OZ Governor alone lets only the proposer cancel, and # only before voting starts). Create the Safe on this chain first: the script -# refuses to run without it, with an address that is not a Safe (getThreshold() -# must return a non-zero threshold), or with the deployer, PEN_TOKEN, -# VAULT_ADDRESS or the vault's guardian. +# refuses to run without it, with an address that is not a Safe, with a Safe +# whose threshold (getThreshold()) is below 2, or with the deployer, +# PEN_TOKEN, VAULT_ADDRESS or the vault's guardian. TIMELOCK_CANCELLER= # Testnet drills only: TEST_DEPLOY=true admits a shorter TIMELOCK_DELAY, # VOTING_DELAY and VOTING_PERIOD, a TIMELOCK_CANCELLER that is not a Safe or diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index 308936d53..28550ef07 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -47,10 +47,10 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// could not be stopped during the delay by anyone. /// Once the delay has passed anyone may execute, so /// the script refuses to deploy without it. It must -/// be a Safe on this chain (getThreshold() returns a -/// non-zero threshold, no EIP-7702 delegated EOA) and -/// must not be the deployer, PEN_TOKEN, VAULT_ADDRESS -/// or the vault's guardian. +/// be a Safe on this chain with a threshold of at +/// least 2 (getThreshold(); no EIP-7702 delegated EOA) +/// and must not be the deployer, PEN_TOKEN, +/// VAULT_ADDRESS or the vault's guardian. /// TEST_DEPLOY optional, testnet drills only: allows a shorter /// TIMELOCK_DELAY, VOTING_DELAY and VOTING_PERIOD, a /// TIMELOCK_CANCELLER that is not a Safe or is the @@ -66,6 +66,8 @@ contract DeployGovernance is Script { // Longer periods are a units slip (milliseconds land ~1000x further), and // a governor whose votes never end could not vote to shorten them. uint256 internal constant MAX_PERIOD = 30 days; + // A 1-of-n canceller is a single key holding a veto over every proposal. + uint256 internal constant MIN_CANCELLER_THRESHOLD = 2; function run() external returns (TimelockController timelock, PENGovernor governor) { bool testDeploy = vm.envOr("TEST_DEPLOY", false); @@ -105,7 +107,10 @@ contract DeployGovernance is Script { require(canceller != msg.sender, "TIMELOCK_CANCELLER must not be the deployer"); require(canceller != token && canceller != address(vault), "TIMELOCK_CANCELLER must not be PEN or the vault"); require(canceller != vault.guardian() || testDeploy, "TIMELOCK_CANCELLER must be separate from the guardian"); - if (!testDeploy) _safeThreshold(canceller); + require( + testDeploy || _safeThreshold(canceller) >= MIN_CANCELLER_THRESHOLD, + "TIMELOCK_CANCELLER Safe threshold is below 2" + ); // Broadcast as msg.sender (forge sets it to --sender or the single // signer), the deployer that is temporary admin so the roles below diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index a50b649b7..6a747fa84 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -99,7 +99,8 @@ contract DeployGovernanceScriptTest is Test { DeployGovernance internal script; PEN internal pen; MigrationVault internal vault; - // The dedicated canceller Safe, separate from the vault's guardian Safe. + // The dedicated canceller Safe, separate from the vault's guardian Safe, + // at the lowest threshold the script accepts. address internal canceller = address(new SafeStub(2)); address internal guardian = address(new SafeStub(2)); @@ -189,6 +190,9 @@ contract DeployGovernanceScriptTest is Test { vm.toString(address(new SafeStub(0))), "TIMELOCK_CANCELLER Safe has threshold 0 (not set up)" ); + _expectRefused( + "TIMELOCK_CANCELLER", vm.toString(address(new SafeStub(1))), "TIMELOCK_CANCELLER Safe threshold is below 2" + ); vm.chainId(8453); _expectRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); From a3a17b75f8ecb6fbbc1343d90bb0a4709ce8634f Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:38:18 +0200 Subject: [PATCH 10/19] fix(contracts): Require a non-zero quorum fraction in the governance deploy With QUORUM_FRACTION=0 the governor's quorum stays at the permanent floor however much PEN circulates. The governance deploy now refuses 0 unless TEST_DEPLOY is set, which the drills use with drill-scale voting power. --- contracts/.env.example | 3 ++- contracts/script/DeployGovernance.s.sol | 9 ++++++--- contracts/test/DeployScripts.t.sol | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index f7ae8f7c6..984a034c9 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -67,7 +67,8 @@ VOTING_PERIOD=432000 # 5 days, seconds, 1 to 30 days PROPOSAL_THRESHOLD= # Percent of CIRCULATING supply (total minus the vault's unmigrated balance, # which setToken parks at the vote sink), counting For+Abstain -# votes actually cast. Bounded below by QUORUM_FLOOR. +# votes actually cast. Bounded below by QUORUM_FLOOR. 1 to 100; the script +# refuses 0, which would leave quorum at the floor for good. QUORUM_FRACTION=2 # PERMANENT (immutable in the governor). Absolute quorum floor, 18-decimal # token units, NOT whole PEN: the script refuses anything below 1 PEN (1e18) diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index 28550ef07..ca8801838 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -33,7 +33,7 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// 50000 is refused /// QUORUM_FRACTION percent of CIRCULATING supply (total minus the /// vault's unmigrated balance parked at the vote -/// sink, see PRD G1) +/// sink, see PRD G1), 1 to 100 /// QUORUM_FLOOR absolute quorum lower bound, 18-decimal token /// units, 1 PEN (1e18) to total supply; PERMANENT. /// Keeps early proposals from being trivially cheap @@ -54,8 +54,8 @@ import {MigrationVault} from "../src/MigrationVault.sol"; /// TEST_DEPLOY optional, testnet drills only: allows a shorter /// TIMELOCK_DELAY, VOTING_DELAY and VOTING_PERIOD, a /// TIMELOCK_CANCELLER that is not a Safe or is the -/// guardian, and QUORUM_FLOOR=0. Refused on Base -/// mainnet. +/// guardian, QUORUM_FRACTION=0 and QUORUM_FLOOR=0. +/// Refused on Base mainnet. contract DeployGovernance is Script { uint256 internal constant BASE_MAINNET = 8453; uint256 internal constant MIN_TIMELOCK_DELAY = 2 days; @@ -95,7 +95,10 @@ contract DeployGovernance is Script { proposalThreshold >= 1e18 && proposalThreshold <= supply, "PROPOSAL_THRESHOLD must be 18-decimal units, 1 PEN to total supply" ); + // Zero leaves quorum at the permanent floor however much PEN + // circulates. uint256 quorumFraction = vm.envUint("QUORUM_FRACTION"); + require(quorumFraction > 0 || testDeploy, "QUORUM_FRACTION must be above 0"); uint256 quorumFloor = vm.envUint("QUORUM_FLOOR"); require( (quorumFloor >= 1e18 || (testDeploy && quorumFloor == 0)) && quorumFloor <= supply, diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 6a747fa84..aee4e1dd9 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -154,6 +154,7 @@ contract DeployGovernanceScriptTest is Test { string memory threshold = "PROPOSAL_THRESHOLD must be 18-decimal units, 1 PEN to total supply"; _expectRefused("PROPOSAL_THRESHOLD", "1000", threshold); // whole PEN _expectRefused("PROPOSAL_THRESHOLD", vm.toString(SUPPLY + 1), threshold); + _expectRefused("QUORUM_FRACTION", "0", "QUORUM_FRACTION must be above 0"); string memory floor = "QUORUM_FLOOR must be 18-decimal units, 1 PEN to total supply"; _expectRefused("QUORUM_FLOOR", "100000", floor); // whole PEN _expectRefused("QUORUM_FLOOR", "0", floor); @@ -216,10 +217,11 @@ contract DeployGovernanceScriptTest is Test { assertEq(governor.votingDelay(), 1 days); assertEq(governor.votingPeriod(), 5 days); assertEq(governor.proposalThreshold(), 1_000e18); + assertEq(governor.quorumNumerator(), 2); assertEq(governor.quorumFloor(), 100_000e18); - // Testnet drills: short delays and voting, no floor and the guardian - // key, not a Safe, as canceller. + // Testnet drills: short delays and voting, no quorum fraction or floor + // and the guardian key, not a Safe, as canceller. address guardianKey = makeAddr("guardianKey"); vault.setGuardian(guardianKey); _setValidEnv(); @@ -227,6 +229,7 @@ contract DeployGovernanceScriptTest is Test { vm.setEnv("TIMELOCK_DELAY", "180"); vm.setEnv("VOTING_DELAY", "60"); vm.setEnv("VOTING_PERIOD", "240"); + vm.setEnv("QUORUM_FRACTION", "0"); vm.setEnv("QUORUM_FLOOR", "0"); vm.setEnv("TIMELOCK_CANCELLER", vm.toString(guardianKey)); (timelock, governor) = script.run(); From 9ad8679c0ff174459971478a214f11fe392ee261 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:40:56 +0200 Subject: [PATCH 11/19] test(contracts): Run both deploy scripts' cases in one test function The scripts read their parameters from the process-wide environment and forge runs test functions in parallel. Both scripts are about to read TEST_DEPLOY, so their cases now share one test function instead of two that could race on it. --- contracts/test/DeployScripts.t.sol | 198 +++++++++++++++-------------- 1 file changed, 101 insertions(+), 97 deletions(-) diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index aee4e1dd9..f521cbbb0 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -11,60 +11,10 @@ import {Deploy} from "../script/Deploy.s.sol"; import {DeployGovernance} from "../script/DeployGovernance.s.sol"; // These run the real scripts against their environment. The environment is -// process-wide and forge runs test functions in parallel, so each script's -// cases share one test function instead of racing on the same variables. -// The scripts broadcast as their caller, so this test contract is the deployer. - -contract DeployScriptTest is Test { - uint256 internal constant SWEEP_FLOOR = 1803859200; // 2027-03-01 00:00 UTC - - Deploy internal script; - address internal adminSafe = makeAddr("adminSafe"); - - function setUp() public { - script = new Deploy(); - vm.warp(1792000000); // October 2026 - } - - function _setValidEnv() internal { - vm.setEnv("ADMIN_SAFE", vm.toString(adminSafe)); - vm.setEnv("GUARDIAN_SAFE", vm.toString(makeAddr("guardianSafe"))); - vm.setEnv("ATTESTOR_1", vm.toString(makeAddr("attestor1"))); - vm.setEnv("ATTESTOR_2", vm.toString(makeAddr("attestor2"))); - vm.setEnv("ATTESTOR_3", vm.toString(makeAddr("attestor3"))); - vm.setEnv("ATTESTOR_4", vm.toString(makeAddr("attestor4"))); - vm.setEnv("MAX_ISSUANCE", "150000000000000000000000000"); - vm.setEnv("PER_RELEASE_CAP", "1000000000000000000000000"); - vm.setEnv("DAILY_CAP", "1000000000000000000000000"); - vm.setEnv("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR)); - } - - function _expectRefused(string memory key, string memory value, string memory reason) internal { - _setValidEnv(); - vm.setEnv(key, value); - vm.expectRevert(bytes(reason)); - script.run(); - } - - function test_RunRefusesBadPermanentValuesAndDeploys() public { - string memory issuance = "MAX_ISSUANCE must be 150M PEN in 18-decimal units"; - _expectRefused("MAX_ISSUANCE", "15000000000000000000000000", issuance); // one zero short - _expectRefused("MAX_ISSUANCE", "150000000", issuance); // whole PEN - - string memory sweep = "EARLIEST_SWEEP_TS must be a future unix time in seconds, at most two years ahead"; - _expectRefused("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR * 1000), sweep); // milliseconds - _expectRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp), sweep); - - _setValidEnv(); - (PEN pen, MigrationVault vault) = script.run(); - assertEq(address(vault.token()), address(pen)); - assertEq(pen.totalSupply(), 150_000_000e18); - assertEq(pen.balanceOf(address(vault)), 150_000_000e18); - assertEq(vault.earliestSweepTimestamp(), SWEEP_FLOOR); - assertEq(vault.admin(), address(this), "interim admin until the Safe accepts"); - assertEq(vault.pendingAdmin(), adminSafe); - } -} +// process-wide, forge runs test functions in parallel and both scripts read +// TEST_DEPLOY, so all cases share one test function instead of racing on the +// same variables. The scripts broadcast as their caller, so this test +// contract is the deployer. // Answers getThreshold() like a Safe. Immutable, so an account delegating to // it answers the same. @@ -93,10 +43,14 @@ contract RawAnswerStub { } } -contract DeployGovernanceScriptTest is Test { +contract DeployScriptsTest is Test { + uint256 internal constant SWEEP_FLOOR = 1803859200; // 2027-03-01 00:00 UTC uint256 internal constant SUPPLY = 150_000_000e18; - DeployGovernance internal script; + Deploy internal deployScript; + address internal adminSafe = makeAddr("adminSafe"); + + DeployGovernance internal governanceScript; PEN internal pen; MigrationVault internal vault; // The dedicated canceller Safe, separate from the vault's guardian Safe, @@ -105,7 +59,9 @@ contract DeployGovernanceScriptTest is Test { address internal guardian = address(new SafeStub(2)); function setUp() public { - script = new DeployGovernance(); + vm.warp(1792000000); // October 2026 + deployScript = new Deploy(); + governanceScript = new DeployGovernance(); address[] memory attestors = new address[](3); attestors[0] = makeAddr("a0"); attestors[1] = makeAddr("a1"); @@ -115,7 +71,51 @@ contract DeployGovernanceScriptTest is Test { vault.setToken(IERC20(address(pen))); } - function _setValidEnv() internal { + function test_RunScriptsRefuseBadValuesAndDeploy() public { + _runDeployCases(); + _runGovernanceCases(); + } + + function _setValidDeployEnv() internal { + vm.setEnv("ADMIN_SAFE", vm.toString(adminSafe)); + vm.setEnv("GUARDIAN_SAFE", vm.toString(makeAddr("guardianSafe"))); + vm.setEnv("ATTESTOR_1", vm.toString(makeAddr("attestor1"))); + vm.setEnv("ATTESTOR_2", vm.toString(makeAddr("attestor2"))); + vm.setEnv("ATTESTOR_3", vm.toString(makeAddr("attestor3"))); + vm.setEnv("ATTESTOR_4", vm.toString(makeAddr("attestor4"))); + vm.setEnv("MAX_ISSUANCE", "150000000000000000000000000"); + vm.setEnv("PER_RELEASE_CAP", "1000000000000000000000000"); + vm.setEnv("DAILY_CAP", "1000000000000000000000000"); + vm.setEnv("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR)); + } + + function _expectDeployRefused(string memory key, string memory value, string memory reason) internal { + _setValidDeployEnv(); + vm.setEnv(key, value); + vm.expectRevert(bytes(reason)); + deployScript.run(); + } + + function _runDeployCases() internal { + string memory issuance = "MAX_ISSUANCE must be 150M PEN in 18-decimal units"; + _expectDeployRefused("MAX_ISSUANCE", "15000000000000000000000000", issuance); // one zero short + _expectDeployRefused("MAX_ISSUANCE", "150000000", issuance); // whole PEN + + string memory sweep = "EARLIEST_SWEEP_TS must be a future unix time in seconds, at most two years ahead"; + _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR * 1000), sweep); // milliseconds + _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp), sweep); + + _setValidDeployEnv(); + (PEN newPen, MigrationVault newVault) = deployScript.run(); + assertEq(address(newVault.token()), address(newPen)); + assertEq(newPen.totalSupply(), 150_000_000e18); + assertEq(newPen.balanceOf(address(newVault)), 150_000_000e18); + assertEq(newVault.earliestSweepTimestamp(), SWEEP_FLOOR); + assertEq(newVault.admin(), address(this), "interim admin until the Safe accepts"); + assertEq(newVault.pendingAdmin(), adminSafe); + } + + function _setValidGovernanceEnv() internal { vm.setEnv("TEST_DEPLOY", "false"); vm.setEnv("PEN_TOKEN", vm.toString(address(pen))); vm.setEnv("VAULT_ADDRESS", vm.toString(address(vault))); @@ -128,79 +128,83 @@ contract DeployGovernanceScriptTest is Test { vm.setEnv("TIMELOCK_CANCELLER", vm.toString(canceller)); } - function _expectRefused(string memory key, string memory value, string memory reason) internal { - _setValidEnv(); + function _expectGovernanceRefused(string memory key, string memory value, string memory reason) internal { + _setValidGovernanceEnv(); vm.setEnv(key, value); vm.expectRevert(bytes(reason)); - script.run(); + governanceScript.run(); } - function test_RunValidatesEveryParameterAndWiresRoles() public { + function _runGovernanceCases() internal { string memory notToken = "PEN_TOKEN is not the migration vault's token"; - _expectRefused("PEN_TOKEN", vm.toString(address(vault)), notToken); - _expectRefused("PEN_TOKEN", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notToken); - - _expectRefused("TIMELOCK_DELAY", "0", "TIMELOCK_DELAY is below 48h"); - _expectRefused("TIMELOCK_DELAY", "172799", "TIMELOCK_DELAY is below 48h"); - _expectRefused("TIMELOCK_DELAY", "172800000", "TIMELOCK_DELAY is above 30 days (seconds expected)"); - _expectRefused("VOTING_DELAY", "1", "VOTING_DELAY is below 1h (seconds expected)"); // days - _expectRefused("VOTING_DELAY", "3599", "VOTING_DELAY is below 1h (seconds expected)"); - _expectRefused("VOTING_DELAY", "86400000", "VOTING_DELAY is above 30 days (seconds expected)"); - _expectRefused("VOTING_PERIOD", "5", "VOTING_PERIOD is below 1 day (seconds expected)"); // days - _expectRefused("VOTING_PERIOD", "86399", "VOTING_PERIOD is below 1 day (seconds expected)"); + _expectGovernanceRefused("PEN_TOKEN", vm.toString(address(vault)), notToken); + _expectGovernanceRefused("PEN_TOKEN", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notToken); + + _expectGovernanceRefused("TIMELOCK_DELAY", "0", "TIMELOCK_DELAY is below 48h"); + _expectGovernanceRefused("TIMELOCK_DELAY", "172799", "TIMELOCK_DELAY is below 48h"); + _expectGovernanceRefused("TIMELOCK_DELAY", "172800000", "TIMELOCK_DELAY is above 30 days (seconds expected)"); + _expectGovernanceRefused("VOTING_DELAY", "1", "VOTING_DELAY is below 1h (seconds expected)"); // days + _expectGovernanceRefused("VOTING_DELAY", "3599", "VOTING_DELAY is below 1h (seconds expected)"); + _expectGovernanceRefused("VOTING_DELAY", "86400000", "VOTING_DELAY is above 30 days (seconds expected)"); + _expectGovernanceRefused("VOTING_PERIOD", "5", "VOTING_PERIOD is below 1 day (seconds expected)"); // days + _expectGovernanceRefused("VOTING_PERIOD", "86399", "VOTING_PERIOD is below 1 day (seconds expected)"); // Would have been narrowed to 432000 by a bare uint32 cast. - _expectRefused("VOTING_PERIOD", "4295399296", "VOTING_PERIOD is above 30 days (seconds expected)"); + _expectGovernanceRefused("VOTING_PERIOD", "4295399296", "VOTING_PERIOD is above 30 days (seconds expected)"); string memory threshold = "PROPOSAL_THRESHOLD must be 18-decimal units, 1 PEN to total supply"; - _expectRefused("PROPOSAL_THRESHOLD", "1000", threshold); // whole PEN - _expectRefused("PROPOSAL_THRESHOLD", vm.toString(SUPPLY + 1), threshold); - _expectRefused("QUORUM_FRACTION", "0", "QUORUM_FRACTION must be above 0"); + _expectGovernanceRefused("PROPOSAL_THRESHOLD", "1000", threshold); // whole PEN + _expectGovernanceRefused("PROPOSAL_THRESHOLD", vm.toString(SUPPLY + 1), threshold); + _expectGovernanceRefused("QUORUM_FRACTION", "0", "QUORUM_FRACTION must be above 0"); string memory floor = "QUORUM_FLOOR must be 18-decimal units, 1 PEN to total supply"; - _expectRefused("QUORUM_FLOOR", "100000", floor); // whole PEN - _expectRefused("QUORUM_FLOOR", "0", floor); - _expectRefused("QUORUM_FLOOR", vm.toString(SUPPLY + 1), floor); + _expectGovernanceRefused("QUORUM_FLOOR", "100000", floor); // whole PEN + _expectGovernanceRefused("QUORUM_FLOOR", "0", floor); + _expectGovernanceRefused("QUORUM_FLOOR", vm.toString(SUPPLY + 1), floor); - _expectRefused( + _expectGovernanceRefused( "TIMELOCK_CANCELLER", "", "TIMELOCK_CANCELLER is required: no veto during the timelock delay otherwise" ); - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(this)), "TIMELOCK_CANCELLER must not be the deployer"); + _expectGovernanceRefused( + "TIMELOCK_CANCELLER", vm.toString(address(this)), "TIMELOCK_CANCELLER must not be the deployer" + ); string memory notPenOrVault = "TIMELOCK_CANCELLER must not be PEN or the vault"; - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(pen)), notPenOrVault); - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(vault)), notPenOrVault); - _expectRefused( + _expectGovernanceRefused("TIMELOCK_CANCELLER", vm.toString(address(pen)), notPenOrVault); + _expectGovernanceRefused("TIMELOCK_CANCELLER", vm.toString(address(vault)), notPenOrVault); + _expectGovernanceRefused( "TIMELOCK_CANCELLER", vm.toString(guardian), "TIMELOCK_CANCELLER must be separate from the guardian" ); - _expectRefused( + _expectGovernanceRefused( "TIMELOCK_CANCELLER", vm.toString(makeAddr("codeless")), "TIMELOCK_CANCELLER has no code on this chain (create the Safe first)" ); address delegated = makeAddr("delegatedEoa"); vm.etch(delegated, abi.encodePacked(hex"ef0100", canceller)); // EIP-7702 delegation to a Safe - _expectRefused( + _expectGovernanceRefused( "TIMELOCK_CANCELLER", vm.toString(delegated), "TIMELOCK_CANCELLER is an EIP-7702 delegated EOA, not a Safe" ); string memory notSafe = "TIMELOCK_CANCELLER is not a Safe (getThreshold() failed)"; - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notSafe); + _expectGovernanceRefused( + "TIMELOCK_CANCELLER", vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), notSafe + ); // Answers that are not exactly one word, whatever their first bytes say. - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new RawAnswerStub(hex"03"))), notSafe); + _expectGovernanceRefused("TIMELOCK_CANCELLER", vm.toString(address(new RawAnswerStub(hex"03"))), notSafe); bytes memory twoWords = abi.encode(uint256(3), uint256(3)); - _expectRefused("TIMELOCK_CANCELLER", vm.toString(address(new RawAnswerStub(twoWords))), notSafe); - _expectRefused( + _expectGovernanceRefused("TIMELOCK_CANCELLER", vm.toString(address(new RawAnswerStub(twoWords))), notSafe); + _expectGovernanceRefused( "TIMELOCK_CANCELLER", vm.toString(address(new SafeStub(0))), "TIMELOCK_CANCELLER Safe has threshold 0 (not set up)" ); - _expectRefused( + _expectGovernanceRefused( "TIMELOCK_CANCELLER", vm.toString(address(new SafeStub(1))), "TIMELOCK_CANCELLER Safe threshold is below 2" ); vm.chainId(8453); - _expectRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); + _expectGovernanceRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); vm.chainId(31337); - _setValidEnv(); - (TimelockController timelock, PENGovernor governor) = script.run(); + _setValidGovernanceEnv(); + (TimelockController timelock, PENGovernor governor) = governanceScript.run(); bytes32[4] memory roles = [ timelock.DEFAULT_ADMIN_ROLE(), timelock.PROPOSER_ROLE(), timelock.CANCELLER_ROLE(), timelock.EXECUTOR_ROLE() ]; @@ -224,7 +228,7 @@ contract DeployGovernanceScriptTest is Test { // and the guardian key, not a Safe, as canceller. address guardianKey = makeAddr("guardianKey"); vault.setGuardian(guardianKey); - _setValidEnv(); + _setValidGovernanceEnv(); vm.setEnv("TEST_DEPLOY", "true"); vm.setEnv("TIMELOCK_DELAY", "180"); vm.setEnv("VOTING_DELAY", "60"); @@ -232,7 +236,7 @@ contract DeployGovernanceScriptTest is Test { vm.setEnv("QUORUM_FRACTION", "0"); vm.setEnv("QUORUM_FLOOR", "0"); vm.setEnv("TIMELOCK_CANCELLER", vm.toString(guardianKey)); - (timelock, governor) = script.run(); + (timelock, governor) = governanceScript.run(); assertEq(timelock.getMinDelay(), 180); assertEq(governor.votingPeriod(), 240); assertTrue(timelock.hasRole(timelock.CANCELLER_ROLE(), guardianKey)); From 937e18a2bed896623d7240479488edd927bf3b0b Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:46:26 +0200 Subject: [PATCH 12/19] fix(contracts): Require the bootstrap vault admin to be a Safe The migration deploy handed vault admin to whatever ADMIN_SAFE named. It now refuses an address without code on the chain and an EIP-7702 delegated EOA. TEST_DEPLOY, refused on Base mainnet as in the governance deploy, admits a plain key for testnet drills and local runs; the Anvil and Base Sepolia harnesses set it. --- contracts/.env.example | 6 ++++++ contracts/script/Deploy.s.sol | 16 +++++++++++++++- contracts/test/DeployScripts.t.sol | 24 +++++++++++++++++++++++- testing/src/deploy.mjs | 2 ++ testing/src/rehearsal-deploy.mjs | 2 ++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index 984a034c9..740ebd638 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -8,6 +8,8 @@ # Bootstrap Safe that becomes vault admin (accepts via vault.acceptAdmin()). # Changeable later via transferAdmin; handed to the Timelock at phase 5. +# Create the Safe on this chain first: the script refuses an address without +# code and an EIP-7702 delegated EOA. ADMIN_SAFE= # Fast pause guardian (small-threshold Safe). Can pause but NOT unpause, so a # compromised guardian can only halt, never release. Its signers must hold no @@ -55,6 +57,10 @@ DAILY_CAP=1000000000000000000000000 # 1803859200 = 2027-03-01 00:00 UTC EARLIEST_SWEEP_TS=1803859200 +# Testnet drills and local runs only: TEST_DEPLOY=true admits an ADMIN_SAFE +# without code. Refused on Base mainnet; leave it unset for production. +# TEST_DEPLOY= + # --- DeployGovernance.s.sol (phase 5) --- # The script checks every value below before broadcasting. PEN_TOKEN= # must be the token of VAULT_ADDRESS diff --git a/contracts/script/Deploy.s.sol b/contracts/script/Deploy.s.sol index f22c125d7..0fa6cb26f 100644 --- a/contracts/script/Deploy.s.sol +++ b/contracts/script/Deploy.s.sol @@ -14,7 +14,9 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// must call acceptAdmin() to complete) /// /// Environment: -/// ADMIN_SAFE bootstrap Safe that becomes vault admin +/// ADMIN_SAFE bootstrap Safe that becomes vault admin; must have +/// code on this chain (not an EOA or an EIP-7702 +/// delegated EOA) /// GUARDIAN_SAFE fast pause guardian /// ATTESTOR_1..4 attestor transaction-sender addresses (D4: 3-of-4) /// MAX_ISSUANCE max issuance in 18-decimal units (decision D3); @@ -23,7 +25,10 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// DAILY_CAP initial daily cap, 18-decimal units /// EARLIEST_SWEEP_TS unix timestamp in SECONDS before which no remainder /// sweep (D5); must lie in the next MAX_SWEEP_FLOOR_AHEAD +/// TEST_DEPLOY optional, testnet drills and local runs only: allows +/// an ADMIN_SAFE without code. Refused on Base mainnet. contract Deploy is Script { + uint256 internal constant BASE_MAINNET = 8453; // 12-decimal pallet amounts -> 18-decimal token amounts (decision D2). uint256 internal constant CONVERSION_FACTOR = 1e6; uint256 internal constant THRESHOLD = 3; @@ -36,7 +41,16 @@ contract Deploy is Script { uint256 internal constant MAX_SWEEP_FLOOR_AHEAD = 2 * 365 days; function run() external returns (PEN pen, MigrationVault vault) { + bool testDeploy = vm.envOr("TEST_DEPLOY", false); + require(!testDeploy || block.chainid != BASE_MAINNET, "TEST_DEPLOY is refused on Base mainnet"); + + // The bootstrap admin holds every vault setting until governance takes + // over, so it must be a Safe, not a single key. address adminSafe = vm.envAddress("ADMIN_SAFE"); + if (!testDeploy) { + require(adminSafe.code.length > 0, "ADMIN_SAFE has no code on this chain (create the Safe first)"); + require(bytes3(adminSafe.code) != 0xef0100, "ADMIN_SAFE is an EIP-7702 delegated EOA, not a Safe"); + } address guardianSafe = vm.envAddress("GUARDIAN_SAFE"); uint256 maxIssuance = vm.envUint("MAX_ISSUANCE"); uint256 perReleaseCap = vm.envUint("PER_RELEASE_CAP"); diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index f521cbbb0..2d968f511 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -48,7 +48,7 @@ contract DeployScriptsTest is Test { uint256 internal constant SUPPLY = 150_000_000e18; Deploy internal deployScript; - address internal adminSafe = makeAddr("adminSafe"); + address internal adminSafe = address(new SafeStub(2)); DeployGovernance internal governanceScript; PEN internal pen; @@ -77,6 +77,7 @@ contract DeployScriptsTest is Test { } function _setValidDeployEnv() internal { + vm.setEnv("TEST_DEPLOY", "false"); vm.setEnv("ADMIN_SAFE", vm.toString(adminSafe)); vm.setEnv("GUARDIAN_SAFE", vm.toString(makeAddr("guardianSafe"))); vm.setEnv("ATTESTOR_1", vm.toString(makeAddr("attestor1"))); @@ -105,6 +106,20 @@ contract DeployScriptsTest is Test { _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR * 1000), sweep); // milliseconds _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp), sweep); + address adminEoa = makeAddr("adminEoa"); + _expectDeployRefused( + "ADMIN_SAFE", vm.toString(adminEoa), "ADMIN_SAFE has no code on this chain (create the Safe first)" + ); + address delegated = makeAddr("delegatedAdmin"); + vm.etch(delegated, abi.encodePacked(hex"ef0100", adminSafe)); // EIP-7702 delegation to a Safe + _expectDeployRefused( + "ADMIN_SAFE", vm.toString(delegated), "ADMIN_SAFE is an EIP-7702 delegated EOA, not a Safe" + ); + + vm.chainId(8453); + _expectDeployRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); + vm.chainId(31337); + _setValidDeployEnv(); (PEN newPen, MigrationVault newVault) = deployScript.run(); assertEq(address(newVault.token()), address(newPen)); @@ -113,6 +128,13 @@ contract DeployScriptsTest is Test { assertEq(newVault.earliestSweepTimestamp(), SWEEP_FLOOR); assertEq(newVault.admin(), address(this), "interim admin until the Safe accepts"); assertEq(newVault.pendingAdmin(), adminSafe); + + // Testnet drills and local runs: an EOA as admin. + _setValidDeployEnv(); + vm.setEnv("TEST_DEPLOY", "true"); + vm.setEnv("ADMIN_SAFE", vm.toString(adminEoa)); + (, newVault) = deployScript.run(); + assertEq(newVault.pendingAdmin(), adminEoa); } function _setValidGovernanceEnv() internal { diff --git a/testing/src/deploy.mjs b/testing/src/deploy.mjs index 848f00b02..b29a489bf 100644 --- a/testing/src/deploy.mjs +++ b/testing/src/deploy.mjs @@ -25,6 +25,8 @@ export function deployStack({ earliestSweepOffsetSeconds = 365 * 24 * 3600 } = { const [deployer, attA, attB, attC, attD, guardian, admin] = accounts; const env = { ...process.env, + // The roles are plain Anvil accounts, not Safes. + TEST_DEPLOY: "true", PRIVATE_KEY: keys[0], ADMIN_SAFE: admin.address, GUARDIAN_SAFE: guardian.address, diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index 5f10c238e..80a149374 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -37,6 +37,8 @@ export function deployToSepolia({ env, roles, log }) { const sweepTs = Math.floor(Date.now() / 1000) + params.sweepOffsetSeconds; const scriptEnv = { ...process.env, + // The admin is a throwaway key, not a Safe. + TEST_DEPLOY: "true", PRIVATE_KEY: env.DEPLOYER_PRIVATE_KEY, ADMIN_SAFE: roles.admin.address, GUARDIAN_SAFE: roles.guardian.address, From e16482894e7ff18920af43a2c80cedf408f541f9 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:47:24 +0200 Subject: [PATCH 13/19] fix(contracts): Require a sweep floor at least 30 days after the deploy The migration deploy accepted any future EARLIEST_SWEEP_TS, so a value an hour or two ahead from a drill environment would have been fixed in the vault for good. It now requires the floor to lie at least 30 days after the deploy block unless TEST_DEPLOY is set; 2027-03-01 still passes for a deploy up to 2027-01-30. --- contracts/.env.example | 10 ++++++---- contracts/script/Deploy.s.sol | 12 ++++++++++-- contracts/test/DeployScripts.t.sol | 18 ++++++++++++++++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index 740ebd638..cc934cbb6 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -41,9 +41,10 @@ PER_RELEASE_CAP=1000000000000000000000000 DAILY_CAP=1000000000000000000000000 # PERMANENT. Unix timestamp in SECONDS before which the remainder CANNOT be -# swept; the script refuses a value in the past or more than two years ahead -# (a milliseconds value lands far beyond that). This is a floor on the -# earliest a sweep could happen, not a deadline for holders: closing the +# swept; the script refuses a value less than 30 days after the deploy block +# (2027-03-01 below passes for any deploy up to 2027-01-30) or more than two +# years ahead (a milliseconds value lands far beyond that). This is a floor on +# the earliest a sweep could happen, not a deadline for holders: closing the # window is a separate governance decision and can be deferred indefinitely. # # Deliberately set beyond the ~3-month window we intend to advertise: a later @@ -58,7 +59,8 @@ DAILY_CAP=1000000000000000000000000 EARLIEST_SWEEP_TS=1803859200 # Testnet drills and local runs only: TEST_DEPLOY=true admits an ADMIN_SAFE -# without code. Refused on Base mainnet; leave it unset for production. +# without code and an EARLIEST_SWEEP_TS less than 30 days ahead. Refused on +# Base mainnet; leave it unset for production. # TEST_DEPLOY= # --- DeployGovernance.s.sol (phase 5) --- diff --git a/contracts/script/Deploy.s.sol b/contracts/script/Deploy.s.sol index 0fa6cb26f..147acecfd 100644 --- a/contracts/script/Deploy.s.sol +++ b/contracts/script/Deploy.s.sol @@ -24,9 +24,11 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// PER_RELEASE_CAP initial per-release cap, 18-decimal units /// DAILY_CAP initial daily cap, 18-decimal units /// EARLIEST_SWEEP_TS unix timestamp in SECONDS before which no remainder -/// sweep (D5); must lie in the next MAX_SWEEP_FLOOR_AHEAD +/// sweep (D5); at least MIN_SWEEP_FLOOR_AHEAD after +/// the deploy block and at most MAX_SWEEP_FLOOR_AHEAD /// TEST_DEPLOY optional, testnet drills and local runs only: allows -/// an ADMIN_SAFE without code. Refused on Base mainnet. +/// an ADMIN_SAFE without code and an EARLIEST_SWEEP_TS +/// any time in the future. Refused on Base mainnet. contract Deploy is Script { uint256 internal constant BASE_MAINNET = 8453; // 12-decimal pallet amounts -> 18-decimal token amounts (decision D2). @@ -39,6 +41,8 @@ contract Deploy is Script { // A sweep floor further out than this is a units slip (milliseconds land // ~1000x further) or a typo, not a decision. uint256 internal constant MAX_SWEEP_FLOOR_AHEAD = 2 * 365 days; + // A sweep floor closer than this is a drill value, not a migration window. + uint256 internal constant MIN_SWEEP_FLOOR_AHEAD = 30 days; function run() external returns (PEN pen, MigrationVault vault) { bool testDeploy = vm.envOr("TEST_DEPLOY", false); @@ -61,6 +65,10 @@ contract Deploy is Script { earliestSweepTs > block.timestamp && earliestSweepTs <= block.timestamp + MAX_SWEEP_FLOOR_AHEAD, "EARLIEST_SWEEP_TS must be a future unix time in seconds, at most two years ahead" ); + require( + earliestSweepTs >= block.timestamp + MIN_SWEEP_FLOOR_AHEAD || testDeploy, + "EARLIEST_SWEEP_TS is less than 30 days after the deploy block" + ); address[] memory attestors = new address[](4); attestors[0] = vm.envAddress("ATTESTOR_1"); diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 2d968f511..0312fba65 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -45,6 +45,7 @@ contract RawAnswerStub { contract DeployScriptsTest is Test { uint256 internal constant SWEEP_FLOOR = 1803859200; // 2027-03-01 00:00 UTC + uint256 internal constant DEPLOY_TIME = 1792000000; // October 2026 uint256 internal constant SUPPLY = 150_000_000e18; Deploy internal deployScript; @@ -59,7 +60,7 @@ contract DeployScriptsTest is Test { address internal guardian = address(new SafeStub(2)); function setUp() public { - vm.warp(1792000000); // October 2026 + vm.warp(DEPLOY_TIME); deployScript = new Deploy(); governanceScript = new DeployGovernance(); address[] memory attestors = new address[](3); @@ -105,6 +106,16 @@ contract DeployScriptsTest is Test { string memory sweep = "EARLIEST_SWEEP_TS must be a future unix time in seconds, at most two years ahead"; _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR * 1000), sweep); // milliseconds _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp), sweep); + string memory sweepSoon = "EARLIEST_SWEEP_TS is less than 30 days after the deploy block"; + _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp + 2 hours), sweepSoon); + _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(block.timestamp + 30 days - 1), sweepSoon); + // The decided floor still passes for a deploy up to 30 days before it. + vm.warp(SWEEP_FLOOR - 30 days + 1); + _expectDeployRefused("EARLIEST_SWEEP_TS", vm.toString(SWEEP_FLOOR), sweepSoon); + vm.warp(SWEEP_FLOOR - 30 days); // 2027-01-30 00:00 UTC + _setValidDeployEnv(); + deployScript.run(); + vm.warp(DEPLOY_TIME); address adminEoa = makeAddr("adminEoa"); _expectDeployRefused( @@ -129,12 +140,15 @@ contract DeployScriptsTest is Test { assertEq(newVault.admin(), address(this), "interim admin until the Safe accepts"); assertEq(newVault.pendingAdmin(), adminSafe); - // Testnet drills and local runs: an EOA as admin. + // Testnet drills and local runs: an EOA as admin and a sweep floor an + // hour ahead. _setValidDeployEnv(); vm.setEnv("TEST_DEPLOY", "true"); vm.setEnv("ADMIN_SAFE", vm.toString(adminEoa)); + vm.setEnv("EARLIEST_SWEEP_TS", vm.toString(block.timestamp + 1 hours)); (, newVault) = deployScript.run(); assertEq(newVault.pendingAdmin(), adminEoa); + assertEq(newVault.earliestSweepTimestamp(), block.timestamp + 1 hours); } function _setValidGovernanceEnv() internal { From 301c1b65d3f9b9a2bb6031178d7d789f4273949e Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 11:51:08 +0200 Subject: [PATCH 14/19] fix(testing): Write the governance drill's quorum floor as a string The drill wrote its parameters to the run manifest with JSON.stringify, which throws on the BigInt quorum floor, so a run aborted after its last check and exited non-zero. --- testing/src/phase5b-governance.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/src/phase5b-governance.mjs b/testing/src/phase5b-governance.mjs index 5addb5a6c..22b8e227c 100644 --- a/testing/src/phase5b-governance.mjs +++ b/testing/src/phase5b-governance.mjs @@ -305,7 +305,7 @@ async function main() { commit: execSync("git rev-parse HEAD", { cwd: ROOT, encoding: "utf8" }).trim(), contracts: { vault, pen, timelock, governor }, voter: voter.address, - params: { ...params, proposalThreshold: params.proposalThreshold.toString() }, + params: { ...params, proposalThreshold: params.proposalThreshold.toString(), quorumFloor: params.quorumFloor.toString() }, proposals: { handover: handoverProposal?.toString(), capsRaise: capsProposal?.toString(), From 4bc5421415da61d8d858069bc83bd94ec9ef9a34 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 15:18:04 +0200 Subject: [PATCH 15/19] test(contracts): Run the production deploy paths on Base mainnet's chain id --- contracts/test/DeployScripts.t.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 0312fba65..6a60a5cd1 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -127,12 +127,12 @@ contract DeployScriptsTest is Test { "ADMIN_SAFE", vm.toString(delegated), "ADMIN_SAFE is an EIP-7702 delegated EOA, not a Safe" ); + // A production deploy on Base mainnet. vm.chainId(8453); _expectDeployRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); - vm.chainId(31337); - _setValidDeployEnv(); (PEN newPen, MigrationVault newVault) = deployScript.run(); + vm.chainId(31337); assertEq(address(newVault.token()), address(newPen)); assertEq(newPen.totalSupply(), 150_000_000e18); assertEq(newPen.balanceOf(address(newVault)), 150_000_000e18); @@ -235,12 +235,12 @@ contract DeployScriptsTest is Test { "TIMELOCK_CANCELLER", vm.toString(address(new SafeStub(1))), "TIMELOCK_CANCELLER Safe threshold is below 2" ); + // A production deploy on Base mainnet. vm.chainId(8453); _expectGovernanceRefused("TEST_DEPLOY", "true", "TEST_DEPLOY is refused on Base mainnet"); - vm.chainId(31337); - _setValidGovernanceEnv(); (TimelockController timelock, PENGovernor governor) = governanceScript.run(); + vm.chainId(31337); bytes32[4] memory roles = [ timelock.DEFAULT_ADMIN_ROLE(), timelock.PROPOSER_ROLE(), timelock.CANCELLER_ROLE(), timelock.EXECUTOR_ROLE() ]; From d0e3258d5f844014cfdcb4e8836edec0cfe602d7 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 15:19:00 +0200 Subject: [PATCH 16/19] refactor(contracts): Move the multisig Safe check into a shared helper --- contracts/script/DeployGovernance.s.sol | 21 +++------------------ contracts/script/SafeCheck.sol | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 contracts/script/SafeCheck.sol diff --git a/contracts/script/DeployGovernance.s.sol b/contracts/script/DeployGovernance.s.sol index ca8801838..8e93792d1 100644 --- a/contracts/script/DeployGovernance.s.sol +++ b/contracts/script/DeployGovernance.s.sol @@ -7,6 +7,7 @@ import {PENGovernor} from "../src/PENGovernor.sol"; import {IVotes} from "@openzeppelin/contracts/governance/utils/IVotes.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {MigrationVault} from "../src/MigrationVault.sol"; +import {requireMultisigSafe} from "./SafeCheck.sol"; /// @notice Deploys the on-chain governance stack (PRD rollout phase 5): /// TimelockController + PENGovernor, wired so that only the Governor @@ -66,8 +67,6 @@ contract DeployGovernance is Script { // Longer periods are a units slip (milliseconds land ~1000x further), and // a governor whose votes never end could not vote to shorten them. uint256 internal constant MAX_PERIOD = 30 days; - // A 1-of-n canceller is a single key holding a veto over every proposal. - uint256 internal constant MIN_CANCELLER_THRESHOLD = 2; function run() external returns (TimelockController timelock, PENGovernor governor) { bool testDeploy = vm.envOr("TEST_DEPLOY", false); @@ -110,10 +109,8 @@ contract DeployGovernance is Script { require(canceller != msg.sender, "TIMELOCK_CANCELLER must not be the deployer"); require(canceller != token && canceller != address(vault), "TIMELOCK_CANCELLER must not be PEN or the vault"); require(canceller != vault.guardian() || testDeploy, "TIMELOCK_CANCELLER must be separate from the guardian"); - require( - testDeploy || _safeThreshold(canceller) >= MIN_CANCELLER_THRESHOLD, - "TIMELOCK_CANCELLER Safe threshold is below 2" - ); + // A 1-of-n canceller is a single key holding a veto over every proposal. + if (!testDeploy) requireMultisigSafe(canceller, "TIMELOCK_CANCELLER"); // Broadcast as msg.sender (forge sets it to --sender or the single // signer), the deployer that is temporary admin so the roles below @@ -149,16 +146,4 @@ contract DeployGovernance is Script { console.log("TimelockController: ", address(timelock)); console.log("PENGovernor: ", address(governor)); } - - // A Safe answers getThreshold() with its non-zero threshold. An EIP-7702 - // delegated EOA stays a single key whatever it delegates to. - function _safeThreshold(address account) internal view returns (uint256) { - require(account.code.length > 0, "TIMELOCK_CANCELLER has no code on this chain (create the Safe first)"); - require(bytes3(account.code) != 0xef0100, "TIMELOCK_CANCELLER is an EIP-7702 delegated EOA, not a Safe"); - (bool ok, bytes memory threshold) = account.staticcall(abi.encodeWithSignature("getThreshold()")); - require(ok && threshold.length == 32, "TIMELOCK_CANCELLER is not a Safe (getThreshold() failed)"); - uint256 value = abi.decode(threshold, (uint256)); - require(value > 0, "TIMELOCK_CANCELLER Safe has threshold 0 (not set up)"); - return value; - } } diff --git a/contracts/script/SafeCheck.sol b/contracts/script/SafeCheck.sol new file mode 100644 index 000000000..f10ce8cff --- /dev/null +++ b/contracts/script/SafeCheck.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity 0.8.26; + +// A 1-of-n Safe is a single key. +uint256 constant MIN_SAFE_THRESHOLD = 2; + +/// @notice Refuses `account`, named `name` in the messages, unless it is a +/// Safe on this chain with a threshold of at least MIN_SAFE_THRESHOLD. +/// A Safe answers getThreshold() with its non-zero threshold. An +/// EIP-7702 delegated EOA stays a single key whatever it delegates to. +function requireMultisigSafe(address account, string memory name) view { + require(account.code.length > 0, string.concat(name, " has no code on this chain (create the Safe first)")); + require(bytes3(account.code) != 0xef0100, string.concat(name, " is an EIP-7702 delegated EOA, not a Safe")); + (bool ok, bytes memory threshold) = account.staticcall(abi.encodeWithSignature("getThreshold()")); + require(ok && threshold.length == 32, string.concat(name, " is not a Safe (getThreshold() failed)")); + uint256 value = abi.decode(threshold, (uint256)); + require(value > 0, string.concat(name, " Safe has threshold 0 (not set up)")); + require(value >= MIN_SAFE_THRESHOLD, string.concat(name, " Safe threshold is below 2")); +} From 1ed5b6dc6309e97bf7ff7e2631b90c40d1e638c9 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 15:19:47 +0200 Subject: [PATCH 17/19] fix(contracts): Require a bootstrap admin Safe with at least two signers --- contracts/.env.example | 9 +++++---- contracts/script/Deploy.s.sol | 17 ++++++++--------- contracts/test/DeployScripts.t.sol | 9 +++++++++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index cc934cbb6..4092b617a 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -8,8 +8,9 @@ # Bootstrap Safe that becomes vault admin (accepts via vault.acceptAdmin()). # Changeable later via transferAdmin; handed to the Timelock at phase 5. -# Create the Safe on this chain first: the script refuses an address without -# code and an EIP-7702 delegated EOA. +# Create the Safe on this chain first: the script refuses an address that is +# not a Safe (no code, an EIP-7702 delegated EOA) and a Safe whose threshold +# (getThreshold()) is below 2. ADMIN_SAFE= # Fast pause guardian (small-threshold Safe). Can pause but NOT unpause, so a # compromised guardian can only halt, never release. Its signers must hold no @@ -59,8 +60,8 @@ DAILY_CAP=1000000000000000000000000 EARLIEST_SWEEP_TS=1803859200 # Testnet drills and local runs only: TEST_DEPLOY=true admits an ADMIN_SAFE -# without code and an EARLIEST_SWEEP_TS less than 30 days ahead. Refused on -# Base mainnet; leave it unset for production. +# that is not a Safe and an EARLIEST_SWEEP_TS less than 30 days ahead. Refused +# on Base mainnet; leave it unset for production. # TEST_DEPLOY= # --- DeployGovernance.s.sol (phase 5) --- diff --git a/contracts/script/Deploy.s.sol b/contracts/script/Deploy.s.sol index 147acecfd..f48db21be 100644 --- a/contracts/script/Deploy.s.sol +++ b/contracts/script/Deploy.s.sol @@ -5,6 +5,7 @@ import {Script, console} from "forge-std/Script.sol"; import {PEN} from "../src/PEN.sol"; import {MigrationVault} from "../src/MigrationVault.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import {requireMultisigSafe} from "./SafeCheck.sol"; /// @notice Deploys the migration stack on Base (PRD rollout phase 3): /// 1. MigrationVault with the deployer as interim admin @@ -14,9 +15,9 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// must call acceptAdmin() to complete) /// /// Environment: -/// ADMIN_SAFE bootstrap Safe that becomes vault admin; must have -/// code on this chain (not an EOA or an EIP-7702 -/// delegated EOA) +/// ADMIN_SAFE bootstrap Safe that becomes vault admin; must be a +/// Safe on this chain with a threshold of at least 2 +/// (getThreshold(); no EIP-7702 delegated EOA) /// GUARDIAN_SAFE fast pause guardian /// ATTESTOR_1..4 attestor transaction-sender addresses (D4: 3-of-4) /// MAX_ISSUANCE max issuance in 18-decimal units (decision D3); @@ -27,8 +28,9 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; /// sweep (D5); at least MIN_SWEEP_FLOOR_AHEAD after /// the deploy block and at most MAX_SWEEP_FLOOR_AHEAD /// TEST_DEPLOY optional, testnet drills and local runs only: allows -/// an ADMIN_SAFE without code and an EARLIEST_SWEEP_TS -/// any time in the future. Refused on Base mainnet. +/// an ADMIN_SAFE that is not a Safe and an +/// EARLIEST_SWEEP_TS any time in the future. Refused +/// on Base mainnet. contract Deploy is Script { uint256 internal constant BASE_MAINNET = 8453; // 12-decimal pallet amounts -> 18-decimal token amounts (decision D2). @@ -51,10 +53,7 @@ contract Deploy is Script { // The bootstrap admin holds every vault setting until governance takes // over, so it must be a Safe, not a single key. address adminSafe = vm.envAddress("ADMIN_SAFE"); - if (!testDeploy) { - require(adminSafe.code.length > 0, "ADMIN_SAFE has no code on this chain (create the Safe first)"); - require(bytes3(adminSafe.code) != 0xef0100, "ADMIN_SAFE is an EIP-7702 delegated EOA, not a Safe"); - } + if (!testDeploy) requireMultisigSafe(adminSafe, "ADMIN_SAFE"); address guardianSafe = vm.envAddress("GUARDIAN_SAFE"); uint256 maxIssuance = vm.envUint("MAX_ISSUANCE"); uint256 perReleaseCap = vm.envUint("PER_RELEASE_CAP"); diff --git a/contracts/test/DeployScripts.t.sol b/contracts/test/DeployScripts.t.sol index 6a60a5cd1..470cba46b 100644 --- a/contracts/test/DeployScripts.t.sol +++ b/contracts/test/DeployScripts.t.sol @@ -126,6 +126,15 @@ contract DeployScriptsTest is Test { _expectDeployRefused( "ADMIN_SAFE", vm.toString(delegated), "ADMIN_SAFE is an EIP-7702 delegated EOA, not a Safe" ); + _expectDeployRefused( + "ADMIN_SAFE", + vm.toString(address(new PEN(makeAddr("other"), SUPPLY))), + "ADMIN_SAFE is not a Safe (getThreshold() failed)" + ); + // A 1-of-n Safe, the Safe singleton's own threshold included. + _expectDeployRefused( + "ADMIN_SAFE", vm.toString(address(new SafeStub(1))), "ADMIN_SAFE Safe threshold is below 2" + ); // A production deploy on Base mainnet. vm.chainId(8453); From 2b52384e6e1a0d4fc1365094a0b02908cd9ad603 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 15:20:07 +0200 Subject: [PATCH 18/19] docs: Note every value TEST_DEPLOY admits and set it in the manual Anvil deploy --- contracts/.env.example | 4 ++-- docs/pen-migration-local-test-plan.md | 3 ++- testing/src/rehearsal-deploy.mjs | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/contracts/.env.example b/contracts/.env.example index 4092b617a..5d9038354 100644 --- a/contracts/.env.example +++ b/contracts/.env.example @@ -95,6 +95,6 @@ QUORUM_FLOOR= TIMELOCK_CANCELLER= # Testnet drills only: TEST_DEPLOY=true admits a shorter TIMELOCK_DELAY, # VOTING_DELAY and VOTING_PERIOD, a TIMELOCK_CANCELLER that is not a Safe or -# is the guardian, and QUORUM_FLOOR=0. Refused on Base mainnet; leave it -# unset for production. +# is the guardian, QUORUM_FRACTION=0 and QUORUM_FLOOR=0. Refused on Base +# mainnet; leave it unset for production. # TEST_DEPLOY= diff --git a/docs/pen-migration-local-test-plan.md b/docs/pen-migration-local-test-plan.md index 40e052243..9ee528188 100644 --- a/docs/pen-migration-local-test-plan.md +++ b/docs/pen-migration-local-test-plan.md @@ -57,7 +57,8 @@ script works with realistic parameters. anvil --port 8545 --slots-in-an-epoch 0 # terminal 1 cd contracts cp .env.example .env # fill in: 4 attestor addrs, Safes, caps, MAX_ISSUANCE, EARLIEST_SWEEP_TS -forge script script/Deploy.s.sol --rpc-url http://localhost:8545 --broadcast +# TEST_DEPLOY admits plain Anvil accounts as the Safes, as phase1-base.mjs does +TEST_DEPLOY=true forge script script/Deploy.s.sol --rpc-url http://localhost:8545 --broadcast ``` Automated by `testing/src/phase1-base.mjs` (`node testing/src/phase1-base.mjs`), diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index 80a149374..ef55d9ecf 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -93,9 +93,9 @@ export function governanceDrillParams(env) { } /** Runs the real DeployGovernance.s.sol — its first execution anywhere. - * TEST_DEPLOY admits the drill-scale delay and voting, the zero floor and - * the guardian EOA as canceller, which the script refuses for a production - * deploy. */ + * TEST_DEPLOY admits the drill-scale delay and voting, the zero quorum + * fraction and floor and the guardian EOA as canceller, which the script + * refuses for a production deploy. */ export function deployGovernanceToSepolia({ env, pen, vault, canceller, log }) { const params = governanceDrillParams(env); const scriptEnv = { From 3adaa11a13e7022ca3a0927df5daaa3b9c3e7532 Mon Sep 17 00:00:00 2001 From: Marcel Ebert Date: Thu, 22 Oct 2026 15:21:16 +0200 Subject: [PATCH 19/19] docs: Tidy comments --- contracts/src/MigrationVault.sol | 2 +- contracts/src/PENGovernor.sol | 2 +- testing/.env.rehearsal.example | 6 +++--- testing/src/phase3-e2e.mjs | 2 +- testing/src/rehearsal-deploy.mjs | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/src/MigrationVault.sol b/contracts/src/MigrationVault.sol index 9d055ff00..afc5deb3d 100644 --- a/contracts/src/MigrationVault.sol +++ b/contracts/src/MigrationVault.sol @@ -434,7 +434,7 @@ contract MigrationVault { // A decrease can retroactively qualify a sub-threshold payload without // routing through approve() (which maintains pendingApprovedAmount); // gate sweeps for a settling period so it can be detected and released - // first (round-4 finding). + // first. if (threshold_ < threshold) thresholdReducedAt = block.timestamp; threshold = threshold_; emit ThresholdUpdated(threshold_); diff --git a/contracts/src/PENGovernor.sol b/contracts/src/PENGovernor.sol index c0c5b4fe7..697eb92ec 100644 --- a/contracts/src/PENGovernor.sol +++ b/contracts/src/PENGovernor.sol @@ -18,7 +18,7 @@ import {IVotes} from "@openzeppelin/contracts/governance/utils/IVotes.sol"; /// bootstrap phase. PEN uses the EIP-6372 timestamp clock, so all /// Governor periods below are in seconds. /// -/// Quorum (PRD G1, revised in review round 8): quorum is a fraction of +/// Quorum (PRD G1): quorum is a fraction of /// the CIRCULATING supply — total supply minus the unmigrated balance /// parked at `QUORUM_SINK` by the MigrationVault — bounded below by an /// absolute `quorumFloor`. A full-supply denominator could deadlock diff --git a/testing/.env.rehearsal.example b/testing/.env.rehearsal.example index 5cb78e569..582678760 100644 --- a/testing/.env.rehearsal.example +++ b/testing/.env.rehearsal.example @@ -34,8 +34,8 @@ RELEASER_PRIVATE_KEY= # release drain by itself inside one session. Production values are validated # separately on Anvil in phase 1, where time can be warped. REHEARSAL_DAILY_CAP_PEN=28800 -# Must not exceed the daily cap: the vault enforces perReleaseCap <= dailyCap -# (round 9), since anything in between could never fit the daily allowance. +# Must not exceed the daily cap: the vault enforces perReleaseCap <= dailyCap, +# since anything in between could never fit the daily allowance. REHEARSAL_PER_RELEASE_CAP_PEN=28800 # How far ahead the immutable sweep floor sits. Short so the sweep path is @@ -52,6 +52,6 @@ GOV_VOTING_DELAY=60 GOV_VOTING_PERIOD=240 GOV_PROPOSAL_THRESHOLD_PEN=1000 GOV_QUORUM_FRACTION=0 -# Absolute quorum floor in whole PEN (round 8: quorum is a fraction of +# Absolute quorum floor in whole PEN (quorum is a fraction of # CIRCULATING supply bounded below by this floor; 0 keeps the drill trivial). GOV_QUORUM_FLOOR_PEN=0 diff --git a/testing/src/phase3-e2e.mjs b/testing/src/phase3-e2e.mjs index ed25769c0..fc49a9cd8 100644 --- a/testing/src/phase3-e2e.mjs +++ b/testing/src/phase3-e2e.mjs @@ -156,7 +156,7 @@ section("Cap deferral and the releaser"); await check("a cap-deferred release is drained by the releaser, unattended", async () => { // Squeeze the daily budget so the next migration cannot release immediately. // Both caps equal one minimum-sized migration: the vault rejects - // perReleaseCap > dailyCap (round 9), and a MIN migration still fits the + // perReleaseCap > dailyCap, and a MIN migration still fits the // per-release cap exactly while the second one must wait for the refill. const tiny = MIN * CF; // one minimum-sized migration's worth await send(admin, { ...V, functionName: "setCaps", args: [tiny, tiny] }); diff --git a/testing/src/rehearsal-deploy.mjs b/testing/src/rehearsal-deploy.mjs index ef55d9ecf..5e9d6a227 100644 --- a/testing/src/rehearsal-deploy.mjs +++ b/testing/src/rehearsal-deploy.mjs @@ -19,8 +19,8 @@ const CONTRACTS = path.join(ROOT, "contracts"); export function rehearsalParams(env) { const dailyCapPen = BigInt(env.REHEARSAL_DAILY_CAP_PEN ?? "28800"); - // Equal to the daily cap: the vault rejects perReleaseCap > dailyCap since - // round 9 (that band could never release). + // Equal to the daily cap: the vault rejects perReleaseCap > dailyCap (that + // band could never release). const perReleaseCapPen = BigInt(env.REHEARSAL_PER_RELEASE_CAP_PEN ?? "28800"); return { maxIssuance: 150_000_000n * PEN_18,