Skip to content

Commit

Permalink
fix codegen failure with nonreentrant keys (#2514)
Browse files Browse the repository at this point in the history
* fix codegen failure with nonreentrant keys

codegen now fails (stops compiling) as of eae0eaf. This is because
the storage layout is generated correctly but the type metadata is not
updated after the first function that references a nonreentrancy key.

This commit also adds a codegen test to check codegen of repeated
nonreentrancy keys.

* clarify the test

it works on either nonreentrant function calling the other
  • Loading branch information
charles-cooper authored Oct 26, 2021
1 parent 03642a0 commit 25aa67e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/parser/features/decorators/test_nonreentrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def updated_protected():
interface Callback:
def updated(): nonpayable
def updated_protected(): nonpayable
interface Self:
def protected_function(val: String[100], do_callback: bool) -> uint256: nonpayable
def protected_function2(val: String[100], do_callback: bool) -> uint256: nonpayable
special_value: public(String[100])
callback: public(Callback)
Expand All @@ -42,6 +45,16 @@ def protected_function(val: String[100], do_callback: bool) -> uint256:
else:
return 2
@external
@nonreentrant('protect_special_value')
def protected_function2(val: String[100], do_callback: bool) -> uint256:
self.special_value = val
if do_callback:
# call other function with same nonreentrancy key
Self(self).protected_function(val, False)
return 1
return 2
@external
def unprotected_function(val: String[100], do_callback: bool):
self.special_value = val
Expand All @@ -66,6 +79,11 @@ def unprotected_function(val: String[100], do_callback: bool):

assert_tx_failed(lambda: reentrant_contract.protected_function("zzz value", True, transact={}))

reentrant_contract.protected_function2("another value", False, transact={})
assert reentrant_contract.special_value() == "another value"

assert_tx_failed(lambda: reentrant_contract.protected_function2("zzz value", True, transact={}))


def test_disallow_on_init_function(get_contract):
# nonreentrant has no effect when used on the __init__ fn
Expand Down
8 changes: 6 additions & 2 deletions vyper/semantics/validation/data_positions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO this doesn't really belong in "validation"
import math
from typing import Dict

from vyper import ast as vy_ast
from vyper.semantics.types.bases import StorageSlot
Expand Down Expand Up @@ -28,7 +29,7 @@ def set_storage_slots(vyper_module: vy_ast.Module) -> StorageLayout:
# note storage is word-addressable, not byte-addressable
storage_slot = 0

ret = {}
ret: Dict[str, Dict] = {}

for node in vyper_module.get_children(vy_ast.FunctionDef):
type_ = node._metadata["type"]
Expand All @@ -38,8 +39,11 @@ def set_storage_slots(vyper_module: vy_ast.Module) -> StorageLayout:
variable_name = f"nonreentrant.{type_.nonreentrant}"

# a nonreentrant key can appear many times in a module but it
# only takes one slot. ignore it after the first time we see it.
# only takes one slot. after the first time we see it, do not
# increment the storage slot.
if variable_name in ret:
_slot = ret[variable_name]["slot"]
type_.set_reentrancy_key_position(StorageSlot(_slot))
continue

type_.set_reentrancy_key_position(StorageSlot(storage_slot))
Expand Down

0 comments on commit 25aa67e

Please sign in to comment.