Skip to content

Commit 8dfdfae

Browse files
committed
Merge branch 'ryan/simplify-start-index' into 'main'
simplify start index calculation See merge request flarenetwork/FSP/flare-system-c-chain-indexer!71
2 parents 3a803c2 + 9fb242f commit 8dfdfae

File tree

3 files changed

+72
-25
lines changed

3 files changed

+72
-25
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ database = "flare_ftso_indexer"
4848
username = "root"
4949
password = "root"
5050
log_queries = false
51-
drop_table_at_start = true
52-
history_drop = 604800 # Enable deleting the transactions and logs in DB that are older (timestamp of the block) than history_drop (in seconds); set 0 to turn off; defaults to 7 days for Flare/Songbird and 2 days for Coston*
51+
drop_table_at_start = false # set to true to drop existing tables at the start of the indexing - useful to force re-indexing
52+
history_drop = 604800 # Enable deleting the transactions and logs in DB that are older (timestamp of the block) than history_drop (in seconds); set 0 to turn off; defaults to 7 days for Flare/Songbird and 2 days for Coston*
5353

5454
[logger]
5555
level = "INFO"
@@ -66,6 +66,21 @@ backoff_max_elapsed_time_seconds = 300 # optional, defaults to 300s = 5 minutes.
6666
timeout_milis = 1000 # optional, defaults to 1000ms = 1s. Try increasing if you see timeout errors often.
6767
```
6868

69+
If the C chain indexer has been previously run and there is existing data in the database,
70+
subsequent runs will resume indexing from after the last indexed block. Only when starting with an
71+
empty database will the indexer have to decide where to start. Normally this will be based on the
72+
history drop configuration parameter - if the history drop is 14 days for example then the indexer
73+
will start indexing from the block that was mined 14 days ago. If the history drop is disabled (set
74+
to 0) then the indexer will start from the configured `start_index` block or from block 0 if not
75+
set.
76+
77+
In case the indexer has been previously run and you need more historical data, you can increase
78+
the history drop parameter or disable history drop and set the start_index to the desired
79+
starting block. You can manually delete all existing data from the database in order to trigger
80+
re-indexing from the new starting block. You can also set the `drop_table_at_start` parameter to
81+
true to have the indexer drop existing tables at startup and force re-indexing - though remember to
82+
set it back to false afterwards to avoid losing data on subsequent runs.
83+
6984
### Database
7085

7186
In `database/docker` we provide a simple database. Navigate to the folder and run

database/history_drop.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@ func dropHistoryIteration(
8787

8888
// GetStartBlock returns the block number to start indexing from based on the history drop parameter.
8989
func GetStartBlock(
90-
ctx context.Context, db *gorm.DB, historyDropIntervalSeconds uint64, client *chain.Client, configuredStartBlock uint64,
90+
ctx context.Context, historyDropIntervalSeconds uint64, client *chain.Client, configuredStartBlock uint64,
9191
) (uint64, error) {
9292
lastBlockTime, lastBlockNumber, err := getBlockTimestamp(ctx, nil, client)
9393
if err != nil {
9494
return 0, errors.Wrap(err, "Failed to get the latest block")
9595
}
9696

97-
db = db.WithContext(ctx)
9897
deleteStartTime := lastBlockTime - historyDropIntervalSeconds
9998

100-
return getNearestBlockByTimestamp(
101-
ctx, deleteStartTime, db, client, configuredStartBlock, lastBlockNumber,
99+
// This function is only ever called when starting with an empty DB state
100+
// so we can skip the DB check and jump straight to the chain search.
101+
return getNearestBlockByTimestampFromChain(
102+
ctx, deleteStartTime, client, configuredStartBlock, lastBlockNumber,
102103
)
103104
}
104105

main.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,60 @@ func run(ctx context.Context) error {
8787
}
8888
}
8989

90-
if historyDrop > 0 {
91-
firstBlockNumber, err := boff.Retry(
92-
ctx,
93-
func() (uint64, error) {
94-
return database.GetStartBlock(
95-
ctx, db, historyDrop, ethClient, cfg.Indexer.StartIndex,
96-
)
97-
},
98-
"GetStartBlock",
99-
)
100-
if err != nil {
101-
return errors.Wrap(err, "GetStartBlock error")
102-
}
90+
startIndex, err := getStartIndex(ctx, db, ethClient, cfg, historyDrop)
91+
if err != nil {
92+
return errors.Wrap(err, "getStartIndex error")
93+
}
10394

104-
logger.Info("firstBlockNumber = %d", firstBlockNumber)
95+
cfg.Indexer.StartIndex = startIndex
10596

106-
if firstBlockNumber > cfg.Indexer.StartIndex {
107-
logger.Info("Setting new startIndex due to history drop: %d", firstBlockNumber)
108-
cfg.Indexer.StartIndex = firstBlockNumber
109-
}
97+
return runIndexer(ctx, cfg, db, ethClient, historyDrop)
98+
}
99+
100+
func getStartIndex(
101+
ctx context.Context,
102+
db *gorm.DB,
103+
ethClient *chain.Client,
104+
cfg *config.Config,
105+
historyDrop uint64,
106+
) (uint64, error) {
107+
var latestIndexedBlock database.Block
108+
err := db.Last(&database.Block{}).Select("number").Scan(&latestIndexedBlock).Error
109+
110+
// If a latest indexed block is found, return the next block number
111+
if err == nil {
112+
logger.Info("Starting after latest indexed block from DB: %d", latestIndexedBlock.Number)
113+
return latestIndexedBlock.Number + 1, nil
110114
}
111115

112-
return runIndexer(ctx, cfg, db, ethClient, historyDrop)
116+
// In case of an unexpected error, return it
117+
if !errors.Is(err, gorm.ErrRecordNotFound) {
118+
return 0, errors.Wrap(err, "DB query error")
119+
}
120+
121+
// No blocks are indexed yet
122+
// If history drop is disabled, return the configured start index
123+
if historyDrop == 0 {
124+
logger.Info("No indexed blocks found in DB, starting from configured start index: %d", cfg.Indexer.StartIndex)
125+
return cfg.Indexer.StartIndex, nil
126+
}
127+
128+
// History drop is enabled so calculate start index based on it.
129+
firstBlockNumber, err := boff.Retry(
130+
ctx,
131+
func() (uint64, error) {
132+
return database.GetStartBlock(
133+
ctx, historyDrop, ethClient, cfg.Indexer.StartIndex,
134+
)
135+
},
136+
"GetStartBlock",
137+
)
138+
if err != nil {
139+
return 0, errors.Wrap(err, "GetStartBlock error")
140+
}
141+
142+
logger.Info("No indexed blocks found in DB, starting from calculated start index based on history drop: %d", firstBlockNumber)
143+
return firstBlockNumber, nil
113144
}
114145

115146
func runIndexer(

0 commit comments

Comments
 (0)