-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
/files/contracts/partial/{chain}
endpoint and explicit ordering…
…, resolve SQL NULL comparison bug (#1416) This PR was intended to add the `/files/contracts/partial/{chain}` endpoint that is missing to list all contracts under a certain chain. However during the process, specifically when adding proper tests, I've noticed the `partial` and `any` endpoints not working as expected, and some other potential issues explained below for further investigation. TLDR; - Adding the `/files/contracts/partial/{chain}` endpoint as specified in #1415 with an option to sort `asc` and `desc` - Adding tests - [SQL NULL comparison bug](#sql-null-comparison-bug) - Potential issues: - [Deduplication not working in `compiled_contracts`](#deduplication-not-working-in-compiled_contracts) - [`matchType` not used in `database-utils.ts`](#matchtype-not-used-in-database-utils-ts) ## SQL NULL comparison bug This was due to the queries being used in `countSourcifyMatchAddresses` and `getSourcifyMatchAddressesByChainAndMatch`. Both SQL queries count the number of perfect matches with `creation_match = 'perfect' OR runtime_match = 'perfect'` and the partial matches with the logical inverse `creation_match != 'perfect' AND runtime_match != 'perfect'`. However this does not work as expected in SQL because the `creation_match` field in our case can be `NULL` and any comparison with `NULL` resolves to `NULL`, and not to `true` as expected. This can also be seen by comparing the contract numbers in `stats.json` vs the API output: `/stats.json` for Mainnet: ```js "1": { ... "full_match": 131482, "partial_match": 529877, }, // total: 661,359 ``` `/files/contracts/full/1/`: ``` "pagination": { ... "totalResults": 132056 } ``` `/files/contracts/any/1/`: ``` "pagination": { ... "totalResults": 273320 } ``` The difference (i.e. partial count) gives 141,264 whereas from stats.json we've got 529,877. The bug can be resolved by adding a `COALESCE(creation_match, '')` to make the `NULL` values `''` for comparisons. # Potential issues ## Deduplication not working in `compiled_contracts` When debugging the tests I've noticed the identical test contracts are getting added to the `compiled_contracts` table multiple times. I'd have expected them to be deduplicated because the table has a unique key property in the schema definition: https://github.com/ethereum/sourcify/blob/653952de199123406cea9c3ebda4eda88cc0652a/services/database/migrations/20231109160022-create-alliance-schema.js#L204 However during testing the contracts get added multiple times: ```SQL SELECT compiler, "language", creation_code_hash, runtime_code_hash FROM public.compiled_contracts x ``` <img width="757" alt="image" src="https://github.com/ethereum/sourcify/assets/13069972/ce877a1f-5e6a-48de-a1e4-2127b67fb8f2"> Am I missing something or is there something wrong? ### To reproduce 1) add a breakpoint at https://github.com/ethereum/sourcify/blob/653952de199123406cea9c3ebda4eda88cc0652a/services/server/test/integration/repository-handlers/files.spec.ts#L56 2) Add an `.only` modifier to describe: https://github.com/ethereum/sourcify/blob/653952de199123406cea9c3ebda4eda88cc0652a/services/server/test/integration/repository-handlers/files.spec.ts#L43 3) run the `Mocha - Server Integration`, deploy and verify multiple contracts, and connect to the test database at `localhost:5431`. ### `matchType` in `database_utils.ts` While looking at the `database_utils.ts` code I've noticed the `matchType` variable is not used in functions: https://github.com/ethereum/sourcify/blob/eeec8244c25db361f2f94981268118b188c1f53a/services/server/src/server/services/utils/database-util.ts#L429 https://github.com/ethereum/sourcify/blob/eeec8244c25db361f2f94981268118b188c1f53a/services/server/src/server/services/utils/database-util.ts#L452 But it seems these functions themselves are not used at all. Can they be removed?
- Loading branch information
Showing
15 changed files
with
285 additions
and
132 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
69 changes: 69 additions & 0 deletions
69
...rver/controllers/repository/get-contract-addresses-paginated-partial.stateless.paths.yaml
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,69 @@ | ||
openapi: "3.0.0" | ||
|
||
paths: | ||
/files/contracts/partial/{chain}: | ||
get: | ||
summary: Get the contract addresses partially verified on a chain | ||
description: Returns the partially verified contracts from the repository for the desired chain. API is paginated. Limit must be a number between 1 and 200. | ||
tags: | ||
- Repository | ||
parameters: | ||
- name: chain | ||
in: path | ||
required: true | ||
schema: | ||
type: string | ||
format: sourcify-chainId | ||
- name: page | ||
in: query | ||
required: false | ||
schema: | ||
type: number | ||
- name: limit | ||
in: query | ||
required: false | ||
schema: | ||
type: number | ||
minimum: 1 | ||
maximum: 200 | ||
- name: order | ||
in: query | ||
required: false | ||
schema: | ||
type: string | ||
enum: [asc, desc] | ||
description: Order of the results. Default is "asc" (earliest verified contract first) | ||
responses: | ||
"200": | ||
description: Chain is available as a full match in the repository | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
results: | ||
type: array | ||
items: | ||
type: string | ||
example: | ||
[ | ||
"0x1fE5d745beABA808AAdF52057Dd7AAA47b42cFD0", | ||
"0xE9c31091868d68598Ac881738D159A63532d12f9", | ||
] | ||
pagination: | ||
type: object | ||
properties: | ||
currentPage: | ||
type: number | ||
totalPages: | ||
type: number | ||
resultsPerPage: | ||
type: number | ||
resultsCurrentPage: | ||
type: number | ||
totalResults: | ||
type: number | ||
hasNextPage: | ||
type: boolean | ||
hasPreviousPage: | ||
type: boolean |
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
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.