-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCaching.fs
650 lines (577 loc) · 32 KB
/
Caching.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
namespace GWallet.Backend
open System
open System.IO
open System.Linq
open System.Net.Http
open Fsdk
open GWallet.Backend.FSharpUtil.UwpHacks
type CachedNetworkData =
{
UsdPrice: Map<Currency,CachedValue<decimal>>;
Balances: Map<Currency,Map<PublicAddress,CachedValue<decimal>>>;
OutgoingTransactions: Map<Currency,Map<PublicAddress,Map<string,CachedValue<decimal>>>>;
}
member self.GetLeastOldDate() =
let allDates =
seq {
for KeyValue(_currency, (_price, date)) in self.UsdPrice do
yield date
for KeyValue(_currency, addressesToBalancesMap) in self.Balances do
for KeyValue(_addr, (_price, date)) in addressesToBalancesMap do
yield date
for KeyValue(_currency, addressesToTxsMap) in self.OutgoingTransactions do
for KeyValue(_addr, txHashToAmountsMap) in addressesToTxsMap do
for KeyValue(_txHash, (_amount, date)) in txHashToAmountsMap do
yield date
}
Seq.sort allDates |> Seq.rev |> Seq.tryHead
static member Empty =
{
UsdPrice = Map.empty
Balances = Map.empty
OutgoingTransactions = Map.empty
}
static member FromDietCache (dietCache: DietCache): CachedNetworkData =
let now = DateTime.UtcNow
let fiatPrices =
[ for KeyValue(currencyString, price) in dietCache.UsdPrice -> (Currency.Parse currencyString,(price,now)) ]
|> Map.ofSeq
let balances =
seq {
for KeyValue(address,currencies) in dietCache.Addresses do
for currencyStr in currencies do
match dietCache.Balances.TryFind currencyStr with
| None -> ()
| Some balance ->
yield (Currency.Parse currencyStr),Map.empty.Add(address,(balance,now))
} |> Map.ofSeq
{ UsdPrice = fiatPrices; Balances = balances
OutgoingTransactions = Map.empty; }
member self.ToDietCache(readOnlyAccounts: seq<ReadOnlyAccount>) =
let rec extractAddressesFromAccounts (acc: Map<PublicAddress,List<DietCurrency>>) (accounts: List<IAccount>)
: Map<PublicAddress,List<DietCurrency>> =
match accounts with
| [] -> acc
| head::tail ->
let existingCurrenciesForHeadAddress =
match acc.TryFind head.PublicAddress with
| None -> List.Empty
| Some currencies -> currencies
let newAcc = acc.Add(head.PublicAddress, head.Currency.ToString()::existingCurrenciesForHeadAddress)
extractAddressesFromAccounts newAcc tail
let fiatPrices =
[ for (KeyValue(currency, (price,_))) in self.UsdPrice -> currency.ToString(),price ]
|> Map.ofSeq
let addresses = extractAddressesFromAccounts
Map.empty (List.ofSeq readOnlyAccounts |> List.map (fun acc -> acc:>IAccount))
let balances =
seq {
for (KeyValue(currency, currencyBalances)) in self.Balances do
for (KeyValue(address, (balance,_))) in currencyBalances do
if readOnlyAccounts.Any(fun account -> (account:>IAccount).PublicAddress = address) then
yield (currency.ToString(),balance)
} |> Map.ofSeq
{ UsdPrice = fiatPrices; Addresses = addresses; Balances = balances; }
type CacheFiles =
{
CachedNetworkData: FileInfo
ServerStats: FileInfo
}
module Caching =
let private defaultCacheFiles =
{
CachedNetworkData = FileInfo(Path.Combine(Config.GetCacheDir().FullName, "networkdata.json"))
ServerStats = FileInfo(Path.Combine(Config.GetCacheDir().FullName,
ServerRegistry.ServersEmbeddedResourceFileName))
}
let public ImportFromJson<'T> (cacheData: string): 'T =
Marshalling.Deserialize cacheData
let private LoadFromDiskInner (file: FileInfo): Option<string> =
let json = File.ReadAllText file.FullName
if String.IsNullOrWhiteSpace json then
None
else
Some json
let droppedCachedMsgWarning = "Warning: cleaning incompatible cache data found from different GWallet version"
let private LoadFromDiskInternal<'T> (file: FileInfo): Option<'T> =
try
match LoadFromDiskInner file with
| None -> None
| Some json ->
try
let deserializedJson = ImportFromJson json
Some deserializedJson
with
| :? VersionMismatchDuringDeserializationException ->
Infrastructure.LogError droppedCachedMsgWarning
None
| :? DeserializationException ->
// FIXME: report a warning to sentry here...
Infrastructure.LogError "Warning: cleaning incompatible cache data found"
Infrastructure.LogDebug (SPrintF1 "JSON content: <<<%s>>>" json)
None
with
| :? FileNotFoundException -> None
// this weird thing could happen because the previous version of GWallet didn't have a new element
// FIXME: we should save each Map<> into its own file
let private WeirdNullCheckToDetectVersionConflicts x =
Object.ReferenceEquals(x, null)
let private LoadFromDisk (files: CacheFiles): bool*CachedNetworkData*ServerRanking =
let maybeNetworkData = LoadFromDiskInternal<CachedNetworkData> files.CachedNetworkData
let maybeFirstRun,resultingNetworkData =
match maybeNetworkData with
| None ->
true,CachedNetworkData.Empty
| Some networkData ->
if WeirdNullCheckToDetectVersionConflicts networkData.OutgoingTransactions then
Infrastructure.LogError droppedCachedMsgWarning
true,CachedNetworkData.Empty
else
false,networkData
let maybeServerStats = LoadFromDiskInternal<ServerRanking> files.ServerStats
match maybeServerStats with
| None ->
maybeFirstRun,resultingNetworkData,Map.empty
| Some serverStats ->
false,resultingNetworkData,serverStats
let rec private MergeRatesInternal (oldMap: Map<'K, CachedValue<'V>>)
(newMap: Map<'K, CachedValue<'V>>)
(currencyList: List<'K>)
(accumulator: Map<'K, CachedValue<'V>>) =
match currencyList with
| [] -> accumulator
| address::tail ->
let maybeCachedBalance = Map.tryFind address oldMap
match maybeCachedBalance with
| None ->
let newCachedBalance = newMap.[address]
let newAcc = accumulator.Add(address, newCachedBalance)
MergeRatesInternal oldMap newMap tail newAcc
| Some(_,time) ->
let newBalance,newTime = newMap.[address]
let newAcc =
if (newTime > time) then
accumulator.Add(address, (newBalance,newTime))
else
accumulator
MergeRatesInternal oldMap newMap tail newAcc
let private MergeRates (oldMap: Map<'K, CachedValue<'V>>) (newMap: Map<'K, CachedValue<'V>>) =
let currencyList = Map.toList newMap |> List.map fst
MergeRatesInternal oldMap newMap currencyList oldMap
let rec private MergeBalancesInternal (oldMap: Map<Currency, Map<PublicAddress,CachedValue<'V>>>)
(newMap: Map<Currency, Map<PublicAddress,CachedValue<'V>>>)
(addressList: List<Currency*PublicAddress>)
(accumulator: Map<Currency, Map<PublicAddress,CachedValue<'V>>>)
: Map<Currency, Map<PublicAddress,CachedValue<'V>>> =
match addressList with
| [] -> accumulator
| (currency,address)::tail ->
let maybeCachedBalances = Map.tryFind currency oldMap
match maybeCachedBalances with
| None ->
let newCachedBalance = newMap.[currency].[address]
let newCachedBalancesForThisCurrency = [(address,newCachedBalance)] |> Map.ofList
let newAcc = accumulator.Add(currency, newCachedBalancesForThisCurrency)
MergeBalancesInternal oldMap newMap tail newAcc
| Some(balancesMapForCurrency) ->
let accBalancesForThisCurrency = accumulator.[currency]
let maybeCachedBalance = Map.tryFind address balancesMapForCurrency
match maybeCachedBalance with
| None ->
let newCachedBalance = newMap.[currency].[address]
let newAccBalances = accBalancesForThisCurrency.Add(address, newCachedBalance)
let newAcc = accumulator.Add(currency, newAccBalances)
MergeBalancesInternal oldMap newMap tail newAcc
| Some(_,time) ->
let newBalance,newTime = newMap.[currency].[address]
let newAcc =
if (newTime > time) then
let newAccBalances = accBalancesForThisCurrency.Add(address, (newBalance,newTime))
accumulator.Add(currency, newAccBalances)
else
accumulator
MergeBalancesInternal oldMap newMap tail newAcc
let private MergeBalances (oldMap: Map<Currency, Map<PublicAddress,CachedValue<'V>>>)
(newMap: Map<Currency, Map<PublicAddress,CachedValue<'V>>>)
: Map<Currency, Map<PublicAddress,CachedValue<'V>>> =
let addressList =
seq {
for currency,subMap in Map.toList newMap do
for address,_ in Map.toList subMap do
yield (currency,address)
} |> List.ofSeq
MergeBalancesInternal oldMap newMap addressList oldMap
// taken from http://www.fssnip.net/2z/title/All-combinations-of-list-elements
let ListCombinations lst =
let rec comb accLst elemLst =
match elemLst with
| h::t ->
let next = [h]::List.map (fun el -> h::el) accLst @ accLst
comb next t
| _ -> accLst
comb List.Empty lst
let MapCombinations<'K,'V when 'K: comparison> (map: Map<'K,'V>): List<List<'K*'V>> =
Map.toList map |> ListCombinations
type MainCache(maybeCacheFiles: Option<CacheFiles>, unconfTxExpirationSpan: TimeSpan) =
let cacheFiles =
match maybeCacheFiles with
| Some files -> files
| None -> defaultCacheFiles
let SaveNetworkDataToDisk (newCachedData: CachedNetworkData) =
let networkDataInJson = Marshalling.Serialize newCachedData
// it is assumed that SaveToDisk is being run under a lock() block
File.WriteAllText (cacheFiles.CachedNetworkData.FullName, networkDataInJson)
// we return back the rankings because the serialization process could remove dupes (and deserialization time
// is basically negligible, i.e. took 15 milliseconds max in my MacBook in Debug mode)
let SaveServerRankingsToDisk (serverStats: ServerRanking): ServerRanking =
let serverStatsInJson = ServerRegistry.Serialize serverStats
// it is assumed that SaveToDisk is being run under a lock() block
File.WriteAllText (cacheFiles.ServerStats.FullName, serverStatsInJson)
match LoadFromDiskInternal<ServerRanking> cacheFiles.ServerStats with
| None -> failwith "should return something after having saved it"
| Some cleansedServerStats -> cleansedServerStats
let InitServers (lastServerStats: ServerRanking) =
let mergedServers = ServerRegistry.MergeWithBaseline lastServerStats
let mergedAndSaved = SaveServerRankingsToDisk mergedServers
for KeyValue(currency,servers) in mergedAndSaved do
for server in servers do
if server.CommunicationHistory.IsNone then
Infrastructure.LogError (SPrintF2 "WARNING: no history stats about %A server %s"
currency server.ServerInfo.NetworkPath)
mergedServers
let firstRun,initialSessionCachedNetworkData,lastServerStats = LoadFromDisk cacheFiles
let initialServerStats = InitServers lastServerStats
let mutable sessionCachedNetworkData = initialSessionCachedNetworkData
let mutable sessionServerRanking = initialServerStats
let GetSumOfAllTransactions (trans: Map<Currency,Map<PublicAddress,Map<string,CachedValue<decimal>>>>)
currency address: decimal =
let now = DateTime.UtcNow
let currencyTrans = trans.TryFind currency
match currencyTrans with
| None -> 0m
| Some someMap ->
let addressTrans = someMap.TryFind address
match addressTrans with
| None -> 0m
| Some someMap ->
Map.toSeq someMap |>
Seq.sumBy (fun (_,(txAmount,txDate)) ->
// FIXME: develop some kind of cache cleanup to remove these expired txs?
if (now < txDate + unconfTxExpirationSpan) then
txAmount
else
0m
)
let rec RemoveRangeFromMap (map: Map<'K,'V>) (list: List<'K*'V>) =
match list with
| [] -> map
| (key,_)::tail -> RemoveRangeFromMap (map.Remove key) tail
let GatherDebuggingInfo (previousBalance) (currency) (address) (newCache) =
let json1 = Marshalling.Serialize previousBalance
let json2 = Marshalling.Serialize currency
let json3 = Marshalling.Serialize address
let json4 = Marshalling.Serialize newCache
String.Join(Environment.NewLine, json1, json2, json3, json4)
let ReportProblem (negativeBalance: decimal) (previousBalance) (currency) (address) (newCache) =
Infrastructure.ReportError (SPrintF2 "Negative balance '%s'. Details: %s"
(negativeBalance.ToString())
(GatherDebuggingInfo
previousBalance
currency
address
newCache))
member __.ClearAll () =
SaveNetworkDataToDisk CachedNetworkData.Empty
SaveServerRankingsToDisk Map.empty
|> ignore<ServerRanking>
member __.SaveSnapshot(newDietCachedData: DietCache) =
let newCachedData = CachedNetworkData.FromDietCache newDietCachedData
lock cacheFiles.CachedNetworkData (fun _ ->
let newSessionCachedNetworkData =
let mergedBalances = MergeBalances sessionCachedNetworkData.Balances newCachedData.Balances
let mergedUsdPrices = MergeRates sessionCachedNetworkData.UsdPrice newCachedData.UsdPrice
{
sessionCachedNetworkData with
UsdPrice = mergedUsdPrices
Balances = mergedBalances
}
sessionCachedNetworkData <- newSessionCachedNetworkData
SaveNetworkDataToDisk newSessionCachedNetworkData
)
member __.GetLastCachedData (): CachedNetworkData =
lock cacheFiles.CachedNetworkData (fun _ ->
sessionCachedNetworkData
)
member __.RetrieveLastKnownUsdPrice currency: NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
try
Cached(sessionCachedNetworkData.UsdPrice.Item currency)
with
// FIXME: rather use tryFind func instead of using a try-with block
| :? System.Collections.Generic.KeyNotFoundException -> NotAvailable
)
member __.StoreLastFiatUsdPrice (currency, lastFiatUsdPrice: decimal): unit =
lock cacheFiles.CachedNetworkData (fun _ ->
let time = DateTime.UtcNow
let newCachedValue =
{ sessionCachedNetworkData
with UsdPrice = sessionCachedNetworkData.UsdPrice.Add(currency, (lastFiatUsdPrice, time)) }
sessionCachedNetworkData <- newCachedValue
SaveNetworkDataToDisk newCachedValue
)
member __.RetrieveLastCompoundBalance (address: PublicAddress) (currency: Currency): NotFresh<decimal> =
lock cacheFiles.CachedNetworkData (fun _ ->
let balance =
try
Cached((sessionCachedNetworkData.Balances.Item currency).Item address)
with
// FIXME: rather use tryFind func instead of using a try-with block
| :? System.Collections.Generic.KeyNotFoundException -> NotAvailable
match balance with
| NotAvailable ->
NotAvailable
| Cached(balance,time) ->
let allTransSum =
GetSumOfAllTransactions sessionCachedNetworkData.OutgoingTransactions currency address
let compoundBalance = balance - allTransSum
if (compoundBalance < 0.0m) then
ReportProblem compoundBalance
None
currency
address
sessionCachedNetworkData
|> ignore<bool>
// FIXME: should we return here just balance, or NotAvailable (as in there's no cache), and remove all transactions?
Cached(0.0m,time)
else
Cached(compoundBalance,time)
)
member self.TryRetrieveLastCompoundBalance (address: PublicAddress) (currency: Currency): Option<decimal> =
let maybeCachedBalance = self.RetrieveLastCompoundBalance address currency
match maybeCachedBalance with
| NotAvailable ->
None
| Cached(cachedBalance,_) ->
Some cachedBalance
member __.RetrieveAndUpdateLastCompoundBalance (address: PublicAddress)
(currency: Currency)
(newBalance: decimal)
: CachedValue<decimal> =
let time = DateTime.UtcNow
lock cacheFiles.CachedNetworkData (fun _ ->
let newCachedValueWithNewBalance,previousBalance =
let newCurrencyBalances,previousBalance =
match sessionCachedNetworkData.Balances.TryFind currency with
| None ->
Map.empty,None
| Some currencyBalances ->
let maybePreviousBalance = currencyBalances.TryFind address
currencyBalances,maybePreviousBalance
{
sessionCachedNetworkData with
Balances = sessionCachedNetworkData.Balances.Add(currency,
newCurrencyBalances.Add(address,
(newBalance, time)))
},previousBalance
let newCachedValueWithNewBalanceAndMaybeLessTransactions =
let maybeNewValue =
FSharpUtil.option {
let! previousCachedBalance,_ = previousBalance
do!
if newBalance <> previousCachedBalance && previousCachedBalance > newBalance then
Some ()
else
None
let! currencyAddresses = newCachedValueWithNewBalance.OutgoingTransactions.TryFind currency
let! addressTransactions = currencyAddresses.TryFind address
let allCombinationsOfTransactions = MapCombinations addressTransactions
let newAddressTransactions =
match List.tryFind (fun combination ->
let txSumAmount = List.sumBy (fun (_,(txAmount,_)) ->
txAmount) combination
previousCachedBalance - txSumAmount = newBalance
) allCombinationsOfTransactions with
| None ->
addressTransactions
| Some combination ->
RemoveRangeFromMap addressTransactions combination
let newOutgoingTransactions =
newCachedValueWithNewBalance
.OutgoingTransactions.Add(currency,
currencyAddresses.Add(address,
newAddressTransactions))
return
{
newCachedValueWithNewBalance with
OutgoingTransactions = newOutgoingTransactions
}
}
match maybeNewValue with
| None -> newCachedValueWithNewBalance
| Some x -> x
sessionCachedNetworkData <- newCachedValueWithNewBalanceAndMaybeLessTransactions
SaveNetworkDataToDisk newCachedValueWithNewBalanceAndMaybeLessTransactions
let allTransSum =
GetSumOfAllTransactions newCachedValueWithNewBalanceAndMaybeLessTransactions.OutgoingTransactions
currency
address
let compoundBalance = newBalance - allTransSum
if (compoundBalance < 0.0m) then
ReportProblem compoundBalance
previousBalance
currency
address
newCachedValueWithNewBalanceAndMaybeLessTransactions
|> ignore<bool>
// FIXME: should we return here just newBalance, and remove all transactions?
0.0m,time
else
compoundBalance,time
)
member private __.StoreTransactionRecord (address: PublicAddress)
(currency: Currency)
(txId: string)
(amount: decimal)
: unit =
let time = DateTime.UtcNow
lock cacheFiles.CachedNetworkData (fun _ ->
let newCurrencyAddresses =
match sessionCachedNetworkData.OutgoingTransactions.TryFind currency with
| None ->
Map.empty
| Some currencyAddresses ->
currencyAddresses
let newAddressTransactions =
match newCurrencyAddresses.TryFind address with
| None ->
Map.empty.Add(txId, (amount, time))
| Some addressTransactions ->
addressTransactions.Add(txId, (amount, time))
let newOutgoingTxs =
sessionCachedNetworkData.OutgoingTransactions.Add(currency,
newCurrencyAddresses.Add(address,
newAddressTransactions))
let newCachedValue = { sessionCachedNetworkData with OutgoingTransactions = newOutgoingTxs }
sessionCachedNetworkData <- newCachedValue
SaveNetworkDataToDisk newCachedValue
)
member self.StoreOutgoingTransaction (address: PublicAddress)
(transactionCurrency: Currency)
(feeCurrency: Currency)
(txId: string)
(amount: decimal)
(feeAmount: decimal)
: unit =
self.StoreTransactionRecord address transactionCurrency txId amount
if transactionCurrency <> feeCurrency && (not Config.EthTokenEstimationCouldBeBuggyAsInNotAccurate) then
self.StoreTransactionRecord address feeCurrency txId feeAmount
member __.SaveServerLastStat (serverMatchFunc: ServerDetails->bool)
(stat: HistoryFact): unit =
lock cacheFiles.ServerStats (fun _ ->
let currency,serverInfo,previousLastSuccessfulCommunication =
match ServerRegistry.TryFindValue sessionServerRanking serverMatchFunc with
| None ->
failwith "Merge&Save didn't happen before launching the FaultTolerantPClient?"
| Some (currency,server) ->
match server.CommunicationHistory with
| None -> currency,server.ServerInfo,None
| Some (prevHistoryInfo, lastComm) ->
match prevHistoryInfo.Status with
| Success -> currency,server.ServerInfo,Some lastComm
| Fault faultInfo -> currency,server.ServerInfo, faultInfo.LastSuccessfulCommunication
let now = DateTime.Now
let newHistoryInfo: CachedValue<HistoryInfo> =
match stat.Fault with
| None ->
({ TimeSpan = stat.TimeSpan; Status = Success }, now)
| Some exInfo ->
({ TimeSpan = stat.TimeSpan
Status = Fault { Exception = exInfo
LastSuccessfulCommunication = previousLastSuccessfulCommunication }}, now)
let newServerDetails =
{
ServerInfo = serverInfo
CommunicationHistory = Some newHistoryInfo
}
let serversForCurrency =
match sessionServerRanking.TryFind currency with
| None -> Seq.empty
| Some servers -> servers
let newServersForCurrency =
Seq.append (seq { yield newServerDetails }) serversForCurrency
let newServerList = sessionServerRanking.Add(currency, newServersForCurrency)
let newCachedValue = SaveServerRankingsToDisk newServerList
sessionServerRanking <- newCachedValue
)
member __.GetServers (currency: Currency): seq<ServerDetails> =
lock cacheFiles.ServerStats (fun _ ->
match sessionServerRanking.TryFind currency with
| None ->
failwith <| SPrintF1 "Initialization of servers' cache failed? currency %A not found" currency
| Some servers -> servers
)
member __.ExportServers (): Option<string> =
lock cacheFiles.ServerStats (fun _ ->
LoadFromDiskInner cacheFiles.ServerStats
)
member __.BootstrapServerStatsFromTrustedSource(): Async<unit> =
let downloadFile url: Async<Option<string>> =
let tryDownloadFile url: Async<string> =
async {
use httpClient = new HttpClient()
let uri = Uri url
let! response = Async.AwaitTask (httpClient.GetAsync uri)
let isSuccess = response.IsSuccessStatusCode
let! content = Async.AwaitTask <| response.Content.ReadAsStringAsync()
if isSuccess then
return content
else
Infrastructure.LogError ("WARNING: error trying to retrieve server stats: " + content)
return failwith content
}
async {
try
let! content = tryDownloadFile url
return Some content
with
// should we specify HttpRequestException?
| ex ->
Infrastructure.ReportWarning ex
|> ignore<bool>
return None
}
let targetBranch = "master"
let orgName1 = "nblockchain"
let orgName2 = "World"
let projName = "geewallet"
let ghBaseUrl,glBaseUrl,gnomeBaseUrl =
"https://raw.githubusercontent.com","https://gitlab.com","https://gitlab.gnome.org"
let pathToFile = SPrintF1 "src/GWallet.Backend/%s" ServerRegistry.ServersEmbeddedResourceFileName
let gitHub =
SPrintF5 "%s/%s/%s/%s/%s"
ghBaseUrl orgName1 projName targetBranch pathToFile
let gitLab =
SPrintF5 "%s/%s/%s/raw/%s/%s"
glBaseUrl orgName1 projName targetBranch pathToFile
// not using GNOME hosting anymore
let _gnomeGitLab =
SPrintF5 "%s/%s/%s/raw/%s/%s"
gnomeBaseUrl orgName2 projName targetBranch pathToFile
let allUrls = [ gitHub; gitLab ]
let allJobs =
allUrls |> Seq.map downloadFile
async {
let! maybeLastServerStatsInJson = Async.Choice allJobs
match maybeLastServerStatsInJson with
| None ->
Infrastructure.LogError "WARNING: Couldn't reach a trusted server to retrieve server stats to bootstrap cache, running in offline mode?"
| Some lastServerStatsInJson ->
let lastServerStats = ImportFromJson<ServerRanking> lastServerStatsInJson
lock cacheFiles.ServerStats (fun _ ->
let savedServerStats = SaveServerRankingsToDisk lastServerStats
sessionServerRanking <- savedServerStats
)
}
member __.FirstRun
with get() = firstRun
let Instance = MainCache (None, TimeSpan.FromDays 1.0)