Skip to content

Commit 050dceb

Browse files
committed
tapdb: add meta store and metrics queries
1 parent 4925c31 commit 050dceb

File tree

10 files changed

+244
-7
lines changed

10 files changed

+244
-7
lines changed

tapcfg/server.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,25 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
4242
lndServices *lndclient.LndServices, enableChannelFeatures bool,
4343
mainErrChan chan<- error) (*tap.Config, error) {
4444

45-
var err error
45+
var (
46+
err error
47+
db databaseBackend
48+
dbType sqlc.BackendType
49+
)
4650

4751
// Now that we know where the database will live, we'll go ahead and
4852
// open up the default implementation of it.
49-
var db databaseBackend
5053
switch cfg.DatabaseBackend {
5154
case DatabaseBackendSqlite:
55+
dbType = sqlc.BackendTypeSqlite
56+
5257
cfgLogger.Infof("Opening sqlite3 database at: %v",
5358
cfg.Sqlite.DatabaseFileName)
5459
db, err = tapdb.NewSqliteStore(cfg.Sqlite)
5560

5661
case DatabaseBackendPostgres:
62+
dbType = sqlc.BackendTypePostgres
63+
5764
cfgLogger.Infof("Opening postgres database at: %v",
5865
cfg.Postgres.DSN(true))
5966
db, err = tapdb.NewPostgresStore(cfg.Postgres)
@@ -85,6 +92,12 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
8592
},
8693
)
8794

95+
metaDB := tapdb.NewTransactionExecutor(
96+
db, func(tx *sql.Tx) tapdb.MetaStore {
97+
return db.WithTx(tx)
98+
},
99+
)
100+
88101
addrBookDB := tapdb.NewTransactionExecutor(
89102
db, func(tx *sql.Tx) tapdb.AddrBook {
90103
return db.WithTx(tx)
@@ -94,7 +107,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
94107
tapdbAddrBook := tapdb.NewTapAddressBook(
95108
addrBookDB, &tapChainParams, defaultClock,
96109
)
97-
assetStore := tapdb.NewAssetStore(assetDB, defaultClock)
110+
assetStore := tapdb.NewAssetStore(assetDB, metaDB, defaultClock, dbType)
98111

99112
keyRing := tap.NewLndRpcKeyRing(lndServices)
100113
walletAnchor := tap.NewLndRpcWalletAnchor(lndServices)

tapdb/asset_minting_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ func newAssetStoreFromDB(db *BaseDB) (*AssetMintingStore, *AssetStore) {
5454
return db.WithTx(tx)
5555
}
5656

57+
metaTxCreator := func(tx *sql.Tx) MetaStore {
58+
return db.WithTx(tx)
59+
}
60+
5761
assetMintingDB := NewTransactionExecutor(db, txCreator)
5862
assetsDB := NewTransactionExecutor(db, activeTxCreator)
63+
metaDB := NewTransactionExecutor(db, metaTxCreator)
64+
5965
testClock := clock.NewTestClock(time.Now())
6066

6167
return NewAssetMintingStore(assetMintingDB),
62-
NewAssetStore(assetsDB, testClock)
68+
NewAssetStore(assetsDB, metaDB, testClock, db.Backend())
6369
}
6470

6571
func assertBatchState(t *testing.T, batch *tapgarden.MintingBatch,

tapdb/assets_store.go

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type (
4343
// script key.
4444
AssetProof = sqlc.FetchAssetProofsRow
4545

46+
// AssetProofSize is the asset proof size for a given asset, identified
47+
// by its script key.
48+
AssetProofSize = sqlc.FetchAssetProofsSizesRow
49+
4650
// AssetProofI is identical to AssetProof but is used for the case
4751
// where the proofs for a specific asset are fetched.
4852
AssetProofI = sqlc.FetchAssetProofRow
@@ -195,6 +199,10 @@ type ActiveAssetsStore interface {
195199
// disk.
196200
FetchAssetProofs(ctx context.Context) ([]AssetProof, error)
197201

202+
// FetchAssetsProofsSizes fetches all the asset proofs lengths that are
203+
// stored on disk.
204+
FetchAssetProofsSizes(ctx context.Context) ([]AssetProofSize, error)
205+
198206
// FetchAssetProof fetches the asset proof for a given asset identified
199207
// by its script key.
200208
FetchAssetProof(ctx context.Context,
@@ -339,6 +347,18 @@ type ActiveAssetsStore interface {
339347
assetID []byte) (sqlc.FetchAssetMetaForAssetRow, error)
340348
}
341349

350+
// MetaStore is a sub-set of the main sqlc.Querier interface that contains
351+
// methods related to metadata of the daemon.
352+
type MetaStore interface {
353+
// AssetsDBSize returns the total size of the taproot assets sqlite
354+
// database.
355+
AssetsDBSizeSqlite(ctx context.Context) (int32, error)
356+
357+
// AssetsDBSize returns the total size of the taproot assets postgres
358+
// database.
359+
AssetsDBSizePostgres(ctx context.Context) (int64, error)
360+
}
361+
342362
// AssetBalance holds a balance query result for a particular asset or all
343363
// assets tracked by this daemon.
344364
type AssetBalance struct {
@@ -378,29 +398,46 @@ type BatchedAssetStore interface {
378398
BatchedTx[ActiveAssetsStore]
379399
}
380400

401+
// BatchedMetaStore combines the MetaStore interface with the BatchedTx
402+
// interface, allowing for multiple queries to be executed in a single SQL
403+
// transaction.
404+
type BatchedMetaStore interface {
405+
MetaStore
406+
407+
BatchedTx[MetaStore]
408+
}
409+
381410
// AssetStore is used to query for the set of pending and confirmed assets.
382411
type AssetStore struct {
383412
db BatchedAssetStore
384413

414+
metaDb BatchedMetaStore
415+
385416
// eventDistributor is an event distributor that will be used to notify
386417
// subscribers about new proofs that are added to the archiver.
387418
eventDistributor *fn.EventDistributor[proof.Blob]
388419

389420
clock clock.Clock
390421

391422
txHeights *lru.Cache[chainhash.Hash, cacheableBlockHeight]
423+
424+
dbType sqlc.BackendType
392425
}
393426

394427
// NewAssetStore creates a new AssetStore from the specified BatchedAssetStore
395428
// interface.
396-
func NewAssetStore(db BatchedAssetStore, clock clock.Clock) *AssetStore {
429+
func NewAssetStore(db BatchedAssetStore, metaDB BatchedMetaStore,
430+
clock clock.Clock, dbType sqlc.BackendType) *AssetStore {
431+
397432
return &AssetStore{
398433
db: db,
434+
metaDb: metaDB,
399435
eventDistributor: fn.NewEventDistributor[proof.Blob](),
400436
clock: clock,
401437
txHeights: lru.NewCache[chainhash.Hash, cacheableBlockHeight](
402438
10_000,
403439
),
440+
dbType: dbType,
404441
}
405442
}
406443

@@ -1171,6 +1208,38 @@ func (a *AssetStore) FetchManagedUTXOs(ctx context.Context) (
11711208
return managedUtxos, nil
11721209
}
11731210

1211+
// FetchAssetProofsSizes fetches the sizes of the proofs in the db.
1212+
func (a *AssetStore) FetchAssetProofsSizes(
1213+
ctx context.Context) ([]AssetProofSize, error) {
1214+
1215+
var pSizes []AssetProofSize
1216+
1217+
readOpts := NewAssetStoreReadTx()
1218+
dbErr := a.db.ExecTx(ctx, &readOpts, func(q ActiveAssetsStore) error {
1219+
proofSizes, err := q.FetchAssetProofsSizes(ctx)
1220+
if err != nil {
1221+
return err
1222+
}
1223+
1224+
for _, v := range proofSizes {
1225+
pSizes = append(
1226+
pSizes, AssetProofSize{
1227+
ScriptKey: v.ScriptKey,
1228+
ProofFileLength: v.ProofFileLength,
1229+
},
1230+
)
1231+
}
1232+
1233+
return nil
1234+
})
1235+
1236+
if dbErr != nil {
1237+
return nil, dbErr
1238+
}
1239+
1240+
return pSizes, nil
1241+
}
1242+
11741243
// FetchAssetProofs returns the latest proof file for either the set of target
11751244
// assets, or all assets if no script keys for an asset are passed in.
11761245
//
@@ -3267,6 +3336,45 @@ func (a *AssetStore) FetchAssetMetaForAsset(ctx context.Context,
32673336
return assetMeta, nil
32683337
}
32693338

3339+
// AssetsDBSize returns the total size of the taproot assets database.
3340+
func (a *AssetStore) AssetsDBSize(ctx context.Context) (int64, error) {
3341+
var totalSize int64
3342+
3343+
readOpts := NewAssetStoreReadTx()
3344+
dbErr := a.metaDb.ExecTx(ctx, &readOpts, func(q MetaStore) error {
3345+
var (
3346+
size int64
3347+
err error
3348+
)
3349+
switch a.dbType {
3350+
case sqlc.BackendTypePostgres:
3351+
size, err = q.AssetsDBSizePostgres(ctx)
3352+
3353+
case sqlc.BackendTypeSqlite:
3354+
var res int32
3355+
res, err = q.AssetsDBSizeSqlite(ctx)
3356+
size = int64(res)
3357+
3358+
default:
3359+
return fmt.Errorf("unsupported db backend type")
3360+
}
3361+
3362+
if err != nil {
3363+
return err
3364+
}
3365+
3366+
totalSize = size
3367+
3368+
return nil
3369+
})
3370+
3371+
if dbErr != nil {
3372+
return 0, dbErr
3373+
}
3374+
3375+
return totalSize, nil
3376+
}
3377+
32703378
// FetchAssetMetaByHash attempts to fetch an asset meta based on an asset hash.
32713379
func (a *AssetStore) FetchAssetMetaByHash(ctx context.Context,
32723380
metaHash [asset.MetaHashLen]byte) (*proof.MetaReveal, error) {

tapdb/sqlc/assets.sql.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/metadata.sql.go

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/querier.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/assets.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,15 @@ FROM asset_proofs
709709
JOIN asset_info
710710
ON asset_info.asset_id = asset_proofs.asset_id;
711711

712+
-- name: FetchAssetProofsSizes :many
713+
SELECT script_keys.tweaked_script_key AS script_key,
714+
LENGTH(asset_proofs.proof_file) AS proof_file_length
715+
FROM asset_proofs
716+
JOIN assets
717+
ON asset_proofs.asset_id = assets.asset_id
718+
JOIN script_keys
719+
ON assets.script_key_id = script_keys.script_key_id;
720+
712721
-- name: FetchAssetProofsByAssetID :many
713722
WITH asset_info AS (
714723
SELECT assets.asset_id, script_keys.tweaked_script_key

tapdb/sqlc/queries/metadata.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- name: AssetsDBSizePostgres :one
2+
SELECT pg_catalog.pg_database_size(current_database()) AS size;
3+
4+
-- name: AssetsDBSizeSqlite :one
5+
SELECT page_count * page_size AS size_in_bytes
6+
FROM pragma_page_count(), pragma_page_size();

tapdb/sqlutils_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,17 @@ func newDbHandleFromDb(db *BaseDB) *DbHandler {
274274
return db.WithTx(tx)
275275
},
276276
)
277-
activeAssetsStore := NewAssetStore(assetsDB, testClock)
277+
278+
// Gain a handle to the meta store.
279+
metaDB := NewTransactionExecutor(
280+
db, func(tx *sql.Tx) MetaStore {
281+
return db.WithTx(tx)
282+
},
283+
)
284+
285+
activeAssetsStore := NewAssetStore(
286+
assetsDB, metaDB, testClock, db.Backend(),
287+
)
278288

279289
return &DbHandler{
280290
UniverseFederationStore: fedStore,

0 commit comments

Comments
 (0)