当前位置:网站首页>Solidity中的mapping为何不提供按照key遍历的功能?
Solidity中的mapping为何不提供按照key遍历的功能?
2022-07-21 05:08:00 【biakia0610】
举个例子
pragma solidity ^0.6.12;
contract Test {
mapping(uint256 => uint256) numMap;
function testMap() public {
numMap[1001] = 1;
}
}
这个Test合约定义了一个mapping数据结构numMap,key和value都是uint256类型
使用solc生成汇编:
solc --bin --asm --optimize Test.sol
结果如下:
======= Test.sol:Test =======
EVM assembly:
/* "Test.sol":26:140 contract Test {... */
mstore(0x40, 0x80)
callvalue
dup1
iszero
tag_1
jumpi
0x00
dup1
revert
tag_1:
pop
dataSize(sub_0)
dup1
dataOffset(sub_0)
0x00
codecopy
0x00
return
stop
sub_0: assembly {
/* "Test.sol":26:140 contract Test {... */
mstore(0x40, 0x80)
callvalue
dup1
iszero
tag_1
jumpi
0x00
dup1
revert
tag_1:
pop
jumpi(tag_2, lt(calldatasize, 0x04))
shr(0xe0, calldataload(0x00))
dup1
0x2010a281
eq
tag_3
jumpi
tag_2:
0x00
dup1
revert
/* "Test.sol":83:138 function testMap() public {... */
tag_3:
tag_4
tag_5
jump // in
tag_4:
stop
tag_5:
/* "Test.sol":124:128 1001 */
0x03e9
/* "Test.sol":117:123 numMap */
0x00
/* "Test.sol":117:129 numMap[1001] */
swap1
dup2
mstore
0x20
mstore
/* "Test.sol":132:133 1 */
0x01
/* "Test.sol":117:129 numMap[1001] */
0x236f539bf6e1b3b15a335e3cd157d360bc4be88c071635c19ba8a82b4f665e9f
/* "Test.sol":117:133 numMap[1001] = 1 */
sstore
/* "Test.sol":83:138 function testMap() public {... */
jump // out
auxdata: 0xa264697066735822122070c83297f2e6f8f0379c000bdf072f080d15e305449aa0dcc7e33858f970961564736f6c634300060c0033
}
可以看到最后一段存储数据的汇编如下:
mstore
/* "Test.sol":132:133 1 */
0x01
/* "Test.sol":117:129 numMap[1001] */
0x236f539bf6e1b3b15a335e3cd157d360bc4be88c071635c19ba8a82b4f665e9f
/* "Test.sol":117:133 numMap[1001] = 1 */
sstore
/* "Test.sol":83:138 function testMap() public {... */
jump // out
分析这段汇编就会发现1并不是存储在key是1001的位置,取而代之的是0x236f539bf6e1b3b15a335e3cd157d360bc4be88c071635c19ba8a82b4f665e9f
这样一段地址,这段地址是通过keccak256( bytes32(1001) + bytes32(0x00) )
计算得到的,1001就是key,而0x00表示这个合约存储的第一个storage类型变量。所以key的计算公式就是keccak256(bytes32(key) + bytes32(position))。
如果mapping提供遍历功能,那么遍历出来的key其实是keccak256(bytes32(key) + bytes32(position))的结果,而通过这个结果是无法推算出具体的key的,所以我们无法得到具体的key到value的映射。
以上就是mapping不提供key遍历功能的原因。
边栏推荐
- PHP基础语法
- JWT realizes TP5 user login function
- 加快无人机智能巡检规模化应用,福建电力输电运检智能化建设提速
- Vulnhub-dc-4 target penetration record
- JS converts hexadecimal color to RGBA format
- Nodejs waits for a period of time
- day1 poc与exp学习之pikachu字符注入
- PHP extracts the phone number in the string and verifies whether it is 11 digits
- BUUCTF(misc)
- Realization of interface displacement by linear Schrodinger equation
猜你喜欢
【内网渗透】内网不出网机器反弹shell以及CS上线
WAP green legend building (pure version)
[reverse analysis] simple learning of C language pointer
Thinkphp6 uses easywechat5 Development of official account of X (I)
20. [char* is equal to string but not equal]
Personal applet: Dream fate
[PHP code audit] Introduction analysis of vulnerabilities in Pikachu shooting range
攻防世界web区 难度等级:3(ics-05,MFW,easytornado)
BUUCTF n1book [第二章 web進階]文件上傳
phpmyadmin后台文件包含漏洞分析
随机推荐
22. [static member access format] class name:: access member, then the member needs to be static]
PHP 处理csv 文件 解决中文乱码
[intranet penetration] the intranet does not go out of the network, and the machine bounces back to the shell and CS goes online
Accelerate the large-scale application of UAV intelligent inspection, and speed up the intelligent construction of Fujian power transmission operation inspection
Buuctf [gxyctf2019] no dolls
【漏洞复现】CVE-2022-22954 VMware Workspace ONE Access漏洞分析
PHP date() function
BUUCTF [SUCTF 2019]EasySQL
DWVA[SQL-Injection]学习记录
Mysql 主从复制
Dwva [SQL injection] learning record
COMSOL heat conduction method to solve maze problem (path planning)
【逆向分析】恶意代码静态分析
Attack and defense World Web Zone difficulty level: 2 (upload1, web2, web_php_include, supersqli, warmup)
PHP基础语法
PHP的魔术方法
Win9x在Ryzen等新处理器虚拟机抽风的原因及补丁
Thinkphp6 learning experience
[reverse analysis] basic introduction - simple console login
【权限提升】提权exp查找思路与利用方法