def __init__(self, accounts, dynamic_loader=None, max_depth=float('inf'), execution_timeout=60, create_timeout=10, strategy=DepthFirstSearchStrategy): world_state = WorldState() world_state.accounts = accounts # this sets the initial world state self.world_state = world_state self.open_states = [world_state] self.nodes = {} self.edges = [] self.coverage = {} self.total_states = 0 self.dynamic_loader = dynamic_loader self.work_list = [] self.strategy = strategy(self.work_list, max_depth) self.max_depth = max_depth self.execution_timeout = execution_timeout self.create_timeout = create_timeout self.time = None self.pre_hooks = {} self.post_hooks = {} logging.info("LASER EVM initialized with dynamic loader: " + str(dynamic_loader))
def exec(self): for global_state in self.strategy: if self.execution_timeout: if self.time + timedelta( seconds=self.execution_timeout) <= datetime.now(): return try: new_states, op_code = self.execute_state(global_state) except NotImplementedError: logging.info( "Encountered unimplemented instruction: {}".format( op_code)) continue if len(new_states) == 0: # TODO: let global state use worldstate open_world_state = WorldState() open_world_state.accounts = global_state.accounts open_world_state.node = global_state.node self.open_states.append(open_world_state) self.manage_cfg(op_code, new_states) self.work_list += new_states self.total_states += len(new_states)
def test_execute_contract_creation(mocked_setup: MagicMock): # Arrange laser_evm = LaserEVM({}) laser_evm.open_states = [WorldState(), WorldState()] laser_evm.exec = MagicMock() mocked_setup.side_effect = _is_contract_creation # Act execute_contract_creation(laser_evm, "606000") # Assert # mocked_setup.assert_called() assert mocked_setup.call_count >= 1 # laser_evm.exec.assert_called_once() assert laser_evm.exec.call_count == 1 assert len(laser_evm.open_states) == 0
def test_execute_message_call(mocked_setup: MagicMock): # Arrange laser_evm = LaserEVM({}) world_state = WorldState() world_state.accounts["address"] = Account("address") laser_evm.open_states = [world_state] laser_evm.exec = MagicMock() mocked_setup.side_effect = _is_message_call # Act execute_message_call(laser_evm, "address") # Assert # laser_evm.exec.assert_called_once() assert laser_evm.exec.call_count == 1 # mocked_setup.assert_called_once() assert mocked_setup.call_count == 1 assert len(laser_evm.open_states) == 0
def test_calldata_constraints_in_transaction(): # Arrange laser_evm = LaserEVM({}) world_state = WorldState() correct_constraints = [MagicMock(), MagicMock(), MagicMock()] transaction = MessageCallTransaction(world_state, Account("ca11ee"), Account("ca114")) transaction.call_data = MagicMock() transaction.call_data.constraints = correct_constraints # Act _setup_global_state_for_execution(laser_evm, transaction) # Assert state = laser_evm.work_list[0] for constraint in correct_constraints: assert constraint in state.environment.calldata.constraints