Skip to content

Commit

Permalink
Merge pull request lightninglabs#1169 from lightninglabs/universe-cache
Browse files Browse the repository at this point in the history
[multiverse RPC]: add better universe root node cache
  • Loading branch information
Roasbeef authored Nov 19, 2024
2 parents a304937 + 67b1928 commit 77e8b28
Show file tree
Hide file tree
Showing 14 changed files with 1,305 additions and 499 deletions.
11 changes: 11 additions & 0 deletions chain_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ const (
maxNumBlocksInCache = 100_000
)

// cacheableTimestamp is a wrapper around an uint32 that can be used as a value
// in an LRU cache.
type cacheableTimestamp uint32

// Size returns the size of the cacheable timestamp. Since we scale the cache by
// the number of items and not the total memory size, we can simply return 1
// here to count each timestamp as 1 item.
func (c cacheableTimestamp) Size() (uint64, error) {
return 1, nil
}

// LndRpcChainBridge is an implementation of the tapgarden.ChainBridge
// interface backed by an active remote lnd node.
type LndRpcChainBridge struct {
Expand Down
19 changes: 5 additions & 14 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ const (
)

type (
// cacheableTimestamp is a wrapper around a uint32 that can be used as a
// value in an LRU cache.
cacheableTimestamp uint32

// devSendEventStream is a type alias for the asset send event
// notification stream.
devSendEventStream = tapdevrpc.TapDev_SubscribeSendAssetEventNtfnsServer
Expand Down Expand Up @@ -148,13 +144,6 @@ type (
}
)

// Size returns the size of the cacheable timestamp. Since we scale the cache by
// the number of items and not the total memory size, we can simply return 1
// here to count each timestamp as 1 item.
func (c cacheableTimestamp) Size() (uint64, error) {
return 1, nil
}

// rpcServer is the main RPC server for the Taproot Assets daemon that handles
// gRPC/REST/Websockets incoming requests.
type rpcServer struct {
Expand Down Expand Up @@ -4592,7 +4581,9 @@ func (r *rpcServer) AssetRoots(ctx context.Context,
}

resp := &unirpc.AssetRootResponse{
UniverseRoots: make(map[string]*unirpc.UniverseRoot),
UniverseRoots: make(
map[string]*unirpc.UniverseRoot, len(assetRoots),
),
}

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

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

if req.Limit > universe.MaxPageSize || req.Limit < 0 {
return nil, fmt.Errorf("invalid request limit")
return nil, fmt.Errorf("invalid request limit: %d", req.Limit)
}

// Check the rate limiter to see if we need to wait at all. If not then
Expand Down
22 changes: 22 additions & 0 deletions sample-tapd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@
; The burst budget for the universe query rate limiting
; universe.req-burst-budget=10

[multiverse-caches]

; The number of proofs that are cached per universe. (default: 5)
; universe.multiverse-caches.proofs-per-universe=5

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

; The number of leaf keys that are cached per cached universe. (default: 50)
; universe.multiverse-caches.leaves-per-universe=50

; If the syncer cache is enabled.
; universe.multiverse-caches.syncer-cache-enabled=false

; The pre-allocated size of the syncer cache. (default: 100000)
; universe.multiverse-caches.syncer-cache-pre-alloc-size=100000

; The size of the root node page cache for all requests that aren't served by
; the syncer cache. (default: 10240)
; universe.multiverse-caches.root-node-page-cache-size=10240


[address]

; If true, tapd will not try to sync issuance proofs for unknown assets when
Expand Down
6 changes: 6 additions & 0 deletions tapcfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/jessevdk/go-flags"
"github.com/lightninglabs/lndclient"
tap "github.com/lightninglabs/taproot-assets"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/monitoring"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfq"
Expand Down Expand Up @@ -286,6 +287,8 @@ type UniverseConfig struct {
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."`

UniverseQueriesBurst int `long:"req-burst-budget" description:"The burst budget for the universe query rate limiting."`

MultiverseCaches *tapdb.MultiverseCacheConfig `group:"multiverse-caches" namespace:"multiverse-caches"`
}

// AddrBookConfig is the config that houses any address Book related config
Expand Down Expand Up @@ -431,6 +434,9 @@ func DefaultConfig() Config {
defaultUniverseMaxQps,
),
UniverseQueriesBurst: defaultUniverseQueriesBurst,
MultiverseCaches: fn.Ptr(
tapdb.DefaultMultiverseCacheConfig(),
),
},
AddrBook: &AddrBookConfig{
DisableSyncer: false,
Expand Down
4 changes: 3 additions & 1 deletion tapcfg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
return db.WithTx(tx)
},
)
multiverse := tapdb.NewMultiverseStore(multiverseDB)
multiverse := tapdb.NewMultiverseStore(
multiverseDB, tapdb.DefaultMultiverseStoreConfig(),
)

uniStatsDB := tapdb.NewTransactionExecutor(
db, func(tx *sql.Tx) tapdb.UniverseStatsStore {
Expand Down
Loading

0 comments on commit 77e8b28

Please sign in to comment.