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

Commit

Permalink
Always use utils.safe_ord() instead ord()
Browse files Browse the repository at this point in the history
  • Loading branch information
JahPowerBit committed Mar 22, 2015
1 parent a3fff19 commit 49ebe2d
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 26 deletions.
5 changes: 3 additions & 2 deletions pyethereum/bloom.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion pyethereum/compress.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rlp.utils import decode_hex, encode_hex, ascii_chr


NULLSHA3 = decode_hex('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')


Expand All @@ -25,6 +26,7 @@ def compress(data):


def decompress(data):
from pyethereum.utils import safe_ord
o = ''
i = 0
while i < len(data):
Expand All @@ -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]
Expand Down
11 changes: 6 additions & 5 deletions pyethereum/fastvm.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions pyethereum/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion pyethereum/processblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions pyethereum/specials.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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


Expand All @@ -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


Expand All @@ -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


Expand Down
4 changes: 2 additions & 2 deletions pyethereum/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:]))
Expand Down
6 changes: 6 additions & 0 deletions pyethereum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 7 additions & 7 deletions pyethereum/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)):
Expand Down

0 comments on commit 49ebe2d

Please sign in to comment.