17
17
package filtermaps
18
18
19
19
import (
20
+ "bytes"
20
21
"errors"
21
22
"sync"
22
23
"time"
@@ -27,10 +28,37 @@ import (
27
28
"github.com/ethereum/go-ethereum/core/rawdb"
28
29
"github.com/ethereum/go-ethereum/core/types"
29
30
"github.com/ethereum/go-ethereum/ethdb"
31
+ "github.com/ethereum/go-ethereum/ethdb/leveldb"
30
32
"github.com/ethereum/go-ethereum/event"
31
33
"github.com/ethereum/go-ethereum/log"
32
34
)
33
35
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
+
34
62
const headCacheSize = 8 // maximum number of recent filter maps cached in memory
35
63
36
64
// blockchain defines functions required by the FilterMaps log indexer.
@@ -175,6 +203,9 @@ func NewFilterMaps(db ethdb.KeyValueStore, chain blockchain, params Params, hist
175
203
log .Error ("Error fetching tail block pointer, resetting log index" , "error" , err )
176
204
fm .filterMapsRange = filterMapsRange {} // updateLoop resets the database
177
205
}
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 )
178
209
}
179
210
return fm
180
211
}
@@ -218,43 +249,37 @@ func (f *FilterMaps) removeBloomBits() {
218
249
// removeDbWithPrefix removes data with the given prefix from the database and
219
250
// returns true if everything was successfully removed.
220
251
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
226
263
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
+ }
227
273
select {
228
274
case <- f .closeCh :
229
275
return false
230
276
default :
231
277
}
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
242
281
}
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" )
256
282
}
257
- return true
258
283
}
259
284
260
285
// setRange updates the covered range and also adds the changes to the given batch.
0 commit comments