diff --git a/pyethereum/bloom.py b/pyethereum/bloom.py index 252a4a00c..12bdff1af 100644 --- a/pyethereum/bloom.py +++ b/pyethereum/bloom.py @@ -1,4 +1,5 @@ from pyethereum import utils +from pyethereum.utils import safe_ord from pyethereum.abi import is_numeric """ Blooms are the 3-point, 2048-bit (11-bits/point) Bloom filter of each @@ -28,13 +29,13 @@ def bloom_insert(bloom, val): h = utils.sha3(val) # print 'bloom_insert', bloom_bits(val), repr(val) for i in range(0, BUCKETS_PER_VAL * 2, 2): - bloom |= 1 << ((ord(h[i + 1]) + (ord(h[i]) << 8)) & 2047) + bloom |= 1 << ((safe_ord(h[i + 1]) + (safe_ord(h[i]) << 8)) & 2047) return bloom def bloom_bits(val): h = utils.sha3(val) - return [bits_in_number(1 << ((ord(h[i + 1]) + (ord(h[i]) << 8)) & 2047)) for i in range(0, BUCKETS_PER_VAL * 2, 2)] + return [bits_in_number(1 << ((safe_ord(h[i + 1]) + (safe_ord(h[i]) << 8)) & 2047)) for i in range(0, BUCKETS_PER_VAL * 2, 2)] def bits_in_number(val): diff --git a/pyethereum/compress.py b/pyethereum/compress.py index afeeab29e..36a9f83e2 100644 --- a/pyethereum/compress.py +++ b/pyethereum/compress.py @@ -1,5 +1,6 @@ from rlp.utils import decode_hex, encode_hex, ascii_chr + NULLSHA3 = decode_hex('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470') @@ -25,6 +26,7 @@ def compress(data): def decompress(data): + from pyethereum.utils import safe_ord o = '' i = 0 while i < len(data): @@ -36,7 +38,7 @@ def decompress(data): elif data[i + 1] == '\x01': o += NULLSHA3 else: - o += '\x00' * ord(data[i + 1]) + o += '\x00' * safe_ord(data[i + 1]) i += 1 else: o += data[i] diff --git a/pyethereum/fastvm.py b/pyethereum/fastvm.py index 2e4de5e0c..099c36ce0 100644 --- a/pyethereum/fastvm.py +++ b/pyethereum/fastvm.py @@ -1,5 +1,6 @@ from pyethereum.opcodes import opcodes, reverse_opcodes from pyethereum import utils +from pyethereum.utils import safe_ord import serpent import rlp from rlp.utils import decode_hex, encode_hex, ascii_chr @@ -188,14 +189,14 @@ def preprocess_vmcode(code): o = [] jumps = {} # Round 1: Locate all JUMP, JUMPI, JUMPDEST, STOP, RETURN, INVALID locs - opcodez = copy.deepcopy([opcodes.get(ord(c), ['INVALID', 0, 0, 1]) + - [ord(c)] for c in code]) + opcodez = copy.deepcopy([opcodes.get(safe_ord(c), ['INVALID', 0, 0, 1]) + + [safe_ord(c)] for c in code]) for i, o in enumerate(opcodez): if o[0] in filter1: jumps[i] = True chunks = {} - chunks["code"] = [ord(x) for x in code] + chunks["code"] = [safe_ord(x) for x in code] c = [] h, reqh, gascost = 0, 0, 0 i = 0 @@ -235,7 +236,7 @@ def preprocess_vmcode(code): # Main function, drop-in replacement for apply_msg in processblock.py def apply_msg(block, tx, msg, code): msg = Message(msg.sender, msg.to, msg.value, msg.gas, bytearray_to_32s( - [ord(x) for x in msg.data]), len(msg.data)) + [safe_ord(x) for x in msg.data]), len(msg.data)) # print '### applying msg ###' callstack = [] msgtop, mem, stk, ops, index = None, None, None, [], [None] @@ -566,7 +567,7 @@ def OP_EXTCODECOPY(): return drop(OUT_OF_GAS) for i in range(s3): if s2 + i < len(extcode): - mem[s1 + i] = ord(extcode[s2 + i]) + mem[s1 + i] = safe_ord(extcode[s2 + i]) else: mem[s1 + i] = 0 diff --git a/pyethereum/peer.py b/pyethereum/peer.py index 71c8fd232..0022d7161 100644 --- a/pyethereum/peer.py +++ b/pyethereum/peer.py @@ -11,7 +11,7 @@ from pyethereum.stoppable import StoppableLoopThread from pyethereum.packeter import packeter from pyethereum.utils import big_endian_to_int as idec -from pyethereum.utils import recursive_int_to_big_endian, to_string +from pyethereum.utils import recursive_int_to_big_endian, to_string, safe_ord import rlp from rlp.utils import encode_hex from pyethereum import blocks @@ -167,7 +167,7 @@ def _recv_Hello(self, data): data = [_decode[i](x) for i, x in enumerate(data)] network_protocol_version, client_version = data[0], data[1] capabilities, listen_port, node_id = data[2], data[3], data[4] - self.capabilities = [(p, ord(v)) for p, v in capabilities] + self.capabilities = [(p, safe_ord(v)) for p, v in capabilities] except (IndexError, ValueError) as e: log_p2p.debug('could not decode Hello', remote_id=self, error=e) return self.send_Disconnect(reason='Incompatible network protocols') @@ -177,7 +177,7 @@ def _recv_Hello(self, data): log_p2p.critical('connected myself') return self.send_Disconnect(reason='Incompatible network protocols') - self.capabilities = [(p, ord(v)) for p, v in capabilities] + self.capabilities = [(p, safe_ord(v)) for p, v in capabilities] log_p2p.debug('received Hello', remote_id=self, network_protocol_version=network_protocol_version, @@ -294,7 +294,7 @@ def _recv_Peers(self, data): addresses = [] for ip, port, pid in data: assert len(ip) == 4 - ip = '.'.join(to_string(ord(b)) for b in ip) + ip = '.'.join(to_string(safe_ord(b)) for b in ip) port = idec(port) log_p2p.trace('received peer address', remote_id=self, ip=ip, port=port) addresses.append([ip, port, pid]) diff --git a/pyethereum/processblock.py b/pyethereum/processblock.py index 39fc2d10d..d6c2148a6 100644 --- a/pyethereum/processblock.py +++ b/pyethereum/processblock.py @@ -14,6 +14,7 @@ from pyethereum import bloom from pyethereum import vm from pyethereum.exceptions import * +from pyethereum.utils import safe_ord sys.setrecursionlimit(100000) @@ -122,7 +123,7 @@ def rp(actual, target): block.delta_balance(tx.sender, -tx.startgas * tx.gasprice) message_gas = tx.startgas - intrinsic_gas_used - message_data = vm.CallData([ord(x) for x in tx.data], 0, len(tx.data)) + 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) diff --git a/pyethereum/specials.py b/pyethereum/specials.py index 2858079bd..1b7b27db5 100644 --- a/pyethereum/specials.py +++ b/pyethereum/specials.py @@ -1,5 +1,6 @@ import bitcoin from pyethereum import utils, opcodes +from pyethereum.utils import safe_ord from rlp.utils import ascii_chr def proc_ecrecover(ext, msg): @@ -17,7 +18,7 @@ def proc_ecrecover(ext, msg): if r >= bitcoin.N or s >= bitcoin.P or v < 27 or v > 28: return 1, msg.gas - opcodes.GECRECOVER, [0] * 32 pub = bitcoin.encode_pubkey(bitcoin.ecdsa_raw_recover(h, (v, r, s)), 'bin') - o = [0] * 12 + [ord(x) for x in utils.sha3(pub[1:])[-20:]] + o = [0] * 12 + [safe_ord(x) for x in utils.sha3(pub[1:])[-20:]] return 1, msg.gas - gas_cost, o @@ -29,7 +30,7 @@ def proc_sha256(ext, msg): if msg.gas < gas_cost: return 0, 0, [] d = msg.data.extract_all() - o = [ord(x) for x in bitcoin.bin_sha256(d)] + o = [safe_ord(x) for x in bitcoin.bin_sha256(d)] return 1, msg.gas - gas_cost, o @@ -41,7 +42,7 @@ def proc_ripemd160(ext, msg): if msg.gas < gas_cost: return 0, 0, [] d = msg.data.extract_all() - o = [0] * 12 + [ord(x) for x in bitcoin.ripemd.RIPEMD160(d).digest()] + o = [0] * 12 + [safe_ord(x) for x in bitcoin.ripemd.RIPEMD160(d).digest()] return 1, msg.gas - gas_cost, o diff --git a/pyethereum/testutils.py b/pyethereum/testutils.py index efa24ea70..d1be182c9 100644 --- a/pyethereum/testutils.py +++ b/pyethereum/testutils.py @@ -6,7 +6,7 @@ import tempfile import copy from pyethereum.db import DB, EphemDB -from pyethereum.utils import to_string +from pyethereum.utils import to_string, safe_ord import json import os import time @@ -164,7 +164,7 @@ def blkhash(n): ext.block_hash = blkhash msg = vm.Message(tx.sender, tx.to, tx.value, tx.startgas, - vm.CallData([ord(x) for x in tx.data])) + vm.CallData([safe_ord(x) for x in tx.data])) time_pre = time.time() success, gas_remained, output = \ vm.vm_execute(ext, msg, decode_hex(exek['code'][2:])) diff --git a/pyethereum/utils.py b/pyethereum/utils.py index 41f03a4f4..1c8ce3801 100644 --- a/pyethereum/utils.py +++ b/pyethereum/utils.py @@ -32,6 +32,12 @@ def to_string(value): if isinstance(value, int): return bytes(str(value), 'utf-8') +def safe_ord(value): + if isinstance(value, int): + return value + else: + return ord(value) + def big_endian_to_int(value): return BigEndianInt().deserialize(value) diff --git a/pyethereum/vm.py b/pyethereum/vm.py index c49404ca5..a064e2235 100644 --- a/pyethereum/vm.py +++ b/pyethereum/vm.py @@ -6,7 +6,7 @@ import time from pyethereum.slogging import get_logger from rlp.utils import encode_hex, ascii_chr -from pyethereum.utils import to_string +from pyethereum.utils import to_string, safe_ord log_log = get_logger('eth.vm.log') log_vm_exit = get_logger('eth.vm.exit') @@ -82,13 +82,13 @@ def preprocess_code(code): i = 0 ops = [] while i < len(code): - o = copy.copy(opcodes.opcodes.get(ord(code[i]), ['INVALID', 0, 0, 0]) + - [ord(code[i]), 0]) + o = copy.copy(opcodes.opcodes.get(safe_ord(code[i]), ['INVALID', 0, 0, 0]) + + [safe_ord(code[i]), 0]) ops.append(o) if o[0][:4] == 'PUSH': for j in range(int(o[0][4:])): i += 1 - byte = ord(code[i]) if i < len(code) else 0 + byte = safe_ord(code[i]) if i < len(code) else 0 o[-1] = (o[-1] << 8) + byte if i < len(code): ops.append(['INVALID', 0, 0, 0, byte, 0]) @@ -350,7 +350,7 @@ def vm_execute(ext, msg, code): return vm_exception('OOG COPY DATA') for i in range(size): if s2 + i < len(extcode): - mem[start + i] = ord(extcode[s2 + i]) + mem[start + i] = safe_ord(extcode[s2 + i]) else: mem[start + i] = 0 elif opcode < 0x50: @@ -457,8 +457,8 @@ def vm_execute(ext, msg, code): return vm_exception('OOG EXTENDING MEMORY') data = ''.join(map(ascii_chr, mem[mstart: mstart + msz])) ext.log(msg.to, topics, data) - log_log.trace('LOG', to=msg.to, topics=topics, data=list(map(ord, data))) - print('LOG', msg.to, topics, list(map(ord, data))) + log_log.trace('LOG', to=msg.to, topics=topics, data=list(map(safe_ord, data))) + print('LOG', msg.to, topics, list(map(safe_ord, data))) elif op == 'CREATE': value, mstart, msz = stk.pop(), stk.pop(), stk.pop() diff --git a/tests/test_contracts.py b/tests/test_contracts.py index 063a18b3c..0a09b8967 100644 --- a/tests/test_contracts.py +++ b/tests/test_contracts.py @@ -4,6 +4,7 @@ from pyethereum import tester, utils, abi import serpent from rlp.utils import decode_hex, encode_hex +from pyethereum.utils import safe_ord # customize VM log output to your needs # hint: use 'py.test' with the '-s' option to dump logs to the console @@ -1089,7 +1090,7 @@ def test_multiarg_code(): s = tester.state() c = s.abi_contract(multiarg_code) o = c.kall([1, 2, 3], 4, [5, 6, 7], "doge", 8) - assert o == [862541, ord('d') + ord('o') + ord('g'), 4] + assert o == [862541, safe_ord('d') + safe_ord('o') + safe_ord('g'), 4] peano_code = """ macro padd($x, psuc($y)):