Skip to content

Commit 9fe32aa

Browse files
committed
multi: move asset meta retrieval to addr book
To make it possible that we can sync an asset when querying for the asset meta information, we move the two meta related queries to the address book. That way we can use the sync enabled asset meta fetch in a later commit in the channel funding controller.
1 parent bbcab76 commit 9fe32aa

File tree

4 files changed

+148
-87
lines changed

4 files changed

+148
-87
lines changed

address/book.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/lightninglabs/taproot-assets/asset"
1616
"github.com/lightninglabs/taproot-assets/commitment"
1717
"github.com/lightninglabs/taproot-assets/fn"
18+
"github.com/lightninglabs/taproot-assets/proof"
1819
"github.com/lightningnetwork/lnd/keychain"
1920
)
2021

@@ -23,6 +24,10 @@ var (
2324
// This means an address can't be created until a Universe bootstrap or
2425
// manual issuance proof insertion.
2526
ErrAssetGroupUnknown = fmt.Errorf("asset group is unknown")
27+
28+
// ErrAssetMetaNotFound is returned when an asset meta is not found in
29+
// the database.
30+
ErrAssetMetaNotFound = fmt.Errorf("asset meta not found")
2631
)
2732

2833
// AddrWithKeyInfo wraps a normal Taproot Asset struct with key descriptor
@@ -100,6 +105,16 @@ type Storage interface {
100105
// (genesis + group key) associated with a given asset.
101106
QueryAssetGroup(context.Context, asset.ID) (*asset.AssetGroup, error)
102107

108+
// FetchAssetMetaByHash attempts to fetch an asset meta based on an
109+
// asset hash.
110+
FetchAssetMetaByHash(ctx context.Context,
111+
metaHash [asset.MetaHashLen]byte) (*proof.MetaReveal, error)
112+
113+
// FetchAssetMetaForAsset attempts to fetch an asset meta based on an
114+
// asset ID.
115+
FetchAssetMetaForAsset(ctx context.Context,
116+
assetID asset.ID) (*proof.MetaReveal, error)
117+
103118
// AddrByTaprootOutput returns a single address based on its Taproot
104119
// output key or a sql.ErrNoRows error if no such address exists.
105120
AddrByTaprootOutput(ctx context.Context,
@@ -218,7 +233,7 @@ func (b *Book) QueryAssetInfo(ctx context.Context,
218233
return nil, err
219234
}
220235

221-
log.Debugf("asset %v is unknown, attempting to bootstrap", id.String())
236+
log.Debugf("Asset %v is unknown, attempting to bootstrap", id.String())
222237

223238
// Use the AssetSyncer to query our universe federation for the asset.
224239
err = b.cfg.Syncer.SyncAssetInfo(ctx, &id)
@@ -233,7 +248,7 @@ func (b *Book) QueryAssetInfo(ctx context.Context,
233248
return nil, err
234249
}
235250

236-
log.Debugf("bootstrap succeeded for asset %v", id.String())
251+
log.Debugf("Bootstrap succeeded for asset %v", id.String())
237252

238253
// If the asset was found after sync, and has an asset group, update our
239254
// universe sync config to ensure that we sync future issuance proofs.
@@ -253,6 +268,55 @@ func (b *Book) QueryAssetInfo(ctx context.Context,
253268
return assetGroup, nil
254269
}
255270

271+
// FetchAssetMetaByHash attempts to fetch an asset meta based on an asset hash.
272+
func (b *Book) FetchAssetMetaByHash(ctx context.Context,
273+
metaHash [asset.MetaHashLen]byte) (*proof.MetaReveal, error) {
274+
275+
return b.cfg.Store.FetchAssetMetaByHash(ctx, metaHash)
276+
}
277+
278+
// FetchAssetMetaForAsset attempts to fetch an asset meta based on an asset ID.
279+
func (b *Book) FetchAssetMetaForAsset(ctx context.Context,
280+
assetID asset.ID) (*proof.MetaReveal, error) {
281+
282+
// Check if we know of this meta hash already.
283+
meta, err := b.cfg.Store.FetchAssetMetaForAsset(ctx, assetID)
284+
switch {
285+
case meta != nil:
286+
return meta, nil
287+
288+
// Asset lookup failed gracefully; continue to asset lookup using the
289+
// AssetSyncer if enabled.
290+
case errors.Is(err, ErrAssetMetaNotFound):
291+
if b.cfg.Syncer == nil {
292+
return nil, ErrAssetMetaNotFound
293+
}
294+
295+
case err != nil:
296+
return nil, err
297+
}
298+
299+
log.Debugf("Asset %v is unknown, attempting to bootstrap",
300+
assetID.String())
301+
302+
// Use the AssetSyncer to query our universe federation for the asset.
303+
err = b.cfg.Syncer.SyncAssetInfo(ctx, &assetID)
304+
if err != nil {
305+
return nil, err
306+
}
307+
308+
// The asset meta info may have been synced from a universe server;
309+
// query for the asset ID again.
310+
meta, err = b.cfg.Store.FetchAssetMetaForAsset(ctx, assetID)
311+
if err != nil {
312+
return nil, err
313+
}
314+
315+
log.Debugf("Bootstrap succeeded for asset %v", assetID.String())
316+
317+
return meta, nil
318+
}
319+
256320
// NewAddress creates a new Taproot Asset address based on the input parameters.
257321
func (b *Book) NewAddress(ctx context.Context, addrVersion Version,
258322
assetID asset.ID, amount uint64,

rpcserver.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4407,7 +4407,7 @@ func (r *rpcServer) FetchAssetMeta(ctx context.Context,
44074407
var assetID asset.ID
44084408
copy(assetID[:], req.GetAssetId())
44094409

4410-
assetMeta, err = r.cfg.AssetStore.FetchAssetMetaForAsset(
4410+
assetMeta, err = r.cfg.AddrBook.FetchAssetMetaForAsset(
44114411
ctx, assetID,
44124412
)
44134413

@@ -4426,7 +4426,7 @@ func (r *rpcServer) FetchAssetMeta(ctx context.Context,
44264426
var assetID asset.ID
44274427
copy(assetID[:], assetIDBytes)
44284428

4429-
assetMeta, err = r.cfg.AssetStore.FetchAssetMetaForAsset(
4429+
assetMeta, err = r.cfg.AddrBook.FetchAssetMetaForAsset(
44304430
ctx, assetID,
44314431
)
44324432

@@ -4438,7 +4438,7 @@ func (r *rpcServer) FetchAssetMeta(ctx context.Context,
44384438
var metaHash [asset.MetaHashLen]byte
44394439
copy(metaHash[:], req.GetMetaHash())
44404440

4441-
assetMeta, err = r.cfg.AssetStore.FetchAssetMetaByHash(
4441+
assetMeta, err = r.cfg.AddrBook.FetchAssetMetaByHash(
44424442
ctx, metaHash,
44434443
)
44444444

@@ -4457,7 +4457,7 @@ func (r *rpcServer) FetchAssetMeta(ctx context.Context,
44574457
var metaHash [asset.MetaHashLen]byte
44584458
copy(metaHash[:], metaHashBytes)
44594459

4460-
assetMeta, err = r.cfg.AssetStore.FetchAssetMetaByHash(
4460+
assetMeta, err = r.cfg.AddrBook.FetchAssetMetaByHash(
44614461
ctx, metaHash,
44624462
)
44634463

@@ -7603,7 +7603,7 @@ func encodeVirtualPackets(packets []*tappsbt.VPacket) ([][]byte, error) {
76037603
func (r *rpcServer) DecDisplayForAssetID(ctx context.Context,
76047604
id asset.ID) (fn.Option[uint32], error) {
76057605

7606-
meta, err := r.cfg.AssetStore.FetchAssetMetaForAsset(ctx, id)
7606+
meta, err := r.cfg.AddrBook.FetchAssetMetaForAsset(ctx, id)
76077607
if err != nil {
76087608
return fn.None[uint32](), fmt.Errorf("unable to fetch asset "+
76097609
"meta for asset_id=%v :%v", id, err)

tapdb/addrs.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ type (
7777
// KeyLocator is a type alias for fetching the key locator information
7878
// for an internal key.
7979
KeyLocator = sqlc.FetchInternalKeyLocatorRow
80+
81+
// AssetMeta is the metadata record for an asset.
82+
AssetMeta = sqlc.FetchAssetMetaForAssetRow
8083
)
8184

8285
// AddrBook is an interface that represents the storage backed needed to create
@@ -160,6 +163,14 @@ type AddrBook interface {
160163
// FetchInternalKeyLocator fetches the key locator for an internal key.
161164
FetchInternalKeyLocator(ctx context.Context, rawKey []byte) (KeyLocator,
162165
error)
166+
167+
// FetchAssetMetaByHash fetches the asset meta for a given meta hash.
168+
FetchAssetMetaByHash(ctx context.Context,
169+
metaDataHash []byte) (sqlc.FetchAssetMetaByHashRow, error)
170+
171+
// FetchAssetMetaForAsset fetches the asset meta for a given asset.
172+
FetchAssetMetaForAsset(ctx context.Context,
173+
assetID []byte) (AssetMeta, error)
163174
}
164175

165176
// AddrBookTxOptions defines the set of db txn options the AddrBook
@@ -1104,8 +1115,68 @@ func (t *TapAddressBook) QueryAssetGroup(ctx context.Context,
11041115
return &assetGroup, nil
11051116
}
11061117

1118+
// FetchAssetMetaByHash attempts to fetch an asset meta based on an asset hash.
1119+
func (t *TapAddressBook) FetchAssetMetaByHash(ctx context.Context,
1120+
metaHash [asset.MetaHashLen]byte) (*proof.MetaReveal, error) {
1121+
1122+
var assetMeta *proof.MetaReveal
1123+
1124+
readOpts := NewAssetStoreReadTx()
1125+
dbErr := t.db.ExecTx(ctx, &readOpts, func(q AddrBook) error {
1126+
dbMeta, err := q.FetchAssetMetaByHash(ctx, metaHash[:])
1127+
if err != nil {
1128+
return err
1129+
}
1130+
1131+
assetMeta = &proof.MetaReveal{
1132+
Data: dbMeta.MetaDataBlob,
1133+
Type: proof.MetaType(dbMeta.MetaDataType.Int16),
1134+
}
1135+
1136+
return nil
1137+
})
1138+
switch {
1139+
case errors.Is(dbErr, sql.ErrNoRows):
1140+
return nil, address.ErrAssetMetaNotFound
1141+
case dbErr != nil:
1142+
return nil, dbErr
1143+
}
1144+
1145+
return assetMeta, nil
1146+
}
1147+
1148+
// FetchAssetMetaForAsset attempts to fetch an asset meta based on an asset ID.
1149+
func (t *TapAddressBook) FetchAssetMetaForAsset(ctx context.Context,
1150+
assetID asset.ID) (*proof.MetaReveal, error) {
1151+
1152+
var assetMeta *proof.MetaReveal
1153+
1154+
readOpts := NewAssetStoreReadTx()
1155+
dbErr := t.db.ExecTx(ctx, &readOpts, func(q AddrBook) error {
1156+
dbMeta, err := q.FetchAssetMetaForAsset(ctx, assetID[:])
1157+
if err != nil {
1158+
return err
1159+
}
1160+
1161+
assetMeta = &proof.MetaReveal{
1162+
Data: dbMeta.MetaDataBlob,
1163+
Type: proof.MetaType(dbMeta.MetaDataType.Int16),
1164+
}
1165+
1166+
return nil
1167+
})
1168+
switch {
1169+
case errors.Is(dbErr, sql.ErrNoRows):
1170+
return nil, address.ErrAssetMetaNotFound
1171+
case dbErr != nil:
1172+
return nil, dbErr
1173+
}
1174+
1175+
return assetMeta, nil
1176+
}
1177+
11071178
// insertFullAssetGen inserts a new asset genesis and optional asset group
1108-
// into the database. A place holder for the asset meta inserted as well.
1179+
// into the database. A placeholder for the asset meta inserted as well.
11091180
func insertFullAssetGen(ctx context.Context,
11101181
gen *asset.Genesis, group *asset.GroupKey) func(AddrBook) error {
11111182

tapdb/assets_store.go

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ type ActiveAssetsStore interface {
216216
// disk.
217217
FetchAssetProofs(ctx context.Context) ([]AssetProof, error)
218218

219-
// FetchAssetsProofsSizes fetches all the asset proofs lengths that are
219+
// FetchAssetProofsSizes fetches all the asset proofs lengths that are
220220
// stored on disk.
221221
FetchAssetProofsSizes(ctx context.Context) ([]AssetProofSize, error)
222222

@@ -353,16 +353,6 @@ type ActiveAssetsStore interface {
353353
// the passed params.
354354
ReAnchorPassiveAssets(ctx context.Context, arg ReAnchorParams) error
355355

356-
// FetchAssetMetaByHash fetches the asset meta for a given meta hash.
357-
//
358-
// TODO(roasbeef): split into MetaStore?
359-
FetchAssetMetaByHash(ctx context.Context,
360-
metaDataHash []byte) (sqlc.FetchAssetMetaByHashRow, error)
361-
362-
// FetchAssetMetaForAsset fetches the asset meta for a given asset.
363-
FetchAssetMetaForAsset(ctx context.Context,
364-
assetID []byte) (sqlc.FetchAssetMetaForAssetRow, error)
365-
366356
// InsertBurn inserts a new row to the asset burns table which
367357
// includes all important data related to the burn.
368358
InsertBurn(ctx context.Context, arg sqlc.InsertBurnParams) (int64,
@@ -376,12 +366,12 @@ type ActiveAssetsStore interface {
376366
// MetaStore is a sub-set of the main sqlc.Querier interface that contains
377367
// methods related to metadata of the daemon.
378368
type MetaStore interface {
379-
// AssetsDBSize returns the total size of the taproot assets sqlite
380-
// database.
369+
// AssetsDBSizeSqlite returns the total size of the taproot assets
370+
// sqlite database.
381371
AssetsDBSizeSqlite(ctx context.Context) (int32, error)
382372

383-
// AssetsDBSize returns the total size of the taproot assets postgres
384-
// database.
373+
// AssetsDBSizePostgres returns the total size of the taproot assets
374+
// postgres database.
385375
AssetsDBSizePostgres(ctx context.Context) (int64, error)
386376
}
387377

@@ -3419,40 +3409,6 @@ func (a *AssetStore) QueryParcels(ctx context.Context,
34193409
return outboundParcels, nil
34203410
}
34213411

3422-
// ErrAssetMetaNotFound is returned when an asset meta is not found in the
3423-
// database.
3424-
var ErrAssetMetaNotFound = fmt.Errorf("asset meta not found")
3425-
3426-
// FetchAssetMetaForAsset attempts to fetch an asset meta based on an asset ID.
3427-
func (a *AssetStore) FetchAssetMetaForAsset(ctx context.Context,
3428-
assetID asset.ID) (*proof.MetaReveal, error) {
3429-
3430-
var assetMeta *proof.MetaReveal
3431-
3432-
readOpts := NewAssetStoreReadTx()
3433-
dbErr := a.db.ExecTx(ctx, &readOpts, func(q ActiveAssetsStore) error {
3434-
dbMeta, err := q.FetchAssetMetaForAsset(ctx, assetID[:])
3435-
if err != nil {
3436-
return err
3437-
}
3438-
3439-
assetMeta = &proof.MetaReveal{
3440-
Data: dbMeta.MetaDataBlob,
3441-
Type: proof.MetaType(dbMeta.MetaDataType.Int16),
3442-
}
3443-
3444-
return nil
3445-
})
3446-
switch {
3447-
case errors.Is(dbErr, sql.ErrNoRows):
3448-
return nil, ErrAssetMetaNotFound
3449-
case dbErr != nil:
3450-
return nil, dbErr
3451-
}
3452-
3453-
return assetMeta, nil
3454-
}
3455-
34563412
// AssetsDBSize returns the total size of the taproot assets database.
34573413
func (a *AssetStore) AssetsDBSize(ctx context.Context) (int64, error) {
34583414
var totalSize int64
@@ -3492,36 +3448,6 @@ func (a *AssetStore) AssetsDBSize(ctx context.Context) (int64, error) {
34923448
return totalSize, nil
34933449
}
34943450

3495-
// FetchAssetMetaByHash attempts to fetch an asset meta based on an asset hash.
3496-
func (a *AssetStore) FetchAssetMetaByHash(ctx context.Context,
3497-
metaHash [asset.MetaHashLen]byte) (*proof.MetaReveal, error) {
3498-
3499-
var assetMeta *proof.MetaReveal
3500-
3501-
readOpts := NewAssetStoreReadTx()
3502-
dbErr := a.db.ExecTx(ctx, &readOpts, func(q ActiveAssetsStore) error {
3503-
dbMeta, err := q.FetchAssetMetaByHash(ctx, metaHash[:])
3504-
if err != nil {
3505-
return err
3506-
}
3507-
3508-
assetMeta = &proof.MetaReveal{
3509-
Data: dbMeta.MetaDataBlob,
3510-
Type: proof.MetaType(dbMeta.MetaDataType.Int16),
3511-
}
3512-
3513-
return nil
3514-
})
3515-
switch {
3516-
case errors.Is(dbErr, sql.ErrNoRows):
3517-
return nil, ErrAssetMetaNotFound
3518-
case dbErr != nil:
3519-
return nil, dbErr
3520-
}
3521-
3522-
return assetMeta, nil
3523-
}
3524-
35253451
// TxHeight returns the block height of a given transaction. This will only
35263452
// return the height if the transaction is known to the store, which is only
35273453
// the case for assets relevant to this node.

0 commit comments

Comments
 (0)