Michael.W基于Foundry精读Openzeppelin

2024年08月13日更新 117 人订阅
专栏简介 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第5期——Counters.sol

  • Michael.W
  • 发布于 2023-07-07 23:36
  • 阅读 3381

从foundry工程化的角度详细解读Openzeppelin中的Counters库及对应测试。

0. 版本

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

0.1 Counters.sol

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

将uint256封装了成一个计数器,可做自增1,自减1或重置操作。一般用该库可以追踪一个mapping中元素个数,ERC721的token id和统计请求次数等。

1. 目标合约

封装Counters library成为一个可调用合约:

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

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "openzeppelin-contracts/contracts/utils/Counters.sol";

contract MockCounters {
    // Counters库中定义的Counter结构体可使用Counters库中的库函数
    using Counters for Counters.Counter;

    Counters.Counter _counter;

    function current() external view returns (uint){
        return _counter.current();
    }

    function increment() external {
        _counter.increment();
    }

    function decrement() external {
        _counter.decrement();
    }

    function reset() external {
        _counter.reset();
    }
}

全部foundry测试合约:

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

2. 代码精读

2.1 结构体Counter

在Counters库内定义的一个结构体。该结构体其实就是一个uint256的封装,在主合约中定义一个Counters.Counter类型的storage变量(其中value初始为0),配合本库的库函数使用。

代码解读

    struct Counter {
        uint256 _value;
    }

注:出于设计安全考虑,在你的合约中定义的storage Counter的所有读写都要限定在使用本库函数。在主合约中直接通过storage指针修改storage Counter的_value值会打破本库的安全性。

2.2 current(Counter storage)

返回计数器的当前值。

代码解读

    function current(Counter storage counter) internal view returns (uint256) {
        // 返回计数器结构体中的_value值
        return counter._value;
    }

2.3 increment(Counter storage)

计数器自增1。

代码解读

    function increment(Counter storage counter) internal {
        // 取消solidity 0.8中对数学运算的溢出检查
        unchecked {
            // 计数器中的_value自增1
            counter._value += 1;
        }
    }

为什么可以取消溢出检查?我认为计数器每次都是自增1,所以只有当counter._value为$2^{256}-1$时才会正溢出(这是个极小概率的事件)。而且做数学运算的溢出检查是要消耗gas的,取消检查可达到节约gas的目的。

foundry代码验证

    function test_Increment() external {
        assertEq(0, mc.current());
        mc.increment();
        assertEq(1, mc.current());
        mc.increment();
        assertEq(2, mc.current());
    }

2.4 decrement(Counter storage)

计数器自减1。

代码解读

    function decrement(Counter storage counter) internal {
        uint256 value = counter._value;
        // 计数器value值为0时报错,因为自减后会负向溢出
        require(value > 0, "Counter: decrement overflow");
        // 取消solidity 0.8中对数学运算的溢出检查
        unchecked {
            // 计数器中的_value自减1
            counter._value = value - 1;
        }
    }

注:其实上面的require(value>0, ...)unchecked{...},就等同于一个做运算溢出检查的counter._value-=1

foundry代码验证

    function test_Decrement() external {
        mc.increment();
        assertEq(1, mc.current());
        mc.decrement();
        assertEq(0, mc.current());
        // overflow
        vm.expectRevert("Counter: decrement overflow");
        mc.decrement();
    }

2.5 reset(Counter storage)

重置计数器,即计数器归0。

代码解读

    function reset(Counter storage counter) internal {
        // 计数器value清0
        counter._value = 0;
    }

foundry代码验证

    function test_Reset() external {
        mc.increment();
        mc.increment();
        assertEq(2, mc.current());
        mc.reset();
        assertEq(0, mc.current());
    }

ps:\ 本人热爱图灵,热爱中本聪,热爱V神。 以下是我个人的公众号,如果有技术问题可以关注我的公众号来跟我交流。 同时我也会在这个公众号上每周更新我的原创文章,喜欢的小伙伴或者老伙计可以支持一下! 如果需要转发,麻烦注明作者。十分感谢!

1.jpeg

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

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

0 条评论

请先 登录 后评论