added timestamp of blocks to postgres db

This commit is contained in:
Dustin Brickwood 2018-05-10 15:19:24 -04:00
parent 3f0cf00b65
commit 361841e460
2 changed files with 13 additions and 7 deletions

View file

@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS blocks (
gasLimit numeric, gasLimit numeric,
txCount numeric, txCount numeric,
uncleCount numeric, uncleCount numeric,
age timestamp,
number bigint number bigint
); );

View file

@ -6,8 +6,8 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"time"
"strconv"
"database/sql" "database/sql"
"log" "log"
@ -91,11 +91,16 @@ func WriteBlock(sqldb *sql.DB, block *types.Block) error {
gasLimit := block.Header().GasLimit gasLimit := block.Header().GasLimit
txCount := block.Transactions().Len() txCount := block.Transactions().Len()
uncleCount := len(block.Uncles()) uncleCount := len(block.Uncles())
age := block.Time()
fmt.Println("TIMESTAMP+++++++", age)
sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txcount, uncleCount) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7)) RETURNING number` i, err := strconv.ParseInt(block.Time().String(), 10, 64)
qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, txCount, uncleCount).Scan(&number) if err != nil {
panic(err)
}
age := time.Unix(i, 0)
fmt.Println("TIME++++++++++++++", age)
sqlStatement := `INSERT INTO blocks(hash, coinbase, number, gasUsed, gasLimit, txcount, uncleCount, age) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8)) RETURNING number`
qerr := sqldb.QueryRow(sqlStatement, block.Header().Hash().Hex(), coinbase, number, gasUsed, gasLimit, txCount, uncleCount, age).Scan(&number)
if qerr != nil { if qerr != nil {
panic(qerr) panic(qerr)
} }