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 GethMiner snake case methods #1579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 37 additions & 10 deletions docs/web3.miner.rst
Expand Up @@ -13,18 +13,24 @@ Methods
The following methods are available on the ``web3.geth.miner`` namespace.


.. py:method:: GethMiner.makeDAG(number)
.. py:method:: GethMiner.make_dag(number)

* Delegates to ``miner_makeDag`` RPC Method

Generate the DAG for the given block number.

.. code-block:: python

>>> web3.geth.miner.makeDag(10000)
>>> web3.geth.miner.make_dag(10000)


.. py:method:: GethMiner.setExtra(extra)
.. py:method:: GethMiner.makeDAG(number)

.. warning:: Deprecated: This method is deprecated in favor of
:meth:`~GethMiner.make_dag`


.. py:method:: GethMiner.set_extra(extra)

* Delegates to ``miner_setExtra`` RPC Method

Expand All @@ -33,10 +39,15 @@ The following methods are available on the ``web3.geth.miner`` namespace.

.. code-block:: python

>>> web3.geth.miner.setExtra('abcdefghijklmnopqrstuvwxyzABCDEF')
>>> web3.geth.miner.set_extra('abcdefghijklmnopqrstuvwxyzABCDEF')

.. py:method:: GethMiner.setExtra(extra)

.. warning:: Deprecated: This method is deprecated in favor of
:meth:`~GethMiner.set_extra`

.. py:method:: GethMiner.setGasPrice(gas_price)

.. py:method:: GethMiner.set_gas_price(gas_price)

* Delegates to ``miner_setGasPrice`` RPC Method

Expand All @@ -46,7 +57,13 @@ The following methods are available on the ``web3.geth.miner`` namespace.

.. code-block:: python

>>> web3.geth.miner.setGasPrice(19999999999)
>>> web3.geth.miner.set_gas_price(19999999999)


.. py:method:: GethMiner.setGasPrice(gas_price)

.. warning:: Deprecated: This method is deprecated in favor of
:meth:`~GethMiner.set_gas_price`


.. py:method:: GethMiner.start(num_threads)
Expand All @@ -71,23 +88,33 @@ The following methods are available on the ``web3.geth.miner`` namespace.
>>> web3.geth.miner.stop()


.. py:method:: GethMiner.startAutoDAG()
.. py:method:: GethMiner.start_auto_dag()

* Delegates to ``miner_startAutoDag`` RPC Method

Enable automatic DAG generation.

.. code-block:: python

>>> web3.geth.miner.startAutoDAG()
>>> web3.geth.miner.start_auto_dag()

.. py:method:: GethMiner.startAutoDag()

.. py:method:: GethMiner.stopAutoDAG()
.. warning:: Deprecated: This method is deprecated in favor of
:meth:`~GethMiner.start_auto_dag`


.. py:method:: GethMiner.stop_auto_dag()

* Delegates to ``miner_stopAutoDag`` RPC Method

Disable automatic DAG generation.

.. code-block:: python

>>> web3.geth.miner.stopAutoDAG()
>>> web3.geth.miner.stop_auto_dag()

.. py:method:: GethMiner.stopAutoDag()

.. warning:: Deprecated: This method is deprecated in favor of
:meth:`~GethMiner.stop_auto_dag`
1 change: 1 addition & 0 deletions newsfragments/1579.feature.rst
@@ -0,0 +1 @@
Add snake_case methods to Geth Miner class, deprecate camelCase methods
27 changes: 26 additions & 1 deletion tests/core/mining-module/test_miner_setExtra.py
@@ -1,3 +1,4 @@
import pytest
import random

from eth_utils import (
Expand All @@ -13,7 +14,7 @@


@flaky(max_runs=3)
def test_miner_setExtra(web3_empty, wait_for_block):
def test_miner_set_extra(web3_empty, wait_for_block):
web3 = web3_empty

initial_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
Expand All @@ -35,3 +36,27 @@ def test_miner_setExtra(web3_empty, wait_for_block):
after_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])

assert after_extra == new_extra_data


@flaky(max_runs=3)
def test_miner_setExtra(web3_empty, wait_for_block):
web3 = web3_empty

initial_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])

new_extra_data = b'-this-is-32-bytes-of-extra-data-'

# sanity
assert initial_extra != new_extra_data

with pytest.warns(DeprecationWarning):
web3.geth.miner.setExtra(new_extra_data)
with Timeout(60) as timeout:
while True:
extra_data = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
if extra_data == new_extra_data:
break
timeout.sleep(random.random())

after_extra = decode_hex(web3.eth.getBlock(web3.eth.blockNumber)['extraData'])
assert after_extra == new_extra_data
25 changes: 23 additions & 2 deletions tests/core/mining-module/test_miner_setGasPrice.py
@@ -1,3 +1,4 @@
import pytest
import random

from flaky import (
Expand All @@ -10,15 +11,35 @@


@flaky(max_runs=3)
def test_miner_setGasPrice(web3_empty, wait_for_block):
def test_miner_set_gas_price(web3_empty, wait_for_block):
web3 = web3_empty

initial_gas_price = web3.eth.gasPrice

# sanity check
assert web3.eth.gasPrice > 1000

web3.geth.miner.setGasPrice(initial_gas_price // 2)
web3.geth.miner.set_gas_price(initial_gas_price // 2)

with Timeout(60) as timeout:
while web3.eth.gasPrice == initial_gas_price:
kclowes marked this conversation as resolved.
Show resolved Hide resolved
timeout.sleep(random.random())

after_gas_price = web3.eth.gasPrice
assert after_gas_price < initial_gas_price


@flaky(max_runs=3)
def test_miner_setGasPrice(web3_empty, wait_for_block):
web3 = web3_empty

initial_gas_price = web3.eth.gasPrice
assert web3.eth.gasPrice > 1000

# sanity check

with pytest.warns(DeprecationWarning):
web3.geth.miner.setGasPrice(initial_gas_price // 2)

with Timeout(60) as timeout:
while web3.eth.gasPrice == initial_gas_price:
Expand Down
13 changes: 12 additions & 1 deletion tests/core/mining-module/test_setEtherBase.py
@@ -1,7 +1,18 @@
import pytest


def test_miner_set_etherbase(web3_empty):
web3 = web3_empty
assert web3.eth.coinbase == web3.eth.accounts[0]
new_account = web3.personal.newAccount('this-is-a-password')
web3.geth.miner.set_etherbase(new_account)
assert web3.eth.coinbase == new_account


def test_miner_setEtherbase(web3_empty):
web3 = web3_empty
assert web3.eth.coinbase == web3.eth.accounts[0]
new_account = web3.personal.newAccount('this-is-a-password')
web3.geth.miner.setEtherBase(new_account)
with pytest.warns(DeprecationWarning):
web3.geth.miner.setEtherbase(new_account)
assert web3.eth.coinbase == new_account
24 changes: 18 additions & 6 deletions web3/_utils/miner.py
Expand Up @@ -10,6 +10,7 @@
RPC,
)
from web3.method import (
DeprecatedMethod,
Method,
default_root_munger,
)
Expand All @@ -18,25 +19,25 @@
Wei,
)

makeDag: Method[Callable[[BlockNumber], bool]] = Method(
make_dag: Method[Callable[[BlockNumber], bool]] = Method(
RPC.miner_makeDag,
mungers=[default_root_munger],
)


setExtra: Method[Callable[[str], bool]] = Method(
set_extra: Method[Callable[[str], bool]] = Method(
RPC.miner_setExtra,
mungers=[default_root_munger],
)


setEtherbase: Method[Callable[[ChecksumAddress], bool]] = Method(
set_etherbase: Method[Callable[[ChecksumAddress], bool]] = Method(
RPC.miner_setEtherbase,
mungers=[default_root_munger],
)


setGasPrice: Method[Callable[[Wei], bool]] = Method(
set_gas_price: Method[Callable[[Wei], bool]] = Method(
RPC.miner_setGasPrice,
mungers=[default_root_munger],
)
Expand All @@ -54,13 +55,24 @@
)


startAutoDag: Method[Callable[[], bool]] = Method(
start_auto_dag: Method[Callable[[], bool]] = Method(
RPC.miner_startAutoDag,
mungers=None,
)


stopAutoDag: Method[Callable[[], bool]] = Method(
stop_auto_dag: Method[Callable[[], bool]] = Method(
RPC.miner_stopAutoDag,
mungers=None,
)


#
# Deprecated Methods
#
makeDag = DeprecatedMethod(make_dag, 'makeDag', 'make_dag')
setExtra = DeprecatedMethod(set_extra, 'setExtra', 'set_extra')
setEtherbase = DeprecatedMethod(set_etherbase, 'setEtherbase', 'set_etherbase')
setGasPrice = DeprecatedMethod(set_gas_price, 'setGasPrice', 'set_gas_price')
startAutoDag = DeprecatedMethod(start_auto_dag, 'startAutoDag', 'start_auto_dag')
stopAutoDag = DeprecatedMethod(stop_auto_dag, 'stopAutoDag', 'stop_auto_dag')
17 changes: 15 additions & 2 deletions web3/geth.py
Expand Up @@ -18,13 +18,19 @@
stopWS,
)
from web3._utils.miner import (
make_dag,
makeDag,
set_etherbase,
set_extra,
set_gas_price,
setEtherbase,
setExtra,
setGasPrice,
start,
start_auto_dag,
startAutoDag,
stop,
stop_auto_dag,
stopAutoDag,
)
from web3._utils.personal import (
Expand Down Expand Up @@ -98,12 +104,19 @@ class GethMiner(ModuleV2):
"""
https://github.com/ethereum/go-ethereum/wiki/Management-APIs#miner
"""
make_dag = make_dag
set_extra = set_extra
set_etherbase = set_etherbase
set_gas_price = set_gas_price
start = start
stop = stop
start_auto_dag = start_auto_dag
stop_auto_dag = stop_auto_dag
# deprecated
makeDag = makeDag
setExtra = setExtra
setEtherbase = setEtherbase
setGasPrice = setGasPrice
start = start
stop = stop
startAutoDag = startAutoDag
stopAutoDag = stopAutoDag

Expand Down
10 changes: 8 additions & 2 deletions web3/providers/eth_tester/defaults.py
Expand Up @@ -383,12 +383,18 @@ def personal_send_transaction(eth_tester: "EthereumTester", params: Any) -> HexS
'writeMemProfile': not_implemented,
},
'miner': {
'make_dag': not_implemented,
'set_extra': not_implemented,
'set_gas_price': not_implemented,
'start': not_implemented,
'stop': not_implemented,
'start_auto_dag': not_implemented,
'stop_auto_dag': not_implemented,
# deprecated
'makeDAG': not_implemented,
'setExtra': not_implemented,
'setGasPrice': not_implemented,
'start': not_implemented,
'startAutoDAG': not_implemented,
'stop': not_implemented,
'stopAutoDAG': not_implemented,
},
'personal': {
Expand Down