Michael.W基于Foundry精读Openzeppelin

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

  • Michael.W
  • 发布于 2023-07-10 15:03
  • 阅读 2433

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

0. 版本

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

0.1 Timers.sol

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

Timers库是一个专门用于控制和管理有时间有效期窗口的工具库。时间有效期可以从区块链时间戳和区块高度两个维度进行管理。

1. 目标合约

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

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

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

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

contract MockTimersTimestamp {
    using Timers for Timers.Timestamp;

    Timers.Timestamp _tt;

    function getDeadline() external view returns (uint64){
        return _tt.getDeadline();
    }

    function setDeadline(uint64 newTimestamp) external {
        _tt.setDeadline(newTimestamp);
    }

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

    function isUnset() external view returns (bool) {
        return _tt.isUnset();
    }

    function isStarted() external view returns (bool) {
        return _tt.isStarted();
    }

    function isPending() external view returns (bool) {
        return _tt.isPending();
    }

    function isExpired() external view returns (bool) {
        return _tt.isExpired();
    }
}

contract MockTimersBlockNumber {
    using Timers for Timers.BlockNumber;

    Timers.BlockNumber _tb;

    function getDeadline() external view returns (uint64){
        return _tb.getDeadline();
    }

    function setDeadline(uint64 newBlockNumber) external {
        _tb.setDeadline(newBlockNumber);
    }

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

    function isUnset() external view returns (bool) {
        return _tb.isUnset();
    }

    function isStarted() external view returns (bool) {
        return _tb.isStarted();
    }

    function isPending() external view returns (bool) {
        return _tb.isPending();
    }

    function isExpired() external view returns (bool) {
        return _tb.isExpired();
    }
}

全部foundry测试合约:

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

2. 代码精读

2.1 区块链时间戳维度

2.1.1 Timestamp结构体
    struct Timestamp {
            // 主合约中设定的时间有效期窗口的截止时间戳,用uint64类型存储
            uint64 _deadline;
    }
2.1.2 setDeadline(Timestamp storage, uint64) && getDeadline(Timestamp memory)

设置和返回当前时间有效期窗口的截止时间戳。

    function setDeadline(Timestamp storage timer, uint64 timestamp) internal {
        // 修改Timestamp中的_deadline成员值
        timer._deadline = timestamp;
    }

    function getDeadline(Timestamp memory timer) internal pure returns (uint64) {
        // 返回Timestamp中的_deadline成员值
        return timer._deadline;
    }

foundry代码验证

contract TimersTest is Test {
    MockTimersTimestamp mtt = new MockTimersTimestamp();

    function test_SetDeadlineAndGetDeadline_ForTimestamp() external {
        mtt.setDeadline(1024);
        assertEq(1024, mtt.getDeadline());
    }
}
2.1.3 reset(Timestamp storage) && isUnset(Timestamp memory)
  • reset(Timestamp storage)为重置截止时间戳;

  • isUnset(Timestamp memory) 为查询当前主合约的截止时间戳是否为未设置的状态,即截止时间戳是否为0。

    function reset(Timestamp storage timer) internal {
        // 将主合约的截止时间戳重置为0
        timer._deadline = 0;
    }

    function isUnset(Timestamp memory timer) internal pure returns (bool) {
        // 判断当前主合约的截止时间戳是否为0
        return timer._deadline == 0;
    }

foundry代码验证

contract TimersTest is Test {
    MockTimersTimestamp mtt = new MockTimersTimestamp();

    function test_ResetAndIsUnset_ForTimestamp() external {
        mtt.setDeadline(1024);
        assertFalse(mtt.isUnset());

        mtt.reset();
        assertTrue(mtt.isUnset());

        assertEq(0, mtt.getDeadline());
    }
}
2.1.4 isStarted(Timestamp memory) && isPending(Timestamp memory) && isExpired(Timestamp memory)
  • isStarted(Timestamp memory)为该时间戳有效期管理系统是否开启。即判断当前主合约的截止时间戳是否大于0;
  • isPending(Timestamp memory)为当前区块链时间是否处于有效状态;
  • isExpired(Timestamp memory) 为当前区块链时间是否处于过期状态。
    function isStarted(Timestamp memory timer) internal pure returns (bool) {
        // 判断当前主合约的截止时间戳是否大于0
        return timer._deadline > 0;
    }

    function isPending(Timestamp memory timer) internal view returns (bool) {
        // 判断当前区块链时间戳是否小于主合约设置的截止时间戳
        return timer._deadline > block.timestamp;
    }

    function isExpired(Timestamp memory timer) internal view returns (bool) {
        // 只有当时间戳有效期管理系统处于开启状态 且 当前区块链时间戳大于等于主合约设置的截止时间戳时,才会被认作是expired。 有任何一点不满足,都会返回false。
        return isStarted(timer) && timer._deadline <= block.timestamp;
    }

foundry代码验证

contract TimersTest is Test {
    MockTimersTimestamp mtt = new MockTimersTimestamp();

    function test_IsStartedAndIsPendingAndIsExpired_ForTimestamp() external {
        mtt.reset();
        assertFalse(mtt.isStarted());
        mtt.setDeadline(1024);
        assertTrue(mtt.isStarted());

        // check pending/expired status
        vm.warp(1024 - 1);
        assertTrue(mtt.isPending());
        assertFalse(mtt.isExpired());

        vm.warp(1024);
        assertFalse(mtt.isPending());
        assertTrue(mtt.isExpired());

        vm.warp(1024 + 1);
        assertFalse(mtt.isPending());
        assertTrue(mtt.isExpired());

        // isExpired() always returns false if unset
        mtt.reset();
        assertFalse(mtt.isExpired());
    }
}

2.2 区块链区块高度维度

2.2.1 BlockNumber结构体
struct BlockNumber {
    // 主合约中设定的时间有效期窗口的截止区块高度,用uint64类型存储
    uint64 _deadline;
}
2.2.2 setDeadline(BlockNumber storage, uint64) && getDeadline(BlockNumber memory)

设置和返回当前时间有效期窗口的截止区块高度。

    function getDeadline(BlockNumber memory timer) internal pure returns (uint64) {
        // 返回BlockNumber中的_deadline成员值
        return timer._deadline;
    }

    function setDeadline(BlockNumber storage timer, uint64 timestamp) internal {
        // 修改BlockNumber中的_deadline成员值
        timer._deadline = timestamp;
    }

foundry代码验证

contract TimersTest is Test {
    MockTimersBlockNumber mtb = new MockTimersBlockNumber();

    function test_SetDeadlineAndGetDeadline_ForBlockNumber() external {
        mtb.setDeadline(1024);
        assertEq(1024, mtb.getDeadline());
    }
}
2.2.3 reset(BlockNumber storage) && isUnset(BlockNumber memory)
  • reset(BlockNumber storage)为重置截止区块高度;

  • isUnset(BlockNumber memory) 为查询当前主合约的截止区块高度是否为未设置的状态,即截止区块高度是否为0。

    function reset(BlockNumber storage timer) internal {
    // 将主合约的截止区块高度重置为0
        timer._deadline = 0;
    }

    function isUnset(BlockNumber memory timer) internal pure returns (bool) {
        // 判断当前主合约的截止区块高度是否为0
        return timer._deadline == 0;
    }

foundry代码验证

contract TimersTest is Test {
    MockTimersBlockNumber mtb = new MockTimersBlockNumber();

    function test_ResetAndIsUnset_ForBlockNumber() external {
        mtb.setDeadline(1024);
        assertFalse(mtb.isUnset());

        mtb.reset();
        assertTrue(mtb.isUnset());

        assertEq(0, mtb.getDeadline());
    }
}
2.2.4 isStarted(BlockNumber memory) && isPending(BlockNumber memory) && isExpired(BlockNumber memory)
  • isStarted(BlockNumber memory)为该区块高度有效期管理系统是否开启。即判断当前主合约的截止区块高度是否大于0;
  • isPending(BlockNumber memory)为当前区块链区块高度是否处于有效状态;
  • isExpired(BlockNumber memory) 为当前区块链区块高度是否处于过期状态。
    function isStarted(BlockNumber memory timer) internal pure returns (bool) {
        // 判断当前主合约的截止区块高度是否大于0
        return timer._deadline > 0;
    }

    function isPending(BlockNumber memory timer) internal view returns (bool) {
        // 判断当前区块链区块高度是否小于主合约设置的截止区块高度        
        return timer._deadline > block.number;
    }

    function isExpired(BlockNumber memory timer) internal view returns (bool) {
        // 只有区块高度有效期管理系统处于开启状态 且 当前区块链区块高度大于等于主合约设置的截止区块高度时,才会被认作是expired。 有任何一点不满足,都会返回false。
        return isStarted(timer) && timer._deadline <= block.number;
    }

foundry代码验证

contract TimersTest is Test {
    MockTimersBlockNumber mtb = new MockTimersBlockNumber();

    function test_IsStartedAndIsPendingAndIsExpired_ForBlockNumber() external {
        mtb.reset();
        assertFalse(mtb.isStarted());
        mtb.setDeadline(1024);
        assertTrue(mtb.isStarted());

        // check pending/expired status
        vm.roll(1024 - 1);
        assertTrue(mtb.isPending());
        assertFalse(mtb.isExpired());

        vm.roll(1024);
        assertFalse(mtb.isPending());
        assertTrue(mtb.isExpired());

        vm.roll(1024 + 1);
        assertFalse(mtb.isPending());
        assertTrue(mtb.isExpired());

        // isExpired() always returns false if unset
        mtb.reset();
        assertFalse(mtb.isExpired());
    }
}

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

1.jpeg

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

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

0 条评论

请先 登录 后评论