Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vyperlang/titanoboa into vyz
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Oct 22, 2024
2 parents 0f44256 + 2badb06 commit 69e24c2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 23 deletions.
23 changes: 9 additions & 14 deletions boa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,20 @@

@contextlib.contextmanager
def swap_env(new_env):
old_env = env
try:
set_env(new_env)
with set_env(new_env):
yield
finally:
set_env(old_env)


def set_env(new_env):
def _set_env(new):
global env
env = new_env
env = new
Env._singleton = new

Env._singleton = new_env


def _env_mgr(new_env):
def set_env(new_env):
global env
get_env = lambda: env # noqa: E731
return Open(get_env, set_env, new_env)
return Open(get_env, _set_env, new_env)


def fork(
Expand All @@ -69,20 +64,20 @@ def fork(

new_env = Env()
new_env.fork(url=url, block_identifier=block_identifier, deprecated=False, **kwargs)
return _env_mgr(new_env)
return set_env(new_env)


def set_browser_env(address=None):
"""Set the environment to use the browser's network in Jupyter/Colab"""
# import locally because jupyter is generally not installed
from boa.integrations.jupyter import BrowserEnv

return _env_mgr(BrowserEnv(address))
return set_env(BrowserEnv(address))


def set_network_env(url):
"""Set the environment to use a custom network URL"""
return _env_mgr(NetworkEnv.from_url(url))
return set_env(NetworkEnv.from_url(url))


def set_etherscan(*args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions boa/contracts/vvm/vvm_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def constructor(self):
return ABIFunction(t, contract_name=self.filename)
return None

def deploy(self, *args, env=None):
def deploy(self, *args, env=None, **kwargs):
encoded_args = b""
if self.constructor is not None:
encoded_args = self.constructor.prepare_calldata(*args)
Expand All @@ -64,7 +64,7 @@ def deploy(self, *args, env=None):
if env is None:
env = Env.get_singleton()

address, _ = env.deploy_code(bytecode=self.bytecode + encoded_args)
address, _ = env.deploy_code(bytecode=self.bytecode + encoded_args, **kwargs)

return self.at(address)

Expand Down
6 changes: 3 additions & 3 deletions boa/contracts/vyper/vyper_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ def stomp(self, address: Any, data_section=None) -> "VyperContract":
address = Address(address)

ret = self.deploy(override_address=address, skip_initcode=True)
vm = ret.env.vm
old_bytecode = vm.state.get_code(address.canonical_address)
vm = ret.env.evm
old_bytecode = vm.get_code(address)
new_bytecode = self.compiler_data.bytecode_runtime

immutables_size = self.compiler_data.global_ctx.immutable_section_bytes
if immutables_size > 0:
data_section = old_bytecode[-immutables_size:]
new_bytecode += data_section

vm.state.set_code(address.canonical_address, new_bytecode)
vm.set_code(address, new_bytecode)
ret.env.register_contract(address, ret)
ret._set_bytecode(new_bytecode)
return ret
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "titanoboa"
version = "0.2.3"
version = "0.2.4"
description = "A Vyper interpreter"
#authors = []
license = { file = "LICENSE" }
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/network/sepolia/test_sepolia_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_verify(simple_contract):
api_key = os.getenv("BLOCKSCOUT_API_KEY")
blockscout = Blockscout("https://eth-sepolia.blockscout.com", api_key)
with boa.set_verifier(blockscout):
result = boa.verify(simple_contract, blockscout)
result = boa.verify(simple_contract)
result.wait_for_verification()
assert result.is_verified()

Expand Down
13 changes: 13 additions & 0 deletions tests/unitary/contracts/vvm/test_vvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ def test_loads_vvm():

assert contract.foo() == 42
assert contract.bar() == 43


def test_forward_args_on_deploy():
with open(mock_3_10_path) as f:
code = f.read()

contract_vvm_deployer = boa.loads_partial(code)

random_addy = boa.env.generate_address()

contract = contract_vvm_deployer.deploy(43, override_address=random_addy)

assert random_addy == contract.address
51 changes: 51 additions & 0 deletions tests/unitary/contracts/vyper/test_vyper_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,54 @@ def foo() -> bool:
c = boa.loads(code)

c.foo()


def test_stomp():
code1 = """
VAR: immutable(uint256)
@deploy
def __init__():
VAR = 12345
@external
def foo() -> uint256:
return VAR
@external
def bar() -> bool:
return True
"""
code2 = """
VAR: immutable(uint256)
@deploy
def __init__():
VAR = 12345
@external
def foo() -> uint256:
return VAR
@external
def bar() -> bool:
return False
"""

deployer = boa.loads_partial(code1)

c = deployer.deploy()

assert c.foo() == 12345
assert c.bar() is True

deployer2 = boa.loads_partial(code2)

c2 = deployer2.stomp(c.address)

assert c2.foo() == 12345
assert c2.bar() is False

# the bytecode at the original contract has been stomped :scream:
assert c.foo() == 12345
assert c.bar() is False
4 changes: 2 additions & 2 deletions tests/unitary/test_boa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def test_env_mgr_noctx():
s = boa.env
t = boa.Env()
boa._env_mgr(t)
boa.set_env(t)
assert boa.env is not s
assert boa.env is t

Expand All @@ -13,7 +13,7 @@ def test_env_mgr_with_ctx():
s = boa.env
t = boa.Env()

with boa._env_mgr(t):
with boa.set_env(t):
assert boa.env is not s
assert boa.env is t

Expand Down

0 comments on commit 69e24c2

Please sign in to comment.