Refactor instruction decorator into smaller methods

pull/499/head
Dominik Muhs 6 years ago
parent 4e56c9fc75
commit c3865b0a2e
  1. 33
      mythril/laser/ethereum/instructions.py

@ -30,17 +30,28 @@ class StopSignal(Exception):
class PCMutator(object): class PCMutator(object):
""" Wrapper that handles copy and original return """ """ Wrapper that handles copy and original return """
def __init__(self, auto_increment=True): def __init__(self, increment_pc=True):
self.auto_increment = auto_increment self.increment_pc = increment_pc
@staticmethod
def call_on_state_copy(func, func_obj, state):
global_state_copy = copy(state)
return func(func_obj, global_state_copy)
def increment_states_pc(self, states):
if self.increment_pc:
for state in states:
state.mstate.pc += 1
return states
def __call__(self, func): def __call__(self, func):
def wrapper(obj, global_state): def wrapper(func_obj, global_state):
global_state_copy = copy(global_state) new_global_states = self.call_on_state_copy(
new_global_states = func(obj, global_state_copy) func,
if self.auto_increment: func_obj,
for state in new_global_states: global_state
state.mstate.pc += 1 )
return new_global_states return self.increment_states_pc(new_global_states)
return wrapper return wrapper
@ -775,7 +786,7 @@ class Instruction:
logging.debug("Error writing to storage: Invalid index") logging.debug("Error writing to storage: Invalid index")
return [global_state] return [global_state]
@PCMutator(auto_increment=False) @PCMutator(increment_pc=False)
def jump_(self, global_state): def jump_(self, global_state):
state = global_state.mstate state = global_state.mstate
disassembly = global_state.environment.code disassembly = global_state.environment.code
@ -804,7 +815,7 @@ class Instruction:
return [new_state] return [new_state]
@PCMutator(auto_increment=False) @PCMutator(increment_pc=False)
def jumpi_(self, global_state): def jumpi_(self, global_state):
state = global_state.mstate state = global_state.mstate
disassembly = global_state.environment.code disassembly = global_state.environment.code

Loading…
Cancel
Save