2019-01-31 23:14:42 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-31 23:14:42 +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/>.
|
|
|
|
|
|
|
|
package repository
|
2018-11-08 17:07:54 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-13 15:08:57 +00:00
|
|
|
"bytes"
|
2018-12-04 15:04:13 +00:00
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
2019-03-06 16:15:32 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-01-31 23:14:42 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
2018-11-08 17:07:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
)
|
|
|
|
|
2019-01-07 14:19:31 +00:00
|
|
|
func MarkHeaderChecked(headerID int64, db *postgres.DB, checkedHeadersColumn string) error {
|
2018-11-08 17:07:54 +00:00
|
|
|
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`)
|
2019-02-08 16:35:46 +00:00
|
|
|
VALUES ($1, $2)
|
2018-11-08 17:07:54 +00:00
|
|
|
ON CONFLICT (header_id) DO
|
2019-02-08 16:35:46 +00:00
|
|
|
UPDATE SET `+checkedHeadersColumn+` = checked_headers.`+checkedHeadersColumn+` + 1`, headerID, 1)
|
2018-11-08 17:07:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:15:32 +00:00
|
|
|
func MarkHeaderCheckedInTransaction(headerID int64, tx *sqlx.Tx, checkedHeadersColumn string) error {
|
2018-11-08 17:07:54 +00:00
|
|
|
_, err := tx.Exec(`INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`)
|
2019-02-08 16:35:46 +00:00
|
|
|
VALUES ($1, $2)
|
2018-11-08 17:07:54 +00:00
|
|
|
ON CONFLICT (header_id) DO
|
2019-02-08 16:35:46 +00:00
|
|
|
UPDATE SET `+checkedHeadersColumn+` = checked_headers.`+checkedHeadersColumn+` + 1`, headerID, 1)
|
2018-11-08 17:07:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-04 15:04:13 +00:00
|
|
|
// Treats a header as missing if it's not in the headers table, or not checked for some log type
|
2019-01-07 14:19:31 +00:00
|
|
|
func MissingHeaders(startingBlockNumber, endingBlockNumber int64, db *postgres.DB, notCheckedSQL string) ([]core.Header, error) {
|
2018-11-08 17:07:54 +00:00
|
|
|
var result []core.Header
|
2018-11-09 10:55:43 +00:00
|
|
|
var query string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if endingBlockNumber == -1 {
|
2018-11-21 15:30:00 +00:00
|
|
|
query = `SELECT headers.id, headers.block_number, headers.hash FROM headers
|
2018-11-09 10:55:43 +00:00
|
|
|
LEFT JOIN checked_headers on headers.id = header_id
|
2018-12-04 15:04:13 +00:00
|
|
|
WHERE (header_id ISNULL OR ` + notCheckedSQL + `)
|
2018-11-09 10:55:43 +00:00
|
|
|
AND headers.block_number >= $1
|
|
|
|
AND headers.eth_node_fingerprint = $2`
|
|
|
|
err = db.Select(&result, query, startingBlockNumber, db.Node.ID)
|
|
|
|
} else {
|
2018-11-21 15:30:00 +00:00
|
|
|
query = `SELECT headers.id, headers.block_number, headers.hash FROM headers
|
2018-11-09 10:55:43 +00:00
|
|
|
LEFT JOIN checked_headers on headers.id = header_id
|
2018-12-04 15:04:13 +00:00
|
|
|
WHERE (header_id ISNULL OR ` + notCheckedSQL + `)
|
2018-11-09 10:55:43 +00:00
|
|
|
AND headers.block_number >= $1
|
|
|
|
AND headers.block_number <= $2
|
|
|
|
AND headers.eth_node_fingerprint = $3`
|
|
|
|
err = db.Select(&result, query, startingBlockNumber, endingBlockNumber, db.Node.ID)
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:07:54 +00:00
|
|
|
return result, err
|
|
|
|
}
|
2018-12-04 15:04:13 +00:00
|
|
|
|
2019-02-08 16:35:46 +00:00
|
|
|
func RecheckHeaders(startingBlockNumber, endingBlockNumber int64, db *postgres.DB, checkedHeadersColumn string) ([]core.Header, error) {
|
|
|
|
var result []core.Header
|
|
|
|
var query string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if endingBlockNumber == -1 {
|
|
|
|
query = `SELECT headers.id, headers.block_number, headers.hash FROM headers
|
|
|
|
LEFT JOIN checked_headers on headers.id = header_id
|
|
|
|
WHERE ` + checkedHeadersColumn + ` between 1 and ` + constants.RecheckHeaderCap + `
|
|
|
|
AND headers.block_number >= $1
|
|
|
|
AND headers.eth_node_fingerprint = $2`
|
|
|
|
err = db.Select(&result, query, startingBlockNumber, db.Node.ID)
|
|
|
|
} else {
|
|
|
|
query = `SELECT headers.id, headers.block_number, headers.hash FROM headers
|
|
|
|
LEFT JOIN checked_headers on headers.id = header_id
|
|
|
|
WHERE ` + checkedHeadersColumn + ` between 1 and ` + constants.RecheckHeaderCap + `
|
|
|
|
AND headers.block_number >= $1
|
|
|
|
AND headers.block_number <= $2
|
|
|
|
AND headers.eth_node_fingerprint = $3`
|
|
|
|
err = db.Select(&result, query, startingBlockNumber, endingBlockNumber, db.Node.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:19:31 +00:00
|
|
|
func GetCheckedColumnNames(db *postgres.DB) ([]string, error) {
|
2018-12-04 15:04:13 +00:00
|
|
|
// Query returns `[]driver.Value`, nullable polymorphic interface
|
|
|
|
var queryResult []driver.Value
|
|
|
|
columnNamesQuery :=
|
|
|
|
`SELECT column_name FROM information_schema.columns
|
|
|
|
WHERE table_schema = 'public'
|
|
|
|
AND table_name = 'checked_headers'
|
2018-12-12 11:13:23 +00:00
|
|
|
AND column_name ~ '_checked';`
|
2018-12-04 15:04:13 +00:00
|
|
|
|
|
|
|
err := db.Select(&queryResult, columnNamesQuery)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform column names from `driver.Value` to strings
|
|
|
|
var columnNames []string
|
|
|
|
for _, result := range queryResult {
|
|
|
|
if columnName, ok := result.(string); ok {
|
|
|
|
columnNames = append(columnNames, columnName)
|
|
|
|
} else {
|
|
|
|
return []string{}, fmt.Errorf("incorrect value for checked_headers column name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return columnNames, nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:35:46 +00:00
|
|
|
// Builds a SQL string that checks if any column value is 0, given the column names.
|
2018-12-04 15:04:13 +00:00
|
|
|
// Defaults to FALSE when no columns are provided.
|
2019-02-08 16:35:46 +00:00
|
|
|
// Ex: ["columnA", "columnB"] => "NOT (columnA!=0 AND columnB!=0)"
|
2018-12-04 15:04:13 +00:00
|
|
|
// [] => "FALSE"
|
2019-02-08 16:35:46 +00:00
|
|
|
func CreateNotCheckedSQL(boolColumns []string, recheckHeaders constants.TransformerExecution) string {
|
|
|
|
|
2018-12-13 15:08:57 +00:00
|
|
|
var result bytes.Buffer
|
2018-12-04 15:04:13 +00:00
|
|
|
|
|
|
|
if len(boolColumns) == 0 {
|
|
|
|
return "FALSE"
|
|
|
|
}
|
|
|
|
|
|
|
|
result.WriteString("NOT (")
|
2019-02-08 16:35:46 +00:00
|
|
|
|
2018-12-13 15:08:57 +00:00
|
|
|
// Loop excluding last column name
|
2018-12-04 15:04:13 +00:00
|
|
|
for _, column := range boolColumns[:len(boolColumns)-1] {
|
2019-02-08 16:35:46 +00:00
|
|
|
|
|
|
|
if recheckHeaders {
|
|
|
|
result.WriteString(fmt.Sprintf("%v>=%s AND ", column, constants.RecheckHeaderCap))
|
|
|
|
} else {
|
|
|
|
result.WriteString(fmt.Sprintf("%v!=0 AND ", column))
|
|
|
|
}
|
2018-12-04 15:04:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 15:08:57 +00:00
|
|
|
// No trailing "OR" for the last column name
|
2019-02-08 16:35:46 +00:00
|
|
|
if recheckHeaders {
|
|
|
|
result.WriteString(fmt.Sprintf("%v>=%s)", boolColumns[len(boolColumns)-1], constants.RecheckHeaderCap))
|
|
|
|
} else {
|
|
|
|
result.WriteString(fmt.Sprintf("%v!=0)", boolColumns[len(boolColumns)-1]))
|
|
|
|
|
|
|
|
}
|
2018-12-04 15:04:13 +00:00
|
|
|
|
|
|
|
return result.String()
|
|
|
|
}
|