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

Commit a20d23b

Browse files
committed
Use bytes instead of hex encoding for keys
1 parent 45c3303 commit a20d23b

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

ethereum/chain.py

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

6161
self.head_hash = self.state.prev_headers[0].hash
6262
self.genesis = Block(self.state.prev_headers[0], [], [])
63-
self.db.put('state:' + encode_hex(self.head_hash), self.state.trie.root_hash)
63+
self.db.put('state:'.encode('utf8') + self.head_hash, self.state.trie.root_hash)
6464
self.db.put('GENESIS_NUMBER', str(self.state.block_number))
6565
self.db.put('GENESIS_HASH', self.state.prev_headers[0].hash)
6666
assert self.state.block_number == self.state.prev_headers[0].number
67-
self.db.put('score:' + encode_hex(self.state.prev_headers[0].hash), "0")
67+
self.db.put('score:'.encode('utf8') + self.state.prev_headers[0].hash, "0")
6868
self.db.put('GENESIS_STATE', json.dumps(self.state.to_snapshot()))
6969
self.db.put(self.head_hash, 'GENESIS')
7070
self.min_gasprice = kwargs.get('min_gasprice', 5 * 10**9)
@@ -91,7 +91,7 @@ def mk_poststate_of_blockhash(self, blockhash):
9191
if self.db.get(blockhash) == 'GENESIS':
9292
return State.from_snapshot(json.loads(self.db.get('GENESIS_STATE')), self.env)
9393
state = State(env=self.env)
94-
state.trie.root_hash = self.db.get('state:' + encode_hex(blockhash))
94+
state.trie.root_hash = self.db.get('state:'.encode('utf8') + blockhash)
9595
block = rlp.decode(self.db.get(blockhash), Block)
9696
update_block_env_variables(state, block)
9797
state.gas_used = block.header.gas_used
@@ -142,14 +142,14 @@ def get_block(self, blockhash):
142142
# parent hash and see that it is one of its children
143143
def add_child(self, child):
144144
try:
145-
existing = self.db.get('child:' + encode_hex(child.header.prevhash))
145+
existing = self.db.get('child:'.encode('utf8') + child.header.prevhash)
146146
except:
147147
existing = b''
148148
existing_hashes = []
149149
for i in range(0, len(existing), 32):
150150
existing_hashes.append(existing[i: i+32])
151151
if child.header.hash not in existing_hashes:
152-
self.db.put('child:' + encode_hex(child.header.prevhash), existing + child.header.hash)
152+
self.db.put('child:'.encode('utf8') + child.header.prevhash, existing + child.header.hash)
153153

154154
def get_blockhash_by_number(self, number):
155155
try:
@@ -164,7 +164,7 @@ def get_block_by_number(self, number):
164164
def get_child_hashes(self, blockhash):
165165
o = []
166166
try:
167-
data = self.db.get('child:' + encode_hex(blockhash))
167+
data = self.db.get('child:'.encode('utf8') + blockhash)
168168
for i in range(0, len(data), 32):
169169
o.append(data[i:i + 32])
170170
return o
@@ -182,14 +182,14 @@ def get_children(self, block):
182182
def get_score(self, block):
183183
if not block:
184184
return 0
185-
key = 'score:' + encode_hex(block.header.hash)
185+
key = 'score:'.encode('utf8') + block.header.hash
186186
if key not in self.db:
187187
try:
188188
parent_score = self.get_score(self.get_parent(block))
189189
self.db.put(key, str(parent_score + block.difficulty +
190190
random.randrange(block.difficulty // 10**6 + 1)))
191191
except:
192-
return int(self.db.get('score:' + encode_hex(block.prevhash)))
192+
return int(self.db.get('score:'.encode('utf8') + block.prevhash))
193193
return int(self.db.get(key))
194194

195195
# These two functions should be called periodically so as to
@@ -235,10 +235,10 @@ def add_block(self, block):
235235
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
236236
return False
237237
self.db.put('block:' + str(block.header.number), block.header.hash)
238-
self.db.put('state:' + encode_hex(block.header.hash), self.state.trie.root_hash)
238+
self.db.put('state:'.encode('utf8') + block.header.hash, self.state.trie.root_hash)
239239
self.head_hash = block.header.hash
240240
for i, tx in enumerate(block.transactions):
241-
self.db.put('txindex:' + encode_hex(tx.hash), rlp.encode([block.number, i]))
241+
self.db.put('txindex:'.encode('utf8') + tx.hash, rlp.encode([block.number, i]))
242242
elif block.header.prevhash in self.env.db:
243243
log.info('Receiving block not on head, adding to secondary post state',
244244
prevhash=encode_hex(block.header.prevhash))
@@ -248,7 +248,7 @@ def add_block(self, block):
248248
except (KeyError, ValueError) as e: # FIXME add relevant exceptions here
249249
log.info('Block %s with parent %s invalid, reason: %s' % (encode_hex(block.header.hash), encode_hex(block.header.prevhash), e))
250250
return False
251-
self.db.put('state:' + encode_hex(block.header.hash), temp_state.trie.root_hash)
251+
self.db.put('state:'.encode('utf8') + block.header.hash, temp_state.trie.root_hash)
252252
block_score = self.get_score(block)
253253
# Replace the head
254254
if block_score > self.get_score(self.head):
@@ -272,13 +272,13 @@ def add_block(self, block):
272272
self.db.delete(key)
273273
orig_block_at_height = self.get_block(orig_at_height)
274274
for tx in orig_block_at_height.transactions:
275-
if 'txindex:' + encode_hex(tx.hash) in self.db:
276-
self.db.delete('txindex:' + encode_hex(tx.hash))
275+
if 'txindex:'.encode('utf8') + tx.hash in self.db:
276+
self.db.delete('txindex:'.encode('utf8') + tx.hash)
277277
if i in new_chain:
278278
new_block_at_height = new_chain[i]
279279
self.db.put(key, new_block_at_height.header.hash)
280280
for i, tx in enumerate(new_block_at_height.transactions):
281-
self.db.put('txindex:' + encode_hex(tx.hash),
281+
self.db.put('txindex:'.encode('utf8') + tx.hash,
282282
rlp.encode([new_block_at_height.number, i]))
283283
if i not in new_chain and not orig_at_height:
284284
break
@@ -335,8 +335,8 @@ def get_chain(self, frm=None, to=2**63 - 1):
335335
def get_transaction(self, tx):
336336
if not isinstance(tx, (str, bytes)):
337337
tx = tx.hash
338-
if 'txindex:' + encode_hex(tx) in self.db:
339-
data = rlp.decode(self.db.get('txindex:' + encode_hex(tx)))
338+
if 'txindex:'.encode('utf8') + tx in self.db:
339+
data = rlp.decode(self.db.get('txindex:'.encode('utf8') + tx))
340340
blk, index = self.get_block_by_number(
341341
big_endian_to_int(data[0])), big_endian_to_int(data[1])
342342
tx = blk.transactions[index]

0 commit comments

Comments
 (0)