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

Commit 8b1f394

Browse files
committed
use rlp encode_hex rather than encode('hex')
1 parent b6132c8 commit 8b1f394

File tree

9 files changed

+20
-39
lines changed

9 files changed

+20
-39
lines changed

ethereum/abi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def listen(self, log, noprint=True):
222222
if indexed[i]]
223223
unindexed_types = [types[i] for i in range(len(types))
224224
if not indexed[i]]
225-
# print('listen', log.data.encode('hex'), log.topics)
225+
# print('listen', encode_hex(log.data), log.topics)
226226
deserialized_args = decode_abi(unindexed_types, log.data)
227227
o = {}
228228
c1, c2 = 0, 0
@@ -501,7 +501,7 @@ def decode_single(typ, data):
501501
i = (o - 2**(high+low)) if o >= 2**(high+low-1) else o
502502
return (i * 1.0 / 2**low)
503503
elif base == 'bool':
504-
return bool(int(data.encode('hex'), 16))
504+
return bool(int(encode_hex(data), 16))
505505

506506

507507
# Decodes multiple arguments using the head/tail mechanism

ethereum/blocks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ def validate_uncles(self):
676676
return False
677677
if uncle.prevhash not in eligible_ancestor_hashes:
678678
log.error("Uncle does not have a valid ancestor", block=self,
679-
eligible=[x.encode('hex') for x in eligible_ancestor_hashes],
680-
uncle_prevhash=uncle.prevhash.encode('hex'))
679+
eligible=[encode_hex(x) for x in eligible_ancestor_hashes],
680+
uncle_prevhash=encode_hex(uncle.prevhash))
681681
return False
682682
if uncle in ineligible:
683683
log.error("Duplicate uncle", block=self,
@@ -1035,7 +1035,7 @@ def commit_state(self):
10351035
# except:
10361036
# pass
10371037
# sys.stderr.write("pre: %r\n" % self.account_to_dict(addr)['storage'])
1038-
# sys.stderr.write("pre: %r\n" % self.get_storage(addr).root_hash.encode('hex'))
1038+
# sys.stderr.write("pre: %r\n" % encode_hex(self.get_storage(addr).root_hash))
10391039
# sys.stderr.write("changed: %s %s %s\n" % (encode_hex(addr), encode_hex(enckey), encode_hex(val)))
10401040
if v:
10411041
t.update(enckey, val)

ethereum/chain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def __init__(self, env, genesis=None, new_head_cb=None, coinbase='\x00' * 20):
121121
self._initialize_blockchain(genesis)
122122
log.debug('chain @', head_hash=self.head)
123123
self.genesis = self.get(self.index.get_block_by_number(0))
124-
log.debug('got genesis', nonce=self.genesis.nonce.encode('hex'),
124+
log.debug('got genesis', nonce=encode_hex(self.genesis.nonce),
125125
difficulty=self.genesis.difficulty)
126126
self._update_head_candidate()
127127

ethereum/keys.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pbkdf2
33
import sys
44

5+
from rlp.utils import encode_hex, decode_hex
6+
57
try:
68
scrypt = __import__('scrypt')
79
except ImportError:
@@ -225,16 +227,6 @@ def zpad(x, l):
225227

226228
if sys.version_info.major == 2:
227229

228-
def decode_hex(s):
229-
if not isinstance(s, (str, unicode)):
230-
raise TypeError('Value must be an instance of str or unicode')
231-
return s.decode('hex')
232-
233-
def encode_hex(s):
234-
if not isinstance(s, (str, unicode)):
235-
raise TypeError('Value must be an instance of str or unicode')
236-
return s.encode('hex')
237-
238230
def int_to_big_endian(value):
239231
cs = []
240232
while value > 0:
@@ -254,20 +246,6 @@ def big_endian_to_int(value):
254246

255247
if sys.version_info.major == 3:
256248

257-
def decode_hex(s):
258-
if isinstance(s, str):
259-
return bytes.fromhex(s)
260-
if isinstance(s, bytes):
261-
return binascii.unhexlify(s)
262-
raise TypeError('Value must be an instance of str or bytes')
263-
264-
def encode_hex(b):
265-
if isinstance(b, str):
266-
b = bytes(b, 'utf-8')
267-
if isinstance(b, bytes):
268-
return binascii.hexlify(b)
269-
raise TypeError('Value must be an instance of str or bytes')
270-
271249
def int_to_big_endian(value):
272250
byte_length = ceil(value.bit_length() / 8)
273251
return (value).to_bytes(byte_length, byteorder='big')

ethereum/processblock.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ def _apply_msg(ext, msg, code):
256256
gas=msg.gas, value=msg.value,
257257
data=encode_hex(msg.data.extract_all()))
258258
if log_state.is_active('trace'):
259-
log_state.trace('MSG PRE STATE SENDER', account=msg.sender.encode('hex'),
259+
log_state.trace('MSG PRE STATE SENDER', account=encode_hex(msg.sender),
260260
bal=ext.get_balance(msg.sender),
261261
state=ext.log_storage(msg.sender))
262-
log_state.trace('MSG PRE STATE RECIPIENT', account=msg.to.encode('hex'),
262+
log_state.trace('MSG PRE STATE RECIPIENT', account=encode_hex(msg.to),
263263
bal=ext.get_balance(msg.to),
264264
state=ext.log_storage(msg.to))
265265
# log_state.trace('CODE', code=code)
@@ -281,10 +281,10 @@ def _apply_msg(ext, msg, code):
281281
log_msg.debug('MSG APPLIED', gas_remained=gas,
282282
sender=encode_hex(msg.sender), to=encode_hex(msg.to), data=dat)
283283
if log_state.is_active('trace'):
284-
log_state.trace('MSG POST STATE SENDER', account=msg.sender.encode('hex'),
284+
log_state.trace('MSG POST STATE SENDER', account=encode_hex(msg.sender),
285285
bal=ext.get_balance(msg.sender),
286286
state=ext.log_storage(msg.sender))
287-
log_state.trace('MSG POST STATE RECIPIENT', account=msg.to.encode('hex'),
287+
log_state.trace('MSG POST STATE RECIPIENT', account=encode_hex(msg.to),
288288
bal=ext.get_balance(msg.to),
289289
state=ext.log_storage(msg.to))
290290

ethereum/pruning_trie.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def replace_root_hash(self, old_node, new_node):
270270
self._delete_node_storage(old_node, is_root=True)
271271
self._encode_node(new_node, is_root=True)
272272
self.root_node = new_node
273-
# sys.stderr.write('nrh: %s\n' % self.root_hash.encode('hex'))
273+
# sys.stderr.write('nrh: %s\n' % encode_hex(self.root_hash))
274274

275275
@root_hash.setter
276276
def root_hash(self, value):

ethereum/tests/profile_vm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pstats
77
import StringIO
88
import time
9+
from rlp.utils import encode_hex
910
from ethereum.utils import sha3
1011
from ethereum.slogging import get_logger
1112
logger = get_logger()
@@ -34,7 +35,7 @@ def run(profiler=None):
3435
seen += str(testname)
3536
i += 1
3637
print('ran %d tests' % i)
37-
print('test key', sha3(seen).encode('hex'))
38+
print('test key', encode_hex(sha3(seen)))
3839

3940
if len(sys.argv) == 1:
4041
pr = cProfile.Profile()

ethereum/tests/test_blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def run_block_test(params, config_overrides = {}):
9595
blockmap[b2.hash] = b2
9696
env.db.put(b2.hash, rlp.encode(b2))
9797
if b2:
98-
print('Block %d with state root %s' % (b2.number, b2.state.root_hash.encode('hex')))
98+
print('Block %d with state root %s' % (b2.number, encode_hex(b2.state.root_hash)))
9999
# blkdict = b.to_dict(False, True, False, True)
100100
# assert blk["blockHeader"] == \
101101
# translate_keys(blkdict["header"], translator_list, lambda y, x: x, [])

ethereum/tests/test_solidity.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
from rlp.utils import encode_hex
7+
68
from ethereum import tester
79
from ethereum import utils
810
from ethereum import _solidity
@@ -24,7 +26,7 @@ def test_library_from_file():
2426
)
2527

2628
libraries = {
27-
'SevenLibrary': library.address.encode('hex'),
29+
'SevenLibrary': encode_hex(library.address),
2830
}
2931
contract = state.abi_contract(
3032
None,
@@ -56,7 +58,7 @@ def test_library_from_code():
5658
)
5759

5860
libraries = {
59-
'SevenLibrary': library.address.encode('hex'),
61+
'SevenLibrary': encode_hex(library.address),
6062
}
6163
contract = state.abi_contract(
6264
contract_code,

0 commit comments

Comments
 (0)