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

Contract call optimization - remove round trip #944

Merged
merged 5 commits into from
Aug 30, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -391,9 +391,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 @@ -1397,12 +1397,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 @@ -1411,6 +1407,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