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第2期——StorageSlot.sol

  • Michael.W
  • 发布于 2023-07-03 17:00
  • 阅读 2489

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

0. 版本

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

0.1 StorageSlot.sol

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

StorageSlot库是用于向storage slots中读和写基本类型数据。使用可升级合约时往往会使用该库用于避免storage slot冲突。

该库在不需要使用内联汇编的情况下,可以对指定的slot进行读写。具体slot中存储的值即StorageSlot库中定义的各结构体中的value值。

注:一个合约中的slot号就是一个bytes32变量。所以只要指定一个bytes32的值就可以以其作为slot号进行对应slot的值读写。

1. 目标合约

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

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

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

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

contract MockStorageSlot {
    using StorageSlot for bytes32;

    bytes32 public constant _ADDRESS_SLOT = keccak256("address slot");
    bytes32 public constant _BOOLEAN_SLOT = keccak256("boolean slot");
    bytes32 public constant _BYTES32_SLOT = keccak256("bytes32 slot");
    bytes32 public constant _UINT256_SLOT = keccak256("uint256 slot");

    uint public slot0 = 0;
    uint public slot1 = 10;
    uint public slot2 = 20;

    function setAddressSlot(address newAddr) external {
        _ADDRESS_SLOT.getAddressSlot().value = newAddr;
    }

    function getAddressSlot() external view returns (address){
        return _ADDRESS_SLOT.getAddressSlot().value;
    }

    function setBooleanSlot(bool newBool) external {
        _BOOLEAN_SLOT.getBooleanSlot().value = newBool;
    }

    function getBooleanSlot() external view returns (bool){
        return _BOOLEAN_SLOT.getBooleanSlot().value;
    }

    function setBytes32Slot(bytes32 newBytes32) external {
        _BYTES32_SLOT.getBytes32Slot().value = newBytes32;
    }

    function getBytes32Slot() external view returns (bytes32){
        return _BYTES32_SLOT.getBytes32Slot().value;
    }

    function setUint256Slot(uint newUint) external {
        _UINT256_SLOT.getUint256Slot().value = newUint;
    }

    function getUint256Slot() external view returns (uint){
        return _UINT256_SLOT.getUint256Slot().value;
    }

    function setUintValueBySlotNumber(bytes32 implementation, uint newUint) external {
        implementation.getUint256Slot().value = newUint;
    }

    function getUintValueBySlotNumber(bytes32 implementation) external view returns (uint){
        return implementation.getUint256Slot().value;
    }
}

全部foundry测试合约:

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

2. 代码精读

2.1 slot结构体

通过定义四种结构体来分别在slot中存储address/bool/bytes32/uint256类型的基本类型变量:

struct AddressSlot {
    // slot中存储的address类型的变量值
    address value;
}

struct BooleanSlot {
    // slot中存储的bool类型的变量值
    bool value;
}

struct Bytes32Slot {
    // slot中存储的bytes32类型的变量值
    bytes32 value;
}

struct Uint256Slot {
    // slot中存储的uint256类型的变量值
    uint256 value;
}

2.2 getAddressSlot(bytes32)

传入slot号,返回指向该slot的AddressSlot结构体的storage指针:

代码解读

function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
    assembly {
        // 设置storage变量指针指向传入的slot号位置
        r.slot := slot
    }
}

foundry代码验证

contract StorageSlotTest is Test {
    MockStorageSlot testing = new MockStorageSlot();

    function test_SetAndGetAddressSlot() external {
        assertEq(testing.getAddressSlot(), address(0));
        testing.setAddressSlot(address(1024));
        assertEq(testing.getAddressSlot(), address(1024));

        // check the slot value by the slot number
        bytes32 slotNumber = testing._ADDRESS_SLOT();
        address valueInSlot = address(uint160(uint(vm.load(address(testing), slotNumber))));
        assertEq(testing.getAddressSlot(), valueInSlot);
    }
}

2.3 getBooleanSlot(bytes32)

传入slot号,返回指向该slot的BooleanSlot结构体的storage指针:

代码解读

function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
    // 设置storage变量指针指向传入的slot号位置 
    assembly {
        r.slot := slot
    }
}

foundry代码验证

contract StorageSlotTest is Test {
    MockStorageSlot testing = new MockStorageSlot();

    function test_SetAndGetBooleanSlot() external {
        assertFalse(testing.getBooleanSlot());
        testing.setBooleanSlot(true);
        assertTrue(testing.getBooleanSlot());

        // check the slot value by the slot number
        bytes32 slotNumber = testing._BOOLEAN_SLOT();
        bool valueInSlot = vm.load(address(testing), slotNumber) == bytes32(0) ? false : true;
        assertEq(testing.getBooleanSlot(), valueInSlot);
    }
}

2.4 getBytes32Slot(bytes32)

传入slot号,返回指向该slot的Bytes32Slot结构体的storage指针:

代码解读

function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
    // 设置storage变量指针指向传入的slot号位置 
    assembly {
        r.slot := slot
    }
}

foundry代码验证

contract StorageSlotTest is Test {
    MockStorageSlot testing = new MockStorageSlot();

    function test_SetAndGetBytes32Slot() external {
        assertEq(testing.getBytes32Slot(), "");
        testing.setBytes32Slot("a");
        assertEq(testing.getBytes32Slot(), "a");

        // check the slot value by the slot number
        bytes32 slotNumber = testing._BYTES32_SLOT();
        bytes32 valueInSlot = vm.load(address(testing), slotNumber);
        assertEq(testing.getBytes32Slot(), valueInSlot);
    }
}

2.5 getUint256Slot(bytes32)

传入slot号,返回指向该slot的Uint256Slot结构体的storage指针:

代码解读

function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
    // 设置storage变量指针指向传入的slot号位置 
    assembly {
        r.slot := slot
    }
}

foundry代码验证

contract StorageSlotTest is Test {
    MockStorageSlot testing = new MockStorageSlot();

    function test_SetAndGetUint256Slot() external {
        assertEq(testing.getUint256Slot(), 0);
        testing.setUint256Slot(1024);
        assertEq(testing.getUint256Slot(), 1024);

        // check the slot value by the slot number
        bytes32 slotNumber = testing._UINT256_SLOT();
        uint valueInSlot = uint(vm.load(address(testing), slotNumber));
        assertEq(testing.getUint256Slot(), valueInSlot);
    }

    function test_SetAndGetUintSlotForTheDefaultStorageVariable() external {
        assertEq(testing.slot0(), 0);
        // set the slot0 by slot number
        testing.setUintValueBySlotNumber(bytes32(0), 1024);
        // check the query by testing.slot0() and slot number
        assertEq(testing.getUintValueBySlotNumber(bytes32(0)), 1024);
        assertEq(testing.getUintValueBySlotNumber(bytes32(0)), testing.slot0());

        // check the value of slot1 and slot2
        assertEq(testing.slot1(), 10);
        assertEq(testing.slot1(), testing.getUintValueBySlotNumber(bytes32(uint(1))));
        assertEq(testing.slot2(), 20);
        assertEq(testing.slot2(), testing.getUintValueBySlotNumber(bytes32(uint(2))));
    }
}

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

1.jpeg

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

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

0 条评论

请先 登录 后评论