2024-11-13 21:10:13 +00:00
|
|
|
-- Note: All timestamps inside this DB have to be in UTC+0
|
|
|
|
-- Otherwise u can travel back to future
|
|
|
|
SET TIME ZONE 'UTC';
|
|
|
|
|
|
|
|
CREATE TABLE blocks (
|
2024-11-17 21:24:19 +00:00
|
|
|
-- UNIX Timestamp
|
|
|
|
-- But not just any timestamp, some timestamp
|
|
|
|
id BIGINT PRIMARY KEY,
|
2024-11-13 21:10:13 +00:00
|
|
|
-- SHA512 sum of all hashes inside the block + prev_hash
|
|
|
|
-- 32 bytes long
|
2024-11-17 21:24:19 +00:00
|
|
|
hash BYTEA UNIQUE,
|
2024-11-13 21:10:13 +00:00
|
|
|
-- NULL only on the first block
|
2024-11-17 21:24:19 +00:00
|
|
|
prev_hash BYTEA UNIQUE,
|
2024-11-13 21:10:13 +00:00
|
|
|
finished_at TIMESTAMP,
|
|
|
|
)
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS transactions (
|
2024-11-17 21:24:19 +00:00
|
|
|
-- SHA512 -- 64 bytes long
|
|
|
|
hash BYTEA(64) PRIMARY KEY,
|
|
|
|
-- references block id
|
|
|
|
-- but may be added before the block itself
|
|
|
|
block_id BIGINT,
|
2024-11-13 21:10:13 +00:00
|
|
|
|
2024-11-17 21:24:19 +00:00
|
|
|
-- ED25519 key -- 32 bytes long
|
|
|
|
sender_public_key BYTEA(32) NOT NULL,
|
|
|
|
receiver_public_key BYTEA(32) NOT NULL,
|
2024-11-13 21:10:13 +00:00
|
|
|
|
|
|
|
-- Rewards doesn't decrease sender's balance
|
|
|
|
-- but generate additional crypto inside the system.
|
|
|
|
-- Not each sender_public_key should be able to send rewards.
|
|
|
|
is_reward BOOLEAN NOT NULL,
|
|
|
|
|
|
|
|
amount DOUBLE PRECISION NOT NULL,
|
|
|
|
amount_burned DOUBLE PRECISION NOT NULL,
|
|
|
|
message VARCHAR,
|
|
|
|
|
|
|
|
-- When sign is decrypted with the sender's
|
|
|
|
-- public key we should get the created_at
|
|
|
|
-- time in unix format
|
2024-11-17 21:24:19 +00:00
|
|
|
signature BYTEA(24) NOT NULL,
|
2024-11-13 21:10:13 +00:00
|
|
|
-- Time provided by the client
|
2024-11-17 21:24:19 +00:00
|
|
|
created_at BIGINT NOT NULL,
|
|
|
|
created_at_ns BIGINT NOT NULL,
|
2024-11-13 21:10:13 +00:00
|
|
|
-- Time when transaction was added to the server
|
2024-11-17 21:24:19 +00:00
|
|
|
added_at TIMESTAMP SET DEFAULT CURRENT TIMESTAMP,
|
2024-11-13 21:10:13 +00:00
|
|
|
|
|
|
|
-- NOTE: Will be extended with smart contracts data
|
|
|
|
);
|
2024-11-17 21:24:19 +00:00
|
|
|
|
2024-11-13 21:10:13 +00:00
|
|
|
CREATE INDEX idx_blocks_hash ON blocks (hash);
|
2024-11-17 21:24:19 +00:00
|
|
|
CREATE INDEX idx_blocks_prev_hash ON blocks (prev_hash);
|
2024-11-13 21:10:13 +00:00
|
|
|
CREATE INDEX idx_transactions_block_id ON transactions (block_id);
|
|
|
|
CREATE INDEX idx_transactions_sender_pk ON transactions (sender_public_key);
|
|
|
|
CREATE INDEX idx_transactions_receiver_pk ON transactions (receiver_public_key);
|