Rename node table and node column

This commit is contained in:
Matt Krump 2018-03-21 09:53:20 -05:00 committed by Matt K
parent 02b23ef748
commit 1b4e57a5b4
6 changed files with 96 additions and 52 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;
--

View File

@ -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) {

View File

@ -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)

View File

@ -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