Michael.W基于Foundry精读Openzeppelin

2024年08月13日更新 116 人订阅
专栏简介 Michael.W基于Foundry精读Openzeppelin第8期——Context.sol Michael.W基于Foundry精读Openzeppelin第1期——Address.sol Michael.W基于Foundry精读Openzeppelin第2期——StorageSlot.sol Michael.W基于Foundry精读Openzeppelin第3期——Arrays.sol Michael.W基于Foundry精读Openzeppelin第4期——Base64.sol Michael.W基于Foundry精读Openzeppelin第5期——Counters.sol Michael.W基于Foundry精读Openzeppelin第6期——Strings.sol Michael.W基于Foundry精读Openzeppelin第7期——Timers.sol Michael.W基于Foundry精读Openzeppelin第9期——Multicall.sol Michael.W基于Foundry精读Openzeppelin第10期——Create2.sol Michael.W基于Foundry精读Openzeppelin第11期——Math.sol Michael.W基于Foundry精读Openzeppelin第12期——SafeCast.sol Michael.W基于Foundry精读Openzeppelin第13期——Checkpoints.sol Michael.W基于Foundry精读Openzeppelin第14期——SafeMath.sol Michael.W基于Foundry精读Openzeppelin第15期——SignedMath.sol Michael.W基于Foundry精读Openzeppelin第16期——SignedSafeMath.sol Michael.W基于Foundry精读Openzeppelin第17期——BitMaps.sol Michael.W基于Foundry精读Openzeppelin第18期——DoubleEndedQueue.sol Michael.W基于Foundry精读Openzeppelin第19期——EnumerableSet.sol Michael.W基于Foundry精读Openzeppelin第20期——EnumerableMap.sol Michael.W基于Foundry精读Openzeppelin第21期——ERC165.sol (番外篇)Michael.W基于Foundry精读Openzeppelin第22期——内联汇编staticcall Michael.W基于Foundry精读Openzeppelin第23期——ERC165Checker.sol Michael.W基于Foundry精读Openzeppelin第24期——ERC165Storage.sol Michael.W基于Foundry精读Openzeppelin第25期——IERC1820Registry.sol Michael.W基于Foundry精读Openzeppelin第26期——ERC1820Implementer.sol Michael.W基于Foundry精读Openzeppelin第27期——Escrow.sol Michael.W基于Foundry精读Openzeppelin第28期——ConditionalEscrow.sol Michael.W基于Foundry精读Openzeppelin第29期——RefundEscrow.sol Michael.W基于Foundry精读Openzeppelin第30期——ECDSA.sol Michael.W基于Foundry精读Openzeppelin第31期——IERC1271.sol Michael.W基于Foundry精读Openzeppelin第32期——SignatureChecker.sol Michael.W基于Foundry精读Openzeppelin第33期——EIP712.sol Michael.W基于Foundry精读Openzeppelin第34期——MerkleProof.sol Michael.W基于Foundry精读Openzeppelin第35期——Ownable.sol Michael.W基于Foundry精读Openzeppelin第36期——Ownable2Step.sol Michael.W基于Foundry精读Openzeppelin第37期——AccessControl.sol Michael.W基于Foundry精读Openzeppelin第38期——AccessControlEnumerable.sol Michael.W基于Foundry精读Openzeppelin第39期——ERC20.sol Michael.W基于Foundry精读Openzeppelin第40期——ERC20Burnable.sol Michael.W基于Foundry精读Openzeppelin第41期——ERC20Capped.sol Michael.W基于Foundry精读Openzeppelin第42期——draft-ERC20Permit.sol Michael.W基于Foundry精读Openzeppelin第43期——Pausable.sol Michael.W基于Foundry精读Openzeppelin第44期——ERC20Pausable.sol Michael.W基于Foundry精读Openzeppelin第45期——ERC20FlashMint.sol Michael.W基于Foundry精读Openzeppelin第46期——ERC20Snapshot.sol Michael.W基于Foundry精读Openzeppelin第47期——SafeERC20.sol Michael.W基于Foundry精读Openzeppelin第48期——TokenTimelock.sol Michael.W基于Foundry精读Openzeppelin第49期——ERC20Wrapper.sol Michael.W基于Foundry精读Openzeppelin第50期——ERC20Votes.sol Michael.W基于Foundry精读Openzeppelin第51期——ERC20VotesComp.sol Michael.W基于Foundry精读Openzeppelin第52期——ERC4626.sol Michael.W基于Foundry精读Openzeppelin第53期——ERC20PresetFixedSupply.sol Michael.W基于Foundry精读Openzeppelin第54期——ERC20PresetMinterPauser.sol Michael.W基于Foundry精读Openzeppelin第55期——PaymentSplitter.sol Michael.W基于Foundry精读Openzeppelin第56期——VestingWallet.sol Michael.W基于Foundry精读Openzeppelin第57期——ReentrancyGuard.sol Michael.W基于Foundry精读Openzeppelin第58期——PullPayment.sol Michael.W基于Foundry精读Openzeppelin第59期——Proxy.sol Michael.W基于Foundry精读Openzeppelin第60期——Clones.sol Michael.W基于Foundry精读Openzeppelin第61期——ERC1967Upgrade.sol Michael.W基于Foundry精读Openzeppelin第62期——ERC1967Proxy.sol Michael.W基于Foundry精读Openzeppelin第63期——Initializable.sol Michael.W基于Foundry精读Openzeppelin第64期——UUPSUpgradeable.sol Michael.W基于Foundry精读Openzeppelin第65期——TransparentUpgradeableProxy.sol Michael.W基于Foundry精读Openzeppelin第66期——ProxyAdmin.sol Michael.W基于Foundry精读Openzeppelin第67期——BeaconProxy.sol Michael.W基于Foundry精读Openzeppelin第68期——UpgradeableBeacon.sol

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

  • Michael.W
  • 发布于 2023-08-07 09:11
  • 阅读 2193

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

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

// 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为例:
bytes4 interfaceIdICustomizedInterface = type(ICustomizedInterface).interfaceId;
  1. interface内包含的所有函数的selector的异或结果。以ICustomizedInterface为例:
bytes4 interfaceIdICustomizedInterface = ICustomizedInterface.viewFunction.selector
        ^ ICustomizedInterface.pureFunction.selector
        ^ ICustomizedInterface.externalFunction.selector;
  1. interface内包含的所有函数的函数签名的异或结果。以ICustomizedInterface为例:
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的查询功能。

    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代码验证
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.jpeg

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

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论