From 366a572d5eef33c09350ea11187f69ed8340effa Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Thu, 14 Mar 2019 12:52:10 +0100 Subject: [PATCH 1/2] Implement eth_submitHashrate and integration test --- docs/web3.eth.rst | 11 +++++++++++ tests/integration/test_ethereum_tester.py | 1 + web3/_utils/module_testing/eth_module.py | 6 ++++++ web3/_utils/rpc_abi.py | 1 + web3/eth.py | 5 +++++ web3/middleware/pythonic.py | 1 + 6 files changed, 25 insertions(+) diff --git a/docs/web3.eth.rst b/docs/web3.eth.rst index 39619395fe..603c779f4a 100644 --- a/docs/web3.eth.rst +++ b/docs/web3.eth.rst @@ -847,6 +847,17 @@ with the filtering API. :meth:`~Eth.filter` for details on allowed filter parameters. +.. py:method:: Eth.submitHashrate(hashrate, nodeid) + + * Delegates to ``eth_submitHashrate`` RPC Method. + + .. code-block:: python + + >>> node_id = '59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c' + >>> web3.eth.submitHashrate(5000, node_id) + True + + Contracts --------- diff --git a/tests/integration/test_ethereum_tester.py b/tests/integration/test_ethereum_tester.py index b6a529b2d6..4c1da5fc26 100644 --- a/tests/integration/test_ethereum_tester.py +++ b/tests/integration/test_ethereum_tester.py @@ -216,6 +216,7 @@ def func_wrapper(self, eth_tester, *args, **kwargs): class TestEthereumTesterEthModule(EthModuleTest): test_eth_sign = not_implemented(EthModuleTest.test_eth_sign, ValueError) + test_eth_submitHashrate = not_implemented(EthModuleTest.test_eth_submitHashrate, ValueError) test_eth_signTransaction = not_implemented(EthModuleTest.test_eth_signTransaction, ValueError) @disable_auto_mine diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index d5a76a6870..dd5b716614 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -832,3 +832,9 @@ def test_eth_getTransactionFromBlock_deprecation(self, web3, block_with_txn): def test_eth_getCompilers_deprecation(self, web3): with pytest.raises(DeprecationWarning): web3.eth.getCompilers() + + def test_eth_submitHashrate(self, web3): + # node_id from EIP 1474: https://github.com/ethereum/EIPs/pull/1474/files + node_id = '59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c' + result = web3.eth.submitHashrate(5000, node_id) + assert result is True diff --git a/web3/_utils/rpc_abi.py b/web3/_utils/rpc_abi.py index 17fc06c678..97fc53bb86 100644 --- a/web3/_utils/rpc_abi.py +++ b/web3/_utils/rpc_abi.py @@ -52,6 +52,7 @@ 'eth_sendTransaction': TRANSACTION_PARAMS_ABIS, 'eth_signTransaction': TRANSACTION_PARAMS_ABIS, 'eth_sign': ['address', 'bytes'], + 'eth_submitHashrate': ['uint', None], # personal 'personal_sendTransaction': TRANSACTION_PARAMS_ABIS, 'personal_lockAccount': ['address'], diff --git a/web3/eth.py b/web3/eth.py index e7047f163c..4eb8a998ba 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -395,6 +395,11 @@ def getLogs(self, filter_params): "eth_getLogs", [filter_params], ) + def submitHashrate(self, hashrate, node_id): + return self.web3.manager.request_blocking( + "eth_submitHashrate", [hashrate, node_id], + ) + def uninstallFilter(self, filter_id): return self.web3.manager.request_blocking( "eth_uninstallFilter", [filter_id], diff --git a/web3/middleware/pythonic.py b/web3/middleware/pythonic.py index 7296c0a2d0..229b721485 100644 --- a/web3/middleware/pythonic.py +++ b/web3/middleware/pythonic.py @@ -296,6 +296,7 @@ def to_hexbytes(num_bytes, val, variable_length=False): (estimate_gas_with_block_id, is_length(2)), )), 'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0), + 'eth_submitHashrate': apply_formatter_at_index(hexstr_if_str(to_hex), 1), # personal 'personal_importRawKey': apply_formatter_at_index( compose(remove_0x_prefix, hexstr_if_str(to_hex)), From fa9207e34dc5562a2a8271972a29aa986c8d271e Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Thu, 14 Mar 2019 14:56:28 +0100 Subject: [PATCH 2/2] Implement eth_submitWork --- docs/releases.rst | 2 ++ docs/web3.eth.rst | 16 +++++++++++++++- tests/integration/test_ethereum_tester.py | 3 ++- web3/_utils/module_testing/eth_module.py | 7 +++++++ web3/_utils/rpc_abi.py | 3 ++- web3/eth.py | 5 +++++ web3/middleware/pythonic.py | 1 - 7 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/releases.rst b/docs/releases.rst index b653796da7..168151892b 100644 --- a/docs/releases.rst +++ b/docs/releases.rst @@ -38,6 +38,8 @@ Unreleased (latest source) -------------------------- - Remove ``web3/utils`` directory in favor of ``web3/_utils`` - `#1282 `_ + - Implement ``eth_submitHashrate`` and ``eth_submitWork`` JSONRPC endpoints. + - `#1280 `_ v5.0.0-alpha.7 -------------- diff --git a/docs/web3.eth.rst b/docs/web3.eth.rst index 603c779f4a..f0dbddf9eb 100644 --- a/docs/web3.eth.rst +++ b/docs/web3.eth.rst @@ -849,7 +849,7 @@ with the filtering API. .. py:method:: Eth.submitHashrate(hashrate, nodeid) - * Delegates to ``eth_submitHashrate`` RPC Method. + * Delegates to ``eth_submitHashrate`` RPC Method .. code-block:: python @@ -858,6 +858,20 @@ with the filtering API. True +.. py:method:: Eth.submitWork(nonce, pow_hash, mix_digest) + + * Delegates to ``eth_submitWork`` RPC Method. + + .. code-block:: python + + >>> web3.eth.submitWork( + 1, + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', + '0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000', + ) + True + + Contracts --------- diff --git a/tests/integration/test_ethereum_tester.py b/tests/integration/test_ethereum_tester.py index 4c1da5fc26..e2be6889f9 100644 --- a/tests/integration/test_ethereum_tester.py +++ b/tests/integration/test_ethereum_tester.py @@ -216,8 +216,9 @@ def func_wrapper(self, eth_tester, *args, **kwargs): class TestEthereumTesterEthModule(EthModuleTest): test_eth_sign = not_implemented(EthModuleTest.test_eth_sign, ValueError) - test_eth_submitHashrate = not_implemented(EthModuleTest.test_eth_submitHashrate, ValueError) test_eth_signTransaction = not_implemented(EthModuleTest.test_eth_signTransaction, ValueError) + test_eth_submitHashrate = not_implemented(EthModuleTest.test_eth_submitHashrate, ValueError) + test_eth_submitWork = not_implemented(EthModuleTest.test_eth_submitWork, ValueError) @disable_auto_mine def test_eth_getTransactionReceipt_unmined(self, eth_tester, web3, unlocked_account): diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index dd5b716614..af68500b9f 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -838,3 +838,10 @@ def test_eth_submitHashrate(self, web3): node_id = '59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c' result = web3.eth.submitHashrate(5000, node_id) assert result is True + + def test_eth_submitWork(self, web3): + nonce = 1 + pow_hash = '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' + mix_digest = '0xD1FE5700000000000000000000000000D1FE5700000000000000000000000000' + result = web3.eth.submitWork(nonce, pow_hash, mix_digest) + assert result is False diff --git a/web3/_utils/rpc_abi.py b/web3/_utils/rpc_abi.py index 97fc53bb86..880a57e69e 100644 --- a/web3/_utils/rpc_abi.py +++ b/web3/_utils/rpc_abi.py @@ -52,7 +52,8 @@ 'eth_sendTransaction': TRANSACTION_PARAMS_ABIS, 'eth_signTransaction': TRANSACTION_PARAMS_ABIS, 'eth_sign': ['address', 'bytes'], - 'eth_submitHashrate': ['uint', None], + 'eth_submitHashrate': ['uint', 'bytes32'], + 'eth_submitWork': ['bytes8', 'bytes32', 'bytes32'], # personal 'personal_sendTransaction': TRANSACTION_PARAMS_ABIS, 'personal_lockAccount': ['address'], diff --git a/web3/eth.py b/web3/eth.py index 4eb8a998ba..9c552d94fd 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -400,6 +400,11 @@ def submitHashrate(self, hashrate, node_id): "eth_submitHashrate", [hashrate, node_id], ) + def submitWork(self, nonce, pow_hash, mix_digest): + return self.web3.manager.request_blocking( + "eth_submitWork", [nonce, pow_hash, mix_digest], + ) + def uninstallFilter(self, filter_id): return self.web3.manager.request_blocking( "eth_uninstallFilter", [filter_id], diff --git a/web3/middleware/pythonic.py b/web3/middleware/pythonic.py index 229b721485..7296c0a2d0 100644 --- a/web3/middleware/pythonic.py +++ b/web3/middleware/pythonic.py @@ -296,7 +296,6 @@ def to_hexbytes(num_bytes, val, variable_length=False): (estimate_gas_with_block_id, is_length(2)), )), 'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0), - 'eth_submitHashrate': apply_formatter_at_index(hexstr_if_str(to_hex), 1), # personal 'personal_importRawKey': apply_formatter_at_index( compose(remove_0x_prefix, hexstr_if_str(to_hex)),