BreakInvariantBounty.sol 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. pragma solidity ^0.4.24;
  2. import "../payment/PullPayment.sol";
  3. import "../ownership/Ownable.sol";
  4. /**
  5. * @title BreakInvariantBounty
  6. * @dev This bounty will pay out to a researcher if they break invariant logic of the contract.
  7. */
  8. contract BreakInvariantBounty is PullPayment, Ownable {
  9. bool private _claimed;
  10. mapping(address => address) private _researchers;
  11. event TargetCreated(address createdAddress);
  12. /**
  13. * @dev Fallback function allowing the contract to receive funds, if they haven't already been claimed.
  14. */
  15. function() external payable {
  16. require(!_claimed);
  17. }
  18. /**
  19. * @dev Determine if the bounty was claimed.
  20. * @return true if the bounty was claimed, false otherwise.
  21. */
  22. function claimed() public view returns(bool) {
  23. return _claimed;
  24. }
  25. /**
  26. * @dev Create and deploy the target contract (extension of Target contract), and sets the
  27. * msg.sender as a researcher
  28. * @return A target contract
  29. */
  30. function createTarget() public returns(Target) {
  31. Target target = Target(_deployContract());
  32. _researchers[target] = msg.sender;
  33. emit TargetCreated(target);
  34. return target;
  35. }
  36. /**
  37. * @dev Transfers the contract funds to the researcher that proved the contract is broken.
  38. * @param target contract
  39. */
  40. function claim(Target target) public {
  41. address researcher = _researchers[target];
  42. require(researcher != address(0));
  43. // Check Target contract invariants
  44. require(!target.checkInvariant());
  45. _asyncTransfer(researcher, address(this).balance);
  46. _claimed = true;
  47. }
  48. /**
  49. * @dev Transfers the current balance to the owner and terminates the contract.
  50. */
  51. function destroy() public onlyOwner {
  52. selfdestruct(owner());
  53. }
  54. /**
  55. * @dev Internal function to deploy the target contract.
  56. * @return A target contract address
  57. */
  58. function _deployContract() internal returns(address);
  59. }
  60. /**
  61. * @title Target
  62. * @dev Your main contract should inherit from this class and implement the checkInvariant method.
  63. */
  64. contract Target {
  65. /**
  66. * @dev Checks all values a contract assumes to be true all the time. If this function returns
  67. * false, the contract is broken in some way and is in an inconsistent state.
  68. * In order to win the bounty, security researchers will try to cause this broken state.
  69. * @return True if all invariant values are correct, false otherwise.
  70. */
  71. function checkInvariant() public returns(bool);
  72. }