Skip to content

Commit

Permalink
refactor: typos and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Otto-AA committed Aug 5, 2024
1 parent af8f38d commit 93a43f4
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions tests/analysis/test_analysis_runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import Mock

from tests.test_utils.test_utils import _test_flow, _test_root
from traces_analyzer.features.feature_extractor import DoulbeInstructionFeatureExtractor
from traces_analyzer.features.feature_extractor import DoubleInstructionFeatureExtractor
from traces_analyzer.features.feature_extraction_runner import (
RunInfo,
FeatureExtractionRunner,
Expand All @@ -12,7 +12,7 @@


def test_analysis_runner_empty_does_not_call_analyzer():
feature_extractor_mock = Mock(spec_set=DoulbeInstructionFeatureExtractor)
feature_extractor_mock = Mock(spec_set=DoubleInstructionFeatureExtractor)
empty_call_tree = CallTree(_test_root())
empty_transaction = ParsedTransaction([], empty_call_tree)

Expand All @@ -28,7 +28,7 @@ def test_analysis_runner_empty_does_not_call_analyzer():


def test_analysis_runner_calls_analyzer() -> None:
feature_extractor_mock = Mock(spec_set=DoulbeInstructionFeatureExtractor)
feature_extractor_mock = Mock(spec_set=DoubleInstructionFeatureExtractor)
empty_call_tree = CallTree(_test_root())
instructions_one = [POP(POP.opcode, "POP", 1, 1, _test_root(), _test_flow())]
instructions_two = instructions_one + [
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test_sample_traces_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def test_sample_traces_analysis_e2e(
)

# Instruction usage has found 17 contracts
assert len(instruction_usage_analyzer.one.get_used_opcodes_per_contract()) == 17
assert len(instruction_usage_analyzer.two.get_used_opcodes_per_contract()) == 17
assert len(instruction_usage_analyzer.normal.get_used_opcodes_per_contract()) == 17
assert len(instruction_usage_analyzer.reverse.get_used_opcodes_per_contract()) == 17

# TOD source
tod_source = tod_source_analyzer.get_tod_source()
Expand Down
8 changes: 4 additions & 4 deletions traces_analyzer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,17 @@ def compare_traces(

evaluations: list[Evaluation] = [
SecurifyPropertiesEvaluation(
calls_grouper.one.instruction_groups, # type: ignore
calls_grouper.two.instruction_groups, # type: ignore
calls_grouper.normal.instruction_groups, # type: ignore
calls_grouper.reverse.instruction_groups, # type: ignore
),
TODSourceEvaluation(tod_source_analyzer.get_tod_source()),
InstructionDifferencesEvaluation(
occurrence_changes=instruction_changes_analyzer.get_instructions_only_executed_by_one_trace(),
input_changes=instruction_changes_analyzer.get_instructions_with_different_inputs(),
),
InstructionUsageEvaluation(
instruction_usage_analyzers.one.get_used_opcodes_per_contract(),
instruction_usage_analyzers.two.get_used_opcodes_per_contract(),
instruction_usage_analyzers.normal.get_used_opcodes_per_contract(),
instruction_usage_analyzers.reverse.get_used_opcodes_per_contract(),
filter_opcodes=[CALL.opcode, STATICCALL.opcode],
),
]
Expand Down
18 changes: 9 additions & 9 deletions traces_analyzer/features/extractors/instruction_differences.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from typing_extensions import override

from traces_analyzer.features.feature_extractor import DoulbeInstructionFeatureExtractor
from traces_analyzer.features.feature_extractor import DoubleInstructionFeatureExtractor
from traces_parser.parser.instructions.instruction import Instruction
from traces_parser.parser.storage.storage_writes import StackAccess
from traces_parser.datatypes import HexString
Expand Down Expand Up @@ -39,7 +39,7 @@ class InstructionInputChange:
memory_input_changes: list[MemoryInputChange]


class InstructionDifferencesFeatureExtractor(DoulbeInstructionFeatureExtractor):
class InstructionDifferencesFeatureExtractor(DoubleInstructionFeatureExtractor):
"""Analyze how the instruction inputs of two traces differ"""

def __init__(self) -> None:
Expand All @@ -50,14 +50,14 @@ def __init__(self) -> None:
@override
def on_instructions(
self,
first_instruction: Instruction | None,
second_instruction: Instruction | None,
normal_instruction: Instruction | None,
reverse_instruction: Instruction | None,
):
if first_instruction:
self._instructions_one.append(first_instruction)
if normal_instruction:
self._instructions_one.append(normal_instruction)

if second_instruction:
self._instructions_two.append(second_instruction)
if reverse_instruction:
self._instructions_two.append(reverse_instruction)

def get_instructions_only_executed_by_one_trace(
self,
Expand All @@ -76,7 +76,7 @@ def get_instructions_only_executed_by_one_trace(
return (only_first, only_second)

def get_instructions_with_different_inputs(self) -> list[InstructionInputChange]:
# TODO: the order of the comparison is non-deterministc. Should we change it to be deterministic somehow?
# TODO: the order of the comparison is non-deterministic. Should we change it to be deterministic somehow?
comparison = _compare_instructions(
self._instructions_one,
self._instructions_two,
Expand Down
18 changes: 9 additions & 9 deletions traces_analyzer/features/extractors/tod_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing_extensions import override

from traces_analyzer.features.feature_extractor import DoulbeInstructionFeatureExtractor
from traces_analyzer.features.feature_extractor import DoubleInstructionFeatureExtractor
from traces_parser.parser.information_flow.constant_step_indexes import (
SPECIAL_STEP_INDEXES,
)
Expand All @@ -16,7 +16,7 @@ class TODSource:
instruction_two: Instruction


class TODSourceFeatureExtractor(DoulbeInstructionFeatureExtractor):
class TODSourceFeatureExtractor(DoubleInstructionFeatureExtractor):
"""Analyze at which instruction the TOD first had an effect"""

def __init__(self) -> None:
Expand All @@ -27,22 +27,22 @@ def __init__(self) -> None:
@override
def on_instructions(
self,
first_instruction: Instruction | None,
second_instruction: Instruction | None,
normal_instruction: Instruction | None,
reverse_instruction: Instruction | None,
):
if self._tod_source_instructions:
return

if not first_instruction or not second_instruction:
if not normal_instruction or not reverse_instruction:
return

# TODO: this does not work if both instructions depend on the prestate
# but something else caused the write change (ie it is no TOD)
if depends_on_prestate(first_instruction) and depends_on_prestate(
second_instruction
if depends_on_prestate(normal_instruction) and depends_on_prestate(
reverse_instruction
):
if first_instruction.get_writes() != second_instruction.get_writes():
self._tod_source_instructions = first_instruction, second_instruction
if normal_instruction.get_writes() != reverse_instruction.get_writes():
self._tod_source_instructions = normal_instruction, reverse_instruction

def get_tod_source(self) -> TODSource:
if not self._tod_source_instructions:
Expand Down
4 changes: 2 additions & 2 deletions traces_analyzer/features/feature_extraction_runner.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from dataclasses import dataclass
from itertools import zip_longest

from traces_analyzer.features.feature_extractor import DoulbeInstructionFeatureExtractor
from traces_analyzer.features.feature_extractor import DoubleInstructionFeatureExtractor
from traces_parser.parser.environment.call_context_manager import CallTree
from traces_parser.parser.instructions.instruction import Instruction
from traces_parser.parser.instructions_parser import ParsedTransaction


@dataclass
class RunInfo:
feature_extractors: list[DoulbeInstructionFeatureExtractor]
feature_extractors: list[DoubleInstructionFeatureExtractor]
transactions: tuple[ParsedTransaction, ParsedTransaction]


Expand Down
24 changes: 12 additions & 12 deletions traces_analyzer/features/feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def on_instruction(self, instruction: Instruction):
pass


class DoulbeInstructionFeatureExtractor(ABC):
class DoubleInstructionFeatureExtractor(ABC):
@abstractmethod
def on_instructions(
self,
first_instruction: Instruction | None,
second_instruction: Instruction | None,
normal_instruction: Instruction | None,
reverse_instruction: Instruction | None,
):
"""Hook each instruction of two traces"""
pass
Expand All @@ -28,21 +28,21 @@ def on_instructions(


class SingleToDoubleInstructionFeatureExtractor(
DoulbeInstructionFeatureExtractor, Generic[A]
DoubleInstructionFeatureExtractor, Generic[A]
):
def __init__(self, feature_extractor_one: A, feature_extractor_two: A) -> None:
super().__init__()

self.one = feature_extractor_one
self.two = feature_extractor_two
self.normal = feature_extractor_one
self.reverse = feature_extractor_two

@override
def on_instructions(
self,
first_instruction: Instruction | None,
second_instruction: Instruction | None,
normal_instruction: Instruction | None,
reverse_instruction: Instruction | None,
):
if first_instruction:
self.one.on_instruction(first_instruction)
if second_instruction:
self.two.on_instruction(second_instruction)
if normal_instruction:
self.normal.on_instruction(normal_instruction)
if reverse_instruction:
self.reverse.on_instruction(reverse_instruction)

0 comments on commit 93a43f4

Please sign in to comment.