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

Commit

Permalink
Start to replace hard coded string by bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JahPowerBit committed Mar 23, 2015
1 parent 49ebe2d commit 72331f8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
18 changes: 9 additions & 9 deletions pyethereum/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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])
Expand Down
33 changes: 17 additions & 16 deletions pyethereum/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions pyethereum/slogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
8 changes: 4 additions & 4 deletions pyethereum/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
20 changes: 15 additions & 5 deletions pyethereum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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:]

Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 72331f8

Please sign in to comment.