2

I am following the tutorial at https://medium.com/@gus_tavo_guim/reentrancy-attack-on-smart-contracts-how-to-identify-the-exploitable-and-an-example-of-an-attack-4470a2d8dfe4 and deploying the following contract:

pragma solidity ^0.4.16;
contract HoneyPot {
  mapping (address => uint) public balances;
  function HoneyPot() payable {
    put();
  }
  function put() payable {
    balances[msg.sender] = msg.value;
  }
  function get() {
    if (!msg.sender.call.value(balances[msg.sender])()) {
      revert();
    }
      balances[msg.sender] = 0;
  }
  function() {
    revert();
  }
}

When this contract receives ether, the HoneyPot() constructor should be executed, which will call the put() function and run the balances[msg.sender] = msg.value; line. However, when I send ether to this contract I get a "bad instruction" error on etherscan (after waiting a couple of minutes and using the ropsten test network). What might the issue be here? as both HoneyPot() and put() functions are payable (as is required when receiving ether).

1 Answer 1

2

HoneyPot() is the constructor of the contract and it is called only once in the contract's lifetime, in particular during the creation of the contract. When you send funds to a contract without specifying a function to call, the fallback function function() is called instead.

Your fallback function contains a revert command, and then it correctly throws an exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.