@@ -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.
344364type 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.
382411type 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//
@@ -3280,6 +3349,45 @@ func (a *AssetStore) FetchAssetMetaForAsset(ctx context.Context,
32803349 return assetMeta , nil
32813350}
32823351
3352+ // AssetsDBSize returns the total size of the taproot assets database.
3353+ func (a * AssetStore ) AssetsDBSize (ctx context.Context ) (int64 , error ) {
3354+ var totalSize int64
3355+
3356+ readOpts := NewAssetStoreReadTx ()
3357+ dbErr := a .metaDb .ExecTx (ctx , & readOpts , func (q MetaStore ) error {
3358+ var (
3359+ size int64
3360+ err error
3361+ )
3362+ switch a .dbType {
3363+ case sqlc .BackendTypePostgres :
3364+ size , err = q .AssetsDBSizePostgres (ctx )
3365+
3366+ case sqlc .BackendTypeSqlite :
3367+ var res int32
3368+ res , err = q .AssetsDBSizeSqlite (ctx )
3369+ size = int64 (res )
3370+
3371+ default :
3372+ return fmt .Errorf ("unsupported db backend type" )
3373+ }
3374+
3375+ if err != nil {
3376+ return err
3377+ }
3378+
3379+ totalSize = size
3380+
3381+ return nil
3382+ })
3383+
3384+ if dbErr != nil {
3385+ return 0 , dbErr
3386+ }
3387+
3388+ return totalSize , nil
3389+ }
3390+
32833391// FetchAssetMetaByHash attempts to fetch an asset meta based on an asset hash.
32843392func (a * AssetStore ) FetchAssetMetaByHash (ctx context.Context ,
32853393 metaHash [asset .MetaHashLen ]byte ) (* proof.MetaReveal , error ) {
0 commit comments