From e2045b34b0cdcea519caf2da61183ab51c8dcdc9 Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Thu, 5 Sep 2024 09:19:26 +0200 Subject: [PATCH 1/7] Add sources tables --- database.sql | 54 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/database.sql b/database.sql index c374aa2..8853073 100644 --- a/database.sql +++ b/database.sql @@ -182,9 +182,6 @@ CREATE TABLE compiled_contracts /* the fully qualified name is compiler-specific and indicates exactly which contract to look for */ fully_qualified_name VARCHAR NOT NULL, - /* map of path to source code (string => string) */ - sources jsonb NOT NULL, - /* compiler-specific settings such as optimization, linking, etc (string => any) */ compiler_settings jsonb NOT NULL, @@ -220,6 +217,53 @@ CREATE TABLE compiled_contracts CREATE INDEX compiled_contracts_creation_code_hash ON compiled_contracts USING btree (creation_code_hash); CREATE INDEX compiled_contracts_runtime_code_hash ON compiled_contracts USING btree (runtime_code_hash); +/* + The `source_codes` table stores the source code related to the contracts. + It includes a hash of the source code and the code content itself. +*/ +CREATE TABLE source_codes +( + /* the sha256 hash of the source code */ + source_code_hash bytea NOT NULL PRIMARY KEY, + + /* the keccak256 hash of the source code */ + source_code_hash_keccak bytea NOT NULL, + + /* the programming language of the source code */ + language varchar NOT NULL, + + /* the actual source code content */ + content varchar NOT NULL, + + /* timestamps */ + created_at timestamptz NOT NULL DEFAULT NOW(), + updated_at timestamptz NOT NULL DEFAULT NOW(), + + /* ownership */ + created_by varchar NOT NULL DEFAULT (current_user), + updated_by varchar NOT NULL DEFAULT (current_user) +); + +/* + The `compiled_contracts_sources` table links a compiled_contract to its associated source files. + This table contains a unique combination of compilation_id and path. +*/ +CREATE TABLE compiled_contracts_sources +( + id uuid NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(), + + /* the specific compilation and the specific source */ + compilation_id uuid NOT NULL REFERENCES compiled_contracts(id), + source_code_hash bytea NOT NULL REFERENCES source_codes(source_code_hash), + + /* the file path associated with this source code in the compilation */ + path varchar NOT NULL, + + CONSTRAINT compiled_contracts_sources_pseudo_pkey UNIQUE (compilation_id, path) +); + +CREATE INDEX compiled_contracts_sources_source_code_hash ON compiled_contracts_sources USING btree (source_code_hash); + /* The verified_contracts table links an on-chain contract with a compiled_contract Note that only one of creation or runtime bytecode must match, because: @@ -414,6 +458,8 @@ $$ ('contracts'), ('contract_deployments'), ('compiled_contracts'), + ('source_codes'), + ('compiled_contracts_sources'), ('verified_contracts')) LOOP EXECUTE format('CREATE TRIGGER insert_set_created_at @@ -493,6 +539,8 @@ $$ ('contracts'), ('contract_deployments'), ('compiled_contracts'), + ('source_codes'), + ('compiled_contracts_sources'), ('verified_contracts')) LOOP EXECUTE format('CREATE TRIGGER insert_set_created_by From 8e30966dfcf185525a8bceef03adb0736bdcc82d Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Thu, 5 Sep 2024 09:34:36 +0200 Subject: [PATCH 2/7] remove sources column from tests --- tests/helpers.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index e78cf93..81b4db9 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -39,7 +39,6 @@ class CompiledContract: language = "" name = "" fully_qualified_name = "" - sources = dict() compiler_settings = dict() compilation_artifacts = dict() creation_code_artifacts = dict() @@ -74,11 +73,11 @@ def insert(self, connection, creation_code_hash, runtime_code_hash): with connection.cursor() as cursor: cursor.execute(""" INSERT INTO compiled_contracts ( - id, compiler, version, language, name, fully_qualified_name, sources, + id, compiler, version, language, name, fully_qualified_name, compiler_settings, compilation_artifacts, creation_code_hash, creation_code_artifacts, runtime_code_hash, runtime_code_artifacts) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) - """, (self.id, self.compiler, self.version, self.language, self.name, self.fully_qualified_name, json.dumps(self.sources), + """, (self.id, self.compiler, self.version, self.language, self.name, self.fully_qualified_name, json.dumps(self.compiler_settings), json.dumps( self.compilation_artifacts), creation_code_hash, json.dumps(self.creation_code_artifacts), runtime_code_hash, json.dumps(self.runtime_code_artifacts))) From 021019947e70fc16020ba4977ca3568887a2a145 Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Thu, 5 Sep 2024 09:37:53 +0200 Subject: [PATCH 3/7] Fix number of query parameters in tests --- tests/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/helpers.py b/tests/helpers.py index 81b4db9..b9cd868 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -76,7 +76,7 @@ def insert(self, connection, creation_code_hash, runtime_code_hash): id, compiler, version, language, name, fully_qualified_name, compiler_settings, compilation_artifacts, creation_code_hash, creation_code_artifacts, runtime_code_hash, runtime_code_artifacts) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """, (self.id, self.compiler, self.version, self.language, self.name, self.fully_qualified_name, json.dumps(self.compiler_settings), json.dumps( self.compilation_artifacts), creation_code_hash, json.dumps(self.creation_code_artifacts), From d5c00c5aa2bde01d0df73a8aea250e953df76ce0 Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Thu, 5 Sep 2024 13:19:14 +0200 Subject: [PATCH 4/7] rename table `contract_sources`, add index on `compilation_id`, various fix --- database.sql | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/database.sql b/database.sql index 8853073..de08156 100644 --- a/database.sql +++ b/database.sql @@ -241,14 +241,16 @@ CREATE TABLE source_codes /* ownership */ created_by varchar NOT NULL DEFAULT (current_user), - updated_by varchar NOT NULL DEFAULT (current_user) + updated_by varchar NOT NULL DEFAULT (current_user), + + CONSTRAINT source_code_hash_check CHECK (source_code_hash = digest(content, 'sha256')) ); /* - The `compiled_contracts_sources` table links a compiled_contract to its associated source files. + The `contracts_sources` table links a compiled_contract to its associated source files. This table contains a unique combination of compilation_id and path. */ -CREATE TABLE compiled_contracts_sources +CREATE TABLE contracts_sources ( id uuid NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(), @@ -259,10 +261,11 @@ CREATE TABLE compiled_contracts_sources /* the file path associated with this source code in the compilation */ path varchar NOT NULL, - CONSTRAINT compiled_contracts_sources_pseudo_pkey UNIQUE (compilation_id, path) + CONSTRAINT contracts_sources_pseudo_pkey UNIQUE (compilation_id, path) ); -CREATE INDEX compiled_contracts_sources_source_code_hash ON compiled_contracts_sources USING btree (source_code_hash); +CREATE INDEX contracts_sources_source_code_hash ON contracts_sources USING btree (source_code_hash); +CREATE INDEX contracts_sources_compilation_id ON contracts_sources (compilation_id); /* The verified_contracts table links an on-chain contract with a compiled_contract @@ -459,7 +462,6 @@ $$ ('contract_deployments'), ('compiled_contracts'), ('source_codes'), - ('compiled_contracts_sources'), ('verified_contracts')) LOOP EXECUTE format('CREATE TRIGGER insert_set_created_at @@ -540,7 +542,6 @@ $$ ('contract_deployments'), ('compiled_contracts'), ('source_codes'), - ('compiled_contracts_sources'), ('verified_contracts')) LOOP EXECUTE format('CREATE TRIGGER insert_set_created_by From c9c740d7ddc3051f43b4028d0d44046f1507943f Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Thu, 5 Sep 2024 13:25:19 +0200 Subject: [PATCH 5/7] Revert `compiled_contracts_sources` table name --- database.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/database.sql b/database.sql index de08156..14f5d6f 100644 --- a/database.sql +++ b/database.sql @@ -247,10 +247,10 @@ CREATE TABLE source_codes ); /* - The `contracts_sources` table links a compiled_contract to its associated source files. + The `compiled_contracts_sources` table links a compiled_contract to its associated source files. This table contains a unique combination of compilation_id and path. */ -CREATE TABLE contracts_sources +CREATE TABLE compiled_contracts_sources ( id uuid NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(), @@ -261,11 +261,11 @@ CREATE TABLE contracts_sources /* the file path associated with this source code in the compilation */ path varchar NOT NULL, - CONSTRAINT contracts_sources_pseudo_pkey UNIQUE (compilation_id, path) + CONSTRAINT compiled_contracts_sources_pseudo_pkey UNIQUE (compilation_id, path) ); -CREATE INDEX contracts_sources_source_code_hash ON contracts_sources USING btree (source_code_hash); -CREATE INDEX contracts_sources_compilation_id ON contracts_sources (compilation_id); +CREATE INDEX compiled_contracts_sources_source_code_hash ON compiled_contracts_sources USING btree (source_code_hash); +CREATE INDEX compiled_contracts_sources_compilation_id ON compiled_contracts_sources (compilation_id); /* The verified_contracts table links an on-chain contract with a compiled_contract From 0d5feedc81c90201542224d3252c53589ea12bc0 Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Mon, 9 Sep 2024 09:28:31 +0200 Subject: [PATCH 6/7] reove source_codes.language --- database.sql | 3 --- 1 file changed, 3 deletions(-) diff --git a/database.sql b/database.sql index 14f5d6f..12b9d8c 100644 --- a/database.sql +++ b/database.sql @@ -229,9 +229,6 @@ CREATE TABLE source_codes /* the keccak256 hash of the source code */ source_code_hash_keccak bytea NOT NULL, - /* the programming language of the source code */ - language varchar NOT NULL, - /* the actual source code content */ content varchar NOT NULL, From e80662c6f7850189dbefa795ce0f4c6534ec666a Mon Sep 17 00:00:00 2001 From: Marco Castignoli Date: Mon, 9 Sep 2024 11:52:49 +0200 Subject: [PATCH 7/7] rename `source_codes` to `sources` --- database.sql | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/database.sql b/database.sql index 12b9d8c..076f09c 100644 --- a/database.sql +++ b/database.sql @@ -218,16 +218,16 @@ CREATE INDEX compiled_contracts_creation_code_hash ON compiled_contracts USING b CREATE INDEX compiled_contracts_runtime_code_hash ON compiled_contracts USING btree (runtime_code_hash); /* - The `source_codes` table stores the source code related to the contracts. - It includes a hash of the source code and the code content itself. + The `sources` table stores the source code related to the contracts. + It includes hashes of the source code and the code content itself. */ -CREATE TABLE source_codes +CREATE TABLE sources ( /* the sha256 hash of the source code */ - source_code_hash bytea NOT NULL PRIMARY KEY, + source_hash bytea NOT NULL PRIMARY KEY, /* the keccak256 hash of the source code */ - source_code_hash_keccak bytea NOT NULL, + source_hash_keccak bytea NOT NULL, /* the actual source code content */ content varchar NOT NULL, @@ -240,7 +240,7 @@ CREATE TABLE source_codes created_by varchar NOT NULL DEFAULT (current_user), updated_by varchar NOT NULL DEFAULT (current_user), - CONSTRAINT source_code_hash_check CHECK (source_code_hash = digest(content, 'sha256')) + CONSTRAINT source_hash_check CHECK (source_hash = digest(content, 'sha256')) ); /* @@ -253,7 +253,7 @@ CREATE TABLE compiled_contracts_sources /* the specific compilation and the specific source */ compilation_id uuid NOT NULL REFERENCES compiled_contracts(id), - source_code_hash bytea NOT NULL REFERENCES source_codes(source_code_hash), + source_hash bytea NOT NULL REFERENCES sources(source_hash), /* the file path associated with this source code in the compilation */ path varchar NOT NULL, @@ -261,7 +261,7 @@ CREATE TABLE compiled_contracts_sources CONSTRAINT compiled_contracts_sources_pseudo_pkey UNIQUE (compilation_id, path) ); -CREATE INDEX compiled_contracts_sources_source_code_hash ON compiled_contracts_sources USING btree (source_code_hash); +CREATE INDEX compiled_contracts_sources_source_hash ON compiled_contracts_sources USING btree (source_hash); CREATE INDEX compiled_contracts_sources_compilation_id ON compiled_contracts_sources (compilation_id); /* @@ -458,7 +458,7 @@ $$ ('contracts'), ('contract_deployments'), ('compiled_contracts'), - ('source_codes'), + ('sources'), ('verified_contracts')) LOOP EXECUTE format('CREATE TRIGGER insert_set_created_at @@ -538,7 +538,7 @@ $$ ('contracts'), ('contract_deployments'), ('compiled_contracts'), - ('source_codes'), + ('sources'), ('verified_contracts')) LOOP EXECUTE format('CREATE TRIGGER insert_set_created_by