Update table columns (#26)

* Update block table names

* Update transaction table names
This commit is contained in:
Matt K 2018-02-02 16:12:14 -06:00 committed by GitHub
parent aea9c7b5e2
commit d5852654bb
9 changed files with 203 additions and 75 deletions

View File

@ -0,0 +1,40 @@
BEGIN;
ALTER TABLE blocks
RENAME COLUMN number TO block_number;
ALTER TABLE blocks
RENAME COLUMN gaslimit TO block_gaslimit;
ALTER TABLE blocks
RENAME COLUMN gasused TO block_gasused;
ALTER TABLE blocks
RENAME COLUMN TIME TO block_time;
ALTER TABLE blocks
RENAME COLUMN difficulty TO block_difficulty;
ALTER TABLE blocks
RENAME COLUMN HASH TO block_hash;
ALTER TABLE blocks
RENAME COLUMN nonce TO block_nonce;
ALTER TABLE blocks
RENAME COLUMN parenthash TO block_parenthash;
ALTER TABLE blocks
RENAME COLUMN size TO block_size;
ALTER TABLE blocks
RENAME COLUMN miner TO block_miner;
ALTER TABLE blocks
RENAME COLUMN extra_data TO block_extra_data;
ALTER TABLE blocks
RENAME COLUMN reward TO block_reward;
ALTER TABLE blocks
RENAME COLUMN uncles_reward TO block_uncles_reward;
COMMIT;

View File

@ -0,0 +1,50 @@
BEGIN;
ALTER TABLE blocks
RENAME COLUMN block_number TO number;
ALTER TABLE blocks
RENAME COLUMN block_gaslimit TO gaslimit;
ALTER TABLE blocks
RENAME COLUMN block_gasused TO gasused;
ALTER TABLE blocks
RENAME COLUMN block_time TO time;
ALTER TABLE blocks
RENAME COLUMN block_difficulty TO difficulty;
ALTER TABLE blocks
RENAME COLUMN block_hash TO hash;
ALTER TABLE blocks
RENAME COLUMN block_nonce TO nonce;
ALTER TABLE blocks
RENAME COLUMN block_parenthash TO parenthash;
ALTER TABLE blocks
RENAME COLUMN block_size TO size;
ALTER TABLE blocks
RENAME COLUMN block_miner TO miner;
ALTER TABLE blocks
RENAME COLUMN block_extra_data TO extra_data;
ALTER TABLE blocks
RENAME COLUMN block_reward TO reward;
ALTER TABLE blocks
RENAME COLUMN block_uncles_reward TO uncles_reward;
COMMIT;

View File

@ -0,0 +1,19 @@
BEGIN;
ALTER TABLE transactions
RENAME COLUMN hash TO tx_hash;
ALTER TABLE transactions
RENAME COLUMN nonce TO tx_nonce;
ALTER TABLE transactions
RENAME COLUMN gaslimit TO tx_gaslimit;
ALTER TABLE transactions
RENAME COLUMN gasprice TO tx_gasprice;
ALTER TABLE transactions
RENAME COLUMN value TO tx_value;
ALTER TABLE transactions
RENAME COLUMN input_data TO tx_input_data;
COMMIT;

View File

@ -0,0 +1,19 @@
BEGIN;
ALTER TABLE transactions
RENAME COLUMN tx_hash TO hash;
ALTER TABLE transactions
RENAME COLUMN tx_nonce TO nonce;
ALTER TABLE transactions
RENAME COLUMN tx_gaslimit TO gaslimit;
ALTER TABLE transactions
RENAME COLUMN tx_gasprice TO gasprice;
ALTER TABLE transactions
RENAME COLUMN tx_value TO value;
ALTER TABLE transactions
RENAME COLUMN tx_input_data TO input_data;
COMMIT;

View File

@ -68,23 +68,23 @@ CREATE VIEW block_stats AS
-- --
CREATE TABLE blocks ( CREATE TABLE blocks (
block_number bigint, number bigint,
block_gaslimit bigint, gaslimit bigint,
block_gasused bigint, gasused bigint,
block_time bigint, "time" bigint,
id integer NOT NULL, id integer NOT NULL,
block_difficulty bigint, difficulty bigint,
block_hash character varying(66), hash character varying(66),
block_nonce character varying(20), nonce character varying(20),
block_parenthash character varying(66), parenthash character varying(66),
block_size bigint, size bigint,
uncle_hash character varying(66), uncle_hash character varying(66),
node_id integer NOT NULL, node_id integer NOT NULL,
is_final boolean, is_final boolean,
block_miner character varying(42), miner character varying(42),
block_extra_data character varying, extra_data character varying,
block_reward double precision, reward double precision,
block_uncles_reward double precision uncles_reward double precision
); );
@ -253,15 +253,15 @@ CREATE TABLE schema_migrations (
CREATE TABLE transactions ( CREATE TABLE transactions (
id integer NOT NULL, id integer NOT NULL,
tx_hash character varying(66), hash character varying(66),
tx_nonce numeric, nonce numeric,
tx_to character varying(66), tx_to character varying(66),
tx_gaslimit numeric, gaslimit numeric,
tx_gasprice numeric, gasprice numeric,
tx_value numeric, value numeric,
block_id integer NOT NULL, block_id integer NOT NULL,
tx_from character varying(66), tx_from character varying(66),
tx_input_data character varying input_data character varying
); );
@ -425,7 +425,7 @@ ALTER TABLE ONLY log_filters
-- --
ALTER TABLE ONLY blocks ALTER TABLE ONLY blocks
ADD CONSTRAINT node_id_block_number_uc UNIQUE (block_number, node_id); ADD CONSTRAINT node_id_block_number_uc UNIQUE (number, node_id);
-- --
@ -487,7 +487,7 @@ CREATE INDEX block_id_index ON transactions USING btree (block_id);
-- Name: block_number_index; Type: INDEX; Schema: public; Owner: - -- Name: block_number_index; Type: INDEX; Schema: public; Owner: -
-- --
CREATE INDEX block_number_index ON blocks USING btree (block_number); CREATE INDEX block_number_index ON blocks USING btree (number);
-- --

View File

@ -1,20 +1,20 @@
package core package core
type Block struct { type Block struct {
Reward float64 `db:"block_reward"` Reward float64 `db:"reward"`
Difficulty int64 `db:"block_difficulty"` Difficulty int64 `db:"difficulty"`
ExtraData string `db:"block_extra_data"` ExtraData string `db:"extra_data"`
GasLimit int64 `db:"block_gaslimit"` GasLimit int64 `db:"gaslimit"`
GasUsed int64 `db:"block_gasused"` GasUsed int64 `db:"gasused"`
Hash string `db:"block_hash"` Hash string `db:"hash"`
IsFinal bool `db:"is_final"` IsFinal bool `db:"is_final"`
Miner string `db:"block_miner"` Miner string `db:"miner"`
Nonce string `db:"block_nonce"` Nonce string `db:"nonce"`
Number int64 `db:"block_number"` Number int64 `db:"number"`
ParentHash string `db:"block_parenthash"` ParentHash string `db:"parenthash"`
Size int64 `db:"block_size"` Size int64 `db:"size"`
Time int64 `db:"block_time"` Time int64 `db:"time"`
Transactions []Transaction Transactions []Transaction
UncleHash string `db:"uncle_hash"` UncleHash string `db:"uncle_hash"`
UnclesReward float64 `db:"block_uncles_reward"` UnclesReward float64 `db:"uncles_reward"`
} }

View File

@ -1,13 +1,13 @@
package core package core
type Transaction struct { type Transaction struct {
Hash string `db:"tx_hash"` Hash string `db:"hash"`
Data string `db:"tx_input_data"` Data string `db:"input_data"`
Nonce uint64 `db:"tx_nonce"` Nonce uint64 `db:"nonce"`
To string `db:"tx_to"` To string `db:"tx_to"`
From string `db:"tx_from"` From string `db:"tx_from"`
GasLimit int64 `db:"tx_gaslimit"` GasLimit int64 `db:"gaslimit"`
GasPrice int64 `db:"tx_gasprice"` GasPrice int64 `db:"gasprice"`
Receipt Receipt
Value string `db:"tx_value"` Value string `db:"value"`
} }

View File

@ -20,7 +20,7 @@ func (db DB) SetBlocksStatus(chainHead int64) {
cutoff := chainHead - blocksFromHeadBeforeFinal cutoff := chainHead - blocksFromHeadBeforeFinal
db.DB.Exec(` db.DB.Exec(`
UPDATE blocks SET is_final = TRUE UPDATE blocks SET is_final = TRUE
WHERE is_final = FALSE AND block_number < $1`, WHERE is_final = FALSE AND number < $1`,
cutoff) cutoff)
} }
@ -49,8 +49,8 @@ func (db DB) MissingBlockNumbers(startingBlockNumber int64, highestBlockNumber i
FROM ( FROM (
SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series
LEFT JOIN blocks LEFT JOIN blocks
ON block_number = all_block_numbers ON number = all_block_numbers
WHERE block_number ISNULL`, WHERE number ISNULL`,
startingBlockNumber, startingBlockNumber,
highestBlockNumber) highestBlockNumber)
return numbers return numbers
@ -59,23 +59,23 @@ func (db DB) MissingBlockNumbers(startingBlockNumber int64, highestBlockNumber i
func (db DB) FindBlockByNumber(blockNumber int64) (core.Block, error) { func (db DB) FindBlockByNumber(blockNumber int64) (core.Block, error) {
blockRows := db.DB.QueryRowx( blockRows := db.DB.QueryRowx(
`SELECT id, `SELECT id,
block_number, number,
block_gaslimit, gaslimit,
block_gasused, gasused,
block_time, time,
block_difficulty, difficulty,
block_hash, hash,
block_nonce, nonce,
block_parenthash, parenthash,
block_size, size,
uncle_hash, uncle_hash,
is_final, is_final,
block_miner, miner,
block_extra_data, extra_data,
block_reward, reward,
block_uncles_reward uncles_reward
FROM blocks FROM blocks
WHERE node_id = $1 AND block_number = $2`, db.nodeId, blockNumber) WHERE node_id = $1 AND number = $2`, db.nodeId, blockNumber)
savedBlock, err := db.loadBlock(blockRows) savedBlock, err := db.loadBlock(blockRows)
if err != nil { if err != nil {
switch err { switch err {
@ -93,7 +93,7 @@ func (db DB) insertBlock(block core.Block) error {
tx, _ := db.DB.BeginTx(context.Background(), nil) tx, _ := db.DB.BeginTx(context.Background(), nil)
err := tx.QueryRow( err := tx.QueryRow(
`INSERT INTO blocks `INSERT INTO blocks
(node_id, block_number, block_gaslimit, block_gasused, block_time, block_difficulty, block_hash, block_nonce, block_parenthash, block_size, uncle_hash, is_final, block_miner, block_extra_data, block_reward, block_uncles_reward) (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) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
RETURNING id `, RETURNING id `,
db.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). db.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).
@ -135,7 +135,7 @@ func (db DB) createTransaction(tx *sql.Tx, blockId int64, transaction core.Trans
var transactionId int var transactionId int
err := tx.QueryRow( err := tx.QueryRow(
`INSERT INTO transactions `INSERT INTO transactions
(block_id, tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value, tx_input_data) (block_id, hash, nonce, tx_to, tx_from, gaslimit, gasprice, value, input_data)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8::NUMERIC, $9) VALUES ($1, $2, $3, $4, $5, $6, $7, $8::NUMERIC, $9)
RETURNING id`, RETURNING id`,
blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, nullStringToZero(transaction.Value), transaction.Data). blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, nullStringToZero(transaction.Value), transaction.Data).
@ -184,9 +184,9 @@ func (db DB) createReceipt(tx *sql.Tx, transactionId int, receipt core.Receipt)
func (db DB) getBlockHash(block core.Block) (string, bool) { func (db DB) getBlockHash(block core.Block) (string, bool) {
var retrievedBlockHash string var retrievedBlockHash string
db.DB.Get(&retrievedBlockHash, db.DB.Get(&retrievedBlockHash,
`SELECT block_hash `SELECT hash
FROM blocks FROM blocks
WHERE block_number = $1 AND node_id = $2`, WHERE number = $1 AND node_id = $2`,
block.Number, db.nodeId) block.Number, db.nodeId)
return retrievedBlockHash, blockExists(retrievedBlockHash) return retrievedBlockHash, blockExists(retrievedBlockHash)
} }
@ -214,7 +214,7 @@ func (db DB) removeBlock(blockNumber int64) error {
_, err := db.DB.Exec( _, err := db.DB.Exec(
`DELETE FROM `DELETE FROM
blocks blocks
WHERE block_number=$1 AND node_id=$2`, WHERE number=$1 AND node_id=$2`,
blockNumber, db.nodeId) blockNumber, db.nodeId)
if err != nil { if err != nil {
return ErrDBDeleteFailed return ErrDBDeleteFailed
@ -233,17 +233,17 @@ func (db DB) loadBlock(blockRows *sqlx.Row) (core.Block, error) {
return core.Block{}, err return core.Block{}, err
} }
transactionRows, err := db.DB.Queryx(` transactionRows, err := db.DB.Queryx(`
SELECT tx_hash, SELECT hash,
tx_nonce, nonce,
tx_to, tx_to,
tx_from, tx_from,
tx_gaslimit, gaslimit,
tx_gasprice, gasprice,
tx_value, value,
tx_input_data input_data
FROM transactions FROM transactions
WHERE block_id = $1 WHERE block_id = $1
ORDER BY tx_hash`, block.ID) ORDER BY hash`, block.ID)
if err != nil { if err != nil {
return core.Block{}, err return core.Block{}, err
} }

View File

@ -51,14 +51,14 @@ func (db DB) FindContract(contractHash string) (core.Contract, error) {
func (db DB) addTransactions(contract core.Contract) core.Contract { func (db DB) addTransactions(contract core.Contract) core.Contract {
transactionRows, _ := db.DB.Queryx(` transactionRows, _ := db.DB.Queryx(`
SELECT tx_hash, SELECT hash,
tx_nonce, nonce,
tx_to, tx_to,
tx_from, tx_from,
tx_gaslimit, gaslimit,
tx_gasprice, gasprice,
tx_value, value,
tx_input_data input_data
FROM transactions FROM transactions
WHERE tx_to = $1 WHERE tx_to = $1
ORDER BY block_id DESC`, contract.Hash) ORDER BY block_id DESC`, contract.Hash)