求解!❤️ 目的:以指定的gas调用hack函数
我在以外部300W的gaslimit调用很简单的hack函数后报错,报错的原因很简单是因为不够hack函数指定的gas数量了。但是问题就在这,这么简单地合约怎么可能会在调用hack函数时只剩下63178gas?
经测试发现,如果将调用hack的gas设为能被2整除的数,就是正常(调用hack时剩下200W+gas),但是如果不是被2整除的gas限制就出现一下情况?这是什么原因?要求必须是一个指定的gas(81910+251)。
附源码:
contract GatekeeperOne {
address public entrant;
modifier gateOne() {
require(msg.sender != tx.origin);
_;
}
modifier gateTwo() {
require(gasleft() % (8191) == 0);
_;
}
modifier gateThree(bytes8 _gateKey) {
require(uint32(uint64(_gateKey)) == uint16(uint64(_gateKey)));
require(uint32(uint64(_gateKey)) != uint64(_gateKey));
require(uint32(uint64(_gateKey)) == uint16(tx.origin));
_;
}
function enter(bytes8 _gateKey) public gateOne gateTwo gateThree(_gateKey) returns (bool) {
entrant = tx.origin;
return true;
}
}
contract Hack {
GatekeeperOne gatekeeperOne = GatekeeperOne(0xd9145CCE52D386f254917e481eB44e9943F39138);
bool public res;
bytes8 public key = 0x0000ffff0000ffff;
bytes8 public gateKey = key & bytes8(uint64(tx.origin));
function hack(uint _gas) public {
// res = gatekeeperOne.enter{gas: 81910+251}(gateKey);
(res, ) = address(gatekeeperOne).call{gas: _gas}(abi.encodeWithSignature("enter(bytes8)", gateKey));
if(!res) {
revert("gas abnormal");
}
}
}