0Pricing
Blockchain Smart Contracts with Solidity · 课时

修饰器与检查—效果—交互模式

使用函数修饰器清晰地强制执行前置条件,并应用检查—效果—交互模式,编写更安全的合约。

修饰器与检查—效果—交互模式 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Are Modifiers?

A modifier is reusable code that runs before (or around) a function body. It is ideal for enforcing preconditions like access control without repeating the same checks everywhere.

Declaring a Modifier

The special _; placeholder marks where the function body executes. Code before it runs first; code after runs when the function returns.

modifier onlyOwner() {
    require(msg.sender == owner, "Not owner");
    _;
}

Applying a Modifier

Attach a modifier to a function declaration. The check runs automatically on every call, keeping the function body focused on logic.

function withdraw() public onlyOwner {
    payable(owner).transfer(address(this).balance);
}

Modifiers with Parameters

Modifiers can take arguments, making them flexible for validating dynamic conditions such as minimum values.

modifier costs(uint price) {
    require(msg.value >= price, "Not enough");
    _;
}

Stacking Multiple Modifiers

You can apply several modifiers to one function; they run left to right. Keep them small and single-purpose so combinations stay predictable.

function buy() public payable onlyWhenOpen costs(1 ether) {
    // ...
}

The Reentrancy Danger

When a contract calls out to an external address, that address can call back into your contract before the first call finishes. This reentrancy can drain funds if state is updated too late.

Checks-Effects-Interactions

The checks-effects-interactions pattern orders your function in three phases: validate inputs (checks), update state (effects), then call external contracts (interactions). External calls come last.

A Vulnerable Withdraw

This sends ETH before zeroing the balance, leaving a reentrancy window.

// VULNERABLE
function withdraw() public {
    uint amt = balances[msg.sender];
    payable(msg.sender).call{value: amt}("");
    balances[msg.sender] = 0; // too late!
}

The Safe Version

Update state before the external call so a reentrant call sees a zero balance.

// SAFE
function withdraw() public {
    uint amt = balances[msg.sender];
    balances[msg.sender] = 0;   // effects first
    payable(msg.sender).call{value: amt}(""); // interaction last
}

Reentrancy Guards

For extra safety, add a reentrancy guard modifier that sets a lock flag during execution, rejecting nested calls. Libraries like OpenZeppelin provide nonReentrant.

Combining the Patterns

Use modifiers for the checks phase (access and validation), structure the body as effects-then-interactions, and add a guard on functions that move value. Together they form a strong defensive baseline.

Quick Check

Check your knowledge of safe contract patterns.

Recap

You learned defensive Solidity patterns:

  • Modifiers centralize preconditions like access control
  • They can take parameters and be stacked
  • Checks-effects-interactions orders functions to prevent reentrancy
  • Add a reentrancy guard on value-moving functions

These patterns are foundational to writing secure contracts.

常见问题解答

「修饰器与检查—效果—交互模式」课时是免费的吗?

是的 — 「修饰器与检查—效果—交互模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

「修饰器与检查—效果—交互模式」这节课中我会学到什么?

使用函数修饰器清晰地强制执行前置条件,并应用检查—效果—交互模式,编写更安全的合约。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Blockchain Smart Contracts with Solidity 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「修饰器与检查—效果—交互模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?

能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 继承与接口
  2. 库与抽象合约
  3. 使用 Revert/Require 处理错误
  4. 修饰器与检查—效果—交互模式
← 返回 Blockchain Smart Contracts with Solidity