From 137a68bdab5136b3971c8c95ae8149005e55cc7c Mon Sep 17 00:00:00 2001 From: Shawn <44221603+shaspitz@users.noreply.github.com> Date: Wed, 17 Sep 2025 13:34:36 -0700 Subject: [PATCH 1/2] Update postgres.go --- cl/singlenode/payloadstore/postgres.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cl/singlenode/payloadstore/postgres.go b/cl/singlenode/payloadstore/postgres.go index e6f50f6b1..f346b9ad3 100644 --- a/cl/singlenode/payloadstore/postgres.go +++ b/cl/singlenode/payloadstore/postgres.go @@ -280,21 +280,26 @@ func (r *PostgresRepository) GetLatestPayload(ctx context.Context) (*types.Paylo func (r *PostgresRepository) GetLatestHeight(ctx context.Context) (uint64, error) { query := ` - SELECT MAX(block_height) FROM execution_payloads; + SELECT block_height + FROM execution_payloads + ORDER BY block_height DESC + LIMIT 1; ` queryCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - var n sql.NullInt64 - if err := r.db.QueryRowContext(queryCtx, query).Scan(&n); err != nil { - // MAX should never return sql.ErrNoRow, always bubble errors + var h int64 + err := r.db.QueryRowContext(queryCtx, query).Scan(&h) + if err != nil { + if err == sql.ErrNoRows { + return 0, nil + } return 0, err } - if !n.Valid { - // Empty table -> new chain - return 0, nil + if h < 0 { + return 0, fmt.Errorf("returned block height is negative: %d", h) } - return uint64(n.Int64), nil + return uint64(h), nil } // Close closes the database connection. From e8ace7a1b7db0a4ce0f85dc8f9bb56c6beed4519 Mon Sep 17 00:00:00 2001 From: Shawn <44221603+shaspitz@users.noreply.github.com> Date: Wed, 17 Sep 2025 13:37:01 -0700 Subject: [PATCH 2/2] Update postgres.go --- cl/singlenode/payloadstore/postgres.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cl/singlenode/payloadstore/postgres.go b/cl/singlenode/payloadstore/postgres.go index f346b9ad3..f2cbab334 100644 --- a/cl/singlenode/payloadstore/postgres.go +++ b/cl/singlenode/payloadstore/postgres.go @@ -291,7 +291,7 @@ func (r *PostgresRepository) GetLatestHeight(ctx context.Context) (uint64, error var h int64 err := r.db.QueryRowContext(queryCtx, query).Scan(&h) if err != nil { - if err == sql.ErrNoRows { + if err == sql.ErrNoRows { // Empty table -> new chain return 0, nil } return 0, err