0

I am wondering how the revert keyword works here in this program. In my interpretation for the function "register", the if statement asks if lookup[name] is equal to an address of 0 (which is a contract address?). Then, if the address is NOT equal to a contract address, revert (which is similar to require?) the error. The program works fine but I know that I'm not understanding revert correctly here.

// 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;
    }
}

1 Answer 1

2

take a look at the doc of mappings

Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value.

so the following line:

 if (lookup[name] != address(0)) revert AlreadyRegistered();

is checking if for the specific key (parameter name) the value of the mapping is different than the default value which is, as you can see in the doc: byte-representation of all zeros (that is what address(0) return)

If the value for that key is address(0) mean that the key was not registered yet

So in order to answer your question:

How does revert work?

the above mentioned line is telling if the name was already registered revert with the custom error AlreadyRegisted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.