2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// 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"
|
2018-11-15 18:53:08 +00:00
|
|
|
|
2019-01-11 09:58:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-01-23 06:37:26 +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) {
|
|
|
|
return repository.insertHeader(header)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-02-14 15:03:57 +00:00
|
|
|
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-01-11 09:58:41 +00:00
|
|
|
err := repository.database.Select(&numbers, `SELECT all_block_numbers
|
2018-07-17 21:23:07 +00:00
|
|
|
FROM (
|
|
|
|
SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series
|
|
|
|
WHERE all_block_numbers NOT IN (
|
|
|
|
SELECT block_number FROM headers WHERE eth_node_fingerprint = $3
|
|
|
|
) `,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository HeaderRepository) insertHeader(header core.Header) (int64, error) {
|
|
|
|
var headerId int64
|
|
|
|
err := repository.database.QueryRowx(
|
2018-10-11 20:25:14 +00:00
|
|
|
`INSERT INTO public.headers (block_number, hash, block_timestamp, raw, eth_node_id, eth_node_fingerprint) VALUES ($1, $2, $3::NUMERIC, $4, $5, $6) RETURNING id`,
|
|
|
|
header.BlockNumber, header.Hash, header.Timestamp, header.Raw, repository.database.NodeID, repository.database.Node.ID).Scan(&headerId)
|
2019-02-14 15:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("insertHeader: error inserting header: ", err)
|
|
|
|
}
|
2018-07-17 21:23:07 +00:00
|
|
|
return headerId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return repository.insertHeader(header)
|
|
|
|
}
|