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

Commit

Permalink
Integer division
Browse files Browse the repository at this point in the history
  • Loading branch information
JahPowerBit committed Mar 22, 2015
1 parent 144c488 commit a3fff19
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyethereum/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

# Difficulty adjustment algo
def calc_difficulty(parent, timestamp):
offset = parent.difficulty / BLOCK_DIFF_FACTOR
offset = parent.difficulty // BLOCK_DIFF_FACTOR
sign = 1 if timestamp - parent.timestamp < DIFF_ADJUSTMENT_CUTOFF else -1
# If we enter a special mode where the genesis difficulty starts off below
# the minimal difficulty, we allow low-difficulty blocks (this will never
Expand Down
4 changes: 2 additions & 2 deletions pyethereum/fastvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def OP_MUL():

def OP_DIV():
s0, s1 = stk.pop(), stk.pop()
stk.append(0 if s1 == 0 else s0 / s1)
stk.append(0 if s1 == 0 else s0 // s1)

def OP_MOD():
s0, s1 = stk.pop(), stk.pop()
Expand Down Expand Up @@ -491,7 +491,7 @@ def OP_BYTE():
if s0 >= 32:
stk.append(0)
else:
stk.append((s1 / 256 ** (31 - s0)) % 256)
stk.append((s1 // 256 ** (31 - s0)) % 256)

def OP_ADDMOD():
s0, s1, s2 = stk.pop(), stk.pop(), stk.pop()
Expand Down
4 changes: 4 additions & 0 deletions pyethereum/processblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def rp(actual, target):
log_tx.debug('_res_', result=result, gas_remained=gas_remained, data=data)
else: # CREATE
result, gas_remained, data = create_contract(ext, message)
assert utils.is_numeric(gas_remained)
log_tx.debug('_create_', result=result, gas_remained=gas_remained, data=data)

assert gas_remained >= 0
Expand Down Expand Up @@ -231,6 +232,7 @@ def _apply_msg(ext, msg, code):

# Main loop
res, gas, dat = vm.vm_execute(ext, msg, code)
assert utils.is_numeric(gas)
if log_msg.is_active:
log_msg.debug('MSG APPLIED', result=o, gas_remained=gas, sender=msg.sender, to=msg.to, data=dat)
if log_state.is_active:
Expand All @@ -253,6 +255,8 @@ def create_contract(ext, msg):
msg.is_create = True
# assert not ext.get_code(msg.to)
res, gas, dat = _apply_msg(ext, msg, msg.data.extract_all())
assert utils.is_numeric(gas)

if res:
if not len(dat):
return 1, gas, msg.to
Expand Down
12 changes: 6 additions & 6 deletions pyethereum/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def preprocess_code(code):

def mem_extend(mem, compustate, op, start, sz):
if sz:
oldsize = len(mem) / 32
oldsize = len(mem) // 32
old_totalfee = oldsize * opcodes.GMEMORY + \
oldsize**2 // opcodes.GQUADRATICMEMDENOM
newsize = utils.ceil32(start + sz) / 32
newsize = utils.ceil32(start + sz) // 32
new_totalfee = newsize * opcodes.GMEMORY + \
newsize**2 // opcodes.GQUADRATICMEMDENOM
if old_totalfee < new_totalfee:
Expand Down Expand Up @@ -222,7 +222,7 @@ def vm_execute(ext, msg, code):
stk.append((stk.pop() * stk.pop()) & TT256M1)
elif op == 'DIV':
s0, s1 = stk.pop(), stk.pop()
stk.append(0 if s1 == 0 else s0 / s1)
stk.append(0 if s1 == 0 else s0 // s1)
elif op == 'MOD':
s0, s1 = stk.pop(), stk.pop()
stk.append(0 if s1 == 0 else s0 % s1)
Expand Down Expand Up @@ -289,11 +289,11 @@ def vm_execute(ext, msg, code):
if s0 >= 32:
stk.append(0)
else:
stk.append((s1 / 256 ** (31 - s0)) % 256)
stk.append((s1 // 256 ** (31 - s0)) % 256)
elif opcode < 0x40:
if op == 'SHA3':
s0, s1 = stk.pop(), stk.pop()
compustate.gas -= opcodes.GSHA3WORD * (utils.ceil32(s1) / 32)
compustate.gas -= opcodes.GSHA3WORD * (utils.ceil32(s1) // 32)
if compustate.gas < 0:
return vm_exception('OOG PAYING FOR SHA3')
if not mem_extend(mem, compustate, op, s0, s1):
Expand Down Expand Up @@ -382,7 +382,7 @@ def vm_execute(ext, msg, code):
v = s1
for i in range(31, -1, -1):
mem[s0 + i] = v % 256
v /= 256
v //= 256
elif op == 'MSTORE8':
s0, s1 = stk.pop(), stk.pop()
if not mem_extend(mem, compustate, op, s0, 1):
Expand Down

0 comments on commit a3fff19

Please sign in to comment.