Skip to content

Commit

Permalink
Merge pull request #944 from medvedev1088/contract-call-optimization
Browse files Browse the repository at this point in the history
Contract call optimization - remove round trip
  • Loading branch information
dylanjw authored Aug 30, 2018
2 parents baaafca + 63ebf56 commit d1495f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
3 changes: 0 additions & 3 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,6 @@ def test_call_fallback_function(fallback_function_contract):

def test_throws_error_if_block_out_of_range(web3, math_contract):
web3.providers[0].make_request(method='evm_mine', params=[20])
with pytest.raises(BlockNumberOutofRange):
math_contract.functions.counter().call(block_identifier=50)

with pytest.raises(BlockNumberOutofRange):
math_contract.functions.counter().call(block_identifier=-50)

Expand Down
13 changes: 13 additions & 0 deletions tests/core/contracts/test_contract_util_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from web3.contract import (
parse_block_identifier_int,
)


# This tests negative block number identifiers, which behave like python
# list slices, with -1 being the latest block and -2 being the block before that.
# This test is necessary because transaction calls allow negative block indexes, although
# getBlock() does not allow negative block identifiers. Support for negative block identifier
# will likely be removed in v5.
def test_parse_block_identifier_int(web3):
last_num = web3.eth.getBlock('latest').number
assert 0 == parse_block_identifier_int(web3, -1 - last_num)
19 changes: 13 additions & 6 deletions web3/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,12 +1411,8 @@ def call_contract_function(


def parse_block_identifier(web3, block_identifier):
last_block = web3.eth.getBlock('latest').number
if isinstance(block_identifier, int) and abs(block_identifier) <= last_block:
if block_identifier >= 0:
return block_identifier
else:
return last_block + block_identifier + 1
if isinstance(block_identifier, int):
return parse_block_identifier_int(web3, block_identifier)
elif block_identifier in ['latest', 'earliest', 'pending']:
return block_identifier
elif isinstance(block_identifier, bytes) or is_hex_encoded_block_hash(block_identifier):
Expand All @@ -1425,6 +1421,17 @@ def parse_block_identifier(web3, block_identifier):
raise BlockNumberOutofRange


def parse_block_identifier_int(web3, block_identifier_int):
if block_identifier_int >= 0:
block_num = block_identifier_int
else:
last_block = web3.eth.getBlock('latest').number
block_num = last_block + block_identifier_int + 1
if block_num < 0:
raise BlockNumberOutofRange
return block_num


def transact_with_contract_function(
address,
web3,
Expand Down

0 comments on commit d1495f3

Please sign in to comment.