Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_block_fail(bad_code):
if isinstance(bad_code, tuple):
with raises(bad_code[1]):
compiler.compile(bad_code[0])
else:
with raises(TypeMismatchException):
compiler.compile(bad_code)
def test_as_wei_fail(bad_code):
with raises(TypeMismatchException):
compiler.compile(bad_code)
def struct_literals(expr, name, context):
member_subs = {}
member_typs = {}
for key, value in zip(expr.keys, expr.values):
if not isinstance(key, ast.Name):
raise TypeMismatchException(
f"Invalid member variable for struct: {getattr(key, 'id', '')}",
key,
)
check_valid_varname(
key.id,
context.custom_units,
context.structs,
context.constants,
"Invalid member variable for struct",
)
if key.id in member_subs:
raise TypeMismatchException("Member variable duplicated: " + key.id, key)
sub = Expr(value, context).lll_node
member_subs[key.id] = sub
member_typs[key.id] = sub.typ
return LLLnode.from_list(
if not is_base_type(addr.typ, 'address'):
raise TypeMismatchException(
"Type mismatch: balance keyword expects an address as input",
self.expr
)
return LLLnode.from_list(
['balance', addr],
typ=BaseType('uint256', {'wei': 1}),
location=None,
pos=getpos(self.expr),
)
# x.codesize: codesize of address x
elif self.expr.attr == 'codesize' or self.expr.attr == 'is_contract':
addr = Expr.parse_value_expr(self.expr.value, self.context)
if not is_base_type(addr.typ, 'address'):
raise TypeMismatchException(
"Type mismatch: codesize keyword expects an address as input",
self.expr,
)
if self.expr.attr == 'codesize':
eval_code = ['extcodesize', addr]
output_type = 'int128'
else:
eval_code = ['gt', ['extcodesize', addr], 0]
output_type = 'bool'
return LLLnode.from_list(
eval_code,
typ=BaseType(output_type),
location=None,
pos=getpos(self.expr),
)
# self.x: global attribute
['sha3', ['add', key.args[0].args[-1], 32], ['mload', key.args[0].args[-1]]]
])
else:
sub = LLLnode.from_list(
['sha3', ['add', key.args[0].value, 32], ['mload', key.args[0].value]]
)
else:
subtype = typ.valuetype
sub = base_type_conversion(key, key.typ, typ.keytype, pos=pos)
if location == 'storage':
return LLLnode.from_list(['sha3_64', parent, sub],
typ=subtype,
location='storage')
elif location in ('memory', 'calldata'):
raise TypeMismatchException(
"Can only have fixed-side arrays in memory, not mappings", pos
)
elif isinstance(typ, ListType):
subtype = typ.subtype
k = unwrap_location(key)
if not is_base_type(key.typ, ('int128', 'uint256')):
raise TypeMismatchException('Invalid type for array index: %r' % key.typ, pos)
if not array_bounds_check:
sub = k
elif key.typ.is_literal: # note: BaseType always has is_literal attr
# perform the check at compile time and elide the runtime check.
if key.value < 0 or key.value >= typ.count:
raise ArrayIndexException(
elif location == 'memory':
return LLLnode.from_list(['mstore', left, right], typ=None)
# Byte arrays
elif isinstance(left.typ, ByteArrayLike):
return make_byte_array_copier(left, right, pos)
# Can't copy mappings
elif isinstance(left.typ, MappingType):
raise TypeMismatchException("Cannot copy mappings; can only copy individual elements", pos)
# Arrays
elif isinstance(left.typ, ListType):
# Cannot do something like [a, b, c] = [1, 2, 3]
if left.value == "multi":
raise Exception("Target of set statement must be a single item")
if not isinstance(right.typ, ListType):
raise TypeMismatchException(
f"Setter type mismatch: left side is {left.typ}, right side is {right.typ}", pos
)
if right.typ.count != left.typ.count:
raise TypeMismatchException("Mismatched number of elements", pos)
left_token = LLLnode.from_list('_L', typ=left.typ, location=left.location)
if left.location == "storage":
left = LLLnode.from_list(['sha3_32', left], typ=left.typ, location="storage_prehashed")
left_token.location = "storage_prehashed"
# If the right side is a literal
if right.value == "multi":
subs = []
for i in range(left.typ.count):
subs.append(make_setter(add_variable_offset(
left_token,
LLLnode.from_list(i, typ='int128'),
def minmax(expr, args, kwargs, context, comparator):
def _can_compare_with_uint256(operand):
if operand.typ.typ == 'uint256':
return True
elif operand.typ.typ == 'int128' and operand.typ.is_literal and SizeLimits.in_bounds('uint256', operand.value): # noqa: E501
return True
return False
left, right = args[0], args[1]
if not are_units_compatible(left.typ, right.typ) and not are_units_compatible(right.typ, left.typ): # noqa: E501
raise TypeMismatchException("Units must be compatible", expr)
if left.typ.typ == right.typ.typ:
if left.typ.typ != 'uint256':
# if comparing like types that are not uint256, use SLT or SGT
comparator = f's{comparator}'
o = ['if', [comparator, '_l', '_r'], '_r', '_l']
otyp = left.typ
otyp.is_literal = False
elif _can_compare_with_uint256(left) and _can_compare_with_uint256(right):
o = ['if', [comparator, '_l', '_r'], '_r', '_l']
if right.typ.typ == 'uint256':
otyp = right.typ
else:
otyp = left.typ
otyp.is_literal = False
else:
raise TypeMismatchException(
if left.typ.typ == right.typ.typ:
if left.typ.typ != 'uint256':
# if comparing like types that are not uint256, use SLT or SGT
comparator = f's{comparator}'
o = ['if', [comparator, '_l', '_r'], '_r', '_l']
otyp = left.typ
otyp.is_literal = False
elif _can_compare_with_uint256(left) and _can_compare_with_uint256(right):
o = ['if', [comparator, '_l', '_r'], '_r', '_l']
if right.typ.typ == 'uint256':
otyp = right.typ
else:
otyp = left.typ
otyp.is_literal = False
else:
raise TypeMismatchException(
f"Minmax types incompatible: {left.typ.typ} {right.typ.typ}"
)
return LLLnode.from_list(
['with', '_l', left, ['with', '_r', right, o]],
typ=otyp,
pos=getpos(expr),
)
def _to_bytelike(expr, args, kwargs, context, bytetype):
if bytetype == 'string':
ReturnType = StringType
elif bytetype == 'bytes':
ReturnType = ByteArrayType
else:
raise TypeMismatchException(f'Invalid {bytetype} supplied')
in_arg = args[0]
if in_arg.typ.maxlen > args[1].slice.value.n:
raise TypeMismatchException(
f'Cannot convert as input {bytetype} are larger than max length',
expr,
)
return LLLnode(
value=in_arg.value,
args=in_arg.args,
typ=ReturnType(in_arg.typ.maxlen),
pos=getpos(expr),
location=in_arg.location
)
def to_bool(expr, args, kwargs, context):
in_arg = args[0]
input_type, _ = get_type(in_arg)
if input_type == 'bytes':
if in_arg.typ.maxlen > 32:
raise TypeMismatchException(
f"Cannot convert bytes array of max length {in_arg.value} to bool",
expr,
)
else:
num = byte_array_to_num(in_arg, expr, 'uint256')
return LLLnode.from_list(
['iszero', ['iszero', num]],
typ=BaseType('bool'),
pos=getpos(expr)
)
else:
return LLLnode.from_list(
['iszero', ['iszero', in_arg]],
typ=BaseType('bool', in_arg.typ.unit),
pos=getpos(expr)