Michael.W基于Foundry精读Openzeppelin第21期——ERC165.sol

Michael.W基于Foundry精读Openzeppelin第21期------ERC165.sol

      • [0. 版本](#0. 版本)
        • [0.1 ERC165.sol](#0.1 ERC165.sol)
      • [1. 目标合约](#1. 目标合约)
      • [2. 代码精读](#2. 代码精读)
        • [2.1 IERC165](#2.1 IERC165)
          • [2.1.1 interfaceId的计算方式](#2.1.1 interfaceId的计算方式)
        • [2.2 ERC165](#2.2 ERC165)
          • [2.2.1 supportsInterface(bytes4 interfaceId)](#2.2.1 supportsInterface(bytes4 interfaceId))
          • [2.2.2 foundry代码验证](#2.2.2 foundry代码验证)

0. 版本

[openzeppelin]:v4.8.3,[forge-std]:v1.5.6

0.1 ERC165.sol

Github: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.8.3/contracts/utils/introspection/ERC165.sol

ERC165合约是IERC165的标准实现。ERC165提供了本合约是否实现了IERC165接口的查询。如果需要额外支持其他interface,可在目标合约内重写supportsInterface(bytes4)方法。

1. 目标合约

继承ERC165成为一个可调用合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/src/utils/introspection/MockERC165.sol

js 复制代码
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol";

interface ICustomizedInterface {
    error CustomizedError();

    event CustomizedEvent(uint n);

    function viewFunction(address addr) external view returns (address);

    function pureFunction(uint n) external pure returns (uint);

    function externalFunction(uint[] calldata nums, address[] calldata addrs) external;
}

contract MockERC165 is ERC165, ICustomizedInterface {
    uint _totalSum;
    uint _totalNumberOfAddress;

    // implementation of ICustomizedInterface
    function viewFunction(address addr) external view returns (address){
        if (addr == msg.sender) {
            return addr;
        }

        return address(1024);
    }

    function pureFunction(uint n) external pure returns (uint){
        return n + 1;
    }

    function externalFunction(uint[] calldata nums, address[] calldata addrs) external {
        for (uint i; i < nums.length; ++i) {
            _totalSum += nums[i];
        }

        _totalNumberOfAddress = addrs.length;
    }

    // override supportsInterface(bytes4) of IERC165
    function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
        return interfaceId == type(ICustomizedInterface).interfaceId || super.supportsInterface(interfaceId);
    }
}

同时该合约实现了一个自定义interface------ICustomizedInterface。

全部foundry测试合约:

Github: https://github.com/RevelationOfTuring/foundry-openzeppelin-contracts/blob/master/test/utils/introspection/ERC165.t.sol

2. 代码精读

2.1 IERC165

通过定义interfaceId作为interface的唯一标记。IERC165主要解决了目标合约已实现接口的检测问题------IERC165的实现合约提供了自身已实现interface的查询功能并且可以被其他继承ERC165Checker的合约识别。

具体详情参见:https://eips.ethereum.org/EIPS/eip-165

注:IERC165中要求方法supportsInterface(bytes4)的gas消耗必须小于30000 gas。

2.1.1 interfaceId的计算方式

interfaceId为interface的标识,有3种方法可以求得一个interface的interfaceId:

  1. solidity内置的type(interface).interfaceId。以ICustomizedInterface为例:
js 复制代码
bytes4 interfaceIdICustomizedInterface = type(ICustomizedInterface).interfaceId;
  1. interface内包含的所有函数的selector的异或结果。以ICustomizedInterface为例:
js 复制代码
bytes4 interfaceIdICustomizedInterface = ICustomizedInterface.viewFunction.selector
        ^ ICustomizedInterface.pureFunction.selector
        ^ ICustomizedInterface.externalFunction.selector;
  1. interface内包含的所有函数的函数签名的异或结果。以ICustomizedInterface为例:
js 复制代码
bytes4 interfaceIdICustomizedInterface = bytes4(keccak256('viewFunction(address)'))
        ^ bytes4(keccak256('pureFunction(uint256)'))
        ^ bytes4(keccak256('externalFunction(uint256[],address[])'));

2.2 ERC165

2.2.1 supportsInterface(bytes4 interfaceId)

对外提供本合约是否实现了传入interfaceId标识的interface的查询功能。

js 复制代码
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
    	// 如果传入的interfaceId为IERC165的interfaceId返回true,否则返回false
        return interfaceId == type(IERC165).interfaceId;
    }

注:ERC165只支持对interface IERC165的确认查询,如果需要额外支持其他interface,可在目标合约内重写该方法。具体例子可见后面的foundry测试代码。

2.2.2 foundry代码验证
js 复制代码
contract ERC165Test is Test {
    MockERC165 me = new MockERC165();

    function test_SupportsInterface_IERC165() external {
        // way 1: from function selector
        bytes4 interfaceIdIERC165 = IERC165.supportsInterface.selector;
        assertTrue(me.supportsInterface(interfaceIdIERC165));
        // way 2: from hash result of function signature
        interfaceIdIERC165 = bytes4(keccak256('supportsInterface(bytes4)'));
        assertTrue(me.supportsInterface(interfaceIdIERC165));
        // way 3: from type(interface).interfaceId
        interfaceIdIERC165 = type(IERC165).interfaceId;
        assertTrue(me.supportsInterface(interfaceIdIERC165));

        // not support
        bytes4 interfaceIdNotSupport = 0xffffffff;
        assertFalse(me.supportsInterface(interfaceIdNotSupport));
    }

    function test_SupportsInterface_ICustomizedInterface() external {
        // way 1: from XOR of all function selectors in the interface
        bytes4 interfaceIdICustomizedInterface = ICustomizedInterface.viewFunction.selector
        ^ ICustomizedInterface.pureFunction.selector
        ^ ICustomizedInterface.externalFunction.selector;
        assertTrue(me.supportsInterface(interfaceIdICustomizedInterface));
        // way 2: from XOR of all hash result of function signature
        interfaceIdICustomizedInterface = bytes4(keccak256('viewFunction(address)'))
        ^ bytes4(keccak256('pureFunction(uint256)'))
        ^ bytes4(keccak256('externalFunction(uint256[],address[])'));
        assertTrue(me.supportsInterface(interfaceIdICustomizedInterface));
        // way 3: from type(interface).interfaceId
        interfaceIdICustomizedInterface = type(ICustomizedInterface).interfaceId;
        assertTrue(me.supportsInterface(interfaceIdICustomizedInterface));

        // not support
        bytes4 interfaceIdNotSupport = ICustomizedInterface.pureFunction.selector
        ^ ICustomizedInterface.externalFunction.selector;
        assertFalse(me.supportsInterface(interfaceIdNotSupport));
    }

    function test_SupportsInterface_Gas() external {
        // for type(IERC165).interfaceId
        uint startGas = gasleft();
        me.supportsInterface(type(IERC165).interfaceId);
        uint endGas = gasleft();
        assertLt(startGas - endGas, 30000);

        // for type(ICustomizedInterface).interfaceId
        startGas = gasleft();
        me.supportsInterface(type(ICustomizedInterface).interfaceId);
        endGas = gasleft();
        assertLt(startGas - endGas, 30000);
    }
}

ps:

本人热爱图灵,热爱中本聪,热爱V神。

以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。

同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下!

如果需要转发,麻烦注明作者。十分感谢!

公众号名称:后现代泼痞浪漫主义奠基人

相关推荐
我是前端小学生1 个月前
单个solidity合约的文件结构
solidity
天涯学馆1 个月前
从零到英雄:Solidity 智能合约开发全攻略
后端·智能合约·solidity
MavenTalk1 个月前
Move开发语言在区块链的开发与应用
开发语言·python·rust·区块链·solidity·move
jc0803kevin2 个月前
solidity call使用
区块链·solidity
jc0803kevin2 个月前
solidity selfdestruct合约销毁
区块链·solidity
jc0803kevin2 个月前
solidity的struct对象,web3j java解析输出参数
java·web3·solidity
Keegan小钢2 个月前
智能合约开发工具Remix
web3·智能合约·solidity
yoona10203 个月前
盲拍合约:让竞拍更公平与神秘的创新解决方案
区块链·学习方法·solidity·remix·盲拍合约
0x派大星3 个月前
Solidity 设计模式:实现灵活与可扩展的智能合约架构
设计模式·架构·web3·区块链·智能合约·solidity
0x派大星3 个月前
Solidity 存储和内存管理:深入理解与高效优化
web3·区块链·智能合约·solidity