Example #1
0
def web3() -> Web3:
    """A py.test fixture to get a Web3 interface to a temporary geth instance.

    This is session scoped fixture.
    Geth is launched only once during the beginning of the test run.

    Geth will have huge instant balance on its coinbase account.
    Geth will also mine our transactions on artificially
    low difficulty level.

    :yield: :py:class:`web3.Web3` instance
    """

    project = Project()

    # Project is configured using populus.config.Config class
    # which is a subclass of Python config parser.
    # Instead of reading .ini file, here we dynamically
    # construct the configuration.
    project.config = Config()

    # Settings come for [populus] section of the config.
    project.config.add_section("populus")

    # Configure where Populus can find our contracts.json
    build_dir = os.path.join(os.getcwd(), "websauna", "wallet", "ethereum")
    project.config.set("populus", "build_dir", build_dir)

    chain_kwargs = {

        # Force RPC provider instead of default IPC one
        "provider": RPCProvider,
        "wait_for_dag_timeout": 20*60,
        "overrides": {
            "jitvm": "false",
        }
    }

    # This returns
    with project.get_chain("temp", **chain_kwargs) as geth_proc:

        web3 = geth_proc.web3

        # Use compatible web3.py version
        assert web3._requestManager.provider.network_timeout

        web3._requestManager.provider.network_timeout = 10
        web3._requestManager.provider.connection_timeout = 10

        # Allow access to sendTransaction() to use coinbase balance
        # to deploy contracts. Password is from py-geth
        # default_blockchain_password file. Assume we don't
        # run tests for more than 9999 seconds
        coinbase = web3.eth.coinbase
        success = web3.personal.unlockAccount(
            coinbase,
            passphrase="this-is-not-a-secure-password",
            duration=9999)

        assert success, "Could not unlock test geth coinbase account"

        yield web3