Skip to content

sunxingdi/solidity-storage-layout

Repository files navigation

solidity存储分布

文章概述

介绍solidity常见类型的存储分布。

固定类型:bool, uint, address, bytes, uint[n] (静态数组)
非固定类型:string, mapping, uint[] (动态数组)
组合类型:mapping, struct

环境准备

命令行进入工程根目录。

安装所需依赖:npm install

创建本地网络:npx hardhat node

编译合约脚本:npx hardhat compile

执行部署脚本:npx hardhat run .\scripts\deploy.ts --network localhost

结果打印如下:

ethers version:  6.9.1
Deploy contract...
Contract address: 0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6

 Get storage main slot...
slot 000 0x0000000000000000000000000000000000000000000000000000000000000001 //bool
slot 001 0x0000000000000000000000000000000000000000000000000000000000000002 //uint256
slot 002 0x0000000000000000000000000000000000000000000000000000000000000003 //uint256
slot 003 0x0000000000000000000000000000000500000000000000000000000000000004 //uint128 + uint128
slot 004 0x0000000000000000000000000000000000000000000000000000000000000006 //uint128
slot 005 0x0000000000000000000000000000000000000000000000000000000000000007 //uint256
slot 006 0x0000000000000000000000000000000000000000000000000000000000000008 //uint128
slot 007 0x000000000000000000000000a513e6e4b8f2a923d98304ec87f64353c4d5c853 //address
slot 008 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef //bytes32
slot 009 0x000000000000000000000000000000000000000000000000000000000000000a //uint256[3]
slot 010 0x000000000000000000000000000000000000000000000000000000000000000b //uint256[3]
slot 011 0x000000000000000000000000000000000000000000000000000000000000000c //uint256[3]
slot 012 0x6162636162636162636162630000000000000000000000000000000000000018 //string1, 最后一个字节存储长度
slot 013 0x616263616263616263616263616263616263616263616263616263616263613e //string2, 最后一个字节存储长度
slot 014 0x0000000000000000000000000000000000000000000000000000000000000041 //string3主槽, 主槽存储长度,子槽存储数据,子槽槽位keccak256(abi.encode(slot))
slot 015 0x0000000000000000000000000000000000000000000000000000000000000081 //string4主槽, 主槽存储长度,子槽存储数据,子槽槽位keccak256(abi.encode(slot))
slot 016 0x0000000000000000000000000000000000000000000000000000000000000000 //mapping主槽, 主槽空置,子槽存储数据,子槽槽位keccak256(abi.encode(key, slot))
slot 017 0x0000000000000000000000000000000000000000000000000000000000000002 //uint256[], 主槽存储长度,子槽存储数据,子槽槽位keccak256(abi.encode(slot))
slot 018 0x0000000000000000000000000000000000000000000000000000000000000000 //mapping主槽, 主槽空置,一级子槽存储数组长度,二级子槽存储数组数据
slot 019 0x73616e647900000000000000000000000000000000000000000000000000000a //struct-string, 最后一个字节存储长度
slot 020 0x000000000000000000000000000000bb000000000000000000000000000000aa //struct-uint128+uint128,  
slot 021 0x0000000000000000000000000000000000000000000000000000000000000002 //struct-uint256[], 主槽存储长度,子槽存储数据,子槽槽位keccak256(abi.encode(slot))
slot 022 0x00000000000000ef00000000000000fe00000000000000ff00000000000000ee //struct-uint64[4]
slot 023 0x0000000000000000000000000000000000000000000000000000000000000000

 Get storage sub slot...

 String storage...
slot 0xbb7b4a454dc3493923482f07822329ed19e8244eff582cc204f8554c3620c3fd 0x6465666465666465666465666465666465666465666465666465666465666465
slot 0x8d1108e10bcb7c27dddfc02ed9d693a074039d026cf4ea4240b40f7d581ac802 0x6667686667686667686667686667686667686667686667686667686667686667
slot 0x8d1108e10bcb7c27dddfc02ed9d693a074039d026cf4ea4240b40f7d581ac803 0x696a6b696a6b696a6b696a6b696a6b696a6b696a6b696a6b696a6b696a6b696a

 Mapping storage...
slot 0x6e0956cda88cad152e89927e53611735b61a5c762d1428573c6931b0a5efcb01 0x00000000000000000000000000000000000000000000000000000000000000aa
slot 0x8c6065603763fec3f5742441d3833f3f43b982453612d76adb39a885e3006b5f 0x00000000000000000000000000000000000000000000000000000000000000bb
slot 0x853b2fefe141400fef543280f93d98bd49996069f632d0d20236afeeed8e46a2 0x00000000000000000000000000000000000000000000000000000000000000cc

 Dynamic_Array storage...
slot 0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c68 0x0000000000000000000000000000000000000000000000000000000000000abc
slot 0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c69 0x0000000000000000000000000000000000000000000000000000000000000def

 Mapping_Array storage...
slot 0x7e7fa33969761a458e04f477e039a608702b4f924981d6653935a8319a08ad7b 0x0000000000000000000000000000000000000000000000000000000000000001
slot 0x29db533c56d9efb8b3a9b3a66dbf4e3fb200159193a596bc6285eff649e3777c 0x0000000000000000000000000000000000000000000000000000000000000aaa
slot 0x71a67924699a20698523213e55fe499d539379d7769cd5567e2c45d583f815a3 0x0000000000000000000000000000000000000000000000000000000000000002
slot 0x09d3634ec98b7a093f4c560246b0e10c09e0e06c46ca9be08ca93e6cf08d8d13 0x0000000000000000000000000000000000000000000000000000000000000bbb
slot 0x09d3634ec98b7a093f4c560246b0e10c09e0e06c46ca9be08ca93e6cf08d8d14 0x0000000000000000000000000000000000000000000000000000000000000ccc
slot 0x8e1fee8c88a9e04123b21e90cae2727a7715bf522a1e46eb5934ccd05203a6b2 0x0000000000000000000000000000000000000000000000000000000000000003
slot 0xd26569e9d5f053def6d3e4ea5afe83fc1c4c0316082fddcc40a8d26094d67155 0x0000000000000000000000000000000000000000000000000000000000000ddd
slot 0xd26569e9d5f053def6d3e4ea5afe83fc1c4c0316082fddcc40a8d26094d67156 0x0000000000000000000000000000000000000000000000000000000000000eee
slot 0xd26569e9d5f053def6d3e4ea5afe83fc1c4c0316082fddcc40a8d26094d67157 0x0000000000000000000000000000000000000000000000000000000000000fff

 Struct storage...
slot 0x55f448fdea98c4d29eb340757ef0a66cd03dbb9538908a6a81d96026b71ec475 0x00000000000000000000000000000000000000000000000000000000000000cc
slot 0x55f448fdea98c4d29eb340757ef0a66cd03dbb9538908a6a81d96026b71ec476 0x00000000000000000000000000000000000000000000000000000000000000dd

补充bytes数据类型存储分布:


//交易哈希:https://bscscan.com/tx/0x120be12320d222acd7d7c679304cec669265d30197fdff90ae306e0603d3d00c

Function: teamIncome(uint256 id,address tokenIn,uint256 amount,uint256 feeNum,address recive,bytes signature)

MethodID: 0x9237e263                                                   ---> 函数选择器
[0]:  0000000000000000000000000000000000000000000000000000000000011595 ---> 操作ID:71061(十进制)
[1]:  00000000000000000000000055d398326f99059ff775485246999027b3197955 ---> 提现币种:BSC-USDT(精度18)
[2]:  000000000000000000000000000000000000000000000000f89edd950f1f8000 ---> 提现金额:17.915000000000000000
[3]:  0000000000000000000000000000000000000000000000000de0b6b3a7640000 ---> 手续费:   1.000000000000000000
[4]:  0000000000000000000000008a1d3fa38391621855b805fa352895947a7364c0 ---> 提现收款地址
[5]:  00000000000000000000000000000000000000000000000000000000000000c0 ---> bytes数据偏移量:192(十进制),bytes数据从192字节处开始,32 x 6 = 192,即数据从第7个槽位开始
[6]:  0000000000000000000000000000000000000000000000000000000000000041 ---> bytes数据长度:65(十进制),以太坊标准签名长度为65字节
[7]:  ef25ca6b69d2dfe608a555ff3912b0e3325949e71570df31ecab57d8cb336f81 ---> ecdsa签名r值,占32字节
[8]:  2af9c7315e11063765d1bf34e70e983758db09c937fe80eeeaca0a181b1ce1bc ---> ecdsa签名s值,占32字节
[9]:  1b00000000000000000000000000000000000000000000000000000000000000 ---> ecdsa签名v值:27,占1字节

参考文章

详解SOLIDITY合约数据存储布局

深入理解合约升级(2) - Solidity 内存布局

About

solidity存储分布研究

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published