Skip to content

Commit

Permalink
make generate_address() generate more realistic addresses
Browse files Browse the repository at this point in the history
use pseudorandom addresses instead of counting from 100. makes tests
more robust against address sets which don't have enough entropy
  • Loading branch information
charles-cooper committed Oct 10, 2023
1 parent 15eb6d3 commit 8485787
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
11 changes: 6 additions & 5 deletions boa/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import contextlib
import logging
import random
import sys
import warnings
from typing import Any, Iterator, Optional, Union
Expand Down Expand Up @@ -258,16 +259,14 @@ def __call__(self, computation):
# wrapper class around py-evm which provides a "contract-centric" API
class Env:
_singleton = None
_initial_address_counter = 100
_random = random.Random("titanoboa") # something reproducible
_coverage_enabled = False

def __init__(self):
self.chain = _make_chain()

self._gas_price = None

self._address_counter = self.__class__._initial_address_counter

self._aliases = {}

# TODO differentiate between origin and sender
Expand All @@ -286,6 +285,9 @@ def __init__(self):

self._init_vm()

def set_random_seed(self, seed=None):
self._random = random.Random(seed)

def get_gas_price(self):
return self._gas_price or 0

Expand Down Expand Up @@ -460,8 +462,7 @@ def get_singleton(cls):
return cls._singleton

def generate_address(self, alias: Optional[str] = None) -> AddressType:
self._address_counter += 1
t = self._address_counter.to_bytes(length=20, byteorder="big")
t = self._random.randbytes(20)
# checksum addr easier for humans to debug
ret = to_checksum_address(t)
if alias is not None:
Expand Down
1 change: 1 addition & 0 deletions boa/vm/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, url: str, cache_file: str = DEFAULT_CACHE_DIR):
# LevelDB has been removed from py-evm 0.8.1 onwards
from eth.db.backends.level import LevelDB
from eth.db.cache import CacheDB

cache_file = os.path.expanduser(cache_file)
# use CacheDB as an additional layer over disk
# (ideally would use leveldb lru cache but it's not configurable
Expand Down
17 changes: 16 additions & 1 deletion docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,22 @@ Low-Level Functionality
>>> import boa
>>> boa.env.generate_address()
'0x0000000000000000000000000000000000000066'
'0xd13f0Bd22AFF8176761AEFBfC052a7490bDe268E'
.. method:: set_random_seed(seed: Any = None) -> None

Set the random seed used by this ``Env`` to generate addresses. Useful in case you want to introduce some more randomization to how ``Env`` generates addresses.

:param seed: The seed to pass to this ``Env``'s instance of ``random.Random``. Can be any value that ``random.Random()`` accepts.

.. rubric:: Example

.. code-block:: python
>>> import boa
>>> boa.env.set_random_seed(100)
>>> boa.env.generate_address()
'0x93944a25b3ADa3759918767471C5A3F3601652c5
.. method:: set_balance(address: str, value: int)

Expand Down

0 comments on commit 8485787

Please sign in to comment.