Guard Vyper State-Changing Functions with nonreentrant
Use Vyper's built-in nonreentrant decorator to revert reentrant calls on guarded external functions, and verify it compiles and reverts in tests.
06 Jul 2025, 22:07 UTC

Problem and takeaway
A reentrancy bug happens when a contract sends ETH or tokens to an external address before updating its own storage, and the recipient contract calls back into the original function while the first call is still active. The second call can read stale balances and withdraw again. Vyper's built-in nonreentrant decorator marks an external function as locked for the duration of its execution. If the same function, or another function sharing the same lock, is entered during an active external call, the transaction reverts instead of proceeding with corrupted state.
Takeaway: apply nonreentrant to external functions that move value before state settles, but treat it as one layer of defense. It only protects decorated functions, does not extend to called contracts, and does not replace updating state before interactions.
Prerequisites
- The Vyper compiler installed and available on
PATH. - A Vyper contract file, for example
vault.vy, with read access for the compiling user. - An EVM execution environment or test framework for running reverting calls.
- Knowledge of which functions in your contract transfer ETH or tokens.
Procedure: decorate a value-transfer function
This example uses a simplified vault where users can withdraw ETH. The state is updated before the external transfer, and the decorator guards the function during that transfer.
1. Write the guarded contract
# @version ^0.4.0
vault: public(HashMap[address, uint256])
@external
@nonreentrant
def withdraw(amount: uint256):
"""
Withdraw ETH from the caller's vault balance.
The nonreentrant lock prevents re-entry while the external
transfer is pending.
"""
balance: uint256 = self.vault[msg.sender]
require balance >= amount, "insufficient balance"
# State update before external interaction
self.vault[msg.sender] = balance - amount
# External call happens after state is settled
send(msg.sender, amount)
Version note: syntax and supported visibilities for nonreentrant can differ between compiler versions. Check the release notes for the exact version you deploy.
2. Compile and confirm the decorator is accepted
Run from the directory containing your contract file:
$ vyper --version
$ vyper vault.vy
Expected check: the compiler exits without syntax errors and emits bytecode and ABI. If nonreentrant is unsupported in your version, compilation fails before deployment. Inspect the ABI to confirm the withdraw function is present; the decorator itself is not a separate ABI entry. Risk: compiling with an outdated or unsupported compiler can silently produce unexpected storage layout, so verify the version before deploying.
3. Test that re-entry reverts
Write a test in your framework of choice that:
- Deposits ETH into the vault.
- Calls
withdrawfrom a test contract whose fallback or receive function attempts to callwithdrawagain. - Asserts the outer transaction reverts.
Do not assert a specific revert reason unless your compiler version documents one; behavior can vary by release.
Expected checks
vyper vault.vycompiles cleanly.- A direct withdrawal succeeds and reduces the caller's recorded balance.
- A reentrant callback to
withdrawduring an active withdrawal reverts. - An unguarded external function in the same contract does not revert on re-entry by default.
Limitations and recovery
nonreentrantprotects only decorated functions. Other external functions in the same contract remain exposed unless also decorated.- The lock is contract-local. It does not prevent reentrancy through a different contract or inherited interface.
- Guarded functions cost extra gas for storage reads and writes compared with unguarded functions. Exact costs depend on compiler version and EVM schedule.
- Placing state updates after an external call inside a guarded function can still expose unguarded entry points.
Practical verification: after deployment, call withdraw through a normal wallet and confirm the balance updates. Then run the reentrancy test against the deployed address on a testnet fork and confirm the callback transaction fails. If a guarded function must be changed, redeploy the contract or use a proxy pattern; there is no runtime toggle to remove the lock.
When not to use it
Avoid relying on nonreentrant as the sole protection for functions that interact with untrusted external contracts. Prefer the checks-effects-interactions ordering and withdrawal patterns where possible, and reserve the decorator for functions where an external call must occur before state fully settles.