How Do State Variables Protect against Unexpected External Calls?
State variables protect against unexpected external calls primarily through the use of a "reentrancy guard" or a "lock" variable. This is a boolean state variable, initially set to false.
At the beginning of a critical function, the guard is set to true, and a check ensures it is not already true. At the end of the function, it is reset to false.
If an external call attempts to re-enter the function before the first execution completes, the check will fail, and the transaction will revert, effectively blocking the reentrancy.