2018-02-13 16:31:57 +00:00
|
|
|
package repositories
|
2018-02-02 21:53:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-02-02 21:53:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
blocksFromHeadBeforeFinal = 20
|
|
|
|
)
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
type BlockRepository struct {
|
2018-02-13 16:31:57 +00:00
|
|
|
*postgres.DB
|
2018-02-12 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (blockRepository BlockRepository) SetBlocksStatus(chainHead int64) {
|
2018-02-02 21:53:16 +00:00
|
|
|
cutoff := chainHead - blocksFromHeadBeforeFinal
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository.DB.Exec(`
|
2018-02-02 21:53:16 +00:00
|
|
|
UPDATE blocks SET is_final = TRUE
|
2018-02-02 22:12:14 +00:00
|
|
|
WHERE is_final = FALSE AND number < $1`,
|
2018-02-02 21:53:16 +00:00
|
|
|
cutoff)
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) CreateOrUpdateBlock(block core.Block) error {
|
2018-02-02 21:53:16 +00:00
|
|
|
var err error
|
2018-02-12 16:54:05 +00:00
|
|
|
retrievedBlockHash, ok := blockRepository.getBlockHash(block)
|
2018-02-02 21:53:16 +00:00
|
|
|
if !ok {
|
2018-02-12 16:54:05 +00:00
|
|
|
err = blockRepository.insertBlock(block)
|
2018-02-02 21:53:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok && retrievedBlockHash != block.Hash {
|
2018-02-12 16:54:05 +00:00
|
|
|
err = blockRepository.removeBlock(block.Number)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
err = blockRepository.insertBlock(block)
|
2018-02-02 21:53:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) MissingBlockNumbers(startingBlockNumber int64, highestBlockNumber int64) []int64 {
|
2018-02-02 21:53:16 +00:00
|
|
|
numbers := make([]int64, 0)
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository.DB.Select(&numbers,
|
2018-02-02 21:53:16 +00:00
|
|
|
`SELECT all_block_numbers
|
|
|
|
FROM (
|
|
|
|
SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series
|
|
|
|
LEFT JOIN blocks
|
2018-02-02 22:12:14 +00:00
|
|
|
ON number = all_block_numbers
|
|
|
|
WHERE number ISNULL`,
|
2018-02-02 21:53:16 +00:00
|
|
|
startingBlockNumber,
|
|
|
|
highestBlockNumber)
|
|
|
|
return numbers
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) GetBlock(blockNumber int64) (core.Block, error) {
|
|
|
|
blockRows := blockRepository.DB.QueryRowx(
|
2018-02-02 21:53:16 +00:00
|
|
|
`SELECT id,
|
2018-02-02 22:12:14 +00:00
|
|
|
number,
|
|
|
|
gaslimit,
|
|
|
|
gasused,
|
|
|
|
time,
|
|
|
|
difficulty,
|
|
|
|
hash,
|
|
|
|
nonce,
|
|
|
|
parenthash,
|
|
|
|
size,
|
2018-02-02 21:53:16 +00:00
|
|
|
uncle_hash,
|
|
|
|
is_final,
|
2018-02-02 22:12:14 +00:00
|
|
|
miner,
|
|
|
|
extra_data,
|
|
|
|
reward,
|
|
|
|
uncles_reward
|
2018-02-02 21:53:16 +00:00
|
|
|
FROM blocks
|
2018-03-21 14:53:20 +00:00
|
|
|
WHERE eth_node_id = $1 AND number = $2`, blockRepository.NodeID, blockNumber)
|
2018-02-12 16:54:05 +00:00
|
|
|
savedBlock, err := blockRepository.loadBlock(blockRows)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
2018-02-13 16:31:57 +00:00
|
|
|
return core.Block{}, datastore.ErrBlockDoesNotExist(blockNumber)
|
2018-02-02 21:53:16 +00:00
|
|
|
default:
|
|
|
|
return savedBlock, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return savedBlock, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) insertBlock(block core.Block) error {
|
2018-02-02 21:53:16 +00:00
|
|
|
var blockId int64
|
2018-02-12 16:54:05 +00:00
|
|
|
tx, _ := blockRepository.DB.BeginTx(context.Background(), nil)
|
2018-02-02 21:53:16 +00:00
|
|
|
err := tx.QueryRow(
|
|
|
|
`INSERT INTO blocks
|
2018-03-21 14:53:20 +00:00
|
|
|
(eth_node_id, number, gaslimit, gasused, time, difficulty, hash, nonce, parenthash, size, uncle_hash, is_final, miner, extra_data, reward, uncles_reward)
|
2018-02-02 21:53:16 +00:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
|
|
|
|
RETURNING id `,
|
2018-02-13 16:31:57 +00:00
|
|
|
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).
|
2018-02-02 21:53:16 +00:00
|
|
|
Scan(&blockId)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2018-02-13 16:31:57 +00:00
|
|
|
return postgres.ErrDBInsertFailed
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
err = blockRepository.createTransactions(tx, blockId, block.Transactions)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2018-02-13 16:31:57 +00:00
|
|
|
return postgres.ErrDBInsertFailed
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) createTransactions(tx *sql.Tx, blockId int64, transactions []core.Transaction) error {
|
2018-02-02 21:53:16 +00:00
|
|
|
for _, transaction := range transactions {
|
2018-02-12 16:54:05 +00:00
|
|
|
err := blockRepository.createTransaction(tx, blockId, transaction)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Fields like value lose precision if converted to
|
|
|
|
//int64 so convert to string instead. But nil
|
|
|
|
//big.Int -> string = "" so convert to "0"
|
|
|
|
func nullStringToZero(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) createTransaction(tx *sql.Tx, blockId int64, transaction core.Transaction) error {
|
2018-02-02 21:53:16 +00:00
|
|
|
var transactionId int
|
|
|
|
err := tx.QueryRow(
|
|
|
|
`INSERT INTO transactions
|
2018-02-02 22:12:14 +00:00
|
|
|
(block_id, hash, nonce, tx_to, tx_from, gaslimit, gasprice, value, input_data)
|
2018-02-02 21:53:16 +00:00
|
|
|
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).
|
|
|
|
Scan(&transactionId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hasReceipt(transaction) {
|
2018-02-12 16:54:05 +00:00
|
|
|
receiptId, err := blockRepository.createReceipt(tx, transactionId, transaction.Receipt)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hasLogs(transaction) {
|
2018-02-12 16:54:05 +00:00
|
|
|
err = blockRepository.createLogs(tx, transaction.Receipt.Logs, receiptId)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasLogs(transaction core.Transaction) bool {
|
|
|
|
return len(transaction.Receipt.Logs) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasReceipt(transaction core.Transaction) bool {
|
|
|
|
return transaction.Receipt.TxHash != ""
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) createReceipt(tx *sql.Tx, transactionId int, receipt core.Receipt) (int, error) {
|
2018-02-02 21:53:16 +00:00
|
|
|
//Not currently persisting log bloom filters
|
|
|
|
var receiptId int
|
|
|
|
err := tx.QueryRow(
|
|
|
|
`INSERT INTO receipts
|
|
|
|
(contract_address, tx_hash, cumulative_gas_used, gas_used, state_root, status, transaction_id)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
|
|
RETURNING id`,
|
|
|
|
receipt.ContractAddress, receipt.TxHash, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, transactionId).Scan(&receiptId)
|
|
|
|
if err != nil {
|
|
|
|
return receiptId, err
|
|
|
|
}
|
|
|
|
return receiptId, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) getBlockHash(block core.Block) (string, bool) {
|
2018-02-02 21:53:16 +00:00
|
|
|
var retrievedBlockHash string
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository.DB.Get(&retrievedBlockHash,
|
2018-02-02 22:12:14 +00:00
|
|
|
`SELECT hash
|
2018-02-02 21:53:16 +00:00
|
|
|
FROM blocks
|
2018-03-21 14:53:20 +00:00
|
|
|
WHERE number = $1 AND eth_node_id = $2`,
|
2018-02-13 16:31:57 +00:00
|
|
|
block.Number, blockRepository.NodeID)
|
2018-02-02 21:53:16 +00:00
|
|
|
return retrievedBlockHash, blockExists(retrievedBlockHash)
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) createLogs(tx *sql.Tx, logs []core.Log, receiptId int) error {
|
2018-02-02 21:53:16 +00:00
|
|
|
for _, tlog := range logs {
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`INSERT INTO logs (block_number, address, tx_hash, index, topic0, topic1, topic2, topic3, data, receipt_id)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
|
|
|
`,
|
|
|
|
tlog.BlockNumber, tlog.Address, tlog.TxHash, tlog.Index, tlog.Topics[0], tlog.Topics[1], tlog.Topics[2], tlog.Topics[3], tlog.Data, receiptId,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-02-13 16:31:57 +00:00
|
|
|
return postgres.ErrDBInsertFailed
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func blockExists(retrievedBlockHash string) bool {
|
|
|
|
return retrievedBlockHash != ""
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) removeBlock(blockNumber int64) error {
|
|
|
|
_, err := blockRepository.DB.Exec(
|
2018-02-02 21:53:16 +00:00
|
|
|
`DELETE FROM
|
|
|
|
blocks
|
2018-03-21 14:53:20 +00:00
|
|
|
WHERE number=$1 AND eth_node_id=$2`,
|
2018-02-13 16:31:57 +00:00
|
|
|
blockNumber, blockRepository.NodeID)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
2018-02-13 16:31:57 +00:00
|
|
|
return postgres.ErrDBDeleteFailed
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) loadBlock(blockRows *sqlx.Row) (core.Block, error) {
|
2018-02-02 21:53:16 +00:00
|
|
|
type b struct {
|
|
|
|
ID int
|
|
|
|
core.Block
|
|
|
|
}
|
|
|
|
var block b
|
|
|
|
err := blockRows.StructScan(&block)
|
|
|
|
if err != nil {
|
|
|
|
return core.Block{}, err
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
transactionRows, err := blockRepository.DB.Queryx(`
|
2018-02-02 22:12:14 +00:00
|
|
|
SELECT hash,
|
|
|
|
nonce,
|
2018-02-02 21:53:16 +00:00
|
|
|
tx_to,
|
|
|
|
tx_from,
|
2018-02-02 22:12:14 +00:00
|
|
|
gaslimit,
|
|
|
|
gasprice,
|
|
|
|
value,
|
|
|
|
input_data
|
2018-02-02 21:53:16 +00:00
|
|
|
FROM transactions
|
|
|
|
WHERE block_id = $1
|
2018-02-02 22:12:14 +00:00
|
|
|
ORDER BY hash`, block.ID)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return core.Block{}, err
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
block.Transactions = blockRepository.LoadTransactions(transactionRows)
|
2018-02-02 21:53:16 +00:00
|
|
|
return block.Block, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) LoadTransactions(transactionRows *sqlx.Rows) []core.Transaction {
|
2018-02-02 21:53:16 +00:00
|
|
|
var transactions []core.Transaction
|
|
|
|
for transactionRows.Next() {
|
|
|
|
var transaction core.Transaction
|
|
|
|
err := transactionRows.StructScan(&transaction)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
transactions = append(transactions, transaction)
|
|
|
|
}
|
|
|
|
return transactions
|
|
|
|
}
|