Skip to content

Commit

Permalink
db: add block timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Jul 26, 2024
1 parent a1c8ca4 commit 377a99c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
15 changes: 6 additions & 9 deletions cmd/core/check-payload-value.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ func startUpdateWorker(wg *sync.WaitGroup, db *database.DatabaseService, client,
return block, err
}

// getBlockByNumber := func(blockNumber int) (*types.Block, error) {
// block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
// if err != nil || block == nil {
// block, err = client2.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
// }
// return block, err
// }

getHeaderByNumber := func(blockNumber *big.Int) (*types.Header, error) {
block, err := client.HeaderByNumber(context.Background(), blockNumber)
if err != nil || block == nil {
Expand All @@ -210,7 +202,8 @@ func startUpdateWorker(wg *sync.WaitGroup, db *database.DatabaseService, client,
coinbase_diff_eth=:coinbase_diff_eth,
found_onchain=:found_onchain, -- should rename field, because getBlockByHash might succeed even though this slot was missed
num_blob_txs=:num_blob_txs,
num_blobs=:num_blobs
num_blobs=:num_blobs,
block_timestamp=:block_timestamp
WHERE slot=:slot`
_, err := db.DB.NamedExec(query, entry)
if err != nil {
Expand Down Expand Up @@ -357,6 +350,10 @@ func startUpdateWorker(wg *sync.WaitGroup, db *database.DatabaseService, client,
entry.ValueDeliveredDiffWei = database.NewNullString(proposerValueDiffFromClaim.String())
entry.ValueDeliveredDiffEth = database.NewNullString(common.WeiToEth(proposerValueDiffFromClaim).String())

// set block timestamp
blockTime := time.Unix(int64(block.Time()), 0).UTC()
entry.BlockTimestamp = database.NewNullTime(blockTime)

log.WithFields(logrus.Fields{
"coinbaseIsProposer": coinbaseIsProposer,
// "coinbase": block.Miner,
Expand Down
1 change: 0 additions & 1 deletion database/migrations/001_init_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ CREATE INDEX IF NOT EXISTS ` + vars.TableDataAPIPayloadDelivered + `_value_wei_i
CREATE INDEX IF NOT EXISTS ` + vars.TableDataAPIPayloadDelivered + `_valuecheck_ok_idx ON ` + vars.TableDataAPIPayloadDelivered + `("value_check_ok");
CREATE INDEX IF NOT EXISTS ` + vars.TableDataAPIPayloadDelivered + `_slotmissed_idx ON ` + vars.TableDataAPIPayloadDelivered + `("slot_missed");
CREATE INDEX IF NOT EXISTS ` + vars.TableDataAPIPayloadDelivered + `_cb_diff_eth_idx ON ` + vars.TableDataAPIPayloadDelivered + `("coinbase_diff_eth");
-- CREATE INDEX CONCURRENTLY IF NOT EXISTS ` + vars.TableDataAPIPayloadDelivered + `_insertedat_relay_idx ON ` + vars.TableDataAPIPayloadDelivered + `("inserted_at", "relay");
CREATE TABLE IF NOT EXISTS ` + vars.TableDataAPIBuilderBid + ` (
Expand Down
18 changes: 18 additions & 0 deletions database/migrations/004_add_block_timestamp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package migrations

import (
"github.com/flashbots/relayscan/database/vars"
migrate "github.com/rubenv/sql-migrate"
)

var migration004SQL = `
ALTER TABLE ` + vars.TableDataAPIPayloadDelivered + ` ADD block_timestamp timestamp DEFAULT NULL;
`

var Migration004AddBlockTimestamp = &migrate.Migration{
Id: "004-add-block-timestamp",
Up: []string{migration004SQL},

DisableTransactionUp: false,
DisableTransactionDown: true,
}
1 change: 1 addition & 0 deletions database/migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ var Migrations = migrate.MemoryMigrationSource{
Migration001InitDatabase,
Migration002AddBlobCount,
Migration003AddBlobIndexes,
Migration004AddBlockTimestamp,
},
}
10 changes: 10 additions & 0 deletions database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func NewNullString(s string) sql.NullString {
}
}

func NewNullTime(t time.Time) sql.NullTime {
return sql.NullTime{
Time: t,
Valid: true,
}
}

type DataAPIPayloadDeliveredEntry struct {
ID int64 `db:"id"`
InsertedAt time.Time `db:"inserted_at"`
Expand Down Expand Up @@ -66,6 +73,9 @@ type DataAPIPayloadDeliveredEntry struct {
// Blob info added 2024-07-25
NumBlobTxs sql.NullInt64 `db:"num_blob_txs"`
NumBlobs sql.NullInt64 `db:"num_blobs"`

// Block time added 2024-07-26
BlockTimestamp sql.NullTime `db:"block_timestamp"`
}

type DataAPIBuilderBidEntry struct {
Expand Down

0 comments on commit 377a99c

Please sign in to comment.