Skip to content

Commit 34a2d4d

Browse files
committed
core/filtermaps: use DeleteRange, checkpoint init of log value pointer
1 parent f6c08b2 commit 34a2d4d

File tree

2 files changed

+78
-34
lines changed

2 files changed

+78
-34
lines changed

core/filtermaps/filtermaps.go

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package filtermaps
1818

1919
import (
20+
"bytes"
2021
"errors"
2122
"sync"
2223
"time"
@@ -27,10 +28,37 @@ import (
2728
"github.com/ethereum/go-ethereum/core/rawdb"
2829
"github.com/ethereum/go-ethereum/core/types"
2930
"github.com/ethereum/go-ethereum/ethdb"
31+
"github.com/ethereum/go-ethereum/ethdb/leveldb"
3032
"github.com/ethereum/go-ethereum/event"
3133
"github.com/ethereum/go-ethereum/log"
3234
)
3335

36+
// checkpoint allows the log indexer to start indexing from the given block
37+
// instead of genesis at the correct absolute log value index.
38+
type checkpoint struct {
39+
blockNumber uint64
40+
blockHash common.Hash
41+
nextLvIndex uint64 // next log value index after the given block
42+
}
43+
44+
var checkpoints = []checkpoint{
45+
{ // Mainnet
46+
blockNumber: 21019982,
47+
blockHash: common.HexToHash("0xc684e4db692fe347e740082665acf91e27c0d9ad2a118822abdd7bb06c2a9250"),
48+
nextLvIndex: 15878969230,
49+
},
50+
{ // Sepolia
51+
blockNumber: 6939193,
52+
blockHash: common.HexToHash("0x659b6e8a711efe8184368ac286f1f4aee74be50d38bb7fe4b24f53e73dfa58b8"),
53+
nextLvIndex: 3392298216,
54+
},
55+
{ // Holesky
56+
blockNumber: 2607449,
57+
blockHash: common.HexToHash("0xa48c4e1ff3857ba44346bc25346d9947cd12c08f5ce8c10e8acaf40e2d6c7dc4"),
58+
nextLvIndex: 966700355,
59+
},
60+
}
61+
3462
const headCacheSize = 8 // maximum number of recent filter maps cached in memory
3563

3664
// blockchain defines functions required by the FilterMaps log indexer.
@@ -175,6 +203,9 @@ func NewFilterMaps(db ethdb.KeyValueStore, chain blockchain, params Params, hist
175203
log.Error("Error fetching tail block pointer, resetting log index", "error", err)
176204
fm.filterMapsRange = filterMapsRange{} // updateLoop resets the database
177205
}
206+
headBlockPtr, _ := fm.getBlockLvPointer(fm.headBlockNumber)
207+
log.Trace("Log index head", "number", fm.headBlockNumber, "hash", fm.headBlockHash.String(), "log value pointer", fm.headLvPointer)
208+
log.Trace("Log index tail", "number", fm.tailBlockNumber, "parentHash", fm.tailParentHash.String(), "log value pointer", fm.tailBlockLvPointer)
178209
}
179210
return fm
180211
}
@@ -218,43 +249,37 @@ func (f *FilterMaps) removeBloomBits() {
218249
// removeDbWithPrefix removes data with the given prefix from the database and
219250
// returns true if everything was successfully removed.
220251
func (f *FilterMaps) removeDbWithPrefix(prefix []byte, action string) bool {
221-
var (
222-
logged bool
223-
lastLogged time.Time
224-
removed uint64
225-
)
252+
it := f.db.NewIterator(prefix, nil)
253+
hasData := it.Next()
254+
it.Release()
255+
if !hasData {
256+
return true
257+
}
258+
259+
end := bytes.Clone(prefix)
260+
end[len(end)-1]++
261+
start := time.Now()
262+
var retry bool
226263
for {
264+
err := f.db.DeleteRange(prefix, end)
265+
if err == nil {
266+
log.Info(action+" finished", "elapsed", time.Since(start))
267+
return true
268+
}
269+
if err != leveldb.ErrTooManyKeys {
270+
log.Error(action+" failed", "error", err)
271+
return false
272+
}
227273
select {
228274
case <-f.closeCh:
229275
return false
230276
default:
231277
}
232-
it := f.db.NewIterator(prefix, nil)
233-
batch := f.db.NewBatch()
234-
var count int
235-
for ; count < 250000 && it.Next(); count++ {
236-
batch.Delete(it.Key())
237-
removed++
238-
}
239-
it.Release()
240-
if count == 0 {
241-
break
278+
if !retry {
279+
log.Info(action + " in progress...")
280+
retry = true
242281
}
243-
if !logged {
244-
log.Info(action + "...")
245-
logged = true
246-
lastLogged = time.Now()
247-
}
248-
if time.Since(lastLogged) >= time.Second*10 {
249-
log.Info(action+" in progress", "removed keys", removed)
250-
lastLogged = time.Now()
251-
}
252-
batch.Write()
253-
}
254-
if logged {
255-
log.Info(action + " finished")
256282
}
257-
return true
258283
}
259284

260285
// setRange updates the covered range and also adds the changes to the given batch.

core/filtermaps/indexer.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,37 @@ func (f *FilterMaps) tryInit(head *types.Header) bool {
203203
if !f.reset() {
204204
return false
205205
}
206-
head = f.chain.GetHeader(f.chain.GetCanonicalHash(0), 0)
207-
receipts := f.chain.GetReceiptsByHash(head.Hash())
206+
207+
hc := newHeaderChain(f.chain, head.Number.Uint64(), head.Hash())
208+
var (
209+
initHeader *types.Header
210+
nextLvPtr uint64
211+
)
212+
for _, cp := range checkpoints {
213+
if initHeader == nil || cp.blockNumber >= initHeader.Number.Uint64() {
214+
if h := f.chain.GetHeader(hc.getBlockHash(cp.blockNumber+1), cp.blockNumber+1); h != nil && h.ParentHash == cp.blockHash {
215+
initHeader, nextLvPtr = h, cp.nextLvIndex
216+
}
217+
}
218+
}
219+
if initHeader == nil {
220+
initHeader = f.chain.GetHeader(f.chain.GetCanonicalHash(0), 0)
221+
}
222+
if initHeader == nil {
223+
log.Error("Could not retrieve init header")
224+
return true
225+
}
226+
receipts := f.chain.GetReceiptsByHash(initHeader.Hash())
208227
if receipts == nil {
209-
log.Error("Could not retrieve block receipts for init block", "number", head.Number, "hash", head.Hash())
228+
log.Error("Could not retrieve block receipts for init block", "number", initHeader.Number, "hash", initHeader.Hash())
210229
return true
211230
}
212231
update := f.newUpdateBatch()
213-
if err := update.initWithBlock(head, receipts, 0); err != nil {
232+
if err := update.initWithBlock(initHeader, receipts, nextLvPtr); err != nil {
214233
log.Error("Could not initialize log index", "error", err)
215234
}
216235
f.applyUpdateBatch(update)
217-
log.Info("Initialized log index", "head", head.Number.Uint64())
236+
log.Info("Initialized log index", "head", initHeader.Number.Uint64(), "log value pointer", nextLvPtr)
218237
return true
219238
}
220239

0 commit comments

Comments
 (0)