@@ -20,6 +20,14 @@ const {
2020 INACTIVE_CHANNEL ,
2121} = ChannelEventUpdate . UpdateType ;
2222
23+ interface AliasCache {
24+ lastUpdated : number ;
25+ aliases : Record < string , string > ;
26+ }
27+
28+ /** cache alias data for 24 hours */
29+ const ALIAS_CACHE_TIMEOUT = 24 * 60 * 60 * 1000 ;
30+
2331export default class ChannelStore {
2432 private _store : Store ;
2533
@@ -110,26 +118,55 @@ export default class ChannelStore {
110118 */
111119 @action . bound
112120 async fetchAliases ( ) {
113- const pubKeys = values ( this . channels )
121+ this . _store . log . info ( 'fetching aliases for channels' ) ;
122+ // create an array of all channel pubkeys
123+ let pubkeys = values ( this . channels )
114124 . map ( c => c . remotePubkey )
115125 . filter ( ( r , i , a ) => a . indexOf ( r ) === i ) ; // remove duplicates
116- // call getNodeInfo for each pubkey and wait for all the requests to complete
117- const nodeInfos = await Promise . all (
118- pubKeys . map ( pk => this . _store . api . lnd . getNodeInfo ( pk ) ) ,
119- ) ;
120126
121- // create a map of pubKey to alias
122- const aliases = nodeInfos . reduce ( ( acc , { node } ) => {
123- if ( node ) acc [ node . pubKey ] = node . alias ;
124- return acc ;
125- } , { } as Record < string , string > ) ;
127+ // create a map of pubkey to alias
128+ let aliases : Record < string , string > = { } ;
129+
130+ // look up cached data in storage
131+ let cachedAliases = this . _store . storage . get < AliasCache > ( 'aliases' ) ;
132+ if ( cachedAliases && cachedAliases . lastUpdated > Date . now ( ) - ALIAS_CACHE_TIMEOUT ) {
133+ // there is cached data and it has not expired
134+ aliases = cachedAliases . aliases ;
135+ // exclude pubkeys which we have aliases for already
136+ pubkeys = pubkeys . filter ( pk => ! aliases [ pk ] ) ;
137+ this . _store . log . info ( `found aliases in cache. ${ pubkeys . length } missing` , pubkeys ) ;
138+ }
139+
140+ // if there are any pubkeys that we do not have a cached alias for
141+ if ( pubkeys . length ) {
142+ // call getNodeInfo for each pubkey and wait for all the requests to complete
143+ const nodeInfos = await Promise . all (
144+ pubkeys . map ( pk => this . _store . api . lnd . getNodeInfo ( pk ) ) ,
145+ ) ;
146+
147+ // add fetched aliases to the mapping
148+ aliases = nodeInfos . reduce ( ( acc , { node } ) => {
149+ if ( node ) acc [ node . pubKey ] = node . alias ;
150+ return acc ;
151+ } , aliases ) ;
152+
153+ // save updated aliases to the cache in storage
154+ cachedAliases = {
155+ lastUpdated : Date . now ( ) ,
156+ aliases,
157+ } ;
158+ this . _store . storage . set ( 'aliases' , cachedAliases ) ;
159+ this . _store . log . info ( `updated cache with ${ pubkeys . length } new aliases` ) ;
160+ }
126161
127162 runInAction ( 'fetchAliasesContinuation' , ( ) => {
163+ // set the alias on each channel in the store
128164 values ( this . channels ) . forEach ( c => {
129165 if ( aliases [ c . remotePubkey ] ) {
130166 c . alias = aliases [ c . remotePubkey ] ;
131167 }
132168 } ) ;
169+ this . _store . log . info ( 'updated channels with aliases' , toJS ( this . channels ) ) ;
133170 } ) ;
134171 }
135172
0 commit comments