Skip to content

Commit

Permalink
Recursion, review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Sep 11, 2024
1 parent 2ce4436 commit 9a0cd05
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ source venv/bin/activate
# Install dev requirements
pip install -r dev-requirements.txt
# Install prod requirements (in the pyproject.tom)
pip install .
pip install .
```

*Note: When you delete your terminal/shell, you will need to reactivate this virtual environment again each time. To exit this python virtual environment, type `deactivate`*
Expand Down
30 changes: 17 additions & 13 deletions boa/contracts/vvm/vvm_contract.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from functools import cached_property
from pathlib import Path

Expand Down Expand Up @@ -171,19 +172,20 @@ def _override_bytecode(self) -> bytes:
return to_bytes(compiled["<stdin>"]["bytecode_runtime"])

@property
def source_code(self):
raise NotImplementedError # to be implemented in subclasses
def source_code(self) -> str:
"""
Returns the source code an internal function.
Must be implemented in subclasses.
"""
raise NotImplementedError

def __call__(self, *args, **kwargs):
env = self.contract.env
assert isinstance(self.contract, VVMContract) # help mypy
balance_before = env.get_balance(env.eoa)
env.set_code(self.contract.address, self._override_bytecode)
env.set_balance(env.eoa, 10**20)
try:
return super().__call__(*args, **kwargs)
finally:
env.set_balance(env.eoa, balance_before)
env.set_code(self.contract.address, self.contract.bytecode_runtime)


Expand Down Expand Up @@ -245,19 +247,21 @@ class VVMStorageVariable(_VVMInternal):
"""

def __init__(self, name, spec, contract):
value_type = spec["type"]
inputs = []
regex = re.compile(r"^HashMap\[([^[]+), (.+)]$")

while value_type.startswith("HashMap"):
key_type, value_type = regex.match(value_type).groups()
inputs.append({"name": f"key{len(inputs)}", "type": key_type})

abi = {
"anonymous": False,
"inputs": [],
"outputs": [{"name": name, "type": spec["type"]}],
"inputs": inputs,
"outputs": [{"name": name, "type": value_type}],
"name": name,
"type": "function",
}

if spec["type"].startswith("HashMap"):
key_type, value_type = spec["type"][8:-1].split(",")
abi["inputs"] = [{"name": "key", "type": key_type}]
abi["outputs"] = [{"name": "value", "type": value_type.strip()}]

super().__init__(abi, contract.contract_name)
self.contract = contract

Expand Down
4 changes: 2 additions & 2 deletions tests/unitary/contracts/vvm/mock_3_10.vy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
foo: public(uint256)
bar: public(uint256)

map: HashMap[address, uint256]
map: HashMap[address, HashMap[uint8, uint256]]
is_empty: bool

@external
Expand All @@ -18,5 +18,5 @@ def set_map(x: uint256):

@internal
def _set_map(addr: address, x: uint256):
self.map[addr] = x
self.map[addr][0] = x
self.is_empty = False
6 changes: 3 additions & 3 deletions tests/unitary/contracts/vvm/test_vvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ def test_loads_vvm():
def test_vvm_storage():
contract = boa.loads(mock_3_10_code, 43)
assert contract._storage.is_empty.get()
assert contract._storage.map.get(boa.env.eoa) == 0
assert contract._storage.map.get(boa.env.eoa, 0) == 0
contract.set_map(69)
assert not contract._storage.is_empty.get()
assert contract._storage.map.get(boa.env.eoa) == 69
assert contract._storage.map.get(boa.env.eoa, 0) == 69


def test_vvm_internal():
contract = boa.loads(mock_3_10_code, 43)
assert not hasattr(contract.internal, "set_map")
address = boa.env.generate_address()
contract.internal._set_map(address, 69)
assert contract._storage.map.get(address) == 69
assert contract._storage.map.get(address, 0) == 69


def test_vvm_eval():
Expand Down

0 comments on commit 9a0cd05

Please sign in to comment.