2018-11-08 17:07:54 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2018-12-04 15:04:13 +00:00
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
2018-11-08 17:07:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-12-04 15:04:13 +00:00
|
|
|
"strings"
|
2018-11-08 17:07:54 +00:00
|
|
|
)
|
|
|
|
|
2018-12-11 10:35:13 +00:00
|
|
|
type Repository struct{}
|
|
|
|
|
|
|
|
func (_ Repository) 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+`)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (header_id) DO
|
|
|
|
UPDATE SET `+checkedHeadersColumn+` = $2`, headerID, true)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:35:13 +00:00
|
|
|
func (_ Repository) MarkHeaderCheckedInTransaction(headerID int64, tx *sql.Tx, checkedHeadersColumn string) error {
|
2018-11-08 17:07:54 +00:00
|
|
|
_, err := tx.Exec(`INSERT INTO public.checked_headers (header_id, `+checkedHeadersColumn+`)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (header_id) DO
|
|
|
|
UPDATE SET `+checkedHeadersColumn+` = $2`, headerID, true)
|
|
|
|
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
|
2018-12-11 10:35:13 +00:00
|
|
|
func (_ Repository) 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
|
|
|
|
2018-12-11 10:35:13 +00:00
|
|
|
func (_ Repository) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds a SQL string that checks if any column value is FALSE, given the column names.
|
|
|
|
// Defaults to FALSE when no columns are provided.
|
|
|
|
// Ex: ["columnA", "columnB"] => "NOT (columnA AND columnB)"
|
|
|
|
// [] => "FALSE"
|
2018-12-11 10:35:13 +00:00
|
|
|
func (_ Repository) CreateNotCheckedSQL(boolColumns []string) string {
|
2018-12-04 15:04:13 +00:00
|
|
|
var result strings.Builder
|
|
|
|
|
|
|
|
if len(boolColumns) == 0 {
|
|
|
|
return "FALSE"
|
|
|
|
}
|
|
|
|
|
|
|
|
result.WriteString("NOT (")
|
|
|
|
for _, column := range boolColumns[:len(boolColumns)-1] {
|
|
|
|
result.WriteString(fmt.Sprintf("%v AND ", column))
|
|
|
|
}
|
|
|
|
|
|
|
|
// No trailing "OR" for last column name
|
|
|
|
result.WriteString(fmt.Sprintf("%v)", boolColumns[len(boolColumns)-1]))
|
|
|
|
|
|
|
|
return result.String()
|
|
|
|
}
|