Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tbcd: fix cache settings #375

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/hemictl/hemictl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -305,7 +305,7 @@ func tbcdb() error {

levelDBHome := "~/.tbcd" // XXX
network := "testnet3"
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network)))
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network), "1mb", "128mb"))
if err != nil {
return err
}
Expand All @@ -322,7 +322,7 @@ func tbcdb() error {

levelDBHome := "~/.tbcd" // XXX
network := "testnet3"
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network)))
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network), "1mb", "128mb"))
if err != nil {
return err
}
Expand All @@ -340,7 +340,7 @@ func tbcdb() error {

levelDBHome := "~/.tbcd" // XXX
network := "testnet3"
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network)))
db, err := level.New(ctx, level.NewConfig(filepath.Join(levelDBHome, network), "1mb", "128mb"))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const (
defaultLogLevel = daemonName + "=INFO;tbc=INFO;level=INFO"
defaultNetwork = "testnet3" // XXX make this mainnet
defaultHome = "~/." + daemonName
bDefaultSize = "1gb" // ~640 blocks on mainnet
bhsDefaultSize = "128mb" // enough for mainnet
bDefaultSize = "512mb" // ~320 blocks on mainnet
bhsDefaultSize = "2mb"
)

var (
Expand Down
25 changes: 7 additions & 18 deletions database/tbcd/level/blockcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"sync"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"

"github.com/hemilabs/heminetwork/database/tbcd"
Expand Down Expand Up @@ -37,23 +36,18 @@ type lowIQLRU struct {
c tbcd.CacheStats
}

func (l *lowIQLRU) Put(v *btcutil.Block) {
func (l *lowIQLRU) Put(hash *chainhash.Hash, block []byte) {
l.mtx.Lock()
defer l.mtx.Unlock()

hash := v.Hash()
if _, ok := l.m[*hash]; ok {
if be, ok := l.m[*hash]; ok {
// update access
l.l.MoveToBack(be.element)
return
}

block, err := v.Bytes()
if err != nil {
// data corruption, panic
panic(err)
}

// evict first element in list
if l.totalSize+len(block) > l.size {
for l.totalSize+len(block) > l.size {
// LET THEM EAT PANIC
re := l.l.Front()
rha := l.l.Remove(re)
Expand All @@ -70,7 +64,7 @@ func (l *lowIQLRU) Put(v *btcutil.Block) {
l.c.Size = l.totalSize
}

func (l *lowIQLRU) Get(k *chainhash.Hash) (*btcutil.Block, bool) {
func (l *lowIQLRU) Get(k *chainhash.Hash) ([]byte, bool) {
l.mtx.Lock()
defer l.mtx.Unlock()

Expand All @@ -79,18 +73,13 @@ func (l *lowIQLRU) Get(k *chainhash.Hash) (*btcutil.Block, bool) {
l.c.Misses++
return nil, false
}
b, err := btcutil.NewBlockFromBytes(be.block)
if err != nil {
// panic for diagnostics at this time
panic(err)
}

// update access
l.l.MoveToBack(be.element)

l.c.Hits++

return b, true
return be.block, true
}

func (l *lowIQLRU) Stats() tbcd.CacheStats {
Expand Down
17 changes: 11 additions & 6 deletions database/tbcd/level/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import (
"github.com/hemilabs/heminetwork/database/tbcd"
)

func newBlock(prevHash *chainhash.Hash, nonce uint32) (chainhash.Hash, *btcutil.Block) {
func newBlock(prevHash *chainhash.Hash, nonce uint32) (chainhash.Hash, *btcutil.Block, []byte) {
bh := wire.NewBlockHeader(0, prevHash, &chainhash.Hash{}, 0, uint32(nonce))
b := wire.NewMsgBlock(bh)
return bh.BlockHash(), btcutil.NewBlock(b)
ub := btcutil.NewBlock(b)
r, err := ub.Bytes()
if err != nil {
panic(err)
}
return bh.BlockHash(), ub, r
}

func TestLRUCache(t *testing.T) {
Expand All @@ -29,10 +34,10 @@ func TestLRUCache(t *testing.T) {
prevHash := chainhash.Hash{} // genesis
blocks := make([]chainhash.Hash, 0, maxCache*2)
for i := 0; i < maxCache; i++ {
h, b := newBlock(&prevHash, uint32(i))
h, _, r := newBlock(&prevHash, uint32(i))
t.Logf("%v: %v", i, h)
blocks = append(blocks, h)
l.Put(b)
l.Put(&h, r)
prevHash = h
}

Expand All @@ -57,10 +62,10 @@ func TestLRUCache(t *testing.T) {

// purge oldest cache entries
for i := maxCache; i < maxCache*2; i++ {
h, b := newBlock(&prevHash, uint32(i))
h, _, r := newBlock(&prevHash, uint32(i))
t.Logf("%v: %v", i, h)
blocks = append(blocks, h)
l.Put(b)
l.Put(&h, r)
prevHash = h
}

Expand Down
54 changes: 38 additions & 16 deletions database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ const (

type IteratorError error

var log = loggo.GetLogger("level")
var (
log = loggo.GetLogger("level")

var ErrIterator = IteratorError(errors.New("iteration error"))
ErrIterator = IteratorError(errors.New("iteration error"))

noStats tbcd.CacheStats
)

func init() {
if err := loggo.ConfigureLoggers(logLevel); err != nil {
Expand Down Expand Up @@ -116,8 +120,10 @@ type Config struct {
blockheaderCacheSize int // parsed size of block header cache
}

func NewConfig(home string) *Config {
blockheaderCacheSizeS := "128mb" // Cache all blockheaders on mainnet
func NewConfig(home, blockheaderCacheSizeS, blockCacheSizeS string) *Config {
if blockheaderCacheSizeS == "" {
blockheaderCacheSizeS = "0"
}
blockheaderCacheSize, err := humanize.ParseBytes(blockheaderCacheSizeS)
if err != nil {
panic(err)
Expand All @@ -126,7 +132,9 @@ func NewConfig(home string) *Config {
panic("invalid blockheaderCacheSize")
}

blockCacheSizeS := "1gb" // ~640 blocks on mainnet
if blockCacheSizeS == "" {
blockCacheSizeS = "0"
}
blockCacheSize, err := humanize.ParseBytes(blockCacheSizeS)
if err != nil {
panic(err)
Expand Down Expand Up @@ -1285,7 +1293,7 @@ func (l *ldb) BlockInsert(ctx context.Context, b *btcutil.Block) (int64, error)
return -1, fmt.Errorf("blocks insert put: %w", err)
}
if l.cfg.blockCacheSize > 0 {
l.blockCache.Put(b)
l.blockCache.Put(b.Hash(), raw)
}
}

Expand Down Expand Up @@ -1321,27 +1329,35 @@ func (l *ldb) BlockByHash(ctx context.Context, hash *chainhash.Hash) (*btcutil.B
log.Tracef("BlockByHash")
defer log.Tracef("BlockByHash exit")

// get from cache
var (
eb []byte
err error
)
if l.cfg.blockCacheSize > 0 {
// Try cache first
if cb, ok := l.blockCache.Get(hash); ok {
return cb, nil
}
eb, _ = l.blockCache.Get(hash)
}

bDB := l.rawPool[level.BlocksDB]
eb, err := bDB.Get(hash[:])
if err != nil {
if errors.Is(err, leveldb.ErrNotFound) {
return nil, database.BlockNotFoundError{Hash: *hash}
// get from db
if eb == nil {
bDB := l.rawPool[level.BlocksDB]
eb, err = bDB.Get(hash[:])
if err != nil {
if errors.Is(err, leveldb.ErrNotFound) {
return nil, database.BlockNotFoundError{Hash: *hash}
}
return nil, fmt.Errorf("block get: %w", err)
}
return nil, fmt.Errorf("block get: %w", err)
}

// if we get here eb MUST exist
b, err := btcutil.NewBlockFromBytes(eb)
if err != nil {
panic(fmt.Errorf("block decode data corruption: %v %w", hash, err))
}
if l.cfg.blockCacheSize > 0 {
l.blockCache.Put(b)
l.blockCache.Put(hash, eb)
}
return b, nil
}
Expand Down Expand Up @@ -1657,9 +1673,15 @@ func (l *ldb) BlockTxUpdate(ctx context.Context, direction int, txs map[tbcd.TxK
}

func (l *ldb) BlockHeaderCacheStats() tbcd.CacheStats {
if l.cfg.blockheaderCacheSize == 0 {
return noStats
}
return l.headerCache.Stats()
}

func (l *ldb) BlockCacheStats() tbcd.CacheStats {
if l.cfg.blockCacheSize == 0 {
return noStats
}
return l.blockCache.Stats()
}
2 changes: 1 addition & 1 deletion database/tbcd/level/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestMD(t *testing.T) {
home := t.TempDir()
t.Logf("temp: %v", home)

cfg := level.NewConfig(home)
cfg := level.NewConfig(home, "128kb", "1m")
db, err := level.New(ctx, cfg)
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 2 additions & 3 deletions service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2211,9 +2211,8 @@ func (s *Server) DBOpen(ctx context.Context) error {

// Open db.
var err error
cfg := level.NewConfig(filepath.Join(s.cfg.LevelDBHome, s.cfg.Network))
cfg.BlockCacheSize = s.cfg.BlockCacheSize
cfg.BlockheaderCacheSize = s.cfg.BlockheaderCacheSize
cfg := level.NewConfig(filepath.Join(s.cfg.LevelDBHome, s.cfg.Network),
s.cfg.BlockheaderCacheSize, s.cfg.BlockCacheSize)
s.db, err = level.New(ctx, cfg)
if err != nil {
return fmt.Errorf("open level database: %w", err)
Expand Down
Loading