Skip to content

Commit f273430

Browse files
committed
tapdb: replace confg arg proof count cache limit with size-based limit
- Replace `proofs-per-universe` configuration with `max-proof-cache-size`, allowing proof cache limits to be defined by total memory size instead of proof count. - Add logic to parse and handle human-readable size values (e.g., "32MB"). - Update default configuration and related comments. - Adjust `NewMultiverseStore` to use parsed cache size values and improve error handling.
1 parent 1b8a8a4 commit f273430

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

sample-tapd.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,9 @@
388388

389389
[multiverse-caches]
390390

391-
; The number of proofs that are cached per universe. (default: 5)
392-
; universe.multiverse-caches.proofs-per-universe=5
391+
; The maximum total size of the cached proofs. Accepts human readable values
392+
; such as 32MB or 1GB. (default: 32MB)
393+
; universe.multiverse-caches.max-proof-cache-size=32MB
393394

394395
; The number of universes that can have a cache of leaf keys. (default: 2000)
395396
; universe.multiverse-caches.leaves-num-cached-universes=2000

tapdb/multiverse.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ type MultiverseStore struct {
144144
func NewMultiverseStore(db BatchedMultiverse,
145145
cfg *MultiverseStoreConfig) (*MultiverseStore, error) {
146146

147+
proofCacheSize, err := cfg.Caches.maxProofCacheSizeBytes()
148+
if err != nil {
149+
return nil, fmt.Errorf("parse max proof cache size: %w", err)
150+
}
151+
147152
return &MultiverseStore{
148153
db: db,
149154
cfg: cfg,
@@ -154,9 +159,7 @@ func NewMultiverseStore(db BatchedMultiverse,
154159
rootNodeCache: newRootNodeCache(
155160
cfg.Caches.RootNodePageCacheSize,
156161
),
157-
proofCache: newUniverseProofCache(
158-
cfg.Caches.ProofsPerUniverse,
159-
),
162+
proofCache: newUniverseProofCache(proofCacheSize),
160163
leafKeysCache: newUniverseLeafPageCache(
161164
cfg.Caches.LeavesNumCachedUniverses,
162165
cfg.Caches.LeavesPerUniverse,

tapdb/multiverse_cache.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@ package tapdb
22

33
import (
44
"bytes"
5+
"fmt"
56
"slices"
67
"sort"
78
"sync"
89
"sync/atomic"
910

11+
"github.com/dustin/go-humanize"
1012
"github.com/lightninglabs/neutrino/cache/lru"
1113
"github.com/lightninglabs/taproot-assets/universe"
1214
"github.com/lightningnetwork/lnd/lnutils"
1315
)
1416

17+
const (
18+
// defaultMaxProofCacheSize is the default maximum size of the proof
19+
// cache expressed as a human-readable string type so that we can
20+
// present it directly to CLI users.
21+
defaultMaxProofCacheSize = "32MB"
22+
)
23+
1524
// MultiverseCacheConfig is the configuration for the different multiverse
1625
// caches that exist.
1726
//
1827
//nolint:lll
1928
type MultiverseCacheConfig struct {
20-
// ProofsPerUniverse is the number of proofs that are cached per
21-
// universe. This number needs to be multiplied by the total number of
22-
// universes to get the total number of proofs that are cached. There is
23-
// no limit to the number of universes that can hold cached keys, so a
24-
// cache is created for each universe that receives a request.
25-
ProofsPerUniverse uint64 `long:"proofs-per-universe" description:"The number of proofs that are cached per universe."`
29+
// MaxProofCacheSize is the maximum size of the proof cache expressed
30+
// as a human-readable string, for example, "32MB" or "1GB".
31+
MaxProofCacheSize string `long:"max-proof-cache-size" description:"The maximum total size of the cached proofs. Accepts human readable values such as 32MB or 1GB."`
2632

2733
// LeavesNumCachedUniverses is the number of universes that can have a
2834
// cache of leaf keys. Each cached universe can have up to
@@ -56,7 +62,7 @@ type MultiverseCacheConfig struct {
5662
// multiverse cache.
5763
func DefaultMultiverseCacheConfig() MultiverseCacheConfig {
5864
return MultiverseCacheConfig{
59-
ProofsPerUniverse: 5,
65+
MaxProofCacheSize: defaultMaxProofCacheSize,
6066
LeavesNumCachedUniverses: 2_000,
6167
LeavesPerUniverse: 50,
6268
SyncerCacheEnabled: false,
@@ -65,6 +71,22 @@ func DefaultMultiverseCacheConfig() MultiverseCacheConfig {
6571
}
6672
}
6773

74+
// maxProofCacheSizeBytes returns the parsed byte representation of the proof
75+
// cache size limit.
76+
func (c MultiverseCacheConfig) maxProofCacheSizeBytes() (uint64, error) {
77+
sizeStr := c.MaxProofCacheSize
78+
if sizeStr == "" {
79+
sizeStr = defaultMaxProofCacheSize
80+
}
81+
82+
sizeBytes, err := humanize.ParseBytes(sizeStr)
83+
if err != nil {
84+
return 0, fmt.Errorf("parse max proof cache size: %w", err)
85+
}
86+
87+
return sizeBytes, nil
88+
}
89+
6890
// cachedProofs is a list of cached proof leaves.
6991
type cachedProofs []*universe.Proof
7092

0 commit comments

Comments
 (0)