-
Notifications
You must be signed in to change notification settings - Fork 1
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: BACK-8055 Detect unused definitions #154
Open
ClaireGuerreGiordano
wants to merge
3
commits into
main
Choose a base branch
from
fixformat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from erc7730.lint.lint_base import MultiLinter | ||
from erc7730.lint.lint_transaction_type_classifier import ClassifyTransactionTypeLinter | ||
from erc7730.lint.lint_validate_abi import ValidateABILinter | ||
from erc7730.lint.lint_validate_definitions import DefinitionLinter | ||
from erc7730.lint.lint_validate_display_fields import ValidateDisplayFieldsLinter | ||
from erc7730.list.list import get_erc7730_files | ||
from erc7730.model.input.descriptor import InputERC7730Descriptor | ||
|
@@ -50,13 +51,8 @@ def lint_all(paths: list[Path], out: OutputAdder) -> int: | |
:param out: output adder | ||
:return: number of files checked | ||
""" | ||
linter = MultiLinter( | ||
[ | ||
ValidateABILinter(), | ||
ValidateDisplayFieldsLinter(), | ||
ClassifyTransactionTypeLinter(), | ||
] | ||
) | ||
input_linter = DefinitionLinter() | ||
output_linter = MultiLinter([ValidateABILinter(), ValidateDisplayFieldsLinter(), ClassifyTransactionTypeLinter()]) | ||
|
||
files = list(get_erc7730_files(*paths, out=out)) | ||
|
||
|
@@ -70,7 +66,11 @@ def label(f: Path) -> Path | None: | |
print(f"🔍 checking {len(files)} descriptor files…\n") | ||
|
||
with ThreadPoolExecutor() as executor: | ||
for future in (executor.submit(lint_file, file, linter, out, label(file)) for file in files): | ||
for future in (executor.submit(lint_file, file, input_linter, out, label(file)) for file in files): | ||
future.result() | ||
|
||
with ThreadPoolExecutor() as executor: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why run twice ? |
||
for future in (executor.submit(lint_file, file, output_linter, out, label(file)) for file in files): | ||
future.result() | ||
|
||
return len(files) | ||
|
@@ -91,6 +91,9 @@ def lint_file(path: Path, linter: ERC7730Linter, out: OutputAdder, show_as: Path | |
|
||
with BufferAdder(file_out, prolog=f"➡️ checking [bold]{label}[/bold]…", epilog="") as out, ExceptionsToOutput(out): | ||
input_descriptor = InputERC7730Descriptor.load(path) | ||
resolved_descriptor = ERC7730InputToResolved().convert(input_descriptor, out) | ||
if resolved_descriptor is not None: | ||
linter.lint(resolved_descriptor, out) | ||
if isinstance(linter, DefinitionLinter): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can see here that it doesn't fit. Interface is not good if you have to do special cases here |
||
linter.lint(input_descriptor, out) | ||
else: | ||
resolved_descriptor = ERC7730InputToResolved().convert(input_descriptor, out) | ||
if resolved_descriptor is not None: | ||
linter.lint(resolved_descriptor, out) |
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,31 @@ | ||
from typing import final, override | ||
|
||
from erc7730.common.output import OutputAdder | ||
from erc7730.lint import ERC7730Linter | ||
from erc7730.model.input.descriptor import InputERC7730Descriptor | ||
from erc7730.model.input.display import InputReference | ||
from erc7730.model.resolved.descriptor import ResolvedERC7730Descriptor | ||
|
||
|
||
@final | ||
class DefinitionLinter(ERC7730Linter): | ||
"""Check that parameters under definitions are used in formats section""" | ||
|
||
@override | ||
def lint(self, descriptor: ResolvedERC7730Descriptor | InputERC7730Descriptor, out: OutputAdder) -> None: | ||
if isinstance(descriptor, InputERC7730Descriptor) and descriptor.display.definitions is not None: | ||
for name, _ in descriptor.display.definitions.items(): | ||
found = False | ||
for _, format in descriptor.display.formats.items(): | ||
if found is False: | ||
for field in format.fields: | ||
if ( | ||
isinstance(field, InputReference) | ||
and field.ref.__str__() == f"$.display.definitions.{name}" | ||
): | ||
found = True | ||
if found is False: | ||
out.error( | ||
title="Unused field definition", | ||
message=f"Field {name} is not used in descriptor formats.", | ||
) |
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,71 @@ | ||
{ | ||
"context": { | ||
"$id": "AugustusSwapper", | ||
"contract" : { | ||
"abi": "https://github.com/LedgerHQ/ledger-asset-dapps/blob/main/ethereum/paraswap/abis/0x1bd435f3c054b6e901b7b108a0ab7617c808677b.abi.json", | ||
"deployments": [ | ||
{ | ||
"chainId": 1, | ||
"address": "0x1bd435f3c054b6e901b7b108a0ab7617c808677b" | ||
}, | ||
{ | ||
"chainId": 1, | ||
"address": "0xdef171fe48cf0115b1d80b88dc8eab59176fee57" | ||
}, | ||
{ | ||
"chainId": 56, | ||
"address": "0x55a0e3b6579972055faa983482aceb4b251dcf15" | ||
}, | ||
{ | ||
"chainId": 56, | ||
"address": "0xdef171fe48cf0115b1d80b88dc8eab59176fee57" | ||
}, | ||
{ | ||
"chainId": 137, | ||
"address": "0x90249ed4d69d70e709ffcd8bee2c5a566f65dade" | ||
}, | ||
{ | ||
"chainId": 137, | ||
"address": "0xdef171fe48cf0115b1d80b88dc8eab59176fee57" | ||
} | ||
] | ||
} | ||
}, | ||
|
||
"metadata": { | ||
"owner": "Paraswap" | ||
}, | ||
|
||
"display": { | ||
"definitions": { | ||
"amountIn": { | ||
"label": "Income Amount", | ||
"format": "amount" | ||
}, | ||
"amountOut": { | ||
"label": "Outcome Amount", | ||
"format": "amount" | ||
}, | ||
"amountOutIn": { | ||
"label": "Outcome/Income Amount", | ||
"format": "amount" | ||
} | ||
}, | ||
"formats": { | ||
"0xcfc0afeb" : { | ||
"$id": "swapOnZeroXv2", | ||
"fields": [ | ||
{ | ||
"path": "amountIn", | ||
"$ref": "$.display.definitions.amountIn" | ||
}, | ||
{ | ||
"path": "amountOut", | ||
"$ref": "$.display.definitions.amountOut" | ||
} | ||
], | ||
"required": ["amountIn", "amountOut"] | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can't be changed like this, linter runs on resolved descriptors. This only tricks the (python) linter but at runtime it takes resolved descriptors (see main class)