在研究solidity的过程中有个疑问,都说使用call函数调用合约函数,如果调用失败只会返回false,并不会回滚。这里的失败是指什么?是指被调用合约函数回滚吗?那么主调用合约会跟着回滚吗?如果使用函数名直接调用又会怎样?来做个实验看看。
假设有First、Second两个合约,如下:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.5.0;
contract First {
uint public data = 1;
//Second calledContract;
address _addr;
constructor (address addr) public {
//calledContract = Second(addr);
_addr = addr;
}
function testFunctionCall() public returns(bool){
data = 2;
//calledContract.revertFunc();
bytes4 SELECTOR = bytes4(keccak256(bytes("revertFunc()")));
bytes memory argument = abi.encodeWithSelector(SELECTOR);
(bool success, ) = _addr.call(argument);
return success;
}
}
contract Second {
function revertFunc() external {
revert();
}
}
把Second合约的地址作为参数传递给First的构造函数,然后在First合约中通过call函数调用Second合约中的回滚测试函数revertFunc(),结果显示,First合约中的状态变量data的值变为了2,说明First合约没有回滚。
下面使用函数名直接调用测试一下:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.5.0;
contract First {
uint public data = 1;
Second calledContract;
//address _addr;
constructor (address addr) public {
calledContract = Second(addr);
//_addr = addr;
}
function testFunctionCall() public {
data = 2;
calledContract.revertFunc();
//bytes4 SELECTOR = bytes4(keccak256(bytes("revertFunc()")));
//bytes memory argument = abi.encodeWithSelector(SELECTOR);
//(bool success, ) = _addr.call(argument);
//return success;
}
}
contract Second {
function revertFunc() external {
revert();
}
}
结果显示First合约中的状态变量data的值仍然为1,同时testFunctionCall也执行失败了,这说明First合约状态回滚了。
结论:
使用call函数调用合约函数与直接使用函数名调用在底层实现上应该是不一样的,有的文档中说call函数会重新建立一个调用帧,那么我猜测使用函数名直接调用这样的方式并不会创建一个新的调用帧,所以调用函数也会失败并回滚。
这个实验也解决了我的一个担忧,我总担心某个通过函数名的直接调用失败后,调用合约的状态变量是否会回滚,现在我知道了,应该是回滚的。