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 (
block_number bigint,
block_gaslimit bigint,
block_gasused bigint,
block_time bigint,
number bigint,
gaslimit bigint,
gasused bigint,
"time" bigint,
id integer NOT NULL,
block_difficulty bigint,
block_hash character varying(66),
block_nonce character varying(20),
block_parenthash character varying(66),
block_size bigint,
difficulty bigint,
hash character varying(66),
nonce character varying(20),
parenthash character varying(66),
size bigint,
uncle_hash character varying(66),
node_id integer NOT NULL,
is_final boolean,
block_miner character varying(42),
block_extra_data character varying,
block_reward double precision,
block_uncles_reward double precision
miner character varying(42),
extra_data character varying,
reward double precision,
uncles_reward double precision
);
@ -253,15 +253,15 @@ CREATE TABLE schema_migrations (
CREATE TABLE transactions (
id integer NOT NULL,
tx_hash character varying(66),
tx_nonce numeric,
hash character varying(66),
nonce numeric,
tx_to character varying(66),
tx_gaslimit numeric,
tx_gasprice numeric,
tx_value numeric,
gaslimit numeric,
gasprice numeric,
value numeric,
block_id integer NOT NULL,
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
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: -
--
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
type Block struct {
Reward float64 `db:"block_reward"`
Difficulty int64 `db:"block_difficulty"`
ExtraData string `db:"block_extra_data"`
GasLimit int64 `db:"block_gaslimit"`
GasUsed int64 `db:"block_gasused"`
Hash string `db:"block_hash"`
Reward float64 `db:"reward"`
Difficulty int64 `db:"difficulty"`
ExtraData string `db:"extra_data"`
GasLimit int64 `db:"gaslimit"`
GasUsed int64 `db:"gasused"`
Hash string `db:"hash"`
IsFinal bool `db:"is_final"`
Miner string `db:"block_miner"`
Nonce string `db:"block_nonce"`
Number int64 `db:"block_number"`
ParentHash string `db:"block_parenthash"`
Size int64 `db:"block_size"`
Time int64 `db:"block_time"`
Miner string `db:"miner"`
Nonce string `db:"nonce"`
Number int64 `db:"number"`
ParentHash string `db:"parenthash"`
Size int64 `db:"size"`
Time int64 `db:"time"`
Transactions []Transaction
UncleHash string `db:"uncle_hash"`
UnclesReward float64 `db:"block_uncles_reward"`
UnclesReward float64 `db:"uncles_reward"`
}

View File

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