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

Disallow use of None, disallow use of del, implemented clear() built-in function. #1106

Merged
merged 12 commits into from
Dec 13, 2018
Merged
2 changes: 1 addition & 1 deletion tests/parser/features/test_map_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get() -> (int128, int128):

@public
def delete():
self.structmap[123] = None
del self.structmap[123]
"""

c = get_contract_with_gas_estimation(code)
Expand Down
297 changes: 297 additions & 0 deletions tests/parser/functions/test_reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@

def test_reset_basic_type(get_contract_with_gas_estimation):
contracts = [
"""
foobar: int128

@public
def foo():
self.foobar = 1
bar: int128 = 1

reset(self.foobar)
reset(bar)

assert self.foobar == 0
assert bar == 0
""",
"""
foobar: uint256

@public
def foo():
self.foobar = 1
bar: uint256 = 1

reset(self.foobar)
reset(bar)

assert self.foobar == 0
assert bar == 0
""",
"""
foobar: bool

@public
def foo():
self.foobar = True
bar: bool = True

reset(self.foobar)
reset(bar)

assert self.foobar == False
assert bar == False
""",
"""
foobar: decimal

@public
def foo():
self.foobar = 1.0
bar: decimal = 1.0

reset(self.foobar)
reset(bar)

assert self.foobar == 0.0
assert bar == 0.0
""",
"""
foobar: bytes32

@public
def foo():
self.foobar = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
bar: bytes32 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

reset(self.foobar)
reset(bar)

assert self.foobar == 0x0000000000000000000000000000000000000000000000000000000000000000
assert bar == 0x0000000000000000000000000000000000000000000000000000000000000000
""",
"""
foobar: address

@public
def foo():
self.foobar = msg.sender
bar: address = msg.sender

reset(self.foobar)
reset(bar)

assert self.foobar == ZERO_ADDRESS
assert bar == ZERO_ADDRESS
"""
]

for contract in contracts:
c = get_contract_with_gas_estimation(contract)
c.foo()


def test_reset_basic_type_lists(get_contract_with_gas_estimation):
contracts = [
"""
foobar: int128[3]

@public
def foo():
self.foobar = [1, 2, 3]
bar: int128[3] = [1, 2, 3]

reset(self.foobar)
reset(bar)

assert self.foobar[0] == 0
assert self.foobar[1] == 0
assert self.foobar[2] == 0
assert bar[0] == 0
assert bar[1] == 0
assert bar[2] == 0
""",
"""
foobar: uint256[3]

@public
def foo():
self.foobar = [1, 2, 3]
bar: uint256[3] = [1, 2, 3]

reset(self.foobar)
reset(bar)

assert self.foobar[0] == 0
assert self.foobar[1] == 0
assert self.foobar[2] == 0
assert bar[0] == 0
assert bar[1] == 0
assert bar[2] == 0
""",
"""
foobar: bool[3]

@public
def foo():
self.foobar = [True, True, True]
bar: bool[3] = [True, True, True]

reset(self.foobar)
reset(bar)

assert self.foobar[0] == False
assert self.foobar[1] == False
assert self.foobar[2] == False
assert bar[0] == False
assert bar[1] == False
assert bar[2] == False
""",
"""
foobar: decimal[3]

@public
def foo():
self.foobar = [1.0, 2.0, 3.0]
bar: decimal[3] = [1.0, 2.0, 3.0]

reset(self.foobar)
reset(bar)

assert self.foobar[0] == 0.0
assert self.foobar[1] == 0.0
assert self.foobar[2] == 0.0
assert bar[0] == 0.0
assert bar[1] == 0.0
assert bar[2] == 0.0
""",
"""
foobar: bytes32[3]

@public
def foo():
self.foobar = [
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000,
0x00000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
]
bar: bytes32[3] = [
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000,
0x00000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
]

reset(self.foobar)
reset(bar)

assert self.foobar[0] == 0x0000000000000000000000000000000000000000000000000000000000000000
assert self.foobar[1] == 0x0000000000000000000000000000000000000000000000000000000000000000
assert self.foobar[2] == 0x0000000000000000000000000000000000000000000000000000000000000000
assert bar[0] == 0x0000000000000000000000000000000000000000000000000000000000000000
assert bar[1] == 0x0000000000000000000000000000000000000000000000000000000000000000
assert bar[2] == 0x0000000000000000000000000000000000000000000000000000000000000000
""",
"""
foobar: address[3]

@public
def foo():
self.foobar = [msg.sender, msg.sender, msg.sender]
bar: address[3] = [msg.sender, msg.sender, msg.sender]

reset(self.foobar)
reset(bar)

assert self.foobar[0] == ZERO_ADDRESS
assert self.foobar[1] == ZERO_ADDRESS
assert self.foobar[2] == ZERO_ADDRESS
assert bar[0] == ZERO_ADDRESS
assert bar[1] == ZERO_ADDRESS
assert bar[2] == ZERO_ADDRESS
"""
]

for contract in contracts:
c = get_contract_with_gas_estimation(contract)
c.foo()


def test_reset_bytes(get_contract_with_gas_estimation):
code = """
foobar: bytes[5]

@public
def foo() -> (bytes[5], bytes[5]):
self.foobar = 'Hello'
bar: bytes[5] = 'World'

reset(self.foobar)
reset(bar)

return (self.foobar, bar)
"""

c = get_contract_with_gas_estimation(code)
a, b = c.foo()
assert a == b''
assert b == b''


def test_reset_struct(get_contract_with_gas_estimation):
code = """
foobar: {
a: int128,
b: uint256,
c: bool,
d: decimal,
e: bytes32,
f: address
}

@public
def foo():
self.foobar = {
a: 1,
b: 2,
c: True,
d: 3.0,
e: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
f: msg.sender
}
bar: {
a: int128,
b: uint256,
c: bool,
d: decimal,
e: bytes32,
f: address
} = {
a: 1,
b: 2,
c: True,
d: 3.0,
e: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
f: msg.sender
}

reset(self.foobar)
reset(bar)

assert self.foobar.a == 0
assert self.foobar.b == 0
assert self.foobar.c == False
assert self.foobar.d == 0.0
assert self.foobar.e == 0x0000000000000000000000000000000000000000000000000000000000000000
assert self.foobar.f == ZERO_ADDRESS

assert bar.a == 0
assert bar.b == 0
assert bar.c == False
assert bar.d == 0.0
assert bar.e == 0x0000000000000000000000000000000000000000000000000000000000000000
assert bar.f == ZERO_ADDRESS
"""

c = get_contract_with_gas_estimation(code)
c.foo()
12 changes: 6 additions & 6 deletions tests/parser/globals/test_setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ def gop() -> int128: # Following a standard naming scheme; nothing to do with th

@public
def hoo() -> int128:
self.dog = None
reset(self.dog)
return(self.dog[0] + self.dog[1] * 10 + self.dog[2] * 100)

@public
def hop() -> int128:
self.bar[1] = None
reset(self.bar[1])
return self.bar[0][0] + self.bar[0][1] * 10 + self.bar[0][2] * 100 + \
self.bar[1][0] * 1000 + self.bar[1][1] * 10000 + self.bar[1][2] * 100000

@public
def joo() -> int128:
goo: int128[3]
goo = [1, 2, 3]
goo = None
reset(goo)
return(goo[0] + goo[1] * 10 + goo[2] * 100)

@public
def jop() -> int128:
gar: int128[3][3]
gar[0] = [1, 2, 3]
gar[1] = [4, 5, 6]
gar[1] = None
reset(gar[1])
return gar[0][0] + gar[0][1] * 10 + gar[0][2] * 100 + \
gar[1][0] * 1000 + gar[1][1] * 10000 + gar[1][2] * 100000

Expand Down Expand Up @@ -175,8 +175,8 @@ def fop() -> int128:
@public
def foq() -> int128:
popp: {a: {c: int128}[3], b:int128} = {a: [{c: 1}, {c: 2}, {c: 3}], b: 4}
popp.a[0] = None
popp.a[2] = None
reset(popp.a[0])
reset(popp.a[2])
return popp.a[0].c + popp.a[1].c * 10 + popp.a[2].c * 100 + popp.b * 1000
"""

Expand Down
2 changes: 1 addition & 1 deletion tests/parser/integration/test_crowdfund.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def refund():
self.refundIndex = self.nextFunderIndex
return
send(self.funders[i].sender, self.funders[i].value)
self.funders[i] = None
self.funders[i] = {sender: ZERO_ADDRESS, value: 0}
self.refundIndex = ind + 30

"""
Expand Down
5 changes: 3 additions & 2 deletions tests/parser/syntax/test_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def foo():
nom: {a: {c: int128}[int128], b: int128}
@public
def foo():
self.nom = None
reset(self.nom)
""",
"""
nom: {a: {c: int128}[int128], b: int128}
Expand Down Expand Up @@ -172,7 +172,8 @@ def foo():
nom: {c: int128}[3]
@public
def foo():
self.mom = {a: None, b: 5}
empty: {c: int128}[3]
self.mom = {a: empty, b: 5}
""",
"""
mom: {a: {c: int128}[3], b: int128}
Expand Down
Loading