Skip to content

Commit 7714730

Browse files
committed
tapcfg+rfq: add group lookup to rfq manager
This commit adds a new interface to the rfq manager which helps us look up the group an asset ID may belong to. The reason we can't reuse the same interface from tapsend is a circular import issue. We also introduce an in memory look-up map to skip a roundtrip to the db, since this information is static.
1 parent f55bd62 commit 7714730

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

rfq/manager.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ type (
6161
SellAcceptMap map[SerialisedScid]rfqmsg.SellAccept
6262
)
6363

64+
// GroupLookup is an interface that helps us look up a group of an asset based
65+
// on the asset ID.
66+
type GroupLookup interface {
67+
// QueryAssetGroup fetches the group information of an asset, if it
68+
// belongs in a group.
69+
QueryAssetGroup(context.Context, asset.ID) (*asset.AssetGroup, error)
70+
}
71+
6472
// ManagerCfg is a struct that holds the configuration parameters for the RFQ
6573
// manager.
6674
type ManagerCfg struct {
@@ -84,6 +92,10 @@ type ManagerCfg struct {
8492
// determine the available channels for routing.
8593
ChannelLister ChannelLister
8694

95+
// GroupLookup is an interface that helps us querry asset groups by
96+
// asset IDs.
97+
GroupLookup GroupLookup
98+
8799
// AliasManager is the SCID alias manager. This component is injected
88100
// into the manager once lnd and tapd are hooked together.
89101
AliasManager ScidAliasManager
@@ -165,6 +177,12 @@ type Manager struct {
165177
SerialisedScid, rfqmsg.SellAccept,
166178
]
167179

180+
// groupKeyLookupCache is a map that helps us quickly perform an
181+
// in-memory look up of the group an asset belongs to. Since this
182+
// information is static and generated during minting, it is not
183+
// possible for an asset to change groups.
184+
groupKeyLookupCache lnutils.SyncMap[asset.ID, *btcec.PublicKey]
185+
168186
// subscribers is a map of components that want to be notified on new
169187
// events, keyed by their subscription ID.
170188
subscribers lnutils.SyncMap[uint64, *fn.EventReceiver[fn.Event]]
@@ -917,6 +935,34 @@ func (m *Manager) RemoveSubscriber(
917935
return nil
918936
}
919937

938+
// getAssetGroupKey retrieves the group key of an asset based on its ID.
939+
func (m *Manager) getAssetGroupKey(ctx context.Context,
940+
id asset.ID) (fn.Option[btcec.PublicKey], error) {
941+
942+
// First, see if we have already queried our DB for this ID.
943+
v, ok := m.groupKeyLookupCache.Load(id)
944+
if ok {
945+
return fn.Some(*v), nil
946+
}
947+
948+
// Perform the DB query.
949+
group, err := m.cfg.GroupLookup.QueryAssetGroup(ctx, id)
950+
if err != nil {
951+
return fn.None[btcec.PublicKey](), err
952+
}
953+
954+
// If the asset does not belong to a group, return early with no error
955+
// or response.
956+
if group == nil || group.GroupKey == nil {
957+
return fn.None[btcec.PublicKey](), nil
958+
}
959+
960+
// Store the result for future calls.
961+
m.groupKeyLookupCache.Store(id, &group.GroupPubKey)
962+
963+
return fn.Some(group.GroupPubKey), nil
964+
}
965+
920966
// publishSubscriberEvent publishes an event to all subscribers.
921967
func (m *Manager) publishSubscriberEvent(event fn.Event) {
922968
// Iterate over the subscribers and deliver the event to each one.

tapcfg/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
405405
HtlcSubscriber: lndRouterClient,
406406
PriceOracle: priceOracle,
407407
ChannelLister: walletAnchor,
408+
GroupLookup: tapdbAddrBook,
408409
AliasManager: lndRouterClient,
409410
// nolint: lll
410411
AcceptPriceDeviationPpm: rfqCfg.AcceptPriceDeviationPpm,

0 commit comments

Comments
 (0)