Merge pull request #454 from crytic/dev-decimal

Use Decimal instead of float
pull/463/head
Feist Josselin 5 years ago committed by GitHub
commit 52520809cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      slither/printers/guidance/echidna.py
  2. 10
      slither/slithir/variables/constant.py
  3. 6
      slither/utils/arithmetic.py

@ -130,7 +130,10 @@ def json_serializable(cls):
@json_serializable @json_serializable
class ConstantValue(NamedTuple): class ConstantValue(NamedTuple):
value: Union[str, int, bool] # Here value should be Union[str, int, bool]
# But the json lib in Echidna does not handle large integer in json
# So we convert everything to string
value: str
type: str type: str
@ -142,17 +145,17 @@ def _extract_constants_from_irs(irs: List[Operation],
if isinstance(ir, Binary): if isinstance(ir, Binary):
for r in ir.read: for r in ir.read:
if isinstance(r, Constant): if isinstance(r, Constant):
all_cst_used_in_binary[BinaryType.str(ir.type)].append(ConstantValue(r.value, str(r.type))) all_cst_used_in_binary[BinaryType.str(ir.type)].append(ConstantValue(str(r.value), str(r.type)))
if isinstance(ir, TypeConversion): if isinstance(ir, TypeConversion):
if isinstance(ir.variable, Constant): if isinstance(ir.variable, Constant):
all_cst_used.append(ConstantValue(ir.variable.value, str(ir.type))) all_cst_used.append(ConstantValue(str(ir.variable.value), str(ir.type)))
continue continue
for r in ir.read: for r in ir.read:
# Do not report struct_name in a.struct_name # Do not report struct_name in a.struct_name
if isinstance(ir, Member): if isinstance(ir, Member):
continue continue
if isinstance(r, Constant): if isinstance(r, Constant):
all_cst_used.append(ConstantValue(r.value, str(r.type))) all_cst_used.append(ConstantValue(str(r.value), str(r.type)))
if isinstance(r, StateVariable): if isinstance(r, StateVariable):
if r.node_initialization: if r.node_initialization:
if r.node_initialization.irs: if r.node_initialization.irs:

@ -1,3 +1,5 @@
from decimal import Decimal
from .variable import SlithIRVariable from .variable import SlithIRVariable
from slither.core.solidity_types.elementary_type import ElementaryType, Int, Uint from slither.core.solidity_types.elementary_type import ElementaryType, Int, Uint
from slither.utils.arithmetic import convert_subdenomination from slither.utils.arithmetic import convert_subdenomination
@ -23,12 +25,12 @@ class Constant(SlithIRVariable):
else: else:
if 'e' in val: if 'e' in val:
base, expo = val.split('e') base, expo = val.split('e')
self._val = int(float(base)* (10 ** int(expo))) self._val = int(Decimal(base) * (10 ** int(expo)))
elif 'E' in val: elif 'E' in val:
base, expo = val.split('E') base, expo = val.split('E')
self._val = int(float(base) * (10 ** int(expo))) self._val = int(Decimal(base) * (10 ** int(expo)))
else: else:
self._val = int(float(val)) self._val = int(Decimal(val))
elif type.type == 'bool': elif type.type == 'bool':
self._val = (val == 'true') | (val == 'True') self._val = (val == 'true') | (val == 'True')
else: else:
@ -36,7 +38,7 @@ class Constant(SlithIRVariable):
else: else:
if val.isdigit(): if val.isdigit():
self._type = ElementaryType('uint256') self._type = ElementaryType('uint256')
self._val = int(val) self._val = int(Decimal(val))
else: else:
self._type = ElementaryType('string') self._type = ElementaryType('string')
self._val = val self._val = val

@ -1,3 +1,5 @@
from decimal import Decimal
from slither.exceptions import SlitherException from slither.exceptions import SlitherException
@ -5,9 +7,9 @@ def convert_subdenomination(value, sub):
# to allow 0.1 ether conversion # to allow 0.1 ether conversion
if value[0:2] == "0x": if value[0:2] == "0x":
value = float(int(value, 16)) value = Decimal(int(value, 16))
else: else:
value = float(value) value = Decimal(value)
if sub == 'wei': if sub == 'wei':
return int(value) return int(value)
if sub == 'szabo': if sub == 'szabo':

Loading…
Cancel
Save