Skip to content

Commit

Permalink
chore: remove neo tables
Browse files Browse the repository at this point in the history
  • Loading branch information
renancloudwalk committed May 2, 2024
1 parent 615572f commit 6bfb3cb
Showing 1 changed file with 0 additions and 108 deletions.
108 changes: 0 additions & 108 deletions static/schema/001-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -664,114 +664,6 @@ CREATE UNIQUE INDEX index_transactions_on_hash ON public.transactions USING btre
-- PostgreSQL database dump complete
--



--- XXX temporary
CREATE TABLE public.neo_blocks (
block_number BIGINT PRIMARY KEY,
block_hash BYTEA NOT NULL,
block JSONB NOT NULL,
account_changes JSONB NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now()
) PARTITION BY RANGE (block_number);

CREATE TABLE public.neo_accounts (
block_number BIGINT NOT NULL,
address BYTEA NOT NULL,
bytecode BYTEA,
code_hash bytea NOT NULL,
balance NUMERIC NOT NULL,
nonce NUMERIC NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
PRIMARY KEY (address, block_number)
) PARTITION BY RANGE (block_number);

CREATE TABLE public.neo_account_slots (
block_number BIGINT NOT NULL,
slot_index BYTEA NOT NULL,
account_address BYTEA NOT NULL,
value BYTEA NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
PRIMARY KEY (account_address, slot_index, block_number)
) PARTITION BY RANGE (block_number);

-- Create indexes on the parent tables
CREATE INDEX neo_blocks_block_number_desc_idx ON public.neo_blocks (block_number DESC);
CREATE INDEX neo_accounts_block_number_desc_idx ON public.neo_accounts (block_number DESC);
CREATE INDEX neo_account_slots_block_number_desc_idx ON public.neo_account_slots (block_number DESC);

DO $$
DECLARE
start_block BIGINT := 0;
end_block BIGINT := 30999999;
partition_suffix TEXT;
BEGIN
FOR i IN 1..7 LOOP -- this will create partitions until 72M blocks
partition_suffix := i::TEXT; -- Convert loop index to text for suffix

-- Create partitions for public.neo_blocks
EXECUTE 'CREATE TABLE public.neo_blocks_p' || partition_suffix || ' PARTITION OF public.neo_blocks FOR VALUES FROM (' || start_block || ') TO (' || end_block || ')';
EXECUTE 'CREATE INDEX neo_blocks_p' || partition_suffix || '_block_number_desc_idx ON public.neo_blocks_p' || partition_suffix || ' (block_number DESC)';

-- Create partitions for public.neo_accounts
EXECUTE 'CREATE TABLE public.neo_accounts_p' || partition_suffix || ' PARTITION OF public.neo_accounts FOR VALUES FROM (' || start_block || ') TO (' || end_block || ')';
EXECUTE 'CREATE INDEX neo_accounts_p' || partition_suffix || '_block_number_desc_idx ON public.neo_accounts_p' || partition_suffix || ' (block_number DESC)';

-- Create partitions for public.neo_account_slots
EXECUTE 'CREATE TABLE public.neo_account_slots_p' || partition_suffix || ' PARTITION OF public.neo_account_slots FOR VALUES FROM (' || start_block || ') TO (' || end_block || ')';
EXECUTE 'CREATE INDEX neo_account_slots_p' || partition_suffix || '_block_number_desc_idx ON public.neo_account_slots_p' || partition_suffix || ' (block_number DESC)';

-- Update range for the next partition, incrementing by 5M each time
start_block := end_block;
end_block := end_block + 7862400; -- Increment by a quarter considering 1 block = 1 second
END LOOP;
END$$;

CREATE TABLE public.neo_transactions (
hash BYTEA NOT NULL,
hash_partition SMALLINT NOT NULL,
block_number BIGINT NOT NULL,
transaction_data JSONB NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
PRIMARY KEY (hash, hash_partition)
) PARTITION BY LIST (hash_partition);
CREATE INDEX idx_neo_transactions_hash ON public.neo_transactions (hash);

CREATE TABLE public.neo_logs (
hash BYTEA NOT NULL,
hash_partition SMALLINT NOT NULL,
block_number BIGINT NOT NULL,
log_idx NUMERIC NOT NULL,
address BYTEA NOT NULL,
log_data JSONB NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
PRIMARY KEY (hash, block_number, log_idx, hash_partition)
) PARTITION BY LIST (hash_partition);
CREATE INDEX idx_neo_logs_hash ON public.neo_logs (hash);

DO $$
DECLARE
partition_id INT;
BEGIN
FOR partition_id IN 0..9 LOOP
-- Create partitions for public.neo_transactions
EXECUTE format('CREATE TABLE public.neo_transactions_%s PARTITION OF public.neo_transactions FOR VALUES IN (%L);', partition_id, partition_id);
EXECUTE format('CREATE INDEX ON public.neo_transactions_%s (hash_partition);', partition_id);
EXECUTE format('CREATE INDEX ON public.neo_transactions_%s (hash);', partition_id);
EXECUTE format('CREATE INDEX ON public.neo_transactions_%s (block_number);', partition_id);

-- Create partitions for public.neo_logs
EXECUTE format('CREATE TABLE public.neo_logs_%s PARTITION OF public.neo_logs FOR VALUES IN (%L);', partition_id, partition_id);
EXECUTE format('CREATE INDEX ON public.neo_logs_%s (hash_partition);', partition_id);
EXECUTE format('CREATE INDEX ON public.neo_logs_%s (hash);', partition_id);
EXECUTE format('CREATE INDEX ON public.neo_logs_%s (block_number);', partition_id);
END LOOP;
END$$;

-- XXX END



SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
Expand Down

0 comments on commit 6bfb3cb

Please sign in to comment.