Skip to content

Commit

Permalink
Updated Python2/3 compatibility (xrange->range) and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Oct 19, 2017
1 parent 3cd5f64 commit d6e4758
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion ethereum/hybrid_casper/chain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import range
import json
import random
import time
Expand Down Expand Up @@ -589,7 +590,7 @@ def get_blockhashes_from_hash(self, hash, max):

header = block.header
hashes = []
for i in xrange(max):
for i in range(max):
hash = header.prevhash
block = self.get_block(hash)
if block is None:
Expand Down
10 changes: 5 additions & 5 deletions ethereum/pow/chain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
import json
import random
import time
Expand Down Expand Up @@ -502,16 +503,15 @@ def db(self):
return self.env.db

# Get blockhashes starting from a hash and going backwards
def get_blockhashes_from_hash(self, hash, max):
block = self.get_block(hash)
def get_blockhashes_from_hash(self, blockhash, max_num):
block = self.get_block(blockhash)
if block is None:
return []

header = block.header
hashes = []
for i in xrange(max):
hash = header.prevhash
block = self.get_block(hash)
for i in range(max_num):
block = self.get_block(header.prevhash)
if block is None:
break
header = block.header
Expand Down
19 changes: 18 additions & 1 deletion ethereum/tests/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ethereum.block import Block
from ethereum.consensus_strategy import get_consensus_strategy
from ethereum.genesis_helpers import mk_basic_state

from ethereum.tools import tester
from ethereum.slogging import get_logger
logger = get_logger()

Expand Down Expand Up @@ -374,6 +374,23 @@ def test_genesis_from_state_snapshot():
assert new_chain.head.number == state.block_number


def test_get_blockhashes_from_hash():
test_chain = tester.Chain()
test_chain.mine(5)

blockhashes = test_chain.chain.get_blockhashes_from_hash(
test_chain.chain.get_block_by_number(5).hash,
2,
)
assert len(blockhashes) == 2


def test_get_blockhash_by_number():
test_chain = tester.Chain()
test_chain.mine(2)

test_chain.chain.get_blockhash_by_number(2) == test_chain.chain.head.hash

# TODO ##########################################
#
# test for remote block with invalid transaction
Expand Down

0 comments on commit d6e4758

Please sign in to comment.