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

Commit 4d0e73d

Browse files
committed
bytes strings everywhere
1 parent 18e7d3a commit 4d0e73d

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

ethereum/abi.py

+39-40
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def __init__(self, full_signature):
2121
for sig_item in full_signature:
2222
encode_types = [f['type'] for f in sig_item['inputs']]
2323
name = sig_item['name']
24-
if '(' in name:
25-
name = name[:name.find('(')]
24+
if b'(' in name:
25+
name = name[:name.find(b'(')]
2626
if name in v:
2727
i = 2
2828
while name + utils.to_string(i) in v:
@@ -31,9 +31,8 @@ def __init__(self, full_signature):
3131
sys.stderr.write("Warning: multiple methods with the same "
3232
" name. Use %s to call %s with types %r"
3333
% (name, sig_item['name'], encode_types))
34-
sig = name + '(' + ','.join(encode_types) + ')'
35-
sig = sig.encode('utf-8')
36-
if sig_item['type'] == 'function':
34+
sig = name + b'(' + b','.join(encode_types) + b')'
35+
if sig_item['type'] == b'function':
3736
prefix = big_endian_to_int(utils.sha3(sig)[:4])
3837
decode_types = [f['type'] for f in sig_item['outputs']]
3938
is_unknown_type = len(sig_item['outputs']) and \
@@ -44,7 +43,7 @@ def __init__(self, full_signature):
4443
"decode_types": decode_types,
4544
"is_unknown_type": is_unknown_type
4645
}
47-
elif sig_item['type'] == 'event':
46+
elif sig_item['type'] == b'event':
4847
prefix = big_endian_to_int(utils.sha3(sig))
4948
indexed = [f['indexed'] for f in sig_item['inputs']]
5049
names = [f['name'] for f in sig_item['inputs']]
@@ -121,30 +120,30 @@ def decint(n):
121120
def encode_single(arg, base, sub):
122121
normal_args, len_args, var_args = b'', b'', b''
123122
# Unsigned integers: uint<sz>
124-
if base == 'uint':
123+
if base == b'uint':
125124
sub = int(sub)
126125
i = decint(arg)
127126
assert 0 <= i < 2**sub, "Value out of bounds: %r" % arg
128127
normal_args = zpad(encode_int(i), 32)
129128
# Signed integers: int<sz>
130-
elif base == 'int':
129+
elif base == b'int':
131130
sub = int(sub)
132131
i = decint(arg)
133132
assert -2**(sub - 1) <= i < 2**sub, "Value out of bounds: %r" % arg
134133
normal_args = zpad(encode_int(i % 2**sub), 32)
135134
# Unsigned reals: ureal<high>x<low>
136-
elif base == 'ureal':
135+
elif base == b'ureal':
137136
high, low = [int(x) for x in sub.split('x')]
138137
assert 0 <= arg < 2**high, "Value out of bounds: %r" % arg
139138
normal_args = zpad(encode_int(arg * 2**low), 32)
140139
# Signed reals: real<high>x<low>
141-
elif base == 'real':
140+
elif base == b'real':
142141
high, low = [int(x) for x in sub.split('x')]
143142
assert -2**(high - 1) <= arg < 2**(high - 1), \
144143
"Value out of bounds: %r" % arg
145144
normal_args = zpad(encode_int((arg % 2**high) * 2**low), 32)
146145
# Strings
147-
elif base == 'string':
146+
elif base == b'string':
148147
if not is_string(arg):
149148
raise Exception("Expecting string: %r" % arg)
150149
# Fixed length: string<sz>
@@ -157,7 +156,7 @@ def encode_single(arg, base, sub):
157156
len_args = zpad(encode_int(len(arg)), 32)
158157
var_args = arg
159158
# Hashes: hash<sz>
160-
elif base == 'hash':
159+
elif base == b'hash':
161160
assert int(sub) and int(sub) <= 32
162161
if isinstance(arg, int):
163162
normal_args = zpad(encode_int(arg), 32)
@@ -168,8 +167,8 @@ def encode_single(arg, base, sub):
168167
else:
169168
raise Exception("Could not parse hash: %r" % arg)
170169
# Addresses: address (== hash160)
171-
elif base == 'address':
172-
assert sub == ''
170+
elif base == b'address':
171+
assert sub == b''
173172
if isinstance(arg, int):
174173
normal_args = zpad(encode_int(arg), 32)
175174
elif len(arg) == 20:
@@ -184,43 +183,43 @@ def encode_single(arg, base, sub):
184183
def process_type(typ):
185184
# Crazy reg expression to separate out base type component (eg. uint),
186185
# size (eg. 256, 128x128, none), array component (eg. [], [45], none)
187-
regexp = '([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)'
186+
regexp = b'([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)'
188187
base, sub, arr, _ = re.match(regexp, typ).groups()
189-
arrlist = re.findall('\[[0-9]*\]', arr)
190-
assert len(''.join(arrlist)) == len(arr), \
188+
arrlist = re.findall(b'\[[0-9]*\]', arr)
189+
assert len(b''.join(arrlist)) == len(arr), \
191190
"Unknown characters found in array declaration"
192191
# Only outermost array can be var-sized
193192
for a in arrlist[:-1]:
194193
assert len(a) > 2, "Inner arrays must have fixed size"
195194
# Check validity of string type
196-
if base == 'string':
197-
assert re.match('^[0-9]*$', sub), \
195+
if base == b'string':
196+
assert re.match(b'^[0-9]*$', sub), \
198197
"String type must have no suffix or numerical suffix"
199198
assert len(sub) or len(arrlist) == 0, \
200199
"Cannot have an array of var-sized strings"
201200
# Check validity of integer type
202-
elif base == 'uint' or base == 'int':
203-
assert re.match('^[0-9]+$', sub), \
201+
elif base == b'uint' or base == b'int':
202+
assert re.match(b'^[0-9]+$', sub), \
204203
"Integer type must have numerical suffix"
205204
assert 8 <= int(sub) <= 256, \
206205
"Integer size out of bounds"
207206
assert int(sub) % 8 == 0, \
208207
"Integer size must be multiple of 8"
209208
# Check validity of real type
210-
elif base == 'ureal' or base == 'real':
211-
assert re.match('^[0-9]+x[0-9]+$', sub), \
209+
elif base == b'ureal' or base == b'real':
210+
assert re.match(b'^[0-9]+x[0-9]+$', sub), \
212211
"Real type must have suffix of form <high>x<low>, eg. 128x128"
213-
high, low = [int(x) for x in sub.split('x')]
212+
high, low = [int(x) for x in sub.split(b'x')]
214213
assert 8 <= (high + low) <= 256, \
215214
"Real size out of bounds (max 32 bytes)"
216215
assert high % 8 == 0 and low % 8 == 0, \
217216
"Real high/low sizes must be multiples of 8"
218217
# Check validity of hash type
219-
elif base == 'hash':
220-
assert re.match('^[0-9]+$', sub), \
218+
elif base == b'hash':
219+
assert re.match(b'^[0-9]+$', sub), \
221220
"Hash type must have numerical suffix"
222221
# Check validity of address type
223-
elif base == 'address':
222+
elif base == b'address':
224223
assert sub == '', "Address cannot have suffix"
225224
return base, sub, arrlist
226225

@@ -232,7 +231,7 @@ def encode_any(arg, base, sub, arrlist):
232231
return encode_single(arg, base, sub)
233232
# Variable-sized arrays
234233
if arrlist[-1] == '[]':
235-
if base == 'string' and sub == '':
234+
if base == b'string' and sub == b'':
236235
raise Exception('Array of dynamic-sized items not allowed: %r'
237236
% arg)
238237
o = b''
@@ -243,7 +242,7 @@ def encode_any(arg, base, sub, arrlist):
243242
return zpad(encode_int(len(arg)), 32), b'', o
244243
# Fixed-sized arrays
245244
else:
246-
if base == 'string' and sub == '':
245+
if base == b'string' and sub == b'':
247246
raise Exception('Array of dynamic-sized items not allowed')
248247
sz = int(arrlist[-1][1:-1])
249248
assert isinstance(arg, list), "Expecting array: %r" % arg
@@ -273,11 +272,11 @@ def encode_abi(types, args):
273272

274273
def is_varsized(base, sub, arrlist):
275274
return (len(arrlist) and arrlist[-1] == '[]') or \
276-
(base == 'string' and sub == '')
275+
(base == b'string' and sub == b'')
277276

278277

279278
def getlen(base, sub, arrlist):
280-
if base == 'string' and not len(sub):
279+
if base == b'string' and not len(sub):
281280
sz = 1
282281
else:
283282
sz = 32
@@ -288,22 +287,22 @@ def getlen(base, sub, arrlist):
288287

289288

290289
def decode_single(data, base, sub):
291-
if base == 'address':
290+
if base == b'address':
292291
return encode_hex(data[12:])
293-
elif base == 'string' or base == 'hash':
292+
elif base == b'string' or base == b'hash':
294293
return data[:int(sub)] if len(sub) else data
295-
elif base == 'uint':
294+
elif base == b'uint':
296295
return big_endian_to_int(data)
297-
elif base == 'int':
296+
elif base == b'int':
298297
o = big_endian_to_int(data)
299298
return (o - 2**int(sub)) if o >= 2**(int(sub) - 1) else o
300-
elif base == 'ureal':
301-
high, low = [int(x) for x in sub.split('x')]
299+
elif base == b'ureal':
300+
high, low = [int(x) for x in sub.split(b'x')]
302301
return big_endian_to_int(data) * 1.0 / 2**low
303-
elif base == 'real':
304-
high, low = [int(x) for x in sub.split('x')]
302+
elif base == b'real':
303+
high, low = [int(x) for x in sub.split(b'x')]
305304
return (big_endian_to_int(data) * 1.0 / 2**low) % 2**high
306-
elif base == 'bool':
305+
elif base == b'bool':
307306
return bool(int(data.encode('hex'), 16))
308307

309308

ethereum/tester.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def kall_factory(f):
122122
def kall(*args, **kwargs):
123123
_state.block.log_listeners.append(
124124
lambda log: self._translator.listen(log))
125+
125126
o = _state._send(kwargs.get('sender', k0),
126127
self.address,
127128
kwargs.get('value', 0),
@@ -146,7 +147,7 @@ def kall(*args, **kwargs):
146147
return kall
147148

148149
for f in self._translator.function_data:
149-
vars(self)[f] = kall_factory(f)
150+
vars(self)[f.decode('utf-8')] = kall_factory(f)
150151

151152
return _abi_contract(me, code, sender, endowment, language)
152153

@@ -175,6 +176,7 @@ def _send(self, sender, to, value, evmdata='', output=None,
175176
" the abi_contract mechanism")
176177
tm, g = time.time(), self.block.gas_used
177178
sendnonce = self.block.get_nonce(u.privtoaddr(sender))
179+
178180
tx = t.Transaction(sendnonce, 1, gas_limit, to, value, evmdata)
179181
self.last_tx = tx
180182
tx.sign(sender)

0 commit comments

Comments
 (0)