vyperlang / titanoboa

a vyper interpreter
https://titanoboa.readthedocs.io
Other
251 stars 49 forks source link

Fix "Memory Leak" when reverting to a Snapshot #214

Closed ritzdorf closed 2 weeks ago

ritzdorf commented 4 months ago

What I did

Currently, when boa.env.anchor() is used to revert to a previous snapshot, the evm state is reverted, but newly created contracts that have been registered since the snapshot has been taken, are not unregistered. Hence, repeated snapshots lead to an ever growing memory consumption. Hence, I added code to unregister contracts that have been deployed since the last snapshot has been taken.

How I did it

I added a list to remember to newly deployed contracts as _new_contracts. The list elements are tuples. They contain:

The _new_contracts list is cleared when a snapshot is taken.

When the state is reverted to a previous snapshot, the newly created contracts are unregistered. Where the bytecode was novel, it is also unregistered.

How to verify it

import boa
import psutil
import os

def get_process_memory():
    process = psutil.Process(os.getpid())
    return process.memory_info().rss / (1024 * 1024)  # Return memory usage in megabytes

novel_src = """

novel_value: public(uint256)
replace_me: public(uint256)

"""
known_src = """

known_value: public(uint256)

"""

known_contract = boa.loads(known_src)
known_contract.eval("self.known_value += 1")

for i in range(500):
    with boa.env.anchor():
        novel_contract = boa.loads(novel_src.replace("replace_me", f"var{i}"))
        novel_contract.novel_value()
        new_known_contract = boa.loads(known_src)
        new_known_contract.known_value()

print(known_contract.known_value())
print(f"Memory Usage: {get_process_memory()} MB")

This script with the current master:

1
Memory Usage: 212.2890625 MB

This script with the new version:

1
Memory Usage: 65.3125 MB

Description for the changelog

Unregistering newly created contracts when reverting to snapshot

Cute Animal Picture

Capybara

ritzdorf commented 4 months ago

Thanks for the feedback. I will try to get it to it. Am a bit busy though.

ritzdorf commented 3 months ago

Hi @DanielSchiavini @charles-cooper, thanks to @trocher we made the journaling changes here. We tested it and so far it works well for us.