From 3ad2bcd9a0bc65b162d2a39d27a1ea456a795234 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Thu, 22 Jun 2023 11:59:01 -0500 Subject: [PATCH] Move the SQL statement. --- statediff/indexer/database/sql/interfaces.go | 1 + statediff/indexer/database/sql/postgres/database.go | 7 +++++++ statediff/indexer/database/sql/writer.go | 10 ++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/statediff/indexer/database/sql/interfaces.go b/statediff/indexer/database/sql/interfaces.go index d59b603eb..bb912cbc7 100644 --- a/statediff/indexer/database/sql/interfaces.go +++ b/statediff/indexer/database/sql/interfaces.go @@ -45,6 +45,7 @@ type Driver interface { // Statements interface to accommodate different SQL query syntax type Statements interface { + DetectGapsStm() string MaxHeaderStm() string ExistsHeaderStm() string InsertHeaderStm() string diff --git a/statediff/indexer/database/sql/postgres/database.go b/statediff/indexer/database/sql/postgres/database.go index 77190fa80..0358e566f 100644 --- a/statediff/indexer/database/sql/postgres/database.go +++ b/statediff/indexer/database/sql/postgres/database.go @@ -41,14 +41,21 @@ type DB struct { sql.Driver } +// MaxHeaderStm satisfies the sql.Statements interface func (db *DB) MaxHeaderStm() string { return fmt.Sprintf("SELECT block_number, block_hash, parent_hash, cid, td, node_ids, reward, state_root, tx_root, receipt_root, uncles_hash, bloom, timestamp, coinbase FROM %s ORDER BY block_number DESC LIMIT 1", schema.TableHeader.Name) } +// ExistsHeaderStm satisfies the sql.Statements interface func (db *DB) ExistsHeaderStm() string { return fmt.Sprintf("SELECT EXISTS(SELECT 1 from %s WHERE block_number = $1::BIGINT AND block_hash = $2::TEXT LIMIT 1)", schema.TableHeader.Name) } +// DetectGapsStm satisfies the sql.Statements interface +func (db *DB) DetectGapsStm() string { + return fmt.Sprintf("SELECT block_number + 1 AS first_missing, (next_bn - 1) AS last_missing FROM (SELECT block_number, LEAD(block_number) OVER (ORDER BY block_number) AS next_bn FROM %s WHERE block_number >= $1::BIGINT AND block_number <= $2::BIGINT) h WHERE next_bn > block_number + 1", schema.TableHeader.Name) +} + // InsertHeaderStm satisfies the sql.Statements interface // Stm == Statement func (db *DB) InsertHeaderStm() string { diff --git a/statediff/indexer/database/sql/writer.go b/statediff/indexer/database/sql/writer.go index 244c42395..ba9407e0a 100644 --- a/statediff/indexer/database/sql/writer.go +++ b/statediff/indexer/database/sql/writer.go @@ -50,7 +50,7 @@ func (w *Writer) Close() error { return w.db.Close() } -// hashHeader returns true if a matching hash+number record exists in the database, else false. +// hasHeader returns true if a matching hash+number record exists in the database, else false. func (w *Writer) hasHeader(blockHash common.Hash, blockNumber uint64) (exists bool, err error) { // pgx misdetects the parameter OIDs and selects int8, which can overflow. // unfortunately there is no good place to override it, so it is safer to pass the uint64s as text @@ -63,18 +63,16 @@ func (w *Writer) hasHeader(blockHash common.Hash, blockNumber uint64) (exists bo // For example, if the database contains blocks the overall range 1000:2000, but is missing blocks 1110:1230 and 1380 // it would return [{FirstMissing: 1110, LastMissing: 1230}, {FirstMissing: 1380, LastMissing: 1380}] func (w *Writer) detectGaps(beginBlockNumber uint64, endBlockNumber uint64) ([]*interfaces.BlockGap, error) { - pgStm := "SELECT block_number + 1 AS first_missing, (next_bn - 1) AS last_missing FROM (SELECT block_number, LEAD(block_number) OVER (ORDER BY block_number) AS next_bn FROM eth.header_cids WHERE block_number >= $1::BIGINT AND block_number <= $2::BIGINT) h WHERE next_bn > block_number + 1" var gaps []*interfaces.BlockGap // pgx misdetects the parameter OIDs and selects int8, which can overflow. // unfortunately there is no good place to override it, so it is safer to pass the uint64s as text // and let PG handle the cast - err := w.db.Select(w.db.Context(), &gaps, pgStm, strconv.FormatUint(beginBlockNumber, 10), strconv.FormatUint(endBlockNumber, 10)) + err := w.db.Select(w.db.Context(), &gaps, w.db.DetectGapsStm(), strconv.FormatUint(beginBlockNumber, 10), strconv.FormatUint(endBlockNumber, 10)) return gaps, err } -/* -SELECT block_number, block_hash, parent_hash, cid, td, node_ids, reward, state_root, tx_root, receipt_root, uncles_hash, bloom, timestamp, coinbase FROM %s ORDER BY block_number DESC LIMIT 1 -*/ +// maxHeader returns the header for the highest block number in the database. +// SELECT block_number, block_hash, parent_hash, cid, td, node_ids, reward, state_root, tx_root, receipt_root, uncles_hash, bloom, timestamp, coinbase FROM %s ORDER BY block_number DESC LIMIT 1 func (w *Writer) maxHeader() (*models.HeaderModel, error) { var model models.HeaderModel var err error