首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >还原是如何工作的?

还原是如何工作的?
EN

Ethereum用户
提问于 2022-06-17 10:01:14
回答 1查看 71关注 0票数 0

我想知道在这个程序中,revert关键字是如何工作的。在我对函数“寄存器”的解释中,if语句询问查找名字是否等于0地址(该地址是契约地址?)。然后,如果该地址不等于合同地址,则还原(与所需的类似吗?)那个错误。该程序运行良好,但我知道我不能正确理解这里的还原。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.10;

contract LilENS {
    /// ERRORS ///

    /// @notice Thrown when trying to update a name you don't own
    error Unauthorized();

    /// @notice Thrown when trying to register a name that's already taken
    error AlreadyRegistered();

    /// @notice Stores the registered names and their addresses
    /// @dev This automatically generates a getter for us!
    mapping(string => address) public lookup;

    /// @notice Registers a new name, and points it to your address
    /// @param name The name to register
    function register(string memory name) public payable {
        if (lookup[name] != address(0)) revert AlreadyRegistered();

        lookup[name] = msg.sender;
    }

    /// @notice Allows the owner of a name to point it to a different address
    /// @param name The name to update
    /// @param addr The new address this name should point to
    function update(string memory name, address addr) public payable {
        if (msg.sender != lookup[name]) revert Unauthorized();

        lookup[name] = addr;
    }
}
EN

回答 1

Ethereum用户

发布于 2022-06-17 10:42:11

看一看映射文档

映射可以看作是哈希表,这些哈希表实际上是初始化的,因此每个可能的键都存在,并且映射到一个值,该值的字节表示形式都是零:类型的默认值。

因此,下面一行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 if (lookup[name] != address(0)) revert AlreadyRegistered();

是检查特定键(参数名称)的值是否与默认值不同(如在doc:字节表示的所有零(即地址(0)返回的值)中所看到的那样)。

如果该键的值为address(0),则表示该密钥尚未注册。

为了回答你的问题:

还原是如何工作的?

上面提到的行显示名称是否已经用自定义错误AlreadyRegisted进行了还原。

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/130388

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文