Skip to content

Commit

Permalink
Add sources tables
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocastignoli committed Sep 5, 2024
1 parent cb8f8a5 commit e2045b3
Showing 1 changed file with 51 additions and 3 deletions.
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
(
/* 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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e2045b3

Please sign in to comment.