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

Commit b610b2a

Browse files
committed
string and bytes compat
1 parent 42be7d7 commit b610b2a

File tree

11 files changed

+48
-32
lines changed

11 files changed

+48
-32
lines changed

ethereum/_solidity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import yaml
88

9+
from rlp.utils import decode_hex
10+
from . import utils
11+
912
BINARY = 'solc'
1013

1114

@@ -49,7 +52,7 @@ def solc_arguments(libraries=None, combined='bin,abi', optimize=True):
4952

5053
if libraries is not None and len(libraries):
5154
addresses = [
52-
'{name}:{address}'.format(name=name, address=address)
55+
'{name}:{address}'.format(name=name, address=address.decode('utf8'))
5356
for name, address in libraries.items()
5457
]
5558
args.extend([

ethereum/chain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Chain(object):
110110
"""
111111
head_candidate = None
112112

113-
def __init__(self, env, genesis=None, new_head_cb=None, coinbase='\x00' * 20):
113+
def __init__(self, env, genesis=None, new_head_cb=None, coinbase=b'\x00' * 20):
114114
assert isinstance(env, Env)
115115
self.env = env
116116
self.db = self.blockchain = env.db

ethereum/ethash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from functools import lru_cache
1010

1111

12-
cache_seeds = ['\x00' * 32]
12+
cache_seeds = [b'\x00' * 32]
1313

1414

1515
def mkcache(block_number):

ethereum/ethpow.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def check_pow(block_number, header_hash, mixhash, nonce, difficulty):
6969
# Grab current cache
7070
cache = get_cache(block_number)
7171
mining_output = hashimoto_light(block_number, cache, header_hash, nonce)
72-
if mining_output['mix digest'] != mixhash:
72+
if mining_output[b'mix digest'] != mixhash:
7373
return False
74-
return utils.big_endian_to_int(mining_output['result']) <= 2**256 / (difficulty or 1)
74+
return utils.big_endian_to_int(mining_output[b'result']) <= 2**256 / (difficulty or 1)
7575

7676

7777
class Miner():
@@ -114,9 +114,9 @@ def mine(block_number, difficulty, mining_hash, start_nonce=0, rounds=1000):
114114
for i in range(1, rounds + 1):
115115
bin_nonce = utils.zpad(utils.int_to_big_endian((nonce + i) & TT64M1), 8)
116116
o = hashimoto_light(block_number, cache, mining_hash, bin_nonce)
117-
if o["result"] <= target:
117+
if o[b"result"] <= target:
118118
log.debug("nonce found")
119119
assert len(bin_nonce) == 8
120-
assert len(o["mix digest"]) == 32
121-
return bin_nonce, o["mix digest"]
120+
assert len(o[b"mix digest"]) == 32
121+
return bin_nonce, o[b"mix digest"]
122122
return None, None

ethereum/keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def big_endian_to_int(value):
239239
if len(value) == 1:
240240
return ord(value)
241241
elif len(value) <= 8:
242-
return struct.unpack('>Q', value.rjust(8, '\x00'))[0]
242+
return struct.unpack('>Q', value.rjust(8, b'\x00'))[0]
243243
else:
244244
return int(encode_hex(value), 16)
245245

ethereum/specials.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def proc_identity(ext, msg):
9292
specials = {
9393
decode_hex(k): v for k, v in
9494
{
95-
'0000000000000000000000000000000000000001': proc_ecrecover,
96-
'0000000000000000000000000000000000000002': proc_sha256,
97-
'0000000000000000000000000000000000000003': proc_ripemd160,
98-
'0000000000000000000000000000000000000004': proc_identity,
95+
b'0000000000000000000000000000000000000001': proc_ecrecover,
96+
b'0000000000000000000000000000000000000002': proc_sha256,
97+
b'0000000000000000000000000000000000000003': proc_ripemd160,
98+
b'0000000000000000000000000000000000000004': proc_identity,
9999
}.items()
100100
}
101101

ethereum/tests/test_abi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_abi_encode_single_int():
3434

3535
def test_abi_encode_single_ureal():
3636
assert abi.encode_single(['ureal', '128x128', []], 0) == (b'\x00'*32)
37-
assert abi.encode_single(['ureal', '128x128', []], 1.125) == (b'\x00'*15 + b'\x01\x20' + '\x00'*15)
37+
assert abi.encode_single(['ureal', '128x128', []], 1.125) == (b'\x00'*15 + b'\x01\x20' + b'\x00'*15)
3838
assert abi.encode_single(['ureal', '128x128', []], 2**127-1) == (b'\x7f' + b'\xff'*15 + b'\x00'*16)
3939

4040
def test_abi_encode_single_real():
@@ -51,10 +51,10 @@ def test_abi_decode_single_hash():
5151

5252
def test_abi_decode_single_bytes():
5353
typ = ['bytes', '8', []]
54-
assert (b'\x01\x02' + b'\x00'*6) == abi.decode_single(typ, abi.encode_single(typ, '\x01\x02'))
54+
assert (b'\x01\x02' + b'\x00'*6) == abi.decode_single(typ, abi.encode_single(typ, b'\x01\x02'))
5555

5656
typ = ['bytes', '', []]
57-
assert b'\x01\x02' == abi.decode_single(typ, abi.encode_single(typ, '\x01\x02'))
57+
assert b'\x01\x02' == abi.decode_single(typ, abi.encode_single(typ, b'\x01\x02'))
5858

5959
def test_abi_encode_single_prefixed_address():
6060
prefixed_address = '0x' + '0'*40

ethereum/tests/test_contracts.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,8 @@ def test_ecrecover():
12071207
signature = pk.ecdsa_recoverable_serialize(
12081208
pk.ecdsa_sign_recoverable(msghash, raw=True)
12091209
)
1210-
signature = signature[0] + chr(signature[1])
1211-
V = ord(signature[64]) + 27
1210+
signature = signature[0] + utils.bytearray_to_bytestr([signature[1]])
1211+
V = utils.safe_ord(signature[64]) + 27
12121212
R = big_endian_to_int(signature[0:32])
12131213
S = big_endian_to_int(signature[32:64])
12141214

@@ -1629,9 +1629,15 @@ def test_string_logging():
16291629
o = []
16301630
s.block.log_listeners.append(lambda x: o.append(c.translator.listen(x)))
16311631
c.moo()
1632-
assert o == [{"_event_type": "foo", "x": "bob", "__hash_x": utils.sha3("bob"),
1633-
"y": "cow", "__hash_y": utils.sha3("cow"), "z": "dog",
1634-
"__hash_z": utils.sha3("dog")}]
1632+
assert o == [{
1633+
"_event_type": b"foo",
1634+
"x": b"bob",
1635+
"__hash_x": utils.sha3(b"bob"),
1636+
"y": b"cow",
1637+
"__hash_y": utils.sha3(b"cow"),
1638+
"z": b"dog",
1639+
"__hash_z": utils.sha3(b"dog"),
1640+
}]
16351641

16361642

16371643
params_code = """
@@ -1654,7 +1660,7 @@ def test_params_contract():
16541660
s = tester.state()
16551661
c = s.abi_contract(params_code, FOO=4, BAR='horse')
16561662
assert c.garble() == 4
1657-
assert c.marble() == 'horse'
1663+
assert c.marble() == b'horse'
16581664

16591665
prefix_types_in_functions_code = """
16601666
type fixedp: fp_

ethereum/testutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def acct_standard_form(a):
6767
"nonce": parse_int_or_hex(a["nonce"]),
6868
"code": to_string(a["code"]),
6969
"storage": {normalize_hex(k): normalize_hex(v) for
70-
k, v in a["storage"].items() if normalize_hex(v).rstrip('0') != '0x'}
70+
k, v in a["storage"].items() if normalize_hex(v).rstrip(b'0') != b'0x'}
7171
}
7272

7373

ethereum/transactions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def sender(self):
8787
pk.public_key = pk.ecdsa_recover(
8888
rawhash,
8989
pk.ecdsa_recoverable_deserialize(
90-
zpad("".join(chr(c) for c in int_to_32bytearray(self.r)), 32) + zpad("".join(chr(c) for c in int_to_32bytearray(self.s)), 32),
90+
zpad(utils.bytearray_to_bytestr(int_to_32bytearray(self.r)), 32) + zpad(utils.bytearray_to_bytestr(int_to_32bytearray(self.s)), 32),
9191
self.v - 27
9292
),
9393
raw=True
@@ -96,7 +96,7 @@ def sender(self):
9696
except Exception:
9797
raise InvalidTransaction("Invalid signature values (x^3+7 is non-residue)")
9898

99-
if pub[1:] == "\x00" * 32:
99+
if pub[1:] == b"\x00" * 32:
100100
raise InvalidTransaction("Invalid signature (zero privkey cannot sign)")
101101
pub = encode_pubkey(pub, 'bin')
102102
self._sender = utils.sha3(pub[1:])[-20:]
@@ -114,7 +114,7 @@ def sign(self, key):
114114
115115
A potentially already existing signature would be overridden.
116116
"""
117-
if key in (0, '', '\x00' * 32, '0' * 64):
117+
if key in (0, '', b'\x00' * 32, '0' * 64):
118118
raise InvalidTransaction("Zero privkey cannot sign")
119119
rawhash = utils.sha3(rlp.encode(self, UnsignedTransaction))
120120

@@ -126,8 +126,8 @@ def sign(self, key):
126126
signature = pk.ecdsa_recoverable_serialize(
127127
pk.ecdsa_sign_recoverable(rawhash, raw=True)
128128
)
129-
signature = signature[0] + chr(signature[1])
130-
self.v = ord(signature[64]) + 27
129+
signature = signature[0] + utils.bytearray_to_bytestr([signature[1]])
130+
self.v = utils.safe_ord(signature[64]) + 27
131131
self.r = big_endian_to_int(signature[0:32])
132132
self.s = big_endian_to_int(signature[32:64])
133133

0 commit comments

Comments
 (0)