@@ -20,21 +20,6 @@ const {
20
20
INACTIVE_CHANNEL ,
21
21
} = ChannelEventUpdate . UpdateType ;
22
22
23
- interface AliasCache {
24
- lastUpdated : number ;
25
- /** mapping from remove pubkey to alias */
26
- aliases : Record < string , string > ;
27
- }
28
-
29
- interface FeeCache {
30
- lastUpdated : number ;
31
- /** mapping form channel id to fee rate */
32
- feeRates : Record < string , number > ;
33
- }
34
-
35
- /** cache alias data for 24 hours */
36
- const CACHE_TIMEOUT = 24 * 60 * 60 * 1000 ;
37
-
38
23
export default class ChannelStore {
39
24
private _store : Store ;
40
25
@@ -127,46 +112,22 @@ export default class ChannelStore {
127
112
*/
128
113
@action . bound
129
114
async fetchAliases ( ) {
130
- this . _store . log . info ( 'fetching aliases for channels' ) ;
131
- // create an array of all channel pubkeys
132
- let pubkeys = values ( this . channels )
133
- . map ( c => c . remotePubkey )
134
- . filter ( ( r , i , a ) => a . indexOf ( r ) === i ) ; // remove duplicates
135
-
136
- // create a map of pubkey to alias
137
- let aliases : Record < string , string > = { } ;
138
-
139
- // look up cached data in storage
140
- let cachedAliases = this . _store . storage . get < AliasCache > ( 'aliases' ) ;
141
- if ( cachedAliases && cachedAliases . lastUpdated > Date . now ( ) - CACHE_TIMEOUT ) {
142
- // there is cached data and it has not expired
143
- aliases = cachedAliases . aliases ;
144
- // exclude pubkeys which we have aliases for already
145
- pubkeys = pubkeys . filter ( pk => ! aliases [ pk ] ) ;
146
- this . _store . log . info ( `found aliases in cache. ${ pubkeys . length } missing` , pubkeys ) ;
147
- }
148
-
149
- // if there are any pubkeys that we do not have a cached alias for
150
- if ( pubkeys . length ) {
151
- // call getNodeInfo for each pubkey and wait for all the requests to complete
152
- const nodeInfos = await Promise . all (
153
- pubkeys . map ( pk => this . _store . api . lnd . getNodeInfo ( pk ) ) ,
154
- ) ;
155
-
156
- // add fetched aliases to the mapping
157
- aliases = nodeInfos . reduce ( ( acc , { node } ) => {
158
- if ( node ) acc [ node . pubKey ] = node . alias ;
159
- return acc ;
160
- } , aliases ) ;
161
-
162
- // save updated aliases to the cache in storage
163
- cachedAliases = {
164
- lastUpdated : Date . now ( ) ,
165
- aliases,
166
- } ;
167
- this . _store . storage . set ( 'aliases' , cachedAliases ) ;
168
- this . _store . log . info ( `updated cache with ${ pubkeys . length } new aliases` ) ;
169
- }
115
+ const aliases = await this . _store . storage . getCached < string > ( {
116
+ cacheKey : 'aliases' ,
117
+ requiredKeys : values ( this . channels ) . map ( c => c . remotePubkey ) ,
118
+ log : this . _store . log ,
119
+ fetchFromApi : async ( missingKeys , data ) => {
120
+ // call getNodeInfo for each pubkey and wait for all the requests to complete
121
+ const nodeInfos = await Promise . all (
122
+ missingKeys . map ( id => this . _store . api . lnd . getNodeInfo ( id ) ) ,
123
+ ) ;
124
+ // return a mapping from pubkey to alias
125
+ return nodeInfos . reduce ( ( acc , { node } ) => {
126
+ if ( node ) acc [ node . pubKey ] = node . alias ;
127
+ return acc ;
128
+ } , data ) ;
129
+ } ,
130
+ } ) ;
170
131
171
132
runInAction ( 'fetchAliasesContinuation' , ( ) => {
172
133
// set the alias on each channel in the store
@@ -186,60 +147,37 @@ export default class ChannelStore {
186
147
*/
187
148
@action . bound
188
149
async fetchFeeRates ( ) {
189
- this . _store . log . info ( 'fetching fees for channels' ) ;
190
- // create an array of all channel ids
191
- let chanIds = values ( this . channels )
192
- . map ( c => c . chanId )
193
- . filter ( ( r , i , a ) => a . indexOf ( r ) === i ) ; // remove duplicates
194
-
195
- // create a map of chan id to fee rate
196
- let feeRates : Record < string , number > = { } ;
197
-
198
- // look up cached data in storage
199
- let cachedFees = this . _store . storage . get < FeeCache > ( 'fee-rates' ) ;
200
- if ( cachedFees && cachedFees . lastUpdated > Date . now ( ) - CACHE_TIMEOUT ) {
201
- // there is cached data and it has not expired
202
- feeRates = cachedFees . feeRates ;
203
- // exclude chanIds which we have feeRates for already
204
- chanIds = chanIds . filter ( id => ! feeRates [ id ] ) ;
205
- this . _store . log . info ( `found feeRates in cache. ${ chanIds . length } missing` , chanIds ) ;
206
- }
207
-
208
- // if there are any chanIds that we do not have a cached fee rate for
209
- if ( chanIds . length ) {
210
- // call getNodeInfo for each chan id and wait for all the requests to complete
211
- const chanInfos = await Promise . all (
212
- chanIds . map ( id => this . _store . api . lnd . getChannelInfo ( id ) ) ,
213
- ) ;
214
-
215
- // add fetched feeRates to the mapping
216
- feeRates = chanInfos . reduce ( ( acc , info ) => {
217
- const { channelId, node1Pub, node1Policy, node2Policy } = info ;
218
- const localPubkey = this . _store . nodeStore . pubkey ;
219
- const policy = node1Pub === localPubkey ? node2Policy : node1Policy ;
220
- if ( policy ) {
221
- acc [ channelId ] = + Big ( policy . feeRateMilliMsat ) . div ( 1000000 ) . mul ( 100 ) ;
222
- }
223
- return acc ;
224
- } , feeRates ) ;
225
-
226
- // save updated feeRates to the cache in storage
227
- cachedFees = {
228
- lastUpdated : Date . now ( ) ,
229
- feeRates,
230
- } ;
231
- this . _store . storage . set ( 'fee-rates' , cachedFees ) ;
232
- this . _store . log . info ( `updated cache with ${ chanIds . length } new feeRates` ) ;
233
- }
150
+ const feeRates = await this . _store . storage . getCached < number > ( {
151
+ cacheKey : 'feeRates' ,
152
+ requiredKeys : values ( this . channels ) . map ( c => c . chanId ) ,
153
+ log : this . _store . log ,
154
+ fetchFromApi : async ( missingKeys , data ) => {
155
+ // call getNodeInfo for each pubkey and wait for all the requests to complete
156
+ const chanInfos = await Promise . all (
157
+ missingKeys . map ( id => this . _store . api . lnd . getChannelInfo ( id ) ) ,
158
+ ) ;
159
+ // return an updated mapping from chanId to fee rate
160
+ return chanInfos . reduce ( ( acc , info ) => {
161
+ const { channelId, node1Pub, node1Policy, node2Policy } = info ;
162
+ const localPubkey = this . _store . nodeStore . pubkey ;
163
+ const policy = node1Pub === localPubkey ? node2Policy : node1Policy ;
164
+ if ( policy ) {
165
+ acc [ channelId ] = + Big ( policy . feeRateMilliMsat ) . div ( 1000000 ) . mul ( 100 ) ;
166
+ }
167
+ return acc ;
168
+ } , data ) ;
169
+ } ,
170
+ } ) ;
234
171
235
172
runInAction ( 'fetchFeesContinuation' , ( ) => {
236
173
// set the fee on each channel in the store
237
174
values ( this . channels ) . forEach ( c => {
238
- if ( feeRates [ c . chanId ] ) {
239
- c . remoteFeeRate = feeRates [ c . chanId ] ;
175
+ const rate = feeRates [ c . chanId ] ;
176
+ if ( rate ) {
177
+ c . remoteFeeRate = rate ;
178
+ this . _store . log . info ( `updated channel ${ c . chanId } with remoteFeeRate ${ rate } ` ) ;
240
179
}
241
180
} ) ;
242
- this . _store . log . info ( 'updated channels with feeRates' , toJS ( this . channels ) ) ;
243
181
} ) ;
244
182
}
245
183
0 commit comments