forked from WalletWasabi/WalletWasabi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request WalletWasabi#12277 from SuperJMN/improvements/opti…
…mize-startup-pull-based Optimize application startup - alternate method
- Loading branch information
Showing
14 changed files
with
98 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,75 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Reactive; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
using DynamicData; | ||
using WalletWasabi.Blockchain.Keys; | ||
using WalletWasabi.Fluent.Extensions; | ||
using WalletWasabi.Blockchain.TransactionProcessing; | ||
using WalletWasabi.Wallets; | ||
|
||
namespace WalletWasabi.Fluent.Models.Wallets; | ||
|
||
[AutoInterface] | ||
public partial class AddressesModel : IDisposable | ||
public partial class AddressesModel | ||
{ | ||
private readonly CompositeDisposable _disposable = new(); | ||
private readonly KeyManager _keyManager; | ||
private readonly ISubject<HdPubKey> _newAddressGenerated = new Subject<HdPubKey>(); | ||
private readonly Wallet _wallet; | ||
private readonly SourceList<HdPubKey> _source; | ||
|
||
public AddressesModel(IObservable<Unit> addressesUpdated, KeyManager keyManager) | ||
public AddressesModel(Wallet wallet) | ||
{ | ||
_keyManager = keyManager; | ||
_wallet = wallet; | ||
_source = new SourceList<HdPubKey>(); | ||
_source.AddRange(GetUnusedKeys()); | ||
|
||
Cache = | ||
addressesUpdated.Fetch(GetAddresses, address => address.Text) | ||
.DisposeWith(_disposable); | ||
Observable.FromEventPattern<ProcessedResult>( | ||
h => wallet.WalletRelevantTransactionProcessed += h, | ||
h => wallet.WalletRelevantTransactionProcessed -= h) | ||
.Do(_ => UpdateUnusedKeys()) | ||
.Subscribe(); | ||
|
||
UnusedAddressesCache = | ||
Cache.Connect() | ||
.AutoRefresh(x => x.IsUsed) | ||
.Filter(x => !x.IsUsed) | ||
.AsObservableCache() | ||
.DisposeWith(_disposable); | ||
_newAddressGenerated | ||
.Do(address => _source.Add(address)) | ||
.Subscribe(); | ||
|
||
HasUnusedAddresses = UnusedAddressesCache.NotEmpty(); | ||
_source.Connect() | ||
.Transform(key => (IAddress) new Address(_wallet.KeyManager, key, Hide)) | ||
.Bind(out var unusedAddresses) | ||
.Subscribe(); | ||
|
||
Unused = unusedAddresses; | ||
} | ||
|
||
public IObservableCache<IAddress, string> Cache { get; } | ||
private IEnumerable<HdPubKey> GetUnusedKeys() => _wallet.KeyManager.GetKeys(x => x is { IsInternal: false, KeyState: KeyState.Clean, Labels.Count: > 0 }); | ||
|
||
public IObservableCache<IAddress, string> UnusedAddressesCache { get; } | ||
public IAddress NextReceiveAddress(IEnumerable<string> destinationLabels) | ||
{ | ||
var pubKey = _wallet.GetNextReceiveAddress(destinationLabels); | ||
var nextReceiveAddress = new Address(_wallet.KeyManager, pubKey, Hide); | ||
_newAddressGenerated.OnNext(pubKey); | ||
|
||
public IObservable<bool> HasUnusedAddresses { get; } | ||
return nextReceiveAddress; | ||
} | ||
|
||
public void Dispose() => _disposable.Dispose(); | ||
public ReadOnlyObservableCollection<IAddress> Unused { get; } | ||
|
||
private IEnumerable<IAddress> GetAddresses() => _keyManager | ||
.GetKeys() | ||
.Reverse() | ||
.Select(x => new Address(_keyManager, x)); | ||
public void Hide(Address address) | ||
{ | ||
_wallet.KeyManager.SetKeyState(KeyState.Locked, address.HdPubKey); | ||
_wallet.KeyManager.ToFile(); | ||
_source.Remove(address.HdPubKey); | ||
} | ||
|
||
private void UpdateUnusedKeys() | ||
{ | ||
var itemsToRemove = _source.Items | ||
.Where(item => item.KeyState != KeyState.Clean) | ||
.ToList(); | ||
|
||
foreach (var item in itemsToRemove) | ||
{ | ||
_source.Remove(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
WalletWasabi.Fluent/ViewModels/Wallets/Receive/AddressViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.