From c3684eb06443f1c18a9cf4df4abc1341cd69506c Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 15 Feb 2018 15:17:12 +0900 Subject: [PATCH] BTCPayWallet should be singleton per cryptcode --- BTCPayServer/Services/Wallets/BTCPayWallet.cs | 1 + .../Services/Wallets/BTCPayWalletProvider.cs | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/BTCPayServer/Services/Wallets/BTCPayWallet.cs b/BTCPayServer/Services/Wallets/BTCPayWallet.cs index efdc01981..c7a307725 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWallet.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWallet.cs @@ -102,6 +102,7 @@ namespace BTCPayServer.Services.Wallets public void InvalidateCache(DerivationStrategyBase strategy) { _MemoryCache.Remove("CACHEDCOINS_" + strategy.ToString()); + _FetchingUTXOs.TryRemove(strategy.ToString(), out var unused); } ConcurrentDictionary> _FetchingUTXOs = new ConcurrentDictionary>(); diff --git a/BTCPayServer/Services/Wallets/BTCPayWalletProvider.cs b/BTCPayServer/Services/Wallets/BTCPayWalletProvider.cs index f29b67538..ae04004c6 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWalletProvider.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWalletProvider.cs @@ -21,8 +21,15 @@ namespace BTCPayServer.Services.Wallets _Client = client; _NetworkProvider = networkProvider; _Options = memoryCacheOption; + + foreach(var network in networkProvider.GetAll()) + { + _Wallets.Add(network.CryptoCode, new BTCPayWallet(_Client.GetExplorerClient(network.CryptoCode), new MemoryCache(_Options), network)); + } } + Dictionary _Wallets = new Dictionary(); + public BTCPayWallet GetWallet(BTCPayNetwork network) { if (network == null) @@ -33,11 +40,8 @@ namespace BTCPayServer.Services.Wallets { if (cryptoCode == null) throw new ArgumentNullException(nameof(cryptoCode)); - var network = _NetworkProvider.GetNetwork(cryptoCode); - var client = _Client.GetExplorerClient(cryptoCode); - if (network == null || client == null) - return null; - return new BTCPayWallet(client, new MemoryCache(_Options), network); + _Wallets.TryGetValue(cryptoCode, out var result); + return result; } public bool IsAvailable(BTCPayNetwork network)