Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ethereum/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ethereum import utils
from ethereum.utils import encode_int, zpad, big_endian_to_int, is_numeric, is_string, ceil32
from ethereum.utils import isnumeric, TT256, TT255
from ethereum.utils import TT256, TT255


def json_decode(data):
Expand Down Expand Up @@ -327,7 +327,7 @@ def encode_single(typ, arg):
elif base == 'hash':
if not (int(sub) and int(sub) <= 32):
raise EncodingError("too long: %r" % arg)
if isnumeric(arg):
if is_numeric(arg):
return zpad(encode_int(arg), 32)
elif len(arg) == int(sub):
return zpad(arg, 32)
Expand All @@ -338,13 +338,13 @@ def encode_single(typ, arg):
# Addresses: address (== hash160)
elif base == 'address':
assert sub == ''
if isnumeric(arg):
if is_numeric(arg):
return zpad(encode_int(arg), 32)
elif len(arg) == 20:
return zpad(arg, 32)
elif len(arg) == 40:
return zpad(decode_hex(arg), 32)
elif len(arg) == 42 and arg[:2] == '0x':
elif len(arg) == 42 and arg[:2] in {'0x', b'0x'}:
return zpad(decode_hex(arg[2:]), 32)
else:
raise EncodingError("Could not parse address: %r" % arg)
Expand Down
11 changes: 6 additions & 5 deletions ethereum/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def _delta_item(self, address, param, value):
def mk_transaction_receipt(self, tx):
"""Create a receipt for a transaction."""
if self.number >= self.config["METROPOLIS_FORK_BLKNUM"]:
return Receipt('\x00' * 32, self.gas_used, self.logs)
return Receipt(b'\x00' * 32, self.gas_used, self.logs)
else:
return Receipt(self.state_root, self.gas_used, self.logs)

Expand Down Expand Up @@ -984,7 +984,7 @@ def get_storage_bytes(self, address, index):
if storage:
return rlp.decode(storage)
else:
return ''
return b''

def get_storage_data(self, address, index):
bytez = self.get_storage_bytes(address, index)
Expand Down Expand Up @@ -1112,7 +1112,7 @@ def account_to_dict(self, address, with_storage_root=False,
v2 = subcache.get(utils.big_endian_to_int(k), None)
hexkey = b'0x' + encode_hex(utils.zunpad(k))
if v2 is not None:
if v2 != '':
if v2 != b'':
med_dict['storage'][hexkey] = \
b'0x' + encode_hex(v2)
elif v is not None:
Expand Down Expand Up @@ -1447,8 +1447,9 @@ def genesis(env, **kwargs):
block.set_nonce(addr, utils.parse_int_or_hex(data['nonce']))
if 'storage' in data:
for k, v in data['storage'].items():
block.set_storage_data(addr, utils.big_endian_to_int(decode_hex(k[2:])),
utils.big_endian_to_int(decode_hex(v[2:])))
block.set_storage_data(addr,
utils.big_endian_to_int(decode_hex(k[2:])),
decode_hex(v[2:]))
block.commit_state()
block.state.db.commit()
# genesis block has predefined state root (so no additional finalization
Expand Down
4 changes: 3 additions & 1 deletion ethereum/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rlp.utils import decode_hex

from ethereum import utils
from ethereum.db import BaseDB

Expand Down Expand Up @@ -52,7 +54,7 @@
METROPOLIS_STATEROOT_STORE=0x10,
METROPOLIS_BLOCKHASH_STORE=0x20,
METROPOLIS_WRAPAROUND=65536,
METROPOLIS_GETTER_CODE='6000355460205260206020f3'.decode('hex'),
METROPOLIS_GETTER_CODE=decode_hex('6000355460205260206020f3'),
METROPOLIS_DIFF_ADJUSTMENT_CUTOFF=9,
# Metropolis fork
)
Expand Down
2 changes: 1 addition & 1 deletion ethereum/ethpow.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def mine(self, rounds=1000, start_nonce=0):


def mine(block_number, difficulty, mining_hash, start_nonce=0, rounds=1000):
assert utils.isnumeric(start_nonce)
assert utils.is_numeric(start_nonce)
cache = get_cache(block_number)
nonce = start_nonce
target = utils.zpad(utils.int_to_big_endian(2**256 // (difficulty or 1)), 32)
Expand Down
6 changes: 3 additions & 3 deletions ethereum/processblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def rp(what, actual, target):
return '%r: %r actual:%r target:%r' % (tx, what, actual, target)

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

print 'b2', block.get_balance(tx.sender)
print('b2', block.get_balance(tx.sender))
# MESSAGE
ext = VMExt(block, tx)
if tx.to and tx.to != CREATE_CONTRACT_ADDRESS:
Expand Down Expand Up @@ -215,7 +215,7 @@ def rp(what, actual, target):
block.del_account(s)
block.add_transaction_to_list(tx)
block.logs = []
print 'b3', block.get_balance(tx.sender), success
print('b3', block.get_balance(tx.sender), success)
return success, output


Expand Down
33 changes: 30 additions & 3 deletions ethereum/slogging.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys
import logging
import json
import textwrap
from json.encoder import JSONEncoder
from logging import StreamHandler, Formatter, FileHandler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konradkonrad removing these two imports makes this module independent from the rest of the package.

from ethereum.utils import bcolors, isnumeric


DEFAULT_LOGLEVEL = 'INFO'
Expand All @@ -20,6 +20,33 @@
log_listeners = []


if sys.version_info.major == 2:
integer_types = (int, long)
number_types = (int, long, float, complex)
else:
integer_types = (int,)
number_types = (int, float, complex)


def is_integer(value):
return isinstance(value, integer_types)


def is_number(value):
return isinstance(value, number_types)


class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[91m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konradkonrad it lives here now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah alright then, I missed that!



def _inject_into_logger(name, code, namespace=None):
# This is a hack to fool the logging module into reporting correct source files.
# It determines the actual source of a logging call by inspecting the stack frame's
Expand Down Expand Up @@ -190,7 +217,7 @@ def format_message(self, msg, kwargs, highlight, level):
msg = json.dumps(message, cls=_LogJSONEncoder)
except UnicodeDecodeError:
message.update({
k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v)
k: v if is_number(v) else repr(v)
for k, v in kwargs.items()
})
msg = json.dumps(message, cls=_LogJSONEncoder)
Expand Down Expand Up @@ -260,7 +287,7 @@ def _stringify_dict_keys(input_):
res = {}
for k, v in input_.items():
v = _stringify_dict_keys(v)
if not isinstance(k, (int, long, bool, None.__class__)):
if not is_integer(k) and not isinstance(k, (bool, None.__class__)):
k = str(k)
res[k] = v
elif isinstance(input_, (list, tuple)):
Expand Down
4 changes: 2 additions & 2 deletions ethereum/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ def blkhash(n):
_shouldbe = params1.get(k, None)
_reallyis = params2.get(k, None)
if _shouldbe != _reallyis:
print 's', shouldbe
print 'r', reallyis
print('s', shouldbe)
print('r', reallyis)
raise Exception("Mismatch: " + k + ':\n shouldbe %r\n reallyis %r' %
(_shouldbe, _reallyis))

Expand Down
23 changes: 5 additions & 18 deletions ethereum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def to_string_for_regexp(value):
def bytearray_to_bytestr(value):
return bytes(value)

isnumeric = is_numeric


def mk_contract_address(sender, nonce):
return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]
Expand Down Expand Up @@ -119,7 +117,7 @@ def sha3(seed):
sha3_count[0] += 1
return sha3_256(to_string(seed))

assert encode_hex(sha3('')) == b'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
assert encode_hex(sha3(b'')) == b'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'


def privtoaddr(x, extended=False):
Expand All @@ -145,11 +143,11 @@ def check_and_strip_checksum(x):


def normalize_address(x, allow_blank=False):
if isinstance(x, (int, long)):
if is_numeric(x):
return int_to_addr(x)
if allow_blank and (x == '' or x == b''):
if allow_blank and x in {'', b''}:
return b''
if len(x) in (42, 50) and x[:2] == '0x':
if len(x) in (42, 50) and x[:2] in {'0x', b'0x'}:
x = x[2:]
if len(x) in (40, 48):
x = decode_hex(x)
Expand All @@ -173,7 +171,7 @@ def zunpad(x):


def int_to_addr(x):
o = [''] * 20
o = [b''] * 20
for i in range(20):
o[19 - i] = ascii_chr(x & 0xff)
x >>= 8
Expand Down Expand Up @@ -428,17 +426,6 @@ def __init__(self):
trie_root = Binary.fixed_length(32, allow_empty=True)


class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[91m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'


def DEBUG(msg, *args, **kwargs):
from ethereum import slogging

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only used by the slogging module so I moved it over to there. Happy to move it back if it's a problem.

Expand Down
2 changes: 1 addition & 1 deletion ethereum/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def vm_execute(ext, msg, code):
if compustate.gas < gas_payment:
return vm_exception('OUT OF GAS')
compustate.gas -= gas_payment
data = ''.join(map(chr, mem[mstart: mstart + msize]))
data = b''.join(map(chr, mem[mstart: mstart + msize]))
ext.set_storage_data(msg.to, data)
ext.add_refund(refund)
elif op == 'SSIZE':
Expand Down