Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 9529b34

Browse files
committed
Add Python3 support for chain
1 parent f84ec57 commit 9529b34

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

ethereum/chain.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, \
5959

6060
self.head_hash = self.state.prev_headers[0].hash
6161
self.genesis = Block(self.state.prev_headers[0], [], [])
62-
self.db.put('state:'+self.head_hash, self.state.trie.root_hash)
62+
self.db.put('state:' + encode_hex(self.head_hash), self.state.trie.root_hash)
6363
self.db.put('GENESIS_NUMBER', str(self.state.block_number))
64-
self.db.put('GENESIS_HASH', str(self.state.prev_headers[0].hash))
64+
self.db.put('GENESIS_HASH', self.state.prev_headers[0].hash)
6565
assert self.state.block_number == self.state.prev_headers[0].number
66-
self.db.put('score:' + self.state.prev_headers[0].hash, "0")
66+
self.db.put('score:' + encode_hex(self.state.prev_headers[0].hash), "0")
6767
self.db.put('GENESIS_STATE', json.dumps(self.state.to_snapshot()))
6868
self.db.put(self.head_hash, 'GENESIS')
6969
self.min_gasprice = kwargs.get('min_gasprice', 5 * 10**9)
@@ -90,7 +90,7 @@ def mk_poststate_of_blockhash(self, blockhash):
9090
if self.db.get(blockhash) == 'GENESIS':
9191
return State.from_snapshot(json.loads(self.db.get('GENESIS_STATE')), self.env)
9292
state = State(env=self.env)
93-
state.trie.root_hash = self.db.get('state:'+blockhash)
93+
state.trie.root_hash = self.db.get('state:' + encode_hex(blockhash))
9494
block = rlp.decode(self.db.get(blockhash), Block)
9595
update_block_env_variables(state, block)
9696
state.gas_used = block.header.gas_used
@@ -141,14 +141,14 @@ def get_block(self, blockhash):
141141
# parent hash and see that it is one of its children
142142
def add_child(self, child):
143143
try:
144-
existing = self.db.get('child:' + child.header.prevhash)
144+
existing = self.db.get('child:' + encode_hex(child.header.prevhash))
145145
except:
146-
existing = ''
146+
existing = b''
147147
existing_hashes = []
148148
for i in range(0, len(existing), 32):
149149
existing_hashes.append(existing[i: i+32])
150150
if child.header.hash not in existing_hashes:
151-
self.db.put('child:' + child.header.prevhash, existing + child.header.hash)
151+
self.db.put('child:' + encode_hex(child.header.prevhash), existing + child.header.hash)
152152

153153
def get_blockhash_by_number(self, number):
154154
try:
@@ -163,7 +163,7 @@ def get_block_by_number(self, number):
163163
def get_child_hashes(self, blockhash):
164164
o = []
165165
try:
166-
data = self.db.get('child:' + blockhash)
166+
data = self.db.get('child:' + encode_hex(blockhash))
167167
for i in range(0, len(data), 32):
168168
o.append(data[i:i + 32])
169169
return o
@@ -181,14 +181,14 @@ def get_children(self, block):
181181
def get_score(self, block):
182182
if not block:
183183
return 0
184-
key = 'score:' + block.header.hash
184+
key = 'score:' + encode_hex(block.header.hash)
185185
if key not in self.db:
186186
try:
187187
parent_score = self.get_score(self.get_parent(block))
188188
self.db.put(key, str(parent_score + block.difficulty +
189189
random.randrange(block.difficulty // 10**6 + 1)))
190190
except:
191-
return int(self.db.get('score:' + block.prevhash))
191+
return int(self.db.get('score:' + encode_hex(block.prevhash)))
192192
return int(self.db.get(key))
193193

194194
# These two functions should be called periodically so as to
@@ -234,20 +234,20 @@ def add_block(self, block):
234234
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
235235
return False
236236
self.db.put('block:' + str(block.header.number), block.header.hash)
237-
self.db.put('state:' + block.header.hash, self.state.trie.root_hash)
237+
self.db.put('state:' + encode_hex(block.header.hash), self.state.trie.root_hash)
238238
self.head_hash = block.header.hash
239239
for i, tx in enumerate(block.transactions):
240-
self.db.put('txindex:' + tx.hash, rlp.encode([block.number, i]))
240+
self.db.put('txindex:' + encode_hex(tx.hash), rlp.encode([block.number, i]))
241241
elif block.header.prevhash in self.env.db:
242242
log.info('Receiving block not on head, adding to secondary post state',
243243
prevhash=encode_hex(block.header.prevhash))
244244
temp_state = self.mk_poststate_of_blockhash(block.header.prevhash)
245245
try:
246246
apply_block(temp_state, block)
247-
except (KeyError, ValueError), e: # FIXME add relevant exceptions here
247+
except (KeyError, ValueError) as e: # FIXME add relevant exceptions here
248248
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
249249
return False
250-
self.db.put('state:' + block.header.hash, temp_state.trie.root_hash)
250+
self.db.put('state:' + encode_hex(block.header.hash), temp_state.trie.root_hash)
251251
block_score = self.get_score(block)
252252
# Replace the head
253253
if block_score > self.get_score(self.head):
@@ -263,21 +263,21 @@ def add_block(self, block):
263263
break
264264
b = self.get_parent(b)
265265
replace_from = b.header.number
266-
for i in xrange(replace_from, 2**63 - 1):
266+
for i in range(replace_from, 2**63 - 1):
267267
log.info('Rewriting height %d' % i)
268268
key = 'block:' + str(i)
269269
orig_at_height = self.db.get(key) if key in self.db else None
270270
if orig_at_height:
271271
self.db.delete(key)
272272
orig_block_at_height = self.get_block(orig_at_height)
273273
for tx in orig_block_at_height.transactions:
274-
if 'txindex:' + tx.hash in self.db:
275-
self.db.delete('txindex:' + tx.hash)
274+
if 'txindex:' + encode_hex(tx.hash) in self.db:
275+
self.db.delete('txindex:' + encode_hex(tx.hash))
276276
if i in new_chain:
277277
new_block_at_height = new_chain[i]
278278
self.db.put(key, new_block_at_height.header.hash)
279279
for i, tx in enumerate(new_block_at_height.transactions):
280-
self.db.put('txindex:' + tx.hash,
280+
self.db.put('txindex:' + encode_hex(tx.hash),
281281
rlp.encode([new_block_at_height.number, i]))
282282
if i not in new_chain and not orig_at_height:
283283
break
@@ -324,7 +324,7 @@ def get_chain(self, frm=None, to=2**63 - 1):
324324
if frm is None:
325325
frm = int(self.db.get('GENESIS_NUMBER')) + 1
326326
chain = []
327-
for i in xrange(frm, to):
327+
for i in range(frm, to):
328328
h = self.get_blockhash_by_number(i)
329329
if not h:
330330
return chain
@@ -334,8 +334,8 @@ def get_chain(self, frm=None, to=2**63 - 1):
334334
def get_transaction(self, tx):
335335
if not isinstance(tx, (str, bytes)):
336336
tx = tx.hash
337-
if 'txindex:' + tx in self.db:
338-
data = rlp.decode(self.db.get('txindex:' + tx))
337+
if 'txindex:' + encode_hex(tx) in self.db:
338+
data = rlp.decode(self.db.get('txindex:' + encode_hex(tx)))
339339
blk, index = self.get_block_by_number(
340340
big_endian_to_int(data[0])), big_endian_to_int(data[1])
341341
tx = blk.transactions[index]
@@ -359,3 +359,4 @@ def db(self):
359359
@property
360360
def config(self):
361361
return self.env.config
362+

ethereum/consensus_strategy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, header_check, header_validate, uncle_validate, block_setup, b
1010

1111
def get_consensus_strategy(config):
1212
if config['CONSENSUS_STRATEGY'] in ('pow', 'ethpow', 'ethash', 'ethereum1'):
13-
from ethpow_utils import ethereum1_check_header, ethereum1_validate_header, \
13+
from ethereum.ethpow_utils import ethereum1_check_header, ethereum1_validate_header, \
1414
ethereum1_validate_uncle, ethereum1_pre_finalize_block, \
1515
ethereum1_post_finalize_block, ethereum1_setup_block
1616
return ConsensusStrategy(
@@ -23,7 +23,7 @@ def get_consensus_strategy(config):
2323
state_initialize=None
2424
)
2525
elif config['CONSENSUS_STRATEGY'] == 'casper':
26-
from casper_utils import casper_validate_header, casper_state_initialize, casper_post_finalize_block, casper_setup_block
26+
from ethereum.casper_utils import casper_validate_header, casper_state_initialize, casper_post_finalize_block, casper_setup_block
2727
return ConsensusStrategy(
2828
header_check=None,
2929
header_validate=casper_validate_header,

ethereum/tests/test_chain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ def test_invalid_transaction(db):
212212
def test_prevhash(db):
213213
chain = Chain({}, difficulty=1, min_gasprice=0)
214214
L1 = mine_on_chain(chain)
215-
assert chain.state.get_block_hash(0) != '\x00'*32
216-
assert chain.state.get_block_hash(1) != '\x00'*32
217-
assert chain.state.get_block_hash(2) == '\x00'*32
215+
assert chain.state.get_block_hash(0) != b'\x00'*32
216+
assert chain.state.get_block_hash(1) != b'\x00'*32
217+
assert chain.state.get_block_hash(2) == b'\x00'*32
218218

219219

220220
def test_genesis_chain(db):

0 commit comments

Comments
 (0)