Add ReentrancyGuard
This commit is contained in:
11
test/helpers/ReentrancyAttack.sol
Normal file
11
test/helpers/ReentrancyAttack.sol
Normal file
@ -0,0 +1,11 @@
|
||||
pragma solidity ^0.4.8;
|
||||
|
||||
contract ReentrancyAttack {
|
||||
|
||||
function callSender(bytes4 data) {
|
||||
if(!msg.sender.call(data)) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
46
test/helpers/ReentrancyMock.sol
Normal file
46
test/helpers/ReentrancyMock.sol
Normal file
@ -0,0 +1,46 @@
|
||||
pragma solidity ^0.4.8;
|
||||
|
||||
import '../../contracts/ReentrancyGuard.sol';
|
||||
import './ReentrancyAttack.sol';
|
||||
|
||||
contract ReentrancyMock is ReentrancyGuard {
|
||||
|
||||
uint256 public counter;
|
||||
|
||||
function ReentrancyMock() {
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
function count() private {
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
function countLocalRecursive(uint n) public nonReentrant {
|
||||
if(n > 0) {
|
||||
count();
|
||||
countLocalRecursive(n - 1);
|
||||
}
|
||||
}
|
||||
|
||||
function countThisRecursive(uint256 n) public nonReentrant {
|
||||
bytes4 func = bytes4(keccak256("countThisRecursive(uint256)"));
|
||||
if(n > 0) {
|
||||
count();
|
||||
bool result = this.call(func, n - 1);
|
||||
if(result != true) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function countAndCall(ReentrancyAttack attacker) public nonReentrant {
|
||||
count();
|
||||
bytes4 func = bytes4(keccak256("callback()"));
|
||||
attacker.callSender(func);
|
||||
}
|
||||
|
||||
function callback() external nonReentrant {
|
||||
count();
|
||||
}
|
||||
|
||||
}
|
||||
20
test/helpers/expectThrow.js
Normal file
20
test/helpers/expectThrow.js
Normal file
@ -0,0 +1,20 @@
|
||||
export default async promise => {
|
||||
try {
|
||||
await promise;
|
||||
} catch (error) {
|
||||
// TODO: Check jump destination to destinguish between a throw
|
||||
// and an actual invalid jump.
|
||||
const invalidJump = error.message.search('invalid JUMP') >= 0;
|
||||
// TODO: When we contract A calls contract B, and B throws, instead
|
||||
// of an 'invalid jump', we get an 'out of gas' error. How do
|
||||
// we distinguish this from an actual out of gas event? (The
|
||||
// testrpc log actually show an 'invalid jump' event.)
|
||||
const outOfGas = error.message.search('out of gas') >= 0;
|
||||
assert(
|
||||
invalidJump || outOfGas,
|
||||
"Expected throw, got '" + error + "' instead",
|
||||
);
|
||||
return;
|
||||
}
|
||||
assert.fail('Expected throw not received');
|
||||
};
|
||||
Reference in New Issue
Block a user