时间锁与执行
安全升级
时间锁与执行 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Timelocks Exist
A timelock is a mandatory delay between when a proposal passes and when it executes.
It protects users by giving them time to react to changes they disagree with — for example, withdrawing funds before a risky upgrade.
The TimelockController
OpenZeppelin's TimelockController is a contract that schedules, delays, and then executes operations.
The Governor proposes and queues actions into the timelock, which actually owns the protocol's admin rights.
contract Timelock is TimelockController {
constructor(uint minDelay, address[] proposers, address[] executors)
TimelockController(minDelay, proposers, executors, msg.sender) {}
}Schedule, Wait, Execute
Operations flow through three steps:
- schedule — queue the action with a delay
- wait — the minDelay must elapse
- execute — run the action after the delay
timelock.schedule(target, value, data, predecessor, salt, delay);
// ... wait minDelay ...
timelock.execute(target, value, data, predecessor, salt);The minDelay Parameter
The minDelay sets how long every scheduled operation must wait. Common values range from 1 to 7 days.
Longer delays mean more safety but slower governance — a deliberate trade-off each DAO must tune.
Roles in the Timelock
The timelock uses access roles:
- PROPOSER — can schedule operations (the Governor)
- EXECUTOR — can run them after the delay
- ADMIN — manages roles (often renounced)
Timelock Owns the Protocol
For governance to be meaningful, the timelock contract should hold the protocol's admin and ownership rights — not any individual.
This ensures every privileged change must pass through the full govern-then-delay pipeline.
protocol.transferOwnership(
address(timelock)
);Defense Against Malicious Proposals
If an attacker sneaks a harmful proposal through voting, the timelock delay is the last line of defense.
The community can spot the queued action and exit or coordinate a response before it executes.
Predecessor and Salt
Each scheduled operation can specify a predecessor (an operation that must run first) and a salt to make its identifier unique.
These let DAOs order dependent operations and avoid hash collisions between similar actions.
Cancelling Operations
A scheduled operation can be cancelled before execution by an address with the canceller role.
This provides an emergency brake if a queued action is found to be dangerous during the delay window.
timelock.cancel(operationId);Upgrades Done Safely
Combining Governor + Timelock + a proxy pattern lets DAOs upgrade contracts safely: vote, queue, delay, then execute the upgrade.
Every step is transparent and reversible up until execution, balancing flexibility with security.
Putting It Together
The timelock enforces a delay between approval and execution, owns the protocol's privileges, and uses roles plus cancellation to keep upgrades safe.
It is the crucial buffer that makes on-chain governance trustworthy.
Quick Check
Test your timelock understanding.
Recap: Timelocks and Execution
You learned that:
- A timelock delays execution after a proposal passes
- TimelockController schedules, waits, then executes
- minDelay tunes the safety vs speed trade-off
- The timelock should own the protocol; roles control access
- Operations can be cancelled during the delay
Course complete! Next course: Layer 2 and Scaling.
常见问题解答
「时间锁与执行」课时是免费的吗?
是的 — 「时间锁与执行」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。
「时间锁与执行」这节课中我会学到什么?
安全升级 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Web3 & DApp Development Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「时间锁与执行」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?
能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。