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

Commit 4e1f64f

Browse files
authored
Merge pull request #369 from pipermerriam/piper/fix-block-tests-invalid-state
Piper/fix block tests invalid state
2 parents 9cd646f + fd0a4ff commit 4e1f64f

File tree

9 files changed

+55
-38
lines changed

9 files changed

+55
-38
lines changed

ethereum/abi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ethereum import utils
1010
from ethereum.utils import encode_int, zpad, big_endian_to_int, is_numeric, is_string, ceil32
11-
from ethereum.utils import isnumeric, TT256, TT255
11+
from ethereum.utils import TT256, TT255
1212

1313

1414
def json_decode(data):
@@ -327,7 +327,7 @@ def encode_single(typ, arg):
327327
elif base == 'hash':
328328
if not (int(sub) and int(sub) <= 32):
329329
raise EncodingError("too long: %r" % arg)
330-
if isnumeric(arg):
330+
if is_numeric(arg):
331331
return zpad(encode_int(arg), 32)
332332
elif len(arg) == int(sub):
333333
return zpad(arg, 32)
@@ -338,13 +338,13 @@ def encode_single(typ, arg):
338338
# Addresses: address (== hash160)
339339
elif base == 'address':
340340
assert sub == ''
341-
if isnumeric(arg):
341+
if is_numeric(arg):
342342
return zpad(encode_int(arg), 32)
343343
elif len(arg) == 20:
344344
return zpad(arg, 32)
345345
elif len(arg) == 40:
346346
return zpad(decode_hex(arg), 32)
347-
elif len(arg) == 42 and arg[:2] == '0x':
347+
elif len(arg) == 42 and arg[:2] in {'0x', b'0x'}:
348348
return zpad(decode_hex(arg[2:]), 32)
349349
else:
350350
raise EncodingError("Could not parse address: %r" % arg)

ethereum/blocks.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def _delta_item(self, address, param, value):
793793
def mk_transaction_receipt(self, tx):
794794
"""Create a receipt for a transaction."""
795795
if self.number >= self.config["METROPOLIS_FORK_BLKNUM"]:
796-
return Receipt('\x00' * 32, self.gas_used, self.logs)
796+
return Receipt(b'\x00' * 32, self.gas_used, self.logs)
797797
else:
798798
return Receipt(self.state_root, self.gas_used, self.logs)
799799

@@ -983,7 +983,7 @@ def get_storage_bytes(self, address, index):
983983
if storage:
984984
return rlp.decode(storage)
985985
else:
986-
return ''
986+
return b''
987987

988988
def get_storage_data(self, address, index):
989989
bytez = self.get_storage_bytes(address, index)
@@ -1111,7 +1111,7 @@ def account_to_dict(self, address, with_storage_root=False,
11111111
v2 = subcache.get(utils.big_endian_to_int(k), None)
11121112
hexkey = b'0x' + encode_hex(utils.zunpad(k))
11131113
if v2 is not None:
1114-
if v2 != '':
1114+
if v2 != b'':
11151115
med_dict['storage'][hexkey] = \
11161116
b'0x' + encode_hex(v2)
11171117
elif v is not None:
@@ -1454,8 +1454,9 @@ def genesis(env, **kwargs):
14541454
block.set_nonce(addr, utils.parse_int_or_hex(data['nonce']))
14551455
if 'storage' in data:
14561456
for k, v in data['storage'].items():
1457-
block.set_storage_data(addr, utils.big_endian_to_int(decode_hex(k[2:])),
1458-
utils.big_endian_to_int(decode_hex(v[2:])))
1457+
block.set_storage_data(addr,
1458+
utils.big_endian_to_int(decode_hex(k[2:])),
1459+
decode_hex(v[2:]))
14591460
block.commit_state()
14601461
block.state.db.commit()
14611462
# genesis block has predefined state root (so no additional finalization

ethereum/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from rlp.utils import decode_hex
2+
13
from ethereum import utils
24
from ethereum.db import BaseDB
35

@@ -52,7 +54,7 @@
5254
METROPOLIS_STATEROOT_STORE=0x10,
5355
METROPOLIS_BLOCKHASH_STORE=0x20,
5456
METROPOLIS_WRAPAROUND=65536,
55-
METROPOLIS_GETTER_CODE='6000355460205260206020f3'.decode('hex'),
57+
METROPOLIS_GETTER_CODE=decode_hex('6000355460205260206020f3'),
5658
METROPOLIS_DIFF_ADJUSTMENT_CUTOFF=9,
5759
# DAO fork
5860
DAO_FORK_BLKNUM = 9999998,

ethereum/ethpow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def mine(self, rounds=1000, start_nonce=0):
107107

108108

109109
def mine(block_number, difficulty, mining_hash, start_nonce=0, rounds=1000):
110-
assert utils.isnumeric(start_nonce)
110+
assert utils.is_numeric(start_nonce)
111111
cache = get_cache(block_number)
112112
nonce = start_nonce
113113
target = utils.zpad(utils.int_to_big_endian(2**256 // (difficulty or 1)), 32)

ethereum/processblock.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def rp(what, actual, target):
146146
return '%r: %r actual:%r target:%r' % (tx, what, actual, target)
147147

148148
intrinsic_gas = tx.intrinsic_gas_used
149-
print 'b1', block.get_balance(tx.sender)
149+
print('b1', block.get_balance(tx.sender))
150150
if block.number >= block.config['HOMESTEAD_FORK_BLKNUM']:
151151
assert tx.s * 2 < transactions.secpk1n
152152
if not tx.to or tx.to == CREATE_CONTRACT_ADDRESS:
@@ -165,7 +165,7 @@ def rp(what, actual, target):
165165
message_data = vm.CallData([safe_ord(x) for x in tx.data], 0, len(tx.data))
166166
message = vm.Message(tx.sender, tx.to, tx.value, message_gas, message_data, code_address=tx.to)
167167

168-
print 'b2', block.get_balance(tx.sender)
168+
print('b2', block.get_balance(tx.sender))
169169
# MESSAGE
170170
ext = VMExt(block, tx)
171171
if tx.to and tx.to != CREATE_CONTRACT_ADDRESS:
@@ -215,7 +215,7 @@ def rp(what, actual, target):
215215
block.del_account(s)
216216
block.add_transaction_to_list(tx)
217217
block.logs = []
218-
print 'b3', block.get_balance(tx.sender), success
218+
print('b3', block.get_balance(tx.sender), success)
219219
return success, output
220220

221221

ethereum/slogging.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import sys
12
import logging
23
import json
34
import textwrap
45
from json.encoder import JSONEncoder
56
from logging import StreamHandler, Formatter, FileHandler
6-
from ethereum.utils import bcolors, isnumeric
77

88

99
DEFAULT_LOGLEVEL = 'INFO'
@@ -20,6 +20,33 @@
2020
log_listeners = []
2121

2222

23+
if sys.version_info.major == 2:
24+
integer_types = (int, long)
25+
number_types = (int, long, float, complex)
26+
else:
27+
integer_types = (int,)
28+
number_types = (int, float, complex)
29+
30+
31+
def is_integer(value):
32+
return isinstance(value, integer_types)
33+
34+
35+
def is_number(value):
36+
return isinstance(value, number_types)
37+
38+
39+
class bcolors:
40+
HEADER = '\033[95m'
41+
OKBLUE = '\033[94m'
42+
OKGREEN = '\033[92m'
43+
WARNING = '\033[91m'
44+
FAIL = '\033[91m'
45+
ENDC = '\033[0m'
46+
BOLD = '\033[1m'
47+
UNDERLINE = '\033[4m'
48+
49+
2350
def _inject_into_logger(name, code, namespace=None):
2451
# This is a hack to fool the logging module into reporting correct source files.
2552
# It determines the actual source of a logging call by inspecting the stack frame's
@@ -190,7 +217,7 @@ def format_message(self, msg, kwargs, highlight, level):
190217
msg = json.dumps(message, cls=_LogJSONEncoder)
191218
except UnicodeDecodeError:
192219
message.update({
193-
k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v)
220+
k: v if is_number(v) else repr(v)
194221
for k, v in kwargs.items()
195222
})
196223
msg = json.dumps(message, cls=_LogJSONEncoder)
@@ -260,7 +287,7 @@ def _stringify_dict_keys(input_):
260287
res = {}
261288
for k, v in input_.items():
262289
v = _stringify_dict_keys(v)
263-
if not isinstance(k, (int, long, bool, None.__class__)):
290+
if not is_integer(k) and not isinstance(k, (bool, None.__class__)):
264291
k = str(k)
265292
res[k] = v
266293
elif isinstance(input_, (list, tuple)):

ethereum/testutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ def blkhash(n):
502502
_shouldbe = params1.get(k, None)
503503
_reallyis = params2.get(k, None)
504504
if _shouldbe != _reallyis:
505-
print 's', shouldbe
506-
print 'r', reallyis
505+
print('s', shouldbe)
506+
print('r', reallyis)
507507
raise Exception("Mismatch: " + k + ':\n shouldbe %r\n reallyis %r' %
508508
(_shouldbe, _reallyis))
509509

ethereum/utils.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def to_string_for_regexp(value):
6262
def bytearray_to_bytestr(value):
6363
return bytes(value)
6464

65-
isnumeric = is_numeric
66-
6765

6866
def mk_contract_address(sender, nonce):
6967
return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]
@@ -120,7 +118,7 @@ def sha3(seed):
120118
# print seed
121119
return sha3_256(to_string(seed))
122120

123-
assert encode_hex(sha3('')) == b'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
121+
assert encode_hex(sha3(b'')) == b'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
124122

125123

126124
def privtoaddr(x, extended=False):
@@ -146,11 +144,11 @@ def check_and_strip_checksum(x):
146144

147145

148146
def normalize_address(x, allow_blank=False):
149-
if isinstance(x, (int, long)):
147+
if is_numeric(x):
150148
return int_to_addr(x)
151-
if allow_blank and (x == '' or x == b''):
149+
if allow_blank and x in {'', b''}:
152150
return b''
153-
if len(x) in (42, 50) and x[:2] == '0x':
151+
if len(x) in (42, 50) and x[:2] in {'0x', b'0x'}:
154152
x = x[2:]
155153
if len(x) in (40, 48):
156154
x = decode_hex(x)
@@ -174,7 +172,7 @@ def zunpad(x):
174172

175173

176174
def int_to_addr(x):
177-
o = [''] * 20
175+
o = [b''] * 20
178176
for i in range(20):
179177
o[19 - i] = ascii_chr(x & 0xff)
180178
x >>= 8
@@ -429,17 +427,6 @@ def __init__(self):
429427
trie_root = Binary.fixed_length(32, allow_empty=True)
430428

431429

432-
class bcolors:
433-
HEADER = '\033[95m'
434-
OKBLUE = '\033[94m'
435-
OKGREEN = '\033[92m'
436-
WARNING = '\033[91m'
437-
FAIL = '\033[91m'
438-
ENDC = '\033[0m'
439-
BOLD = '\033[1m'
440-
UNDERLINE = '\033[4m'
441-
442-
443430
def DEBUG(msg, *args, **kwargs):
444431
from ethereum import slogging
445432

ethereum/vm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def vm_execute(ext, msg, code):
482482
if compustate.gas < gas_payment:
483483
return vm_exception('OUT OF GAS')
484484
compustate.gas -= gas_payment
485-
data = ''.join(map(chr, mem[mstart: mstart + msize]))
485+
data = b''.join(map(chr, mem[mstart: mstart + msize]))
486486
ext.set_storage_data(msg.to, data)
487487
ext.add_refund(refund)
488488
elif op == 'SSIZE':

0 commit comments

Comments
 (0)