Skip to content

Commit 99611e2

Browse files
committed
feat: gc for blocks saved in mem for dedup
1 parent dcf1035 commit 99611e2

File tree

2 files changed

+70
-26
lines changed

2 files changed

+70
-26
lines changed

internal/config/networks.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
const (
10-
ToleranceMs = 300000
11-
NullTrieHash = "0000000000000000000000000000000000000000000000000000000000000000"
10+
ToleranceMs = 300000
11+
NullTrieHash = "0000000000000000000000000000000000000000000000000000000000000000"
12+
FinalitySlotsNeeded = 129600
1213
)
1314

1415
type Network struct {

internal/strategy/chain_event_processor_strategy.go

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/hex"
77
"encoding/json"
88
"fmt"
9-
"strconv"
109
"strings"
1110
"time"
1211
"zenGate-Global/merkle-oracle-node/internal/cloud"
@@ -292,7 +291,7 @@ type ChainEventProcessorActor struct {
292291
lastSucessfullyIndexedSlot uint64
293292
lastSucessfullyIndexedBlockHash string
294293

295-
processedBlockMap map[string]bool
294+
processedBlockMap map[BlockKey]bool
296295

297296
blockTransactionCountMap map[uint64]uint64
298297
}
@@ -322,19 +321,61 @@ func NewChainEventProcessorStrategy(
322321
trieRootMismatchCount: 0,
323322
lastSucessfullyIndexedSlot: cfg.StartBlockSlot,
324323
lastSucessfullyIndexedBlockHash: cfg.StartBlockHash,
325-
processedBlockMap: make(map[string]bool),
324+
processedBlockMap: make(map[BlockKey]bool),
326325
blockTransactionCountMap: make(map[uint64]uint64),
327326
}
328327
}
329328
}
330329

331-
// createBlockKey creates a composite key from block number and block hash
330+
// BlockKey represents a composite key for identifying blocks uniquely
331+
// This ensures we can distinguish between different blocks with the same number due to rollbacks
332+
// and enables garbage collection based on slot numbers
333+
type BlockKey struct {
334+
BlockNumber uint64 `json:"blockNumber"`
335+
BlockHash string `json:"blockHash"`
336+
SlotNumber uint64 `json:"slotNumber"`
337+
}
338+
339+
// String returns a string representation of the BlockKey for logging purposes
340+
func (bk BlockKey) String() string {
341+
return fmt.Sprintf("%d:%s:%d", bk.BlockNumber, bk.BlockHash, bk.SlotNumber)
342+
}
343+
344+
// createBlockKey creates a composite key from block number, block hash, and slot number
332345
// This ensures we can distinguish between different blocks with the same number due to rollbacks
333346
func (s *ChainEventProcessorActor) createBlockKey(
334347
blockNumber uint64,
335348
blockHash string,
336-
) string {
337-
return fmt.Sprintf("%d:%s", blockNumber, blockHash)
349+
slotNumber uint64,
350+
) BlockKey {
351+
return BlockKey{
352+
BlockNumber: blockNumber,
353+
BlockHash: blockHash,
354+
SlotNumber: slotNumber,
355+
}
356+
}
357+
358+
// garbageCollectOldBlocks removes processed blocks with slot numbers less than N
359+
func (s *ChainEventProcessorActor) garbageCollectOldBlocks(cutoffSlot uint64) {
360+
var removedBlocks []BlockKey
361+
for blockKey := range s.processedBlockMap {
362+
if blockKey.SlotNumber < cutoffSlot {
363+
delete(s.processedBlockMap, blockKey)
364+
removedBlocks = append(removedBlocks, blockKey)
365+
}
366+
}
367+
368+
if len(removedBlocks) > 0 {
369+
s.logger.Debugf(
370+
"Garbage collected %d old processed blocks (cutoff slot: %d)",
371+
len(removedBlocks),
372+
cutoffSlot,
373+
)
374+
s.logger.Debugf(
375+
"Remaining processed blocks count: %d",
376+
len(s.processedBlockMap),
377+
)
378+
}
338379
}
339380

340381
func (s *ChainEventProcessorActor) Receive(c *actor.Context) {
@@ -511,13 +552,13 @@ func (s *ChainEventProcessorActor) processBlockEvent(
511552
) error {
512553
blockNumber := event.EventContext.BlockNumber
513554
blockHash := event.EventTransaction.BlockHash
514-
blockKey := s.createBlockKey(blockNumber, blockHash)
555+
slotNumber := event.EventContext.SlotNumber
556+
blockKey := s.createBlockKey(blockNumber, blockHash, slotNumber)
515557

516558
if _, exists := s.processedBlockMap[blockKey]; exists {
517559
s.logger.Infof(
518-
"Block %d:%s already processed, skipping block",
519-
blockNumber,
520-
blockHash,
560+
"Block %s already processed, skipping block",
561+
blockKey.String(),
521562
)
522563
return nil
523564
}
@@ -574,6 +615,9 @@ func (s *ChainEventProcessorActor) processBlockTipReached(
574615
s.logger.Debugw("Current pending transactions", "count", c)
575616
}
576617

618+
currentSlot := uint64(blockEvent.EventContext.SlotNumber)
619+
s.garbageCollectOldBlocks(currentSlot - uint64(config.FinalitySlotsNeeded))
620+
577621
memTrie, err := s.db.GetInMemoryTrie()
578622
if err != nil {
579623
return fmt.Errorf("get in-memory trie: %w", err)
@@ -740,7 +784,6 @@ func (s *ChainEventProcessorActor) processBlockTipReached(
740784
uploadPayload.TrieData.Deletions = dels
741785

742786
// apply ops to local trie
743-
currentSlot := uint64(blockEvent.EventContext.SlotNumber)
744787
if err := s.applyTrieOperations(memTrie, ins, ups, dels, currentSlot); err != nil {
745788
return err
746789
}
@@ -1248,24 +1291,24 @@ func (s *ChainEventProcessorActor) processRollbackEvent(
12481291
s.logger.Debugf("Trie state unchanged after rollback (root: %x)", originalRoot)
12491292
}
12501293

1251-
var deletedBlocks []string
1294+
var deletedBlocks []BlockKey
12521295
for blockKey := range s.processedBlockMap {
1253-
// extract block number from composite key (format: "blockNumber:blockHash")
1254-
if colonIndex := strings.Index(blockKey, ":"); colonIndex != -1 {
1255-
if blockNumberStr := blockKey[:colonIndex]; blockNumberStr != "" {
1256-
if blockNumber, err := strconv.ParseUint(blockNumberStr, 10, 64); err == nil {
1257-
if blockNumber > rollbackEvent.SlotNumber {
1258-
delete(s.processedBlockMap, blockKey)
1259-
deletedBlocks = append(deletedBlocks, blockKey)
1260-
}
1261-
}
1262-
}
1296+
// Check if block slot number is greater than rollback slot number
1297+
if blockKey.SlotNumber > rollbackEvent.SlotNumber {
1298+
delete(s.processedBlockMap, blockKey)
1299+
deletedBlocks = append(deletedBlocks, blockKey)
12631300
}
12641301
}
12651302

12661303
if len(deletedBlocks) > 0 {
1267-
s.logger.Debugf("Removed %d processed block entries for blocks > %d",
1268-
len(deletedBlocks), rollbackEvent.SlotNumber)
1304+
s.logger.Debugf(
1305+
"Removed %d processed block entries for blocks with slot > %d",
1306+
len(deletedBlocks),
1307+
rollbackEvent.SlotNumber,
1308+
)
1309+
for _, deletedBlock := range deletedBlocks {
1310+
s.logger.Debugf("Removed block: %s", deletedBlock.String())
1311+
}
12691312
}
12701313

12711314
return nil

0 commit comments

Comments
 (0)