Skip to content

Commit

Permalink
Refactor code (#1577)
Browse files Browse the repository at this point in the history
* clean up code

* Update lolcat
  • Loading branch information
norhh authored Dec 31, 2021
1 parent c72ac81 commit 965513f
Show file tree
Hide file tree
Showing 29 changed files with 50 additions and 77 deletions.
4 changes: 2 additions & 2 deletions mythril/analysis/call_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_call_from_state(state: GlobalState) -> Union[Call, None]:
stack = state.mstate.stack

if op in ("CALL", "CALLCODE"):
gas, to, value, meminstart, meminsz, memoutstart, memoutsz = (
gas, to, value, meminstart, meminsz, _, _ = (
get_variable(stack[-1]),
get_variable(stack[-2]),
get_variable(stack[-3]),
Expand Down Expand Up @@ -47,7 +47,7 @@ def get_call_from_state(state: GlobalState) -> Union[Call, None]:
return Call(state.node, state, None, op, to, gas, value)

else:
gas, to, meminstart, meminsz, memoutstart, memoutsz = (
gas, to, meminstart, meminsz, _, _ = (
get_variable(stack[-1]),
get_variable(stack[-2]),
get_variable(stack[-3]),
Expand Down
2 changes: 1 addition & 1 deletion mythril/analysis/module/modules/dependence_on_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _execute(self, state: GlobalState) -> None:
self.issues.extend(issues)

@staticmethod
def _analyze_state(state: GlobalState) -> list:
def _analyze_state(state: GlobalState) -> List[Issue]:
"""
:param state:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _execute(self, state: GlobalState) -> None:
self.issues.extend(issues)

@staticmethod
def _analyze_state(state: GlobalState) -> list:
def _analyze_state(state: GlobalState) -> List[Issue]:
"""
:param state:
Expand Down
2 changes: 1 addition & 1 deletion mythril/analysis/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def as_swc_standard_format(self):
# Setup issues
_issues = []

for key, issue in self.issues.items():
for _, issue in self.issues.items():

idx = self.source.get_source_index(issue.bytecode_hash)
try:
Expand Down
1 change: 0 additions & 1 deletion mythril/analysis/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from mythril.exceptions import UnsatError
from mythril.laser.ethereum.function_managers import (
exponent_function_manager,
keccak_function_manager,
)

Expand Down
4 changes: 2 additions & 2 deletions mythril/analysis/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def __init__(
stack = state.mstate.stack

if op in ("CALL", "CALLCODE"):
gas, to, value, meminstart, meminsz, memoutstart, memoutsz = (
gas, to, value, meminstart, meminsz, _, _ = (
get_variable(stack[-1]),
get_variable(stack[-2]),
get_variable(stack[-3]),
Expand Down Expand Up @@ -286,7 +286,7 @@ def __init__(
)
)
else:
gas, to, meminstart, meminsz, memoutstart, memoutsz = (
gas, to, meminstart, meminsz, _, _ = (
get_variable(stack[-1]),
get_variable(stack[-2]),
get_variable(stack[-3]),
Expand Down
1 change: 0 additions & 1 deletion mythril/ethereum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

if sys.version_info[1] >= 6:
import solcx
from solcx.exceptions import SolcNotInstalled

log = logging.getLogger(__name__)

Expand Down
7 changes: 0 additions & 7 deletions mythril/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ class CriticalError(MythrilBaseException):
pass


class AddressNotFoundError(MythrilBaseException):
"""A Mythril exception denoting the given smart contract address was not
found."""

pass


class DetectorNotFoundError(MythrilBaseException):
"""A Mythril exception denoting attempted usage of a non-existant
detection module."""
Expand Down
1 change: 0 additions & 1 deletion mythril/interfaces/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import mythril.support.signatures as sigs
from argparse import ArgumentParser, Namespace, RawTextHelpFormatter
from mythril.exceptions import (
AddressNotFoundError,
DetectorNotFoundError,
CriticalError,
)
Expand Down
42 changes: 20 additions & 22 deletions mythril/interfaces/epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import re
import sys
import time
import argparse

PY3 = sys.version_info >= (3,)

Expand Down Expand Up @@ -165,7 +166,7 @@ def println_ani(self, s, options):
if not s:
return

for i in range(1, options.duration):
for _ in range(1, options.duration):
self.output.write("\x1b[%dD" % (len(s),))
self.output.flush()
options.os += options.spread
Expand Down Expand Up @@ -208,70 +209,67 @@ def detect_mode(term_hint="xterm-256color"):

def run():
"""Main entry point."""
import optparse

parser = optparse.OptionParser(usage=r"%prog [<options>] [file ...]")
parser.add_option(
"-p", "--spread", type="float", default=3.0, help="Rainbow spread"
parser = argparse.ArgumentParser(usage=r"%prog [<options>] [file ...]")
parser.add_argument(
"-p", "--spread", type=float, default=3.0, help="Rainbow spread"
)
parser.add_option(
"-F", "--freq", type="float", default=0.1, help="Rainbow frequency"
parser.add_argument(
"-F", "--freq", type=float, default=0.1, help="Rainbow frequency"
)
parser.add_option("-S", "--seed", type="int", default=0, help="Rainbow seed")
parser.add_option(
parser.add_argument("-S", "--seed", type=int, default=0, help="Rainbow seed")
parser.add_argument(
"-a",
"--animate",
action="store_true",
default=False,
help="Enable psychedelics",
)
parser.add_option(
"-d", "--duration", type="int", default=12, help="Animation duration"
parser.add_argument(
"-d", "--duration", type=int, default=12, help="Animation duration"
)
parser.add_option(
"-s", "--speed", type="float", default=20.0, help="Animation speed"
parser.add_argument(
"-s", "--speed", type=float, default=20.0, help="Animation speed"
)
parser.add_option(
parser.add_argument(
"-f",
"--force",
action="store_true",
default=False,
help="Force colour even when stdout is not a tty",
)

parser.add_option(
parser.add_argument(
"-3", action="store_const", dest="mode", const=8, help="Force 3 bit colour mode"
)
parser.add_option(
parser.add_argument(
"-4",
action="store_const",
dest="mode",
const=16,
help="Force 4 bit colour mode",
)
parser.add_option(
parser.add_argument(
"-8",
action="store_const",
dest="mode",
const=256,
help="Force 8 bit colour mode",
)

parser.add_option(
parser.add_argument(
"-c",
"--charset-py2",
default="utf-8",
help="Manually set a charset to convert from, for python 2.7",
)

options, args = parser.parse_args()
options = parser.parse_args()
options.os = random.randint(0, 256) if options.seed == 0 else options.seed
options.mode = options.mode or detect_mode()

lolcat = LolCat(mode=options.mode)

if not args:
args = ["-"]
args = ["-"]

for filename in args:
if filename == "-":
Expand Down
4 changes: 2 additions & 2 deletions mythril/laser/ethereum/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
import re
from typing import Union, List, cast, Callable, Optional
from typing import Union, List, cast, Optional
from eth.constants import GAS_CALLSTIPEND

import mythril.laser.ethereum.util as util
Expand All @@ -18,7 +18,7 @@
ConcreteCalldata,
)
from mythril.laser.ethereum.state.global_state import GlobalState
from mythril.laser.smt import BitVec, is_true, If
from mythril.laser.smt import BitVec, If
from mythril.laser.smt import simplify, Expression, symbol_factory
from mythril.support.loader import DynLoader

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, List, Optional, Tuple
from typing import Tuple


from mythril.laser.smt import And, BitVec, Bool, Function, URem, symbol_factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def create_keccak(self, data: BitVec) -> BitVec:
:return: Tuple of keccak and the condition it should satisfy
"""
length = data.size()
func, inverse = self.get_function(length)
func, _ = self.get_function(length)

if data.symbolic is False:
concrete_hash = self.find_concrete_keccak(data)
Expand Down
2 changes: 1 addition & 1 deletion mythril/laser/ethereum/instruction_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from eth._utils.numeric import ceil32
from typing import Callable, Dict, Tuple, Union
from typing import Tuple
from mythril.support.opcodes import OPCODES, STACK, GAS
from eth.constants import (
GAS_ECRECOVER,
Expand Down
24 changes: 11 additions & 13 deletions mythril/laser/ethereum/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
from mythril.laser.smt import (
Extract,
Expression,
Function,
UDiv,
simplify,
Concat,
ULT,
UGT,
BitVec,
is_false,
is_true,
URem,
SRem,
If,
Expand Down Expand Up @@ -1705,9 +1703,9 @@ def log_(self, global_state: GlobalState) -> List[GlobalState]:
"""
# TODO: implement me
state = global_state.mstate
dpth = int(self.op_code[3:])
depth = int(self.op_code[3:])
state.stack.pop(), state.stack.pop()
log_data = [state.stack.pop() for _ in range(dpth)]
_ = [state.stack.pop() for _ in range(depth)]
# Not supported
return [global_state]

Expand Down Expand Up @@ -2117,10 +2115,10 @@ def callcode_post(self, global_state: GlobalState) -> List[GlobalState]:
memory_out_size, memory_out_offset = global_state.mstate.stack[-7:-5]
try:
(
callee_address,
_,
_,
value,
_,
_,
_,
memory_out_offset,
memory_out_size,
Expand Down Expand Up @@ -2261,10 +2259,10 @@ def delegatecall_post(self, global_state: GlobalState) -> List[GlobalState]:

try:
(
callee_address,
_,
_,
value,
_,
_,
_,
memory_out_offset,
memory_out_size,
Expand Down Expand Up @@ -2405,11 +2403,11 @@ def post_handler(self, global_state, function_name: str):
try:
with_value = function_name != "staticcall"
(
callee_address,
callee_account,
call_data,
value,
gas,
_,
_,
_,
_,
_,
memory_out_offset,
memory_out_size,
) = get_call_parameters(global_state, self.dynamic_loader, with_value)
Expand Down
1 change: 0 additions & 1 deletion mythril/laser/ethereum/state/calldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import cast, Union, Tuple, List


from enum import Enum
from typing import Any, Union

from z3 import Model
Expand Down
1 change: 0 additions & 1 deletion mythril/laser/ethereum/state/world_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from mythril.support.loader import DynLoader
from mythril.laser.smt import symbol_factory, Array, BitVec
from mythril.disassembler.disassembly import Disassembly
from mythril.laser.ethereum.state.account import Account
from mythril.laser.ethereum.state.annotation import StateAnnotation
from mythril.laser.ethereum.state.constraints import Constraints
Expand Down
4 changes: 2 additions & 2 deletions mythril/laser/ethereum/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mythril.laser.ethereum.cfg import NodeFlags, Node, Edge, JumpType
from mythril.laser.ethereum.evm_exceptions import StackUnderflowException
from mythril.laser.ethereum.evm_exceptions import VmException
from mythril.laser.ethereum.instructions import Instruction, transfer_ether
from mythril.laser.ethereum.instructions import Instruction
from mythril.laser.ethereum.instruction_data import get_required_stack_elements
from mythril.laser.plugin.signals import PluginSkipWorldState, PluginSkipState
from mythril.laser.ethereum.state.global_state import GlobalState
Expand Down Expand Up @@ -287,7 +287,7 @@ def _add_world_state(self, global_state: GlobalState):
def handle_vm_exception(
self, global_state: GlobalState, op_code: str, error_msg: str
) -> List[GlobalState]:
transaction, return_global_state = global_state.transaction_stack.pop()
_, return_global_state = global_state.transaction_stack.pop()

if return_global_state is None:
# In this case we don't put an unmodified world state in the open_states list Since in the case of an
Expand Down
1 change: 0 additions & 1 deletion mythril/laser/ethereum/transaction/transaction_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This module contians the transaction models used throughout LASER's symbolic
execution."""

import array
from copy import deepcopy
from z3 import ExprRef
from typing import Union, Optional
Expand Down
1 change: 0 additions & 1 deletion mythril/laser/plugin/builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from mythril.laser.plugin.interface import LaserPlugin

from abc import ABC, abstractmethod
from typing import Optional


class PluginBuilder(ABC):
Expand Down
2 changes: 1 addition & 1 deletion mythril/laser/plugin/plugins/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _store_report(self):
def _write_to_graph(self):
"""Write the coverage results to a graph"""
traces = []
for byte_code, trace_data in self.coverage.items():
for _, trace_data in self.coverage.items():
traces += [list(trace_data.keys()), list(trace_data.values()), "r--"]

plt.plot(*traces)
Expand Down
2 changes: 1 addition & 1 deletion mythril/laser/smt/bool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This module provides classes for an SMT abstraction of boolean
expressions."""

from typing import Union, cast, List, Set
from typing import Union, cast, Set

import z3

Expand Down
1 change: 0 additions & 1 deletion mythril/mythril/mythril_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import traceback
from typing import Optional, List

from mythril.laser.ethereum.iprof import InstructionProfiler
from . import MythrilDisassembler
from mythril.support.source_support import Source
from mythril.support.loader import DynLoader
Expand Down
Loading

0 comments on commit 965513f

Please sign in to comment.