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

Investigate deduplication in compiled_contracts table #1420

Closed
kuzdogan opened this issue Jun 4, 2024 · 3 comments · Fixed by #1424
Closed

Investigate deduplication in compiled_contracts table #1420

kuzdogan opened this issue Jun 4, 2024 · 3 comments · Fixed by #1424
Assignees
Labels
🪲 bug Something isn't working

Comments

@kuzdogan
Copy link
Member

kuzdogan commented Jun 4, 2024

As explained in #1416 the deplucation of the rows seem to be not working in the compiled_contracts table. Copying the explanation here:

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:

CONSTRAINT compiled_contracts_pseudo_pkey UNIQUE (compiler, language, creation_code_hash, runtime_code_hash)

However during testing the contracts get added multiple times:

SELECT compiler, "language", creation_code_hash, runtime_code_hash FROM public.compiled_contracts x
image

Am I missing something or is there something wrong?

To reproduce

  1. add a breakpoint at

    const address = await deployAndVerifyContract(

  2. Add an .only modifier to describe:

    describe.only(`Pagination in /files/contracts/{full|any|partial}/${chainFixture.chainId}`, async function () {

  3. run the Mocha - Server Integration, deploy and verify multiple contracts, and connect to the test database at localhost:5431.

@kuzdogan kuzdogan added the 🪲 bug Something isn't working label Jun 4, 2024
@manuelwedler
Copy link
Collaborator

I was interested and had a quick look why this happens. It seems like the NULL is treated as a different value each time. See this: https://stackoverflow.com/questions/8289100/create-unique-constraint-with-null-columns

We should be able to easily fix it when recreating the database. We just need:

CONSTRAINT compiled_contracts_pseudo_pkey UNIQUE NULLS NOT DISTINCT (compiler, language, creation_code_hash, runtime_code_hash) 

@marcocastignoli
Copy link
Member

marcocastignoli commented Jun 6, 2024

Nice! So we need to add this in either the sourcify migration or a new migration (Right now we are not using migrations properly)

-- Drop the existing unique constraint
ALTER TABLE compiled_contracts
DROP CONSTRAINT compiled_contracts_pseudo_pkey;

-- Add the new unique constraint with NULLS NOT DISTINCT
CONSTRAINT compiled_contracts_pseudo_pkey UNIQUE NULLS NOT DISTINCT (compiler, language, creation_code_hash, runtime_code_hash) 

@manuelwedler manuelwedler self-assigned this Jun 6, 2024
@manuelwedler manuelwedler moved this from Triage to Sprint - In Progress in Sourcify Public Jun 6, 2024
@kuzdogan
Copy link
Member Author

kuzdogan commented Jun 6, 2024

First #1416 then this. NULLs in Postgres are a bit headache 😄

@github-project-automation github-project-automation bot moved this from Sprint - In Progress to Sprint - Done in Sourcify Public Jun 7, 2024
@kuzdogan kuzdogan moved this from Sprint - Done to COMPLETED in Sourcify Public Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪲 bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants