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 3 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: 3 additions & 0 deletions tests/core/contracts/test_contract_util_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_parse_block_identifier_int(web3):
last_num = web3.eth.getBlock('latest').number
assert web3.eth.getBlock(0) == web3.eth.getBlock(-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