Came across this little confusing situation of underscores in modifier so I want to post it here to check if my understanding is correct.
contract whatever{
bool public locked;
uint public x = 10;
modifier noReentrancy() {
require(!locked, "No reentrancy");
locked = true;
_;
locked = false;
}
function decrement(uint i) public noReentrancy {
x -= i;
if (i > 1) {
decrement(i - 1);
}
}
}
the underscore in the middle of the modifier function is little trick to me. so my understanding is: when modifier is first ran, the value of locked is true. then the underscore means run the function code which is x-= i ... this will trigger the if condition and if i >i then the same function will be called again while being executed (the point of noReentry modifier) then the value of locked will be false which will trigger the function decrement() to stop. the value of x will be 10. so unless the value of i is 1 or 0 (so the if condition is not triggered), the value of x will always be 10 in this case?
thank you for your patience!