@@ -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.
6674type 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.
921967func (m * Manager ) publishSubscriberEvent (event fn.Event ) {
922968 // Iterate over the subscribers and deliver the event to each one.
0 commit comments