2018-02-13 16:31:57 +00:00
|
|
|
package repositories
|
2018-02-02 21:53:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2018-05-02 16:17:02 +00:00
|
|
|
"errors"
|
2018-02-02 21:53:16 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2018-05-02 16:17:02 +00:00
|
|
|
|
2018-02-02 21:53:16 +00:00
|
|
|
"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-05-02 16:17:02 +00:00
|
|
|
var ErrBlockExists = errors.New("Won't add block that already exists.")
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
type BlockRepository struct {
|
2018-05-07 15:41:02 +00:00
|
|
|
database *postgres.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockRepository(database *postgres.DB) *BlockRepository {
|
|
|
|
return &BlockRepository{database: database}
|
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-05-07 15:41:02 +00:00
|
|
|
blockRepository.database.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-05-02 16:17:02 +00:00
|
|
|
func (blockRepository BlockRepository) CreateOrUpdateBlock(block core.Block) (int64, error) {
|
2018-02-02 21:53:16 +00:00
|
|
|
var err error
|
2018-05-02 16:17:02 +00:00
|
|
|
var blockId int64
|
2018-02-12 16:54:05 +00:00
|
|
|
retrievedBlockHash, ok := blockRepository.getBlockHash(block)
|
2018-02-02 21:53:16 +00:00
|
|
|
if !ok {
|
2018-05-02 16:17:02 +00:00
|
|
|
return blockRepository.insertBlock(block)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
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 {
|
2018-05-02 16:17:02 +00:00
|
|
|
return 0, err
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
return blockRepository.insertBlock(block)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
return blockId, ErrBlockExists
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 15:41:02 +00:00
|
|
|
func (blockRepository BlockRepository) MissingBlockNumbers(startingBlockNumber int64, highestBlockNumber int64, nodeId string) []int64 {
|
2018-02-02 21:53:16 +00:00
|
|
|
numbers := make([]int64, 0)
|
2018-05-07 15:41:02 +00:00
|
|
|
blockRepository.database.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
|
2018-05-07 15:41:02 +00:00
|
|
|
WHERE number ISNULL OR eth_node_fingerprint != $3`,
|
2018-02-02 21:53:16 +00:00
|
|
|
startingBlockNumber,
|
2018-05-07 15:41:02 +00:00
|
|
|
highestBlockNumber, nodeId)
|
2018-02-02 21:53:16 +00:00
|
|
|
return numbers
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (blockRepository BlockRepository) GetBlock(blockNumber int64) (core.Block, error) {
|
2018-05-07 15:41:02 +00:00
|
|
|
blockRows := blockRepository.database.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-05-07 15:41:02 +00:00
|
|
|
WHERE eth_node_id = $1 AND number = $2`, blockRepository.database.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-05-02 16:17:02 +00:00
|
|
|
func (blockRepository BlockRepository) insertBlock(block core.Block) (int64, error) {
|
2018-02-02 21:53:16 +00:00
|
|
|
var blockId int64
|
2018-05-07 15:41:02 +00:00
|
|
|
tx, _ := blockRepository.database.BeginTx(context.Background(), nil)
|
2018-02-02 21:53:16 +00:00
|
|
|
err := tx.QueryRow(
|
|
|
|
`INSERT INTO blocks
|
2018-05-07 15:41:02 +00:00
|
|
|
(eth_node_id, number, gaslimit, gasused, time, difficulty, hash, nonce, parenthash, size, uncle_hash, is_final, miner, extra_data, reward, uncles_reward, eth_node_fingerprint)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
|
2018-02-02 21:53:16 +00:00
|
|
|
RETURNING id `,
|
2018-05-07 15:41:02 +00:00
|
|
|
blockRepository.database.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, blockRepository.database.Node.ID).
|
2018-02-02 21:53:16 +00:00
|
|
|
Scan(&blockId)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2018-05-07 15:41:02 +00:00
|
|
|
return 0, err
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-05-02 16:17:02 +00:00
|
|
|
if len(block.Transactions) > 0 {
|
|
|
|
err = blockRepository.createTransactions(tx, blockId, block.Transactions)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return 0, postgres.ErrDBInsertFailed
|
|
|
|
}
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
tx.Commit()
|
2018-05-02 16:17:02 +00:00
|
|
|
return blockId, nil
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
|
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-05-01 16:35:01 +00:00
|
|
|
_, err := tx.Exec(
|
2018-02-02 21:53:16 +00:00
|
|
|
`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`,
|
2018-05-01 16:35:01 +00:00
|
|
|
blockId, transaction.Hash, transaction.Nonce, transaction.To, transaction.From, transaction.GasLimit, transaction.GasPrice, nullStringToZero(transaction.Value), transaction.Data)
|
2018-02-02 21:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hasReceipt(transaction) {
|
2018-05-01 16:35:01 +00:00
|
|
|
receiptId, err := blockRepository.createReceipt(tx, blockId, 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-05-01 16:35:01 +00:00
|
|
|
func (blockRepository BlockRepository) createReceipt(tx *sql.Tx, blockId int64, 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
|
2018-05-01 16:35:01 +00:00
|
|
|
(contract_address, tx_hash, cumulative_gas_used, gas_used, state_root, status, block_id)
|
2018-02-02 21:53:16 +00:00
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
|
|
RETURNING id`,
|
2018-05-01 16:35:01 +00:00
|
|
|
receipt.ContractAddress, receipt.TxHash, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, blockId).Scan(&receiptId)
|
2018-02-02 21:53:16 +00:00
|
|
|
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-05-07 15:41:02 +00:00
|
|
|
blockRepository.database.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-05-07 15:41:02 +00:00
|
|
|
block.Number, blockRepository.database.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 {
|
2018-05-07 15:41:02 +00:00
|
|
|
_, err := blockRepository.database.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-05-07 15:41:02 +00:00
|
|
|
blockNumber, blockRepository.database.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-05-07 15:41:02 +00:00
|
|
|
transactionRows, err := blockRepository.database.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
|
|
|
|
}
|