Skip to content

Commit 1b181d5

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 cfae77b commit 1b181d5

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

address/book.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ 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.
86+
SyncAssetInfo(ctx context.Context, specifier asset.Specifier) error
8787

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

234234
// Use the AssetSyncer to query our universe federation for the asset.
235-
err = b.cfg.Syncer.SyncAssetInfo(ctx, &id)
235+
err = b.cfg.Syncer.SyncAssetInfo(ctx, asset.NewSpecifierFromId(id))
236236
if err != nil {
237237
return nil, err
238238
}
@@ -264,6 +264,27 @@ func (b *Book) QueryAssetInfo(ctx context.Context,
264264
return assetGroup, nil
265265
}
266266

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

298319
// Use the AssetSyncer to query our universe federation for the asset.
299-
err = b.cfg.Syncer.SyncAssetInfo(ctx, &assetID)
320+
err = b.cfg.Syncer.SyncAssetInfo(ctx, asset.NewSpecifierFromId(assetID))
300321
if err != nil {
301322
return nil, err
302323
}

tapgarden/mock.go

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

826826
func (m *MockAssetSyncer) SyncAssetInfo(_ context.Context,
827-
id *asset.ID) error {
827+
s asset.Specifier) error {
828828

829-
if id == nil {
829+
if !s.HasId() {
830830
return fmt.Errorf("no asset ID provided")
831831
}
832832

833-
_, err := m.FetchAsset(*id)
833+
_, err := m.FetchAsset(*s.UnwrapIdToPtr())
834834
return err
835835
}
836836

universe/auto_syncer.go

Lines changed: 26 additions & 15 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"
@@ -805,31 +806,42 @@ func (f *FederationEnvoy) tryFetchServers() ([]ServerAddr, error) {
805806
}
806807

807808
// SyncAssetInfo queries the universes in our federation for genesis and asset
808-
// group information about the given asset ID.
809+
// group information about the given asset.
809810
func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
810-
assetID *asset.ID) error {
811+
specifier asset.Specifier) error {
811812

812-
if assetID == nil {
813-
return fmt.Errorf("no asset ID provided")
813+
uniID := Identifier{
814+
ProofType: ProofTypeIssuance,
814815
}
815816

817+
// One of asset ID or group key must be set, but not both.
818+
if specifier.HasId() == specifier.HasGroupPubKey() {
819+
return fmt.Errorf("must set either asset ID or group key for " +
820+
"asset sync")
821+
}
822+
823+
specifier.WhenId(func(id asset.ID) {
824+
uniID.AssetID = id
825+
})
826+
specifier.WhenGroupPubKey(func(groupKey btcec.PublicKey) {
827+
uniID.GroupKey = &groupKey
828+
})
829+
816830
// Fetch the set of universe servers in our federation.
817831
fedServers, err := f.tryFetchServers()
818832
if err != nil {
819833
return err
820834
}
821835

822836
assetConfig := FedUniSyncConfig{
823-
UniverseID: Identifier{
824-
AssetID: *assetID,
825-
ProofType: ProofTypeIssuance,
826-
},
837+
UniverseID: uniID,
827838
AllowSyncInsert: true,
828839
AllowSyncExport: false,
829840
}
830841
fullConfig := SyncConfigs{
831842
UniSyncConfigs: []*FedUniSyncConfig{&assetConfig},
832843
}
844+
833845
// We'll sync with Universe servers in parallel and collect the diffs
834846
// from any successful syncs. There can only be one diff per server, as
835847
// we're only syncing one universe root.
@@ -846,8 +858,8 @@ func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
846858
// Sync failures are expected from Universe servers that do not
847859
// have a relevant universe root.
848860
if err != nil {
849-
log.Warnf("Asset lookup failed: asset_id=%v, "+
850-
"remote_server=%v: %v", assetID.String(),
861+
log.Warnf("Asset lookup failed: id=%v, "+
862+
"remote_server=%v: %v", uniID.String(),
851863
addr.HostStr(), err)
852864

853865
// We don't want to abort syncing here, as this might
@@ -863,8 +875,8 @@ func (f *FederationEnvoy) SyncAssetInfo(ctx context.Context,
863875
if len(syncDiff) != 1 {
864876
log.Warnf("Unexpected number of sync diffs "+
865877
"when looking up asset: num_diffs=%d, "+
866-
"asset_id=%v, remote_server=%v",
867-
len(syncDiff), assetID.String(),
878+
"id=%v, remote_server=%v",
879+
len(syncDiff), uniID.String(),
868880
addr.HostStr())
869881

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

892904
syncDiffs := fn.Collect(returnedSyncDiffs)
893905
log.Infof("Synced new Universe leaves for asset %v, diff_size=%v",
894-
assetID.String(), len(syncDiffs))
906+
uniID.String(), len(syncDiffs))
895907

896-
// TODO(jhb): Log successful syncs?
897908
if len(syncDiffs) == 0 {
898909
return fmt.Errorf("asset lookup failed for asset: %v",
899-
assetID.String())
910+
uniID.String())
900911
}
901912

902913
return nil

0 commit comments

Comments
 (0)