Created
June 23, 2016 20:09
-
-
Save christoph2806/858df56093267fe8b2d468421a7f21f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract Safe_Send { | |
bool reentrant = false; | |
event Attack(); | |
event LOG_Send(uint amount, address recipient, bool success); | |
modifier noReentrant { | |
if (reentrant) { | |
Attack(); | |
return; | |
} | |
reentrant = true; | |
_ | |
reentrant = false; | |
} | |
mapping (address => uint) balances; | |
uint totalSupply; | |
function () noReentrant { | |
balances[msg.sender] += msg.value; | |
totalSupply += msg.value; | |
} | |
function payOut(uint amount, address recipient) noReentrant { | |
if (balances[msg.sender] > amount) { | |
uint balance = balances[msg.sender]; | |
balances[msg.sender] -= amount; | |
uint oldSupply = totalSupply; | |
totalSupply -= amount; | |
if(!recipient.send(amount)) { | |
totalSupply = oldSupply; | |
balances[msg.sender] = balance; | |
LOG_Send(amount, recipient, false); | |
return; | |
} | |
LOG_Send(amount, recipient, true); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment