当前位置:网站首页>关于solidity的delegatecall的坑
关于solidity的delegatecall的坑
2022-06-22 06:28:00 【youngqqcn】
今天遇到一个问题,记录这个坑
pragma solidity ^0.4.26;
contract Proxy {
address internal masterCopy;
constructor(address _masterCopy)
public
{
require(_masterCopy != address(0), "Invalid master copy address provided");
masterCopy = _masterCopy;
}
/// @dev Fallback function forwards all transactions and returns all received return data.
function ()
external
payable
{
// solium-disable-next-line security/no-inline-assembly
assembly {
let masterCopy := and(sload(0), 0xffffffffffffffffffffffffffffffffffffffff)
// 0xa619486e == keccak("masterCopy()"). The value is right padded to 32-bytes with 0s
if eq(calldataload(0), 0xa619486e00000000000000000000000000000000000000000000000000000000) {
mstore(0, masterCopy)
return(0, 0x20)
}
calldatacopy(0, 0, calldatasize())
let success := delegatecall(gas, masterCopy, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
if eq(success, 0) { revert(0, returndatasize()) }
return(0, returndatasize())
}
}
}
contract Erc20 {
address public sender;
event DoSomething(address);
function dosomthing() public returns(bool){
sender = msg.sender;
emit DoSomething(msg.sender);
return true;
}
}
contract Hacker {
event Ok(address,bytes,uint256);
event Failed(bool);
function setErc20Addr(address addr) public returns(bool) {
erc20 = Erc20(addr);
}
Erc20 public erc20 ;
function exec(address addr, bytes data, uint256 amount) public payable returns(bool){
bool success = erc20.dosomthing();
if(success) {
emit Ok(addr, data, amount);
return true;
} else {
emit Failed(false);
return false;
}
}
}
调用setErc20Addr之后,直接调用Hacker则成功,但是通过调用proxy的fallback 函数总是失败。
remix中报错如下
到底是什么原因呢?
原因是delegatecall的机制: https://solidity-cn.readthedocs.io/zh/develop/introduction-to-smart-contracts.html?highlight=delegatecall#index-13

也就是说, bool success = erc20.dosomthing(); 这一句,erc20是Hacker的成员,当在Proxy的fallback调用起Hacker的exec时,仅仅把exec函数代码拿来用来,并没有把Hacker的erc20一起复制过去,所以,erc20是空的!
修改之后:
contract Hacker {
event Ok(address,bytes,uint256);
event Failed(bool);
function exec(address addr, bytes data, uint256 amount) public payable returns(bool){
Erc20 erc20 = Erc20(addr);
bool success = erc20.dosomthing();
if(success) {
emit Ok(addr, data, amount);
return true;
} else {
emit Failed(false);
return false;
}
}
}
也就是说,erc20合约地址由Proxy的fallback调用时传进来,再初始化erc20。现在就可以正常调用了。
边栏推荐
- Using Monte Carlo method to calculate pi
- Linux link sqlserver, offline installation
- [5g NR] RRC connection reconstruction analysis
- Clickhouse compares data from two machines
- CGIC文件上传----菜鸟笔记
- Laravel excel 3.1 column width setting does not work
- 常用的辅助类—(重点)
- ForkJoinPool
- 【M32】单片机 svn 设置忽略文件
- Dongjiao home development technical service
猜你喜欢
![[php]tp6 cli mode to create tp6 and multi application configurations and common problems](/img/19/0a3319b04fe6449c90ade6f27fca4a.png)
[php]tp6 cli mode to create tp6 and multi application configurations and common problems

Modeling and Simulation of Radar Seeker Servo System

Expert system clips from introduction to mastery (I) introduction and overview of clips

四大函数式接口(必需掌握)

BlockingQueue four sets of APIs

Surfer grid file clipping

Class load memory analysis

Current harmonic suppression strategy of grid connected inverter with PIR controller regulator

Logback custom pattern parameter resolution

New GDI functions and functions introduced in MiniGUl version 1.1.0 (II)
随机推荐
Discrete PID control based on MATLAB
5g-guti detailed explanation
Oracle之trim,ltrim,rtrim三个函数的用法
【5G NR】RRC连接重建解析
博客添加邮箱私信 快捷
东郊到家开发技术服务
Detailed interpretation of tab[i = (n - 1) & hash]
On the matrix order of MNIST linear model
Machine learning concept sorting (no formula)
用蒙特卡洛法求圆周率pi
MySQL-IFNULL处理N/A
BlockingQueue four sets of APIs
Geoswath plus technology and data acquisition and processing
C skill tree evaluation - customer first, making excellent products
Class load memory analysis
ForkJoinPool
[PHP]TP6 CLI模式下创建tp6和多应用配置以及常见问题
Performance comparison and analysis
Current harmonic suppression strategy of grid connected inverter with PIR controller regulator
C技能树评测——用户至上做精品