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

feat: Refactor assert_tx_failed into a context #3706

Merged
merged 17 commits into from
Dec 23, 2023

Conversation

DanielSchiavini
Copy link
Contributor

PR based on #3704

What I did

  • Converted asset_tx_failed into a context
  • Updated all tests using it

How I did it

  • Replacing a callback by a context

How to verify it

  • Tests should continue passing

Commit message

Convert assert_tx_failed into tx_failed context

Description for the changelog

n/a

Cute Animal Picture

image

@codecov-commenter
Copy link

codecov-commenter commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (88c09a2) 83.92% compared to head (219bfce) 83.95%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3706      +/-   ##
==========================================
+ Coverage   83.92%   83.95%   +0.03%     
==========================================
  Files          92       92              
  Lines       13065    13065              
  Branches     2934     2934              
==========================================
+ Hits        10965    10969       +4     
+ Misses       1666     1662       -4     
  Partials      434      434              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -198,7 +198,7 @@ def foo() -> uint256:


def test_minmax_var_uint256_negative_int128(
get_contract_with_gas_estimation, assert_tx_failed, assert_compile_failed
get_contract_with_gas_estimation, tx_failed, assert_compile_failed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: looks like the fixture isn't actually used in this test



def test_checkable_raw_call(get_contract, assert_tx_failed):
def test_checkable_raw_call(get_contract, tx_failed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the fixture is not actually used

Comment on lines +1152 to +1153
with tx_failed(exception=StructureException):
get_contract(contract)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be

Suggested change
with tx_failed(exception=StructureException):
get_contract(contract)
with pytest.raises(StructureException):
compile_code(contract)

@@ -449,7 +449,7 @@ def whoami() -> address:
assert logged_addr == addr, "oh no"


def test_nested_static_params_only(get_contract, assert_tx_failed):
def test_nested_static_params_only(get_contract, tx_failed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: unused fixture

@@ -193,7 +194,7 @@ def bar():


def test_event_logging_cannot_have_more_than_three_topics(
assert_tx_failed, get_contract_with_gas_estimation
tx_failed, get_contract_with_gas_estimation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: get_contract_with_gas_estimation no longer used

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tx_failed is also no logner used

@@ -962,7 +966,7 @@ def set_list():
]


def test_logging_fails_when_input_is_too_big(assert_tx_failed, get_contract_with_gas_estimation):
def test_logging_fails_when_input_is_too_big(tx_failed, get_contract_with_gas_estimation):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of the tests in this file can be rewritten with with pytest.raises(...): compile_code(...)

TransactionFailed,
exc_text=f"execution reverted: {revert_bytes}",
)
with tx_failed(TransactionFailed, exc_text=f"execution reverted: {revert_bytes}"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary specification of TransactionFailed

TransactionFailed,
exc_text=f"execution reverted: {revert_bytes}",
)
with tx_failed(TransactionFailed, exc_text=f"execution reverted: {revert_bytes}"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary specification of TransactionFailed

exc_text=f"execution reverted: {revert_bytes}",
)
with tx_failed(TransactionFailed, exc_text=f"execution reverted: {revert_bytes}"):
get_contract_with_gas_estimation(reverty_code).foo(transact={})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be better written over multiple lines --

c = get_contract(reverty_code)
with tx_failed(exc_text=...):
    c.foo(transact={})

exc_text=f"execution reverted: {revert_bytes}",
)
with tx_failed(TransactionFailed, exc_text=f"execution reverted: {revert_bytes}"):
get_contract_with_gas_estimation(reverty_code).foo(transact={})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be clearer if rewritten over multiple lines

Comment on lines +49 to +50
with tx_failed(TransactionFailed, exc_text=f"execution reverted: {revert_bytes}"):
get_contract_with_gas_estimation(reverty_code).foo(transact={})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TransactionFailed + multiple side effects on one line

assert_compile_failed(lambda code=code_2: get_contract(code), ZeroDivisionException)
assert_tx_failed(lambda p=y, code=code_3: get_contract(code).foo(p))
with tx_failed():
get_contract(code_3).foo(y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiple side effects on one line

with tx_failed():
c.foo(x, y)
with tx_failed():
get_contract(code_2).foo(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiple side effects on one line

# Check if buyer can call receive
c.received(transact={"from": a1})
# Final check if everything worked. 1 value has been transferred
assert get_balance() == (init_bal_a0 + 1, init_bal_a1 - 1)


def test_received_reentrancy(w3, get_contract, assert_tx_failed, get_balance, contract_code):
def test_received_reentrancy(w3, get_contract, tx_failed, get_balance, contract_code):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

)
assert_tx_failed(
lambda: erc1155.balanceOfBatch([a2, a2, a2], [21, 22], transact={"from": owner})
== [1, 1, 1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of weird stray check, nice catch


assert lambda: erc1155.balanceOfBatch([a3, a3, a3], [1, 2, 3]) == [0, 0, 1]
assert erc1155.balanceOfBatch([a3, a3, a3], [1, 2, 3]) == [0, 0, 0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, interesting catch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, good catch, will check the rest of the codebase that we don't have such uncalled lambdas elsewhere



@pytest.mark.parametrize("denom,multiplier", wei_denoms.items())
@pytest.mark.parametrize("data_type", ["decimal", "int128", "uint256"])
def test_zero_value(get_contract, assert_tx_failed, denom, multiplier, data_type):
def test_zero_value(get_contract, tx_failed, denom, multiplier, data_type):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

@@ -154,4 +155,5 @@ def testin(x: address) -> bool:
return True
return False
"""
assert_tx_failed(lambda: get_contract_with_gas_estimation(code), TypeMismatch)
with tx_failed(TypeMismatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with pytest.raises(...): compile_code(...)

@@ -8,7 +8,7 @@ def _fixup_err_str(s):
return s.replace("execution reverted: ", "")


def test_assert_refund(w3, get_contract_with_gas_estimation, assert_tx_failed):
def test_assert_refund(w3, get_contract_with_gas_estimation, tx_failed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

@@ -26,7 +26,7 @@ def foo():
assert tx_receipt["gasUsed"] < gas_sent


def test_assert_reason(w3, get_contract_with_gas_estimation, assert_tx_failed, memory_mocker):
def test_assert_reason(w3, get_contract_with_gas_estimation, tx_failed, memory_mocker):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture?

@@ -565,10 +565,11 @@ def foo_():
log MyLog(b'yo')
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), InvalidType)
with tx_failed(InvalidType):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -579,12 +580,11 @@ def foo():
log MyLog(b'bars')
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), InvalidType)
with tx_failed(InvalidType):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -594,10 +594,11 @@ def foo(arg1: Bytes[4]):
log MyLog(arg1)
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), TypeMismatch)
with tx_failed(TypeMismatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -607,12 +608,11 @@ def foo():
log MyLog(b'bars')
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), InvalidType)
with tx_failed(InvalidType):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -622,7 +622,8 @@ def foo(arg1: Bytes[4]):
log MyLog(arg1)
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), TypeMismatch)
with tx_failed(TypeMismatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

assert_tx_failed(
lambda: get_contract_with_gas_estimation(loggy_code), EventDeclarationException
)
with tx_failed(EventDeclarationException):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -665,23 +665,23 @@ def foo():
log MyLog()
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), NamespaceCollision)
with tx_failed(NamespaceCollision):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

loggy_code = """

@external
def foo():
log MyLog()
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), UndeclaredDefinition)
with tx_failed(UndeclaredDefinition):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -691,10 +691,11 @@ def foo():
log MyLog(self)
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), TypeMismatch)
with tx_failed(TypeMismatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -704,11 +705,12 @@ def foo():
log MyLog(self)
"""

assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), TypeMismatch)
with tx_failed(TypeMismatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -718,11 +720,12 @@ def test_logging_fails_when_number_of_arguments_is_greater_than_declaration(
def foo():
log MyLog(1, 2)
"""
assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), ArgumentException)
with tx_failed(ArgumentException):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -733,7 +736,8 @@ def test_logging_fails_when_number_of_arguments_is_less_than_declaration(
def foo():
log MyLog(1)
"""
assert_tx_failed(lambda: get_contract_with_gas_estimation(loggy_code), ArgumentException)
with tx_failed(ArgumentException):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

@@ -971,7 +975,8 @@ def test_logging_fails_when_input_is_too_big(assert_tx_failed, get_contract_with
def foo(inp: Bytes[33]):
log Bar(inp)
"""
assert_tx_failed(lambda: get_contract_with_gas_estimation(code), TypeMismatch)
with tx_failed(TypeMismatch):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest.raises/compile_code

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving and merging this -- i reviewed for semantic changes to the tests. i found two examples (#3706 (comment), #3706 (comment)), but these seem to be fixes to existing mistakes in the tests. otherwise, everything looks clean.

@tserg and @cyberthirst if you could look over this PR for any potential breaking semantic changes to the tests, that would be very helpful.

i also found a bunch of potential style/performance improvements, e.g. there are a lot of places where with tx_failed(<compiler exception>): get_contract(code) can be rewritten with with pytest.raises(<compiler exception>): compile_code(code). however, since this PR was somewhat hard to review (and also to prevent potential merge conflicts / blocking other PRs) i'd rather have these fixed in a follow-up PR please @DanielSchiavini.

@charles-cooper charles-cooper enabled auto-merge (squash) December 23, 2023 17:15
@charles-cooper charles-cooper merged commit 2e41873 into vyperlang:master Dec 23, 2023
84 checks passed
Copy link
Collaborator

@tserg tserg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look through and did not spot any other breaking semantic changes to the tests.



@pytest.mark.parametrize("denom,multiplier", wei_denoms.items())
def test_wei_int128(get_contract, assert_tx_failed, denom, multiplier):
def test_wei_int128(get_contract, tx_failed, denom, multiplier):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

@@ -54,7 +55,7 @@ def foo(a: int128) -> uint256:


@pytest.mark.parametrize("denom,multiplier", wei_denoms.items())
def test_wei_decimal(get_contract, assert_tx_failed, denom, multiplier):
def test_wei_decimal(get_contract, tx_failed, denom, multiplier):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture



def test_another_zero_method_id(w3, get_logs, get_contract, assert_tx_failed):
def test_another_zero_method_id(w3, get_logs, get_contract, tx_failed):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

@@ -193,7 +194,7 @@ def bar():


def test_event_logging_cannot_have_more_than_three_topics(
assert_tx_failed, get_contract_with_gas_estimation
tx_failed, get_contract_with_gas_estimation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tx_failed is also no logner used

assert erc1155.balanceOf(a1, 21) == 0


# TODO: mint 20 NFTs [1:20] and check the balance for each
def test_mintBatch_balanceOf(erc1155, w3, assert_tx_failed): # test_mint_batch
def test_mintBatch_balanceOf(erc1155, w3, tx_failed): # test_mint_batch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture



def test_abs_lower_bound_folded(get_contract, assert_tx_failed):
def test_abs_lower_bound_folded(get_contract, tx_failed):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

@@ -152,11 +153,11 @@ def test3() -> (address, int128):
assert c.test3() == [c.out_literals()[2], 1]


def test_tuple_return_typecheck(assert_tx_failed, get_contract_with_gas_estimation):
def test_tuple_return_typecheck(tx_failed, get_contract_with_gas_estimation):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused fixture

@cyberthirst
Copy link
Collaborator

Didn't spot any additional semantics-breaking changes.

charles-cooper added a commit to charles-cooper/vyper that referenced this pull request Dec 27, 2023
rename `assert_tx_failed` to `tx_failed` and change it into a context
manager which has a similar API to `pytest.raises()`.

---------

Co-authored-by: Charles Cooper <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants