I am writing a basic contract to split ethereum evenly between two addresses.
Within my withdraw()
function, I have the following code:
function withdraw() {
uint256 amount = this.balance / 2;
addressOne.transfer(amount);
addressTwo.transfer(amount);
}
The problem is, having subsequent transfers like this enables recursive attacks. The owner of addressOne
could call the function and overflow the stack right before the other half of the balance is sent to addressTwo
.
Executed repeatedly, the owner of addressOne
could drain the contract according to the series (assuming an even balance, i.e. no remainder):
My proposed solution then is the following:
if(!addressOne.send(amount)) throw;
if(!addressTwo.send(amount)) throw;
But would throwing like this prevent the same attack?
truffle
is a pretty good tool to write and test contracts.