-
-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: complex arguments to builtin functions (#3167)
prior to this commit, some builtin functions including ceil, would panic if their arguments were function calls (or otherwise determined to be complex expressions by `is_complex_ir`). this commit fixes the relevant builtin functions by using `cache_when_complex` where appropriate. --------- Co-authored-by: Charles Cooper <[email protected]>
- Loading branch information
1 parent
a8382f5
commit 6ee74f5
Showing
9 changed files
with
422 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
def test_uint256_addmod(assert_tx_failed, get_contract_with_gas_estimation): | ||
uint256_code = """ | ||
@external | ||
def _uint256_addmod(x: uint256, y: uint256, z: uint256) -> uint256: | ||
return uint256_addmod(x, y, z) | ||
""" | ||
|
||
c = get_contract_with_gas_estimation(uint256_code) | ||
|
||
assert c._uint256_addmod(1, 2, 2) == 1 | ||
assert c._uint256_addmod(32, 2, 32) == 2 | ||
assert c._uint256_addmod((2**256) - 1, 0, 2) == 1 | ||
assert c._uint256_addmod(2**255, 2**255, 6) == 4 | ||
assert_tx_failed(lambda: c._uint256_addmod(1, 2, 0)) | ||
|
||
|
||
def test_uint256_addmod_ext_call( | ||
w3, side_effects_contract, assert_side_effects_invoked, get_contract | ||
): | ||
code = """ | ||
@external | ||
def foo(f: Foo) -> uint256: | ||
return uint256_addmod(32, 2, f.foo(32)) | ||
interface Foo: | ||
def foo(x: uint256) -> uint256: payable | ||
""" | ||
|
||
c1 = side_effects_contract("uint256") | ||
c2 = get_contract(code) | ||
|
||
assert c2.foo(c1.address) == 2 | ||
assert_side_effects_invoked(c1, lambda: c2.foo(c1.address, transact={})) | ||
|
||
|
||
def test_uint256_addmod_internal_call(get_contract_with_gas_estimation): | ||
code = """ | ||
@external | ||
def foo() -> uint256: | ||
return uint256_addmod(self.a(), self.b(), self.c()) | ||
@internal | ||
def a() -> uint256: | ||
return 32 | ||
@internal | ||
def b() -> uint256: | ||
return 2 | ||
@internal | ||
def c() -> uint256: | ||
return 32 | ||
""" | ||
|
||
c = get_contract_with_gas_estimation(code) | ||
|
||
assert c.foo() == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
def test_ext_call(w3, side_effects_contract, assert_side_effects_invoked, get_contract): | ||
code = """ | ||
@external | ||
def foo(a: Foo) -> uint256: | ||
return as_wei_value(a.foo(7), "ether") | ||
interface Foo: | ||
def foo(x: uint8) -> uint8: nonpayable | ||
""" | ||
|
||
c1 = side_effects_contract("uint8") | ||
c2 = get_contract(code) | ||
|
||
assert c2.foo(c1.address) == w3.to_wei(7, "ether") | ||
assert_side_effects_invoked(c1, lambda: c2.foo(c1.address, transact={})) | ||
|
||
|
||
def test_internal_call(w3, get_contract_with_gas_estimation): | ||
code = """ | ||
@external | ||
def foo() -> uint256: | ||
return as_wei_value(self.bar(), "ether") | ||
@internal | ||
def bar() -> uint8: | ||
return 7 | ||
""" | ||
|
||
c = get_contract_with_gas_estimation(code) | ||
|
||
assert c.foo() == w3.to_wei(7, "ether") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
def test_uint256_mulmod(assert_tx_failed, get_contract_with_gas_estimation): | ||
uint256_code = """ | ||
@external | ||
def _uint256_mulmod(x: uint256, y: uint256, z: uint256) -> uint256: | ||
return uint256_mulmod(x, y, z) | ||
""" | ||
|
||
c = get_contract_with_gas_estimation(uint256_code) | ||
|
||
assert c._uint256_mulmod(3, 1, 2) == 1 | ||
assert c._uint256_mulmod(200, 3, 601) == 600 | ||
assert c._uint256_mulmod(2**255, 1, 3) == 2 | ||
assert c._uint256_mulmod(2**255, 2, 6) == 4 | ||
assert_tx_failed(lambda: c._uint256_mulmod(2, 2, 0)) | ||
|
||
|
||
def test_uint256_mulmod_complex(get_contract_with_gas_estimation): | ||
modexper = """ | ||
@external | ||
def exponential(base: uint256, exponent: uint256, modulus: uint256) -> uint256: | ||
o: uint256 = 1 | ||
for i in range(256): | ||
o = uint256_mulmod(o, o, modulus) | ||
if exponent & shift(1, 255 - i) != 0: | ||
o = uint256_mulmod(o, base, modulus) | ||
return o | ||
""" | ||
|
||
c = get_contract_with_gas_estimation(modexper) | ||
assert c.exponential(3, 5, 100) == 43 | ||
assert c.exponential(2, 997, 997) == 2 | ||
|
||
|
||
def test_uint256_mulmod_ext_call( | ||
w3, side_effects_contract, assert_side_effects_invoked, get_contract | ||
): | ||
code = """ | ||
@external | ||
def foo(f: Foo) -> uint256: | ||
return uint256_mulmod(200, 3, f.foo(601)) | ||
interface Foo: | ||
def foo(x: uint256) -> uint256: nonpayable | ||
""" | ||
|
||
c1 = side_effects_contract("uint256") | ||
c2 = get_contract(code) | ||
|
||
assert c2.foo(c1.address) == 600 | ||
|
||
assert_side_effects_invoked(c1, lambda: c2.foo(c1.address, transact={})) | ||
|
||
|
||
def test_uint256_mulmod_internal_call(get_contract_with_gas_estimation): | ||
code = """ | ||
@external | ||
def foo() -> uint256: | ||
return uint256_mulmod(self.a(), self.b(), self.c()) | ||
@internal | ||
def a() -> uint256: | ||
return 200 | ||
@internal | ||
def b() -> uint256: | ||
return 3 | ||
@internal | ||
def c() -> uint256: | ||
return 601 | ||
""" | ||
|
||
c = get_contract_with_gas_estimation(code) | ||
|
||
assert c.foo() == 600 |
Oops, something went wrong.