Skip to content

Commit

Permalink
Backup cache purge
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Jan 8, 2025
1 parent 4043b67 commit ca32e24
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
11 changes: 9 additions & 2 deletions database/tbcd/level/headercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ func (l *lowIQMap) Get(k *chainhash.Hash) (*tbcd.BlockHeader, bool) {
return bh, ok
}

func (l *lowIQMap) Purge(k *chainhash.Hash) {
l.mtx.RLock()
defer l.mtx.RUnlock()

delete(l.m, *k)
}

func lowIQMapNewCount(count int) (*lowIQMap, error) {
if count <= 0 {
return nil, fmt.Errorf("invalid size %v", count)
return nil, fmt.Errorf("invalid count %v", count)
}
return &lowIQMap{
count: count,
Expand All @@ -69,5 +76,5 @@ func lowIQMapNewSize(size int) (*lowIQMap, error) {
return nil, fmt.Errorf("invalid size %v", size)
}
// approximate number of headers
return lowIQMapNewCount(blockHeaderSize / size)
return lowIQMapNewCount(size / blockHeaderSize)
}
6 changes: 5 additions & 1 deletion database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func New(ctx context.Context, cfg *Config) (*ldb, error) {
if cfg.blockheaderCacheSize > 0 {
l.headerCache, err = lowIQMapNewSize(cfg.blockheaderCacheSize)
if err != nil {
panic(spew.Sdump(cfg))
return nil, fmt.Errorf("couldn't setup block header cache: %w", err)

Check failure on line 165 in database/tbcd/level/level.go

View workflow job for this annotation

GitHub Actions / Lint

unreachable: unreachable code (govet)
}

log.Infof("blockheader cache: %v",
humanize.Bytes(uint64(cfg.blockheaderCacheSize)))
} else {
Expand Down Expand Up @@ -845,6 +845,10 @@ func (l *ldb) BlockHeadersRemove(ctx context.Context, bhs *wire.MsgHeaders, tipA
bhash := headersParsed[i].BlockHash()
fh := fullHeadersFromDb[i]
bhsBatch.Delete(bhash[:])
// Make batch
if l.cfg.blockheaderCacheSize > 0 {
l.headerCache.Purge(&bhash)
}

// Delete height mapping for header i
hhKey := heightHashToKey(fh.Height, bhash[:])
Expand Down
7 changes: 0 additions & 7 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,6 @@ func NewServer(cfg *Config) (*Server, error) {
if cfg == nil {
cfg = NewDefaultConfig()
}
if cfg.BlockheaderCacheSize != "" {
var err error
cfg.blockheaderCacheSize, err = humanize.ParseBytes(cfg.BlockheaderCacheSize)
if err != nil {
return nil, fmt.Errorf("invalid blockheader cache size: %w", err)
}
}

if cfg.MempoolEnabled {
log.Infof("mempool forced disabled")
Expand Down
2 changes: 1 addition & 1 deletion service/tbc/tbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ func createTbcServerExternalHeaderMode(ctx context.Context, t *testing.T) *Serve
cfg.ExternalHeaderMode = true
cfg.Network = networkLocalnet
cfg.BlockCache = 0
cfg.BlockheaderCacheSize = "1mb"
cfg.BlockheaderCacheSize = ""
cfg.MempoolEnabled = false

tbcServer, err := NewServer(cfg)
Expand Down

0 comments on commit ca32e24

Please sign in to comment.