Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor evm refactor #1329

Merged
merged 1 commit into from Jan 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 21 additions & 19 deletions manticore/platforms/evm.py
Expand Up @@ -785,7 +785,7 @@ def _consume(self, fee):
if fee > (1 << 512) - 1:
raise ValueError
elif isinstance(fee, BitVec):
if (fee.size != 512):
if fee.size != 512:
raise ValueError("Fees should be 512 bit long")

def get_possible_solutions():
Expand All @@ -803,49 +803,51 @@ def get_possible_solutions():
# ignore: Ignore gas. Do not account for it. Do not OOG.

if consts.oog in ('pedantic', 'complete'):
#gas is faithfully accounted and ogg checked at instruction/BB level.
# gas is faithfully accounted and ogg checked at instruction/BB level.
if consts.oog == 'pedantic' or self.instruction.is_terminator:
#explore both options / fork
#FIXME this will reenter here and generate redundant queries
# explore both options / fork
# FIXME this will reenter here and generate redundant queries
enough_gas_solutions = get_possible_solutions()
if len(enough_gas_solutions) == 2:
#if gas can be both enough an insuficient, fork
# if gas can be both enough and insufficient, fork
raise Concretize("Concretize gas fee",
expression=Operators.UGT(self._gas, fee),
setstate=None,
policy='ALL')
elif enough_gas_solutions[0] is False:
#if gas if only insuficient OOG!
logger.debug("Not enough gas for instruction")
logger.debug(f"Not enough gas for instruction {self.instruction.name} at 0x{self.pc:x}")
raise NotEnoughGas()
else:
assert enough_gas_solutions[0] == True
#if there is enough gas keep going
assert enough_gas_solutions[0] is True
# if there is enough gas keep going
elif consts.oog == 'concrete':
# Keep gas concrete. Concretize symbolic fees to some values.
#this can happen only if symbolic gas is provided for the TX
# this can happen only if symbolic gas is provided for the TX
if issymbolic(self._gas):
raise ConcretizeGas()
if issymbolic(fee):
raise ConcretizeFee()
elif consts.oog == 'optimistic':
#Try not to OOG. If it may be enough gas we ignore the OOG case. A constraint is added to assert the gas is enough and the OOG state is ignored.
#explore only when there is enough gas if possible
# Try not to OOG. If it may be enough gas we ignore the OOG case.
# A constraint is added to assert the gas is enough and the OOG state is ignored.
# explore only when there is enough gas if possible
if solver.can_be_true(self.constraints, Operators.UGT(self.gas, fee)):
self.constraints.add(Operators.UGT(self.gas, fee))
else:
logger.debug("Not enough gas for instruction")
logger.debug(f"Not enough gas for instruction {self.instruction.name} at 0x{self.pc:x}")
raise NotEnoughGas()
elif consts.oog == 'pesimistic':
# OOG soon. If it may NOT be enough gas we ignore the normal case. A constraint is added to assert the gas is NOT enough and the other state is ignored.
#explore only when there is enough gas if possible
# OOG soon. If it may NOT be enough gas we ignore the normal case.
# A constraint is added to assert the gas is NOT enough and the other state is ignored.
# explore only when there is enough gas if possible
if solver.can_be_true(self.constraints, Operators.ULE(self.gas, fee)):
self.constraints.add(Operators.ULE(self.gas, fee))
raise NotEnoughGas()
else:
if consts.oog != 'ignore':
raise Exception("Wrong oog config variable")
#do nothing. gas is not even changed
# do nothing. gas is not even changed
return
self._gas -= fee

Expand All @@ -857,7 +859,7 @@ def _indemnify(self, fee):
self._gas += fee

def _pop_arguments(self):
#Get arguments (imm, pop)
# Get arguments (imm, pop)
current = self.instruction
arguments = []
if current.has_operand:
Expand All @@ -871,7 +873,7 @@ def _pop_arguments(self):
return arguments

def _top_arguments(self):
#Get arguments (imm, top). Stack is not changed
# Get arguments (imm, top). Stack is not changed
current = self.instruction
arguments = []
if current.has_operand:
Expand All @@ -882,7 +884,7 @@ def _top_arguments(self):
return arguments

def _push_arguments(self, arguments):
#Immediate operands should not be pushed
# Immediate operands should not be pushed
start = int(self.instruction.has_operand)
for arg in reversed(arguments[start:]):
self._push(arg)
Expand Down Expand Up @@ -910,7 +912,7 @@ def _handler(self, *arguments):
current = self.instruction
implementation = getattr(self, current.semantics, None)
if implementation is None:
raise TerminateState("Instruction not implemented %s" % current.semantics, testcase=True)
raise TerminateState(f"Instruction not implemented {current.semantics}", testcase=True)
return implementation(*arguments)

def _checkpoint(self):
Expand Down