2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2018-07-26 21:15:24 +00:00
|
|
|
"errors"
|
2019-03-21 19:52:24 +00:00
|
|
|
|
2019-03-22 03:43:06 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-01-11 09:58:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-03-21 19:52:24 +00:00
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
)
|
|
|
|
|
2018-07-26 21:15:24 +00:00
|
|
|
var ErrValidHeaderExists = errors.New("valid header already exists")
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
type HeaderRepository struct {
|
|
|
|
database *postgres.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHeaderRepository(database *postgres.DB) HeaderRepository {
|
|
|
|
return HeaderRepository{database: database}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository HeaderRepository) CreateOrUpdateHeader(header core.Header) (int64, error) {
|
|
|
|
hash, err := repository.getHeaderHash(header)
|
|
|
|
if err != nil {
|
|
|
|
if headerDoesNotExist(err) {
|
2019-09-26 18:58:48 +00:00
|
|
|
return repository.InternalInsertHeader(header)
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
2019-02-14 15:03:57 +00:00
|
|
|
log.Error("CreateOrUpdateHeader: error getting header hash: ", err)
|
2018-07-17 21:23:07 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if headerMustBeReplaced(hash, header) {
|
|
|
|
return repository.replaceHeader(header)
|
|
|
|
}
|
2018-07-26 21:15:24 +00:00
|
|
|
return 0, ErrValidHeaderExists
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 17:26:04 +00:00
|
|
|
func (repository HeaderRepository) CreateTransactions(headerID int64, transactions []core.TransactionModel) error {
|
|
|
|
for _, transaction := range transactions {
|
2019-05-01 06:02:30 +00:00
|
|
|
_, err := repository.database.Exec(`INSERT INTO public.header_sync_transactions
|
2019-04-04 20:21:39 +00:00
|
|
|
(header_id, hash, gas_limit, gas_price, input_data, nonce, raw, tx_from, tx_index, tx_to, "value")
|
2019-03-27 04:05:30 +00:00
|
|
|
VALUES ($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9::NUMERIC, $10, $11::NUMERIC)
|
|
|
|
ON CONFLICT DO NOTHING`, headerID, transaction.Hash, transaction.GasLimit, transaction.GasPrice,
|
2019-03-27 17:26:04 +00:00
|
|
|
transaction.Data, transaction.Nonce, transaction.Raw, transaction.From, transaction.TxIndex, transaction.To,
|
|
|
|
transaction.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-03-19 19:39:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 03:43:06 +00:00
|
|
|
func (repository HeaderRepository) CreateTransactionInTx(tx *sqlx.Tx, headerID int64, transaction core.TransactionModel) (int64, error) {
|
2019-10-18 16:16:19 +00:00
|
|
|
var txID int64
|
2019-05-01 06:02:30 +00:00
|
|
|
err := tx.QueryRowx(`INSERT INTO public.header_sync_transactions
|
2019-03-21 19:52:24 +00:00
|
|
|
(header_id, hash, gas_limit, gas_price, input_data, nonce, raw, tx_from, tx_index, tx_to, "value")
|
2019-03-22 03:43:06 +00:00
|
|
|
VALUES ($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9::NUMERIC, $10, $11::NUMERIC)
|
|
|
|
ON CONFLICT (header_id, hash) DO UPDATE
|
2019-03-21 19:52:24 +00:00
|
|
|
SET (gas_limit, gas_price, input_data, nonce, raw, tx_from, tx_index, tx_to, "value") = ($3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9::NUMERIC, $10, $11::NUMERIC)
|
2019-03-22 03:43:06 +00:00
|
|
|
RETURNING id`,
|
|
|
|
headerID, transaction.Hash, transaction.GasLimit, transaction.GasPrice,
|
|
|
|
transaction.Data, transaction.Nonce, transaction.Raw, transaction.From,
|
2019-10-18 16:16:19 +00:00
|
|
|
transaction.TxIndex, transaction.To, transaction.Value).Scan(&txID)
|
2019-03-22 03:43:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("header_repository: error inserting transaction: ", err)
|
2019-10-18 16:16:19 +00:00
|
|
|
return txID, err
|
2019-03-22 03:43:06 +00:00
|
|
|
}
|
2019-10-18 16:16:19 +00:00
|
|
|
return txID, err
|
2019-03-22 03:43:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (repository HeaderRepository) GetHeader(blockNumber int64) (core.Header, error) {
|
|
|
|
var header core.Header
|
2019-01-28 22:52:47 +00:00
|
|
|
err := repository.database.Get(&header, `SELECT id, block_number, hash, raw, block_timestamp FROM headers WHERE block_number = $1 AND eth_node_fingerprint = $2`,
|
2018-07-17 21:23:07 +00:00
|
|
|
blockNumber, repository.database.Node.ID)
|
2019-03-19 19:39:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetHeader: error getting headers: ", err)
|
|
|
|
}
|
2018-07-17 21:23:07 +00:00
|
|
|
return header, err
|
|
|
|
}
|
|
|
|
|
2019-01-11 09:58:41 +00:00
|
|
|
func (repository HeaderRepository) MissingBlockNumbers(startingBlockNumber, endingBlockNumber int64, nodeID string) ([]int64, error) {
|
2018-07-17 21:23:07 +00:00
|
|
|
numbers := make([]int64, 0)
|
2019-03-05 10:52:38 +00:00
|
|
|
err := repository.database.Select(&numbers,
|
|
|
|
`SELECT series.block_number
|
|
|
|
FROM (SELECT generate_series($1::INT, $2::INT) AS block_number) AS series
|
|
|
|
LEFT OUTER JOIN (SELECT block_number FROM headers
|
|
|
|
WHERE eth_node_fingerprint = $3) AS synced
|
|
|
|
USING (block_number)
|
|
|
|
WHERE synced.block_number IS NULL`,
|
2018-07-17 21:23:07 +00:00
|
|
|
startingBlockNumber, endingBlockNumber, nodeID)
|
2019-01-11 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("MissingBlockNumbers failed to get blocks between %v - %v for node %v",
|
|
|
|
startingBlockNumber, endingBlockNumber, nodeID)
|
|
|
|
return []int64{}, err
|
|
|
|
}
|
|
|
|
return numbers, nil
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func headerMustBeReplaced(hash string, header core.Header) bool {
|
|
|
|
return hash != header.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func headerDoesNotExist(err error) bool {
|
|
|
|
return err == sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository HeaderRepository) getHeaderHash(header core.Header) (string, error) {
|
|
|
|
var hash string
|
|
|
|
err := repository.database.Get(&hash, `SELECT hash FROM headers WHERE block_number = $1 AND eth_node_fingerprint = $2`,
|
|
|
|
header.BlockNumber, repository.database.Node.ID)
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-09-26 18:58:48 +00:00
|
|
|
// Function is public so we can test insert being called for the same header
|
|
|
|
// Can happen when concurrent processes are inserting headers
|
|
|
|
// Otherwise should not occur since only called in CreateOrUpdateHeader
|
|
|
|
func (repository HeaderRepository) InternalInsertHeader(header core.Header) (int64, error) {
|
2019-10-18 16:16:19 +00:00
|
|
|
var headerID int64
|
2019-09-26 18:58:48 +00:00
|
|
|
row := repository.database.QueryRowx(
|
|
|
|
`INSERT INTO public.headers (block_number, hash, block_timestamp, raw, eth_node_id, eth_node_fingerprint)
|
|
|
|
VALUES ($1, $2, $3::NUMERIC, $4, $5, $6) ON CONFLICT DO NOTHING RETURNING id`,
|
|
|
|
header.BlockNumber, header.Hash, header.Timestamp, header.Raw, repository.database.NodeID, repository.database.Node.ID)
|
2019-10-18 16:16:19 +00:00
|
|
|
err := row.Scan(&headerID)
|
2019-02-14 15:03:57 +00:00
|
|
|
if err != nil {
|
2019-09-26 18:58:48 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, ErrValidHeaderExists
|
|
|
|
}
|
|
|
|
log.Error("InternalInsertHeader: error inserting header: ", err)
|
2019-02-14 15:03:57 +00:00
|
|
|
}
|
2019-10-18 16:16:19 +00:00
|
|
|
return headerID, err
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repository HeaderRepository) replaceHeader(header core.Header) (int64, error) {
|
|
|
|
_, err := repository.database.Exec(`DELETE FROM headers WHERE block_number = $1 AND eth_node_fingerprint = $2`,
|
|
|
|
header.BlockNumber, repository.database.Node.ID)
|
|
|
|
if err != nil {
|
2019-02-14 15:03:57 +00:00
|
|
|
log.Error("replaceHeader: error deleting headers: ", err)
|
2018-07-17 21:23:07 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2019-09-26 18:58:48 +00:00
|
|
|
return repository.InternalInsertHeader(header)
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|