Skip to content

Commit

Permalink
refactor: use epochs to gc eth tx hashes from chain indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
akaladarshi committed Sep 26, 2024
1 parent 8190b4d commit d57f643
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion chain/index/ddls.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func preparedStatementMapping(ps *preparedStatements) map[**sql.Stmt]string {
&ps.updateTipsetToNonRevertedStmt: "UPDATE tipset_message SET reverted = 0 WHERE tipset_key_cid = ?",
&ps.updateTipsetToRevertedStmt: "UPDATE tipset_message SET reverted = 1 WHERE tipset_key_cid = ?",
&ps.removeTipsetsBeforeHeightStmt: "DELETE FROM tipset_message WHERE height < ?",
&ps.removeEthHashesOlderThanStmt: "DELETE FROM eth_tx_hash WHERE inserted_at < datetime('now', ?)",
&ps.removeEthHashesBeforeTimeStmt: "DELETE FROM eth_tx_hash WHERE inserted_at < ?",
&ps.updateTipsetsToRevertedFromHeightStmt: "UPDATE tipset_message SET reverted = 1 WHERE height >= ?",
&ps.updateEventsToRevertedFromHeightStmt: "UPDATE event SET reverted = 1 WHERE message_id IN (SELECT message_id FROM tipset_message WHERE height >= ?)",
&ps.isIndexEmptyStmt: "SELECT NOT EXISTS(SELECT 1 FROM tipset_message LIMIT 1)",
Expand Down
20 changes: 11 additions & 9 deletions chain/index/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package index

import (
"context"
"strconv"
"time"

logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -75,17 +74,20 @@ func (si *SqliteIndexer) gc(ctx context.Context) {
// -------------------------------------------------------------------------------------------------
// Also GC eth hashes

// Convert gcRetentionEpochs to number of days
gcRetentionDays := si.gcRetentionEpochs / (builtin.EpochsInDay)
if gcRetentionDays < 1 {
log.Infof("skipping gc of eth hashes as retention days is less than 1")
currHeadTime := time.Unix(int64(head.MinTimestamp()), 0)
retentionDuration := time.Duration(si.gcRetentionEpochs * builtin.EpochDurationSeconds)
// gcTime is the time that is gcRetentionEpochs before currHeadTime
gcTime := currHeadTime.Add(-retentionDuration)
if gcTime.After(time.Now()) {
log.Info("no eth_tx_hash entries to gc")
return
}

log.Infof("gc'ing eth hashes older than %d days", gcRetentionDays)
res, err = si.stmts.removeEthHashesOlderThanStmt.ExecContext(ctx, "-"+strconv.Itoa(int(gcRetentionDays))+" day")
log.Infof("gc'ing eth hashes before time %s", gcTime.String())

res, err = si.stmts.removeEthHashesBeforeTimeStmt.ExecContext(ctx, gcTime)
if err != nil {
log.Errorf("failed to gc eth hashes older than %d days: %w", gcRetentionDays, err)
log.Errorf("failed to gc eth hashes before time %s: %w", gcTime.String(), err)
return
}

Expand All @@ -95,5 +97,5 @@ func (si *SqliteIndexer) gc(ctx context.Context) {
return
}

log.Infof("gc'd %d eth hashes older than %d days", rows, gcRetentionDays)
log.Infof("gc'd %d eth hashes before time %s", rows, gcTime.String())
}
2 changes: 1 addition & 1 deletion chain/index/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type preparedStatements struct {
hasTipsetStmt *sql.Stmt
updateTipsetToNonRevertedStmt *sql.Stmt
removeTipsetsBeforeHeightStmt *sql.Stmt
removeEthHashesOlderThanStmt *sql.Stmt
removeEthHashesBeforeTimeStmt *sql.Stmt
updateTipsetsToRevertedFromHeightStmt *sql.Stmt
updateEventsToRevertedFromHeightStmt *sql.Stmt
isIndexEmptyStmt *sql.Stmt
Expand Down

0 comments on commit d57f643

Please sign in to comment.