From 1b4e57a5b4ce88b0d332c02f37028bddd72a3448 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Wed, 21 Mar 2018 09:53:20 -0500 Subject: [PATCH] Rename node table and node column --- .../1521640994_rename_node_table.down.sql | 23 +++++ .../1521640994_rename_node_table.up.sql | 21 +++++ db/schema.sql | 84 +++++++++---------- libraries/shared/watcher.go | 6 +- pkg/datastore/postgres/postgres.go | 6 +- .../postgres/repositories/block_repository.go | 8 +- 6 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 db/migrations/1521640994_rename_node_table.down.sql create mode 100644 db/migrations/1521640994_rename_node_table.up.sql diff --git a/db/migrations/1521640994_rename_node_table.down.sql b/db/migrations/1521640994_rename_node_table.down.sql new file mode 100644 index 00000000..70d459c2 --- /dev/null +++ b/db/migrations/1521640994_rename_node_table.down.sql @@ -0,0 +1,23 @@ +BEGIN; +ALTER TABLE public.eth_nodes + RENAME TO nodes; + +ALTER TABLE public.nodes + RENAME COLUMN eth_node_id TO node_id; + +ALTER TABLE public.nodes + DROP CONSTRAINT eth_node_uc; +ALTER TABLE public.nodes + ADD CONSTRAINT node_uc UNIQUE (genesis_block, network_id, node_id); + +ALTER TABLE public.blocks RENAME COLUMN eth_node_id TO node_id; + +ALTER TABLE public.blocks DROP CONSTRAINT eth_node_id_block_number_uc; +ALTER TABLE public.blocks + ADD CONSTRAINT node_id_block_number_uc UNIQUE (number, node_id); + +ALTER TABLE public.blocks DROP CONSTRAINT node_fk; +ALTER TABLE public.blocks + ADD CONSTRAINT node_fk +FOREIGN KEY (node_id) REFERENCES nodes (id) ON DELETE CASCADE; +COMMIT; \ No newline at end of file diff --git a/db/migrations/1521640994_rename_node_table.up.sql b/db/migrations/1521640994_rename_node_table.up.sql new file mode 100644 index 00000000..3b6343c0 --- /dev/null +++ b/db/migrations/1521640994_rename_node_table.up.sql @@ -0,0 +1,21 @@ +BEGIN; +ALTER TABLE public.nodes RENAME TO eth_nodes; + +ALTER TABLE public.eth_nodes RENAME COLUMN node_id TO eth_node_id; + +ALTER TABLE public.eth_nodes DROP CONSTRAINT node_uc; +ALTER TABLE public.eth_nodes + ADD CONSTRAINT eth_node_uc UNIQUE (genesis_block, network_id, eth_node_id); + +ALTER TABLE public.blocks RENAME COLUMN node_id TO eth_node_id; + +ALTER TABLE public.blocks DROP CONSTRAINT node_id_block_number_uc; +ALTER TABLE public.blocks + ADD CONSTRAINT eth_node_id_block_number_uc UNIQUE (number, eth_node_id); + +ALTER TABLE public.blocks DROP CONSTRAINT node_fk; +ALTER TABLE public.blocks + ADD CONSTRAINT node_fk +FOREIGN KEY (eth_node_id) REFERENCES eth_nodes (id) ON DELETE CASCADE; + +COMMIT; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index 5e139be6..2248efcb 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -79,7 +79,7 @@ CREATE TABLE blocks ( parenthash character varying(66), size character varying, uncle_hash character varying(66), - node_id integer NOT NULL, + eth_node_id integer NOT NULL, is_final boolean, miner character varying(42), extra_data character varying, @@ -108,6 +108,19 @@ CREATE SEQUENCE blocks_id_seq ALTER SEQUENCE blocks_id_seq OWNED BY blocks.id; +-- +-- Name: eth_nodes; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE eth_nodes ( + id integer NOT NULL, + genesis_block character varying(66), + network_id numeric, + eth_node_id character varying(128), + client_name character varying +); + + -- -- Name: log_filters; Type: TABLE; Schema: public; Owner: - -- @@ -168,19 +181,6 @@ CREATE SEQUENCE logs_id_seq ALTER SEQUENCE logs_id_seq OWNED BY logs.id; --- --- Name: nodes; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE nodes ( - id integer NOT NULL, - genesis_block character varying(66), - network_id numeric, - node_id character varying(128), - client_name character varying -); - - -- -- Name: nodes_id_seq; Type: SEQUENCE; Schema: public; Owner: - -- @@ -198,7 +198,7 @@ CREATE SEQUENCE nodes_id_seq -- Name: nodes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - -- -ALTER SEQUENCE nodes_id_seq OWNED BY nodes.id; +ALTER SEQUENCE nodes_id_seq OWNED BY eth_nodes.id; -- @@ -346,6 +346,13 @@ CREATE VIEW watched_event_logs AS ALTER TABLE ONLY blocks ALTER COLUMN id SET DEFAULT nextval('blocks_id_seq'::regclass); +-- +-- Name: eth_nodes id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY eth_nodes ALTER COLUMN id SET DEFAULT nextval('nodes_id_seq'::regclass); + + -- -- Name: log_filters id; Type: DEFAULT; Schema: public; Owner: - -- @@ -360,13 +367,6 @@ ALTER TABLE ONLY log_filters ALTER COLUMN id SET DEFAULT nextval('log_filters_id ALTER TABLE ONLY logs ALTER COLUMN id SET DEFAULT nextval('logs_id_seq'::regclass); --- --- Name: nodes id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY nodes ALTER COLUMN id SET DEFAULT nextval('nodes_id_seq'::regclass); - - -- -- Name: receipts id; Type: DEFAULT; Schema: public; Owner: - -- @@ -404,6 +404,22 @@ ALTER TABLE ONLY watched_contracts ADD CONSTRAINT contract_hash_uc UNIQUE (contract_hash); +-- +-- Name: blocks eth_node_id_block_number_uc; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY blocks + ADD CONSTRAINT eth_node_id_block_number_uc UNIQUE (number, eth_node_id); + + +-- +-- Name: eth_nodes eth_node_uc; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY eth_nodes + ADD CONSTRAINT eth_node_uc UNIQUE (genesis_block, network_id, eth_node_id); + + -- -- Name: logs logs_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -421,26 +437,10 @@ ALTER TABLE ONLY log_filters -- --- Name: blocks node_id_block_number_uc; Type: CONSTRAINT; Schema: public; Owner: - +-- Name: eth_nodes nodes_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY blocks - ADD CONSTRAINT node_id_block_number_uc UNIQUE (number, node_id); - - --- --- Name: nodes node_uc; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY nodes - ADD CONSTRAINT node_uc UNIQUE (genesis_block, network_id, node_id); - - --- --- Name: nodes nodes_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY nodes +ALTER TABLE ONLY eth_nodes ADD CONSTRAINT nodes_pkey PRIMARY KEY (id); @@ -494,7 +494,7 @@ CREATE INDEX block_number_index ON blocks USING btree (number); -- Name: node_id_index; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX node_id_index ON blocks USING btree (node_id); +CREATE INDEX node_id_index ON blocks USING btree (eth_node_id); -- @@ -531,7 +531,7 @@ ALTER TABLE ONLY transactions -- ALTER TABLE ONLY blocks - ADD CONSTRAINT node_fk FOREIGN KEY (node_id) REFERENCES nodes(id) ON DELETE CASCADE; + ADD CONSTRAINT node_fk FOREIGN KEY (eth_node_id) REFERENCES eth_nodes(id) ON DELETE CASCADE; -- diff --git a/libraries/shared/watcher.go b/libraries/shared/watcher.go index 369b20a0..5132bee3 100644 --- a/libraries/shared/watcher.go +++ b/libraries/shared/watcher.go @@ -6,9 +6,9 @@ import ( ) type Watcher struct { - Transformers []Transformer - DB postgres.DB - Blockchain core.Blockchain + Transformers []Transformer + DB postgres.DB + Blockchain core.Blockchain } func (watcher *Watcher) AddTransformers(us []TransformerInitializer) { diff --git a/pkg/datastore/postgres/postgres.go b/pkg/datastore/postgres/postgres.go index 60c25255..0dcdfce8 100644 --- a/pkg/datastore/postgres/postgres.go +++ b/pkg/datastore/postgres/postgres.go @@ -39,13 +39,13 @@ func NewDB(databaseConfig config.Database, node core.Node) (*DB, error) { func (db *DB) CreateNode(node *core.Node) error { var nodeId int64 err := db.QueryRow( - `INSERT INTO nodes (genesis_block, network_id, node_id, client_name) + `INSERT INTO eth_nodes (genesis_block, network_id, eth_node_id, client_name) VALUES ($1, $2, $3, $4) - ON CONFLICT (genesis_block, network_id, node_id) + ON CONFLICT (genesis_block, network_id, eth_node_id) DO UPDATE SET genesis_block = $1, network_id = $2, - node_id = $3, + eth_node_id = $3, client_name = $4 RETURNING id`, node.GenesisBlock, node.NetworkID, node.ID, node.ClientName).Scan(&nodeId) diff --git a/pkg/datastore/postgres/repositories/block_repository.go b/pkg/datastore/postgres/repositories/block_repository.go index 525cfc06..4de07f46 100644 --- a/pkg/datastore/postgres/repositories/block_repository.go +++ b/pkg/datastore/postgres/repositories/block_repository.go @@ -79,7 +79,7 @@ func (blockRepository BlockRepository) GetBlock(blockNumber int64) (core.Block, reward, uncles_reward FROM blocks - WHERE node_id = $1 AND number = $2`, blockRepository.NodeID, blockNumber) + WHERE eth_node_id = $1 AND number = $2`, blockRepository.NodeID, blockNumber) savedBlock, err := blockRepository.loadBlock(blockRows) if err != nil { switch err { @@ -97,7 +97,7 @@ func (blockRepository BlockRepository) insertBlock(block core.Block) error { tx, _ := blockRepository.DB.BeginTx(context.Background(), nil) err := tx.QueryRow( `INSERT INTO blocks - (node_id, number, gaslimit, gasused, time, difficulty, hash, nonce, parenthash, size, uncle_hash, is_final, miner, extra_data, reward, uncles_reward) + (eth_node_id, number, gaslimit, gasused, time, difficulty, hash, nonce, parenthash, size, uncle_hash, is_final, miner, extra_data, reward, uncles_reward) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING id `, blockRepository.NodeID, block.Number, block.GasLimit, block.GasUsed, block.Time, block.Difficulty, block.Hash, block.Nonce, block.ParentHash, block.Size, block.UncleHash, block.IsFinal, block.Miner, block.ExtraData, block.Reward, block.UnclesReward). @@ -190,7 +190,7 @@ func (blockRepository BlockRepository) getBlockHash(block core.Block) (string, b blockRepository.DB.Get(&retrievedBlockHash, `SELECT hash FROM blocks - WHERE number = $1 AND node_id = $2`, + WHERE number = $1 AND eth_node_id = $2`, block.Number, blockRepository.NodeID) return retrievedBlockHash, blockExists(retrievedBlockHash) } @@ -218,7 +218,7 @@ func (blockRepository BlockRepository) removeBlock(blockNumber int64) error { _, err := blockRepository.DB.Exec( `DELETE FROM blocks - WHERE number=$1 AND node_id=$2`, + WHERE number=$1 AND eth_node_id=$2`, blockNumber, blockRepository.NodeID) if err != nil { return postgres.ErrDBDeleteFailed