Skip to content

Commit 77e8b28

Browse files
authored
Merge pull request #1169 from lightninglabs/universe-cache
[multiverse RPC]: add better universe root node cache
2 parents a304937 + 67b1928 commit 77e8b28

File tree

14 files changed

+1305
-499
lines changed

14 files changed

+1305
-499
lines changed

chain_bridge.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ const (
3131
maxNumBlocksInCache = 100_000
3232
)
3333

34+
// cacheableTimestamp is a wrapper around an uint32 that can be used as a value
35+
// in an LRU cache.
36+
type cacheableTimestamp uint32
37+
38+
// Size returns the size of the cacheable timestamp. Since we scale the cache by
39+
// the number of items and not the total memory size, we can simply return 1
40+
// here to count each timestamp as 1 item.
41+
func (c cacheableTimestamp) Size() (uint64, error) {
42+
return 1, nil
43+
}
44+
3445
// LndRpcChainBridge is an implementation of the tapgarden.ChainBridge
3546
// interface backed by an active remote lnd node.
3647
type LndRpcChainBridge struct {

rpcserver.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ const (
100100
)
101101

102102
type (
103-
// cacheableTimestamp is a wrapper around a uint32 that can be used as a
104-
// value in an LRU cache.
105-
cacheableTimestamp uint32
106-
107103
// devSendEventStream is a type alias for the asset send event
108104
// notification stream.
109105
devSendEventStream = tapdevrpc.TapDev_SubscribeSendAssetEventNtfnsServer
@@ -148,13 +144,6 @@ type (
148144
}
149145
)
150146

151-
// Size returns the size of the cacheable timestamp. Since we scale the cache by
152-
// the number of items and not the total memory size, we can simply return 1
153-
// here to count each timestamp as 1 item.
154-
func (c cacheableTimestamp) Size() (uint64, error) {
155-
return 1, nil
156-
}
157-
158147
// rpcServer is the main RPC server for the Taproot Assets daemon that handles
159148
// gRPC/REST/Websockets incoming requests.
160149
type rpcServer struct {
@@ -4592,7 +4581,9 @@ func (r *rpcServer) AssetRoots(ctx context.Context,
45924581
}
45934582

45944583
resp := &unirpc.AssetRootResponse{
4595-
UniverseRoots: make(map[string]*unirpc.UniverseRoot),
4584+
UniverseRoots: make(
4585+
map[string]*unirpc.UniverseRoot, len(assetRoots),
4586+
),
45964587
}
45974588

45984589
// Retrieve config for use in filtering asset roots based on sync export
@@ -4770,7 +4761,7 @@ func (r *rpcServer) QueryAssetRoots(ctx context.Context,
47704761
return nil, err
47714762
}
47724763

4773-
// Query for both a issaunce and transfer universe root.
4764+
// Query for both an issuance and transfer universe root.
47744765
assetRoots, err := r.queryAssetProofRoots(ctx, universeID)
47754766
if err != nil {
47764767
return nil, err
@@ -4960,7 +4951,7 @@ func (r *rpcServer) AssetLeafKeys(ctx context.Context,
49604951
}
49614952

49624953
if req.Limit > universe.MaxPageSize || req.Limit < 0 {
4963-
return nil, fmt.Errorf("invalid request limit")
4954+
return nil, fmt.Errorf("invalid request limit: %d", req.Limit)
49644955
}
49654956

49664957
// Check the rate limiter to see if we need to wait at all. If not then

sample-tapd.conf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,28 @@
310310
; The burst budget for the universe query rate limiting
311311
; universe.req-burst-budget=10
312312

313+
[multiverse-caches]
314+
315+
; The number of proofs that are cached per universe. (default: 5)
316+
; universe.multiverse-caches.proofs-per-universe=5
317+
318+
; The number of universes that can have a cache of leaf keys. (default: 2000)
319+
; universe.multiverse-caches.leaves-num-cached-universes=2000
320+
321+
; The number of leaf keys that are cached per cached universe. (default: 50)
322+
; universe.multiverse-caches.leaves-per-universe=50
323+
324+
; If the syncer cache is enabled.
325+
; universe.multiverse-caches.syncer-cache-enabled=false
326+
327+
; The pre-allocated size of the syncer cache. (default: 100000)
328+
; universe.multiverse-caches.syncer-cache-pre-alloc-size=100000
329+
330+
; The size of the root node page cache for all requests that aren't served by
331+
; the syncer cache. (default: 10240)
332+
; universe.multiverse-caches.root-node-page-cache-size=10240
333+
334+
313335
[address]
314336

315337
; If true, tapd will not try to sync issuance proofs for unknown assets when

tapcfg/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jessevdk/go-flags"
2222
"github.com/lightninglabs/lndclient"
2323
tap "github.com/lightninglabs/taproot-assets"
24+
"github.com/lightninglabs/taproot-assets/fn"
2425
"github.com/lightninglabs/taproot-assets/monitoring"
2526
"github.com/lightninglabs/taproot-assets/proof"
2627
"github.com/lightninglabs/taproot-assets/rfq"
@@ -286,6 +287,8 @@ type UniverseConfig struct {
286287
UniverseQueriesPerSecond rate.Limit `long:"max-qps" description:"The maximum number of queries per second across the set of active universe queries that is permitted. Anything above this starts to get rate limited."`
287288

288289
UniverseQueriesBurst int `long:"req-burst-budget" description:"The burst budget for the universe query rate limiting."`
290+
291+
MultiverseCaches *tapdb.MultiverseCacheConfig `group:"multiverse-caches" namespace:"multiverse-caches"`
289292
}
290293

291294
// AddrBookConfig is the config that houses any address Book related config
@@ -431,6 +434,9 @@ func DefaultConfig() Config {
431434
defaultUniverseMaxQps,
432435
),
433436
UniverseQueriesBurst: defaultUniverseQueriesBurst,
437+
MultiverseCaches: fn.Ptr(
438+
tapdb.DefaultMultiverseCacheConfig(),
439+
),
434440
},
435441
AddrBook: &AddrBookConfig{
436442
DisableSyncer: false,

tapcfg/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
126126
return db.WithTx(tx)
127127
},
128128
)
129-
multiverse := tapdb.NewMultiverseStore(multiverseDB)
129+
multiverse := tapdb.NewMultiverseStore(
130+
multiverseDB, tapdb.DefaultMultiverseStoreConfig(),
131+
)
130132

131133
uniStatsDB := tapdb.NewTransactionExecutor(
132134
db, func(tx *sql.Tx) tapdb.UniverseStatsStore {

0 commit comments

Comments
 (0)