diff --git a/pyethereum/blocks.py b/pyethereum/blocks.py index 7d929a311..9e99a4cac 100644 --- a/pyethereum/blocks.py +++ b/pyethereum/blocks.py @@ -32,11 +32,11 @@ # Genesis block gas limit GENESIS_GAS_LIMIT = 10 ** 6 # Genesis block prevhash, coinbase, nonce -GENESIS_PREVHASH = '\00' * 32 -GENESIS_COINBASE = decode_hex("0" * 40) +GENESIS_PREVHASH = b'\x00' * 32 +GENESIS_COINBASE = b'\x00' * 20 GENESIS_NONCE = utils.zpad(utils.encode_int(42), 8) -GENESIS_SEEDHASH = '\x00' * 32 -GENESIS_MIXHASH = '\x00' * 32 +GENESIS_SEEDHASH = b'\x00' * 32 +GENESIS_MIXHASH = b'\x00' * 32 # Minimum gas limit MIN_GAS_LIMIT = 125000 # Gas limit adjustment algo: @@ -137,8 +137,8 @@ def blank_account(cls, db): :param db: the db in which the account will store its code. """ - code_hash = utils.sha3('') - db.put(code_hash, '') + code_hash = utils.sha3(b'') + db.put(code_hash, b'') return cls(0, 0, trie.BLANK_ROOT, code_hash, db) @@ -328,7 +328,7 @@ def to_dict(self): d = {} for field in ('prevhash', 'uncles_hash', 'extra_data', 'nonce', 'seedhash', 'mixhash'): - d[field] = '0x' + encode_hex(getattr(self, field)) + d[field] = b'0x' + encode_hex(getattr(self, field)) for field in ('state_root', 'tx_list_root', 'receipts_root', 'coinbase'): d[field] = encode_hex(getattr(self, field)) @@ -573,7 +573,7 @@ def init_from_header(cls, header_rlp, db): return cls(header, None, [], db=db) @classmethod - def init_from_parent(cls, parent, coinbase, nonce='', extra_data='', + def init_from_parent(cls, parent, coinbase, nonce=b'', extra_data=b'', timestamp=int(time.time()), uncles=[]): """Create a new block based on a parent block. @@ -974,7 +974,7 @@ def commit_state(self): # storage t = SecureTrie(Trie(self.db, acct.storage)) - for k, v in self.caches.get('storage:' + address, {}).items(): + for k, v in self.caches.get(b'storage:' + address, {}).items(): enckey = utils.zpad(utils.coerce_to_bytes(k), 32) val = rlp.encode(v) changes.append(['storage', address, k, v]) diff --git a/pyethereum/compress.py b/pyethereum/compress.py index 36a9f83e2..f64a3221f 100644 --- a/pyethereum/compress.py +++ b/pyethereum/compress.py @@ -5,42 +5,43 @@ def compress(data): - o = '' + from pyethereum.utils import int_to_bytes + o = b'' i = 0 while i < len(data): - if data[i] == '\xfe': - o += '\xfe\x00' + if int_to_bytes(data[i]) == b'\xfe': + o += b'\xfe\x00' elif data[i:i + 32] == NULLSHA3: - o += '\xfe\x01' + o += b'\xfe\x01' i += 31 - elif data[i:i + 2] == '\x00\x00': + elif data[i:i + 2] == b'\x00\x00': p = 2 - while p < 255 and i + p < len(data) and data[i + p] == '\x00': + while p < 255 and i + p < len(data) and int_to_bytes(data[i + p]) == b'\x00': p += 1 - o += '\xfe' + ascii_chr(p) + o += b'\xfe' + ascii_chr(p) i += p - 1 else: - o += data[i] + o += int_to_bytes(data[i]) i += 1 return o def decompress(data): - from pyethereum.utils import safe_ord - o = '' + from pyethereum.utils import safe_ord, int_to_bytes + o = b'' i = 0 while i < len(data): - if data[i] == '\xfe': + if int_to_bytes(data[i]) == b'\xfe': if i == len(data) - 1: raise Exception("Invalid encoding, \\xfe at end") - elif data[i + 1] == '\x00': - o += '\xfe' - elif data[i + 1] == '\x01': + elif int_to_bytes(data[i + 1]) == b'\x00': + o += b'\xfe' + elif int_to_bytes(data[i + 1]) == b'\x01': o += NULLSHA3 else: - o += '\x00' * safe_ord(data[i + 1]) + o += b'\x00' * safe_ord(data[i + 1]) i += 1 else: - o += data[i] + o += int_to_bytes(data[i]) i += 1 return o diff --git a/pyethereum/slogging.py b/pyethereum/slogging.py index 62e4c163e..94bb96f59 100644 --- a/pyethereum/slogging.py +++ b/pyethereum/slogging.py @@ -24,10 +24,7 @@ """ def hexprint(x): - if type(x) == bytes: - return ('0x' + binascii.hexlify(x).decode('ascii')) - else: - return repr(x) + return repr(x) class KeyValueRenderer(structlog.processors.KeyValueRenderer): diff --git a/pyethereum/trie.py b/pyethereum/trie.py index d4c12f49a..35a0d4ca6 100644 --- a/pyethereum/trie.py +++ b/pyethereum/trie.py @@ -11,7 +11,7 @@ bin_to_nibbles_cache = {} hti = {} -for i, c in enumerate('0123456789abcdef'): +for i, c in enumerate(b'0123456789abcdef'): hti[c] = i @@ -139,7 +139,7 @@ def pack_nibbles(nibbles): nibbles = [flags] + nibbles else: nibbles = [flags, 0] + nibbles - o = '' + o = b'' for i in range(0, len(nibbles), 2): o += ascii_chr(16 * nibbles[i] + nibbles[i + 1]) return o @@ -183,8 +183,8 @@ def is_key_value_type(node_type): return node_type in [NODE_TYPE_LEAF, NODE_TYPE_EXTENSION] -BLANK_NODE = '' -BLANK_ROOT = utils.sha3rlp('') +BLANK_NODE = b'' +BLANK_ROOT = utils.sha3rlp(b'') def transient_trie_exception(*args): diff --git a/pyethereum/utils.py b/pyethereum/utils.py index 1c8ce3801..ae77a73c8 100644 --- a/pyethereum/utils.py +++ b/pyethereum/utils.py @@ -20,6 +20,11 @@ def to_string(value): return str(value) + def int_to_bytes(value): + if isinstance(value, str): + return value + return int_to_big_endian(value) + else: is_numeric = lambda x: isinstance(x, int) is_string = lambda x: isinstance(x, bytes) @@ -32,6 +37,11 @@ def to_string(value): if isinstance(value, int): return bytes(str(value), 'utf-8') + def int_to_bytes(value): + if isinstance(value, bytes): + return value + return int_to_big_endian(value) + def safe_ord(value): if isinstance(value, int): return value @@ -61,7 +71,7 @@ def flatten(li): o.extend(l) return o -big_endian_to_int = lambda x: big_endian_int.deserialize(x.lstrip('\x00')) +big_endian_to_int = lambda x: big_endian_int.deserialize(x.lstrip(b'\x00')) int_to_big_endian = lambda x: big_endian_int.serialize(x) @@ -91,12 +101,12 @@ def privtoaddr(x): def zpad(x, l): - return '\x00' * max(0, l - len(x)) + x + return b'\x00' * max(0, l - len(x)) + x def zunpad(x): i = 0 - while i < len(x) and x[i] == '\x00': + while i < len(x) and x[i] == b'\x00': i += 1 return x[i:] @@ -106,7 +116,7 @@ def int_to_addr(x): for i in range(20): o[19 - i] = ascii_chr(x & 0xff) x >>= 8 - return encode_hex(''.join(o)) + return encode_hex(b''.join(o)) def coerce_addr_to_bin(x): @@ -200,7 +210,7 @@ def decode_addr(v): def decode_int(v): '''decodes and integer from serialization''' - if len(v) > 0 and v[0] == '\x00': + if len(v) > 0 and v[0] == b'\x00': raise Exception("No leading zero bytes allowed for integers") return big_endian_to_int(v)