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

Replace compiled_contracts.sources column with source_codes and compiled_contracts_sources tables #15

Merged
merged 7 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
(
/* 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
rimrakhimov marked this conversation as resolved.
Show resolved Hide resolved
(
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);
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved

/*
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:
Expand Down Expand Up @@ -414,6 +458,8 @@ $$
('contracts'),
('contract_deployments'),
('compiled_contracts'),
('source_codes'),
('compiled_contracts_sources'),
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
('verified_contracts'))
LOOP
EXECUTE format('CREATE TRIGGER insert_set_created_at
Expand Down Expand Up @@ -493,6 +539,8 @@ $$
('contracts'),
('contract_deployments'),
('compiled_contracts'),
('source_codes'),
('compiled_contracts_sources'),
marcocastignoli marked this conversation as resolved.
Show resolved Hide resolved
('verified_contracts'))
LOOP
EXECUTE format('CREATE TRIGGER insert_set_created_by
Expand Down
7 changes: 3 additions & 4 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class CompiledContract:
language = ""
name = ""
fully_qualified_name = ""
sources = dict()
compiler_settings = dict()
compilation_artifacts = dict()
creation_code_artifacts = dict()
Expand Down Expand Up @@ -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),
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),
runtime_code_hash, json.dumps(self.runtime_code_artifacts)))
Expand Down