I'm concerned about the security of the Reentrancy pattern that we can use in Solidity to prevent from calling multiple times a same function at the same time. This protection is nicely implemented here: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ReentrancyGuard.sol. But for my question, I did a smaller version merging the code of the modifier inside the function:
contract Contract1 {
bool locked;
function f() external {
require(!locked, "Reentrant call");
locked = true;
// Do something
locked = false;
}
}
According to my understanding, in Solidity, on the Ethereum Virtual Machine, it is impossible to execute more than 1 instruction at a time, each instruction is executed sequentially, not in parallel. That's why before to execute locked = true
, first require(!locked, "Reentrant call")
have to be executed (and valid).
But would there be a possibility for 2 concurrent requests (let's say from user A and user B, or even from the same user) on f()
to sequentially execute those instructions in this order:
require(!locked, "Reentrant call")
require(!locked, "Reentrant call")
(which would also be valid at this stage)locked = true
locked = true
If so, // Do something
would be executed twice.