diff --git a/go/enclave/storage/init/edgelessdb/001_init.sql b/go/enclave/storage/init/edgelessdb/001_init.sql index b118cd1111..8dc56204a6 100644 --- a/go/enclave/storage/init/edgelessdb/001_init.sql +++ b/go/enclave/storage/init/edgelessdb/001_init.sql @@ -7,6 +7,7 @@ create table if not exists obsdb.keyvalue ky varbinary(64) NOT NULL, val mediumblob NOT NULL, primary key (id), + UNIQUE (ky), INDEX USING HASH (ky) ); GRANT ALL ON obsdb.keyvalue TO obscuro; @@ -85,11 +86,12 @@ create table if not exists obsdb.batch is_canonical boolean NOT NULL, header blob NOT NULL, body int NOT NULL, - l1_proof_hash binary(32), + l1_proof_hash binary(32) NOT NULL, l1_proof INTEGER, is_executed boolean NOT NULL, primary key (sequence), INDEX USING HASH (hash(8)), + INDEX USING HASH (l1_proof_hash(8)), INDEX (body, l1_proof), INDEX (height) ); diff --git a/go/enclave/storage/init/sqlite/001_init.sql b/go/enclave/storage/init/sqlite/001_init.sql index 14daa713b0..6f07aaa91a 100644 --- a/go/enclave/storage/init/sqlite/001_init.sql +++ b/go/enclave/storage/init/sqlite/001_init.sql @@ -1,8 +1,8 @@ create table if not exists keyvalue ( id INTEGER PRIMARY KEY AUTOINCREMENT, - ky varbinary(64), - val mediumblob NOT NULL + ky varbinary(64) UNIQUE NOT NULL, + val mediumblob NOT NULL ); create index IDX_KV on keyvalue (ky); @@ -25,10 +25,10 @@ create table if not exists attestation_key create table if not exists block ( id INTEGER PRIMARY KEY AUTOINCREMENT, - hash binary(32), - is_canonical boolean NOT NULL, - header blob NOT NULL, - height int NOT NULL + hash binary(32) NOT NULL, + is_canonical boolean NOT NULL, + header blob NOT NULL, + height int NOT NULL -- the unique constraint is commented for now because there might be multiple non-canonical blocks for the same height -- unique (height, is_canonical) ); @@ -40,19 +40,19 @@ create table if not exists l1_msg id INTEGER PRIMARY KEY AUTOINCREMENT, message varbinary(1024) NOT NULL, block INTEGER NOT NULL REFERENCES block, - is_transfer boolean + is_transfer boolean NOT NULL ); create index L1_MSG_BLOCK_IDX on l1_msg (block); create table if not exists rollup ( id INTEGER PRIMARY KEY AUTOINCREMENT, - hash binary(32), - start_seq int NOT NULL, - end_seq int NOT NULL, - time_stamp int NOT NULL, - header blob NOT NULL, - compression_block INTEGER NOT NULL REFERENCES block + hash binary(32) NOT NULL, + start_seq int NOT NULL, + end_seq int NOT NULL, + time_stamp int NOT NULL, + header blob NOT NULL, + compression_block INTEGER NOT NULL REFERENCES block ); create index ROLLUP_COMPRESSION_BLOCK_IDX on rollup (compression_block); create index ROLLUP_COMPRESSION_HASH_IDX on rollup (hash); @@ -66,35 +66,34 @@ create table if not exists batch_body create table if not exists batch ( sequence int primary key, - converted_hash binary(32), + converted_hash binary(32) NOT NULL, hash binary(32) NOT NULL, height int NOT NULL, is_canonical boolean NOT NULL, header blob NOT NULL, body int NOT NULL REFERENCES batch_body, - l1_proof_hash binary(32), + l1_proof_hash binary(32) NOT NULL, l1_proof INTEGER, -- normally this would be a FK, but there is a weird edge case where an L2 node might not have the block used to create this batch is_executed boolean NOT NULL -- the unique constraint is commented for now because there might be multiple non-canonical batches for the same height -- unique (height, is_canonical, is_executed) ); create index IDX_BATCH_HASH on batch (hash); -create index IDX_BATCH_HEIGHT on batch (height, is_canonical); -create index IDX_BATCH_BLOCK on batch (l1_proof); -create index IDX_BATCH_BODY on batch (body); +create index IDX_BATCH_BLOCK on batch (l1_proof_hash); +create index IDX_BATCH_BODY on batch (body, l1_proof); +create index IDX_BATCH_HEIGHT on batch (height); create table if not exists tx ( id INTEGER PRIMARY KEY AUTOINCREMENT, - hash binary(32), + hash binary(32) NOT NULL, content mediumblob NOT NULL, sender_address binary(20) NOT NULL, nonce int NOT NULL, idx int NOT NULL, - body int REFERENCES batch_body + body int NOT NULL REFERENCES batch_body ); create index IDX_TX_HASH on tx (hash); -create index IDX_TX_BODY on tx (body); create table if not exists exec_tx ( @@ -105,8 +104,8 @@ create table if not exists exec_tx tx INTEGER, batch INTEGER NOT NULL REFERENCES batch ); -create index IDX_EX_TX_BATCH on exec_tx (batch, tx); -create index IDX_EX_TX_CCA on exec_tx (created_contract_address); +create index IDX_EX_TX_BATCH on exec_tx (batch); +create index IDX_EX_TX_CCA on exec_tx (tx, created_contract_address); -- todo denormalize. Extract contract and user table and point topic0 and rel_addreses to it create table if not exists events @@ -125,10 +124,17 @@ create table if not exists events rel_address2 binary(20), rel_address3 binary(20), rel_address4 binary(20), - tx INTEGER, + tx INTEGER NOT NULL references tx, batch INTEGER NOT NULL REFERENCES batch ); -create index IDX_BATCH_TX on events (batch, tx); +create index IDX_BATCH_TX on events (tx, batch); create index IDX_AD on events (address); -create index IDX_RAD1 on events (rel_address1, rel_address2, rel_address3, rel_address4); -create index IDX_T0 on events (topic0, topic1, topic2, topic3, topic4); +create index IDX_RAD1 on events (rel_address1); +create index IDX_RAD2 on events (rel_address2); +create index IDX_RAD3 on events (rel_address3); +create index IDX_RAD4 on events (rel_address4); +create index IDX_T0 on events (topic0); +create index IDX_T1 on events (topic1); +create index IDX_T2 on events (topic2); +create index IDX_T3 on events (topic3); +create index IDX_T4 on events (topic4);