安全性

这个 crate 提供了处理常见安全相关任务的组件。

Initializable

InitializableComponent

use openzeppelin_security::InitializableComponent;

该组件支持合约的一次性初始化。

可嵌入的实现
InitializableImpl
内部实现
InternalImpl

可嵌入的函数

is_initialized(self: @ComponentState) → bool external

返回合约是否已初始化。

内部函数

initialize(ref self: ComponentState) internal

初始化合约。只能被调用一次。

要求:

  • 合约之前必须未被初始化。

Pausable

PausableComponent

use openzeppelin_security::PausableComponent;

用于实现紧急停止机制的组件。

可嵌入的实现
PausableImpl

可嵌入的函数

is_paused(self: @ComponentState) → bool external

返回合约当前是否已暂停。

内部函数

assert_not_paused(self: @ComponentState) internal

如果合约已暂停,则 panic。

assert_paused(self: @ComponentState) internal

如果合约未暂停,则 panic。

pause(ref self: ComponentState) internal

暂停合约。

要求:

  • 合约必须未暂停。

发出一个 已暂停 事件。

unpause(ref self: ComponentState) internal

取消暂停合约。

要求:

  • 合约必须已暂停。

发出一个 未暂停 事件。

事件

Paused(account: ContractAddress) event

当合约被 account 暂停时发出。

Unpaused(account: ContractAddress) event

当合约被 account 取消暂停时发出。

ReentrancyGuard

ReentrancyGuardComponent

use openzeppelin_security::ReentrancyGuardComponent;

有助于防止重入调用的组件。

内部实现
InternalImpl

内部函数

start(ref self: ComponentState) internal

阻止合约函数直接或间接地调用自身或另一个受保护的函数。

要求:

  • guard 当前必须未启用。

end(ref self: ComponentState) internal

移除重入 guard。