Skip to content

Commit 39ec043

Browse files
committed
address+universe: allow syncing of specific group key
We'll want to be able to query the asset leaves for a group key to map them to one or more asset IDs.
1 parent 470e4fa commit 39ec043

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

address/book.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ type QueryParams struct {
8282
// known to universe servers in our federation.
8383
type AssetSyncer interface {
8484
// SyncAssetInfo queries the universes in our federation for genesis
85-
// and asset group information about the given asset ID.
86-
SyncAssetInfo(ctx context.Context, assetID *asset.ID) error
85+
// and asset group information about the given asset ID or group key.
86+
SyncAssetInfo(ctx context.Context, assetID *asset.ID,
87+
groupKey *btcec.PublicKey) error
8788

8889
// EnableAssetSync updates the sync config for the given asset so that
8990
// we sync future issuance proofs.
@@ -232,7 +233,7 @@ func (b *Book) QueryAssetInfo(ctx context.Context,
232233
log.Debugf("Asset %v is unknown, attempting to bootstrap", id.String())
233234

234235
// Use the AssetSyncer to query our universe federation for the asset.
235-
err = b.cfg.Syncer.SyncAssetInfo(ctx, &id)
236+
err = b.cfg.Syncer.SyncAssetInfo(ctx, &id, nil)
236237
if err != nil {
237238
return nil, err
238239
}
@@ -264,6 +265,25 @@ func (b *Book) QueryAssetInfo(ctx context.Context,
264265
return assetGroup, nil
265266
}
266267

268+
// SyncAssetGroup attempts to enable asset sync for the given asset group, then
269+
// perform an initial sync with the federation for that group.
270+
func (b *Book) SyncAssetGroup(ctx context.Context,
271+
groupKey *btcec.PublicKey) error {
272+
273+
groupInfo := &asset.AssetGroup{
274+
GroupKey: &asset.GroupKey{
275+
GroupPubKey: *groupKey,
276+
},
277+
}
278+
err := b.cfg.Syncer.EnableAssetSync(ctx, groupInfo)
279+
if err != nil {
280+
return fmt.Errorf("unable to enable asset sync: %w", err)
281+
}
282+
283+
// Use the AssetSyncer to query our universe federation for the asset.
284+
return b.cfg.Syncer.SyncAssetInfo(ctx, nil, groupKey)
285+
}
286+
267287
// FetchAssetMetaByHash attempts to fetch an asset meta based on an asset hash.
268288
func (b *Book) FetchAssetMetaByHash(ctx context.Context,
269289
metaHash [asset.MetaHashLen]byte) (*proof.MetaReveal, error) {
@@ -296,7 +316,7 @@ func (b *Book) FetchAssetMetaForAsset(ctx context.Context,
296316
assetID.String())
297317

298318
// Use the AssetSyncer to query our universe federation for the asset.
299-
err = b.cfg.Syncer.SyncAssetInfo(ctx, &assetID)
319+
err = b.cfg.Syncer.SyncAssetInfo(ctx, &assetID, nil)
300320
if err != nil {
301321
return nil, err
302322
}

tapgarden/mock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,8 @@ func (m *MockAssetSyncer) FetchAsset(id asset.ID) (*asset.AssetGroup, error) {
823823
}
824824
}
825825

826-
func (m *MockAssetSyncer) SyncAssetInfo(_ context.Context,
827-
id *asset.ID) error {
826+
func (m *MockAssetSyncer) SyncAssetInfo(_ context.Context, id *asset.ID,
827+
_ *btcec.PublicKey) error {
828828

829829
if id == nil {
830830
return fmt.Errorf("no asset ID provided")

universe/auto_syncer.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"time"
99

10+
"github.com/btcsuite/btcd/btcec/v2"
1011
"github.com/davecgh/go-spew/spew"
1112
"github.com/lightninglabs/taproot-assets/address"
1213
"github.com/lightninglabs/taproot-assets/asset"
@@ -807,10 +808,21 @@ func (f *FederationEnvoy) tryFetchServers() ([]ServerAddr, error) {
807808
// SyncAssetInfo queries the universes in our federation for genesis and asset
808809
// group information about the given asset ID.
809810
func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
810-
assetID *asset.ID) error {
811+
assetID *asset.ID, groupKey *btcec.PublicKey) error {
811812

812-
if assetID == nil {
813-
return fmt.Errorf("no asset ID provided")
813+
uniID := Identifier{
814+
ProofType: ProofTypeIssuance,
815+
}
816+
817+
switch {
818+
case assetID != nil:
819+
uniID.AssetID = *assetID
820+
821+
case groupKey != nil:
822+
uniID.GroupKey = groupKey
823+
824+
default:
825+
return fmt.Errorf("no asset ID or group key provided")
814826
}
815827

816828
// Fetch the set of universe servers in our federation.
@@ -820,16 +832,14 @@ func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
820832
}
821833

822834
assetConfig := FedUniSyncConfig{
823-
UniverseID: Identifier{
824-
AssetID: *assetID,
825-
ProofType: ProofTypeIssuance,
826-
},
835+
UniverseID: uniID,
827836
AllowSyncInsert: true,
828837
AllowSyncExport: false,
829838
}
830839
fullConfig := SyncConfigs{
831840
UniSyncConfigs: []*FedUniSyncConfig{&assetConfig},
832841
}
842+
833843
// We'll sync with Universe servers in parallel and collect the diffs
834844
// from any successful syncs. There can only be one diff per server, as
835845
// we're only syncing one universe root.
@@ -846,8 +856,8 @@ func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
846856
// Sync failures are expected from Universe servers that do not
847857
// have a relevant universe root.
848858
if err != nil {
849-
log.Warnf("Asset lookup failed: asset_id=%v, "+
850-
"remote_server=%v: %v", assetID.String(),
859+
log.Warnf("Asset lookup failed: id=%v, "+
860+
"remote_server=%v: %v", uniID.String(),
851861
addr.HostStr(), err)
852862

853863
// We don't want to abort syncing here, as this might
@@ -863,8 +873,8 @@ func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
863873
if len(syncDiff) != 1 {
864874
log.Warnf("Unexpected number of sync diffs "+
865875
"when looking up asset: num_diffs=%d, "+
866-
"asset_id=%v, remote_server=%v",
867-
len(syncDiff), assetID.String(),
876+
"id=%v, remote_server=%v",
877+
len(syncDiff), uniID.String(),
868878
addr.HostStr())
869879

870880
// We don't want to abort syncing here, as this
@@ -891,12 +901,11 @@ func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
891901

892902
syncDiffs := fn.Collect(returnedSyncDiffs)
893903
log.Infof("Synced new Universe leaves for asset %v, diff_size=%v",
894-
assetID.String(), len(syncDiffs))
904+
uniID.String(), len(syncDiffs))
895905

896-
// TODO(jhb): Log successful syncs?
897906
if len(syncDiffs) == 0 {
898907
return fmt.Errorf("asset lookup failed for asset: %v",
899-
assetID.String())
908+
uniID.String())
900909
}
901910

902911
return nil

0 commit comments

Comments
 (0)