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

Commit 46dc700

Browse files
committed
Reflect gas cost updates for EIP150 in vm #413
1 parent dd4eda3 commit 46dc700

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

ethereum/vm.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ def data_copy(compustate, size):
135135
return True
136136

137137

138+
def eat_gas(compustate, amount):
139+
if compustate.gas < amount:
140+
compustate.gas = 0
141+
return False
142+
else:
143+
compustate.gas -= amount
144+
return True
145+
146+
138147
def vm_exception(error, **kargs):
139148
log_vm_exit.trace('EXCEPTION', cause=error, **kargs)
140149
return 0, 0, []
@@ -331,7 +340,11 @@ def vm_execute(ext, msg, code):
331340
elif op == 'ADDRESS':
332341
stk.append(utils.coerce_to_int(msg.to))
333342
elif op == 'BALANCE':
334-
addr = utils.coerce_addr_to_hex(stk.pop() % 2**160)
343+
# EIP150: Increase the gas cost of BALANCE to 400
344+
if ext.post_anti_dos_hardfork():
345+
if not eat_gas(compustate, opcodes.BALANCE_SUPPLEMENTAL_GAS):
346+
return vm_exception("OUT OF GAS")
347+
addr = utils.coerce_addr_to_hex(stk.pop() % 2 ** 160)
335348
stk.append(ext.get_balance(addr))
336349
elif op == 'ORIGIN':
337350
stk.append(utils.coerce_to_int(ext.tx_origin))
@@ -366,10 +379,18 @@ def vm_execute(ext, msg, code):
366379
elif op == 'GASPRICE':
367380
stk.append(ext.tx_gasprice)
368381
elif op == 'EXTCODESIZE':
369-
addr = utils.coerce_addr_to_hex(stk.pop() % 2**160)
382+
# EIP150: Increase the gas cost of EXTCODESIZE to 700
383+
if ext.post_anti_dos_hardfork():
384+
if not eat_gas(compustate, opcodes.EXTCODELOAD_SUPPLEMENTAL_GAS):
385+
return vm_exception("OUT OF GAS")
386+
addr = utils.coerce_addr_to_hex(stk.pop() % 2 ** 160)
370387
stk.append(len(ext.get_code(addr) or b''))
371388
elif op == 'EXTCODECOPY':
372-
addr = utils.coerce_addr_to_hex(stk.pop() % 2**160)
389+
# EIP150: Increase the base gas cost of EXTCODECOPY to 700
390+
if ext.post_anti_dos_hardfork():
391+
if not eat_gas(compustate, opcodes.EXTCODELOAD_SUPPLEMENTAL_GAS):
392+
return vm_exception("OUT OF GAS")
393+
addr = utils.coerce_addr_to_hex(stk.pop() % 2 ** 160)
373394
start, s2, size = stk.pop(), stk.pop(), stk.pop()
374395
extcode = ext.get_code(addr) or b''
375396
assert utils.is_string(extcode)
@@ -418,6 +439,10 @@ def vm_execute(ext, msg, code):
418439
return vm_exception('OOG EXTENDING MEMORY')
419440
mem[s0] = s1 % 256
420441
elif op == 'SLOAD':
442+
# EIP150: Increase the gas cost of SLOAD to 200
443+
if ext.post_anti_dos_hardfork():
444+
if not eat_gas(compustate, opcodes.SLOAD_SUPPLEMENTAL_GAS):
445+
return vm_exception("OUT OF GAS")
421446
stk.append(ext.get_storage_data(msg.to, stk.pop()))
422447
elif op == 'SSTORE':
423448
s0, s1 = stk.pop(), stk.pop()
@@ -514,10 +539,12 @@ def vm_execute(ext, msg, code):
514539
to = utils.encode_int(to)
515540
to = ((b'\x00' * (32 - len(to))) + to)[12:]
516541
extra_gas = (not ext.account_exists(to)) * opcodes.GCALLNEWACCOUNT + \
517-
(value > 0) * opcodes.GCALLVALUETRANSFER
542+
(value > 0) * opcodes.GCALLVALUETRANSFER + \
543+
ext.post_anti_dos_hardfork() * opcodes.CALL_SUPPLEMENTAL_GAS
544+
# ^ EIP150 Increase the gas cost of CALL to 700
518545
submsg_gas = gas + opcodes.GSTIPEND * (value > 0)
519546
if compustate.gas < gas + extra_gas:
520-
return vm_exception('OUT OF GAS', needed=gas+extra_gas)
547+
return vm_exception('OUT OF GAS', needed=gas + extra_gas)
521548
if ext.get_balance(msg.to) >= value and msg.depth < 1024:
522549
compustate.gas -= (gas + extra_gas)
523550
cd = CallData(mem, meminstart, meminsz)
@@ -545,10 +572,12 @@ def vm_execute(ext, msg, code):
545572
if not mem_extend(mem, compustate, op, meminstart, meminsz) or \
546573
not mem_extend(mem, compustate, op, memoutstart, memoutsz):
547574
return vm_exception('OOG EXTENDING MEMORY')
548-
extra_gas = (value > 0) * opcodes.GCALLVALUETRANSFER
575+
extra_gas = (value > 0) * opcodes.GCALLVALUETRANSFER + \
576+
ext.post_anti_dos_hardfork() * opcodes.CALL_SUPPLEMENTAL_GAS
577+
# ^ EIP150 Increase the gas cost of CALLCODE, DELEGATECALL to 700
549578
submsg_gas = gas + opcodes.GSTIPEND * (value > 0)
550579
if compustate.gas < gas + extra_gas:
551-
return vm_exception('OUT OF GAS', needed=gas+extra_gas)
580+
return vm_exception('OUT OF GAS', needed=gas + extra_gas)
552581
if ext.get_balance(msg.to) >= value and msg.depth < 1024:
553582
compustate.gas -= (gas + extra_gas)
554583
to = utils.encode_int(to)
@@ -581,6 +610,13 @@ def vm_execute(ext, msg, code):
581610
elif op == 'SUICIDE':
582611
to = utils.encode_int(stk.pop())
583612
to = ((b'\x00' * (32 - len(to))) + to)[12:]
613+
614+
if ext.post_anti_dos_hardfork():
615+
# EIP150 Increase the gas cost of SUICIDE to 5000
616+
extra_gas = opcodes.SUICIDE_SUPPLEMENTAL_GAS
617+
if not eat_gas(compustate, extra_gas):
618+
return vm_exception("OUT OF GAS")
619+
584620
xfer = ext.get_balance(msg.to)
585621
ext.set_balance(to, ext.get_balance(to) + xfer)
586622
ext.set_balance(msg.to, 0)

0 commit comments

Comments
 (0)