BreakInvariantBounty.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. require(!_claimed);
  42. address researcher = _researchers[target];
  43. require(researcher != address(0));
  44. // Check Target contract invariants
  45. require(!target.checkInvariant());
  46. _asyncTransfer(researcher, address(this).balance);
  47. _claimed = true;
  48. }
  49. /**
  50. * @dev Transfers the current balance to the owner and terminates the contract.
  51. */
  52. function destroy() public onlyOwner {
  53. selfdestruct(owner());
  54. }
  55. /**
  56. * @dev Internal function to deploy the target contract.
  57. * @return A target contract address
  58. */
  59. function _deployContract() internal returns(address);
  60. }
  61. /**
  62. * @title Target
  63. * @dev Your main contract should inherit from this class and implement the checkInvariant method.
  64. */
  65. contract Target {
  66. /**
  67. * @dev Checks all values a contract assumes to be true all the time. If this function returns
  68. * false, the contract is broken in some way and is in an inconsistent state.
  69. * In order to win the bounty, security researchers will try to cause this broken state.
  70. * @return True if all invariant values are correct, false otherwise.
  71. */
  72. function checkInvariant() public returns(bool);
  73. }