Skip to content

Commit

Permalink
Require compatible setuptools version
Browse files Browse the repository at this point in the history
see ethereum#1053

added feature for inputing block_identifier in eth_estimateGas command

variable inputs in estimateGas and tests

formatter for block_identifier and passing tests

added feature for inputing block_identifier in eth_estimateGas command

variable inputs in estimateGas and tests

passing lint
  • Loading branch information
dylanjw authored and ankitchiplunkar committed Sep 19, 2018
1 parent 3d14361 commit 0461819
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
8 changes: 8 additions & 0 deletions tests/integration/go_ethereum/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def test_eth_modifyTransaction(self, web3, unlocked_account):
pytest.xfail('Needs ability to efficiently control mining')
super().test_eth_modifyTransaction(web3, unlocked_account)

def test_eth_estimateGas_with_block(self,
web3,
unlocked_account_dual_type):
pytest.xfail('Block identifier has not been implemented in geth')
super().test_eth_estimateGas_with_block(
web3, unlocked_account_dual_type
)


class GoEthereumVersionModuleTest(VersionModuleTest):
pass
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ def test_eth_getStorageAt(self, web3, emitter_contract_address):
pytest.xfail('json-rpc method is not implemented on eth-tester')
super().test_eth_getStorageAt(web3, emitter_contract_address)

def test_eth_estimateGas_with_block(self,
web3,
unlocked_account_dual_type):
pytest.xfail('Block identifier has not been implemented in eth-tester')
super().test_eth_estimateGas_with_block(
web3, unlocked_account_dual_type
)


class TestEthereumTesterVersionModule(VersionModuleTest):
pass
Expand Down
5 changes: 5 additions & 0 deletions web3/_utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ def is_string_type(abi_type):
return abi_type == 'string'


@curry
def is_length(target_length, value):
return len(value) == target_length


def size_of_type(abi_type):
"""
Returns size in bits of abi_type
Expand Down
11 changes: 11 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,17 @@ def test_eth_estimateGas(self, web3, unlocked_account_dual_type):
assert is_integer(gas_estimate)
assert gas_estimate > 0

def test_eth_estimateGas_with_block(self,
web3,
unlocked_account_dual_type):
gas_estimate = web3.eth.estimateGas({
'from': unlocked_account_dual_type,
'to': unlocked_account_dual_type,
'value': 1,
}, 'latest')
assert is_integer(gas_estimate)
assert gas_estimate > 0

def test_eth_getBlockByHash(self, web3, empty_block):
block = web3.eth.getBlock(empty_block['hash'])
assert block['hash'] == empty_block['hash']
Expand Down
17 changes: 12 additions & 5 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,22 @@ def call(self, transaction, block_identifier=None):
[transaction, block_identifier],
)

def estimateGas(self, transaction):
def estimateGas(self, transaction, block_identifier=None):
# TODO: move to middleware
if 'from' not in transaction and is_checksum_address(self.defaultAccount):
transaction = assoc(transaction, 'from', self.defaultAccount)

return self.web3.manager.request_blocking(
"eth_estimateGas",
[transaction],
)
# TODO: move to middleware
if block_identifier is None:
return self.web3.manager.request_blocking(
"eth_estimateGas",
[transaction],
)
else:
return self.web3.manager.request_blocking(
"eth_estimateGas",
[transaction, block_identifier],
)

def filter(self, filter_params=None, filter_id=None):
if filter_id and filter_params:
Expand Down
14 changes: 13 additions & 1 deletion web3/middleware/pythonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
HexBytes,
)

from web3._utils.abi import (
is_length,
)
from web3._utils.encoding import (
hexstr_if_str,
to_hex,
Expand Down Expand Up @@ -244,6 +247,12 @@ def to_hexbytes(num_bytes, val, variable_length=False):
apply_formatters_to_dict(TRANSACTION_PARAM_FORMATTERS),
)

estimate_gas_without_block_id = apply_formatter_at_index(transaction_param_formatter, 0)
estimate_gas_with_block_id = combine_argument_formatters(
transaction_param_formatter,
block_number_formatter,
)


pythonic_middleware = construct_formatting_middleware(
request_formatters={
Expand Down Expand Up @@ -273,7 +282,10 @@ def to_hexbytes(num_bytes, val, variable_length=False):
transaction_param_formatter,
block_number_formatter,
),
'eth_estimateGas': apply_formatter_at_index(transaction_param_formatter, 0),
'eth_estimateGas': apply_one_of_formatters((
(estimate_gas_without_block_id, is_length(1)),
(estimate_gas_with_block_id, is_length(2)),
)),
'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0),
# personal
'personal_importRawKey': apply_formatter_at_index(
Expand Down

0 comments on commit 0461819

Please sign in to comment.