Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add env addresses to address strategy. Resolves #208 #222

Merged

Conversation

theodorecurtil
Copy link

What I did

included in address strategy all env contract addresses + eoa

How I did it

How to verify it

Description for the changelog

Cute Animal Picture


# TODO: add addresses generated by the env through the env.generate_address() method
env_accounts = (
{obj.address for obj in boa.env._code_registry.values()}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit weird here to introduce a dependency on the global env. should we maybe make it parametrizable? like if None is provided then we use boa.env, but otherwise a specific env can be given.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like that?

@_exclude_filter
def _address_strategy(length: Optional[int] = 100, environment: Optional[EnvType] = None) -> SearchStrategy:

    if environment is None:
        environment = boa.env

    # TODO: add addresses generated by the env through the env.generate_address() method
    env_accounts = (
            {obj.address for obj in environment._code_registry.values()}
    )

    # generate `length` random addresses
    random_strings = generate_random_string(length)
    random_accounts = {format_addr(i) for i in random_strings}

    return _DeferredStrategyRepr(
        lambda: st.sampled_from(list(env_accounts | random_accounts)), "accounts"
    )

Then would need to modify all tests to pass a specific env

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strategies are only used by pytest right? it would make sense to use the environment that is available in the context of the tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true, but you can set up contracts without using the global env

return _DeferredStrategyRepr(
lambda: st.sampled_from(list(accounts)[:length]), "accounts"
lambda: st.sampled_from(list(env_accounts | random_accounts)), "accounts"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think using st.one_of(st.sampled_from(list(env_accounts), list(random_accounts))) will generate a slightly better distribution

we can also use st.binary() for the random addresses actually

@charles-cooper charles-cooper merged commit 85cefd8 into vyperlang:master Jun 7, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants