Implement ThirpartyFeeProvider
MODIFY
WalletWasabi/Blockchain/Analysis/FeesEstimation/ThirdPartyFeeProvider.cs
WalletWasabi/Blockchain/Analysis/FeesEstimation/ThirdPartyFeeProvider.cs
@@ -6,6 +6,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Backend.Models.Responses;
+using WalletWasabi.Bases;
using WalletWasabi.Models;
using WalletWasabi.Nito.AsyncEx;
using WalletWasabi.Services;
@@ -13,9 +14,10 @@ using WalletWasabi.WebClients.BlockstreamInfo;
namespace WalletWasabi.Blockchain.Analysis.FeesEstimation
{
- public class ThirdPartyFeeProvider : IHostedService
+ public class ThirdPartyFeeProvider : PeriodicRunner
{
- public ThirdPartyFeeProvider(WasabiSynchronizer synchronizer, BlockstreamInfoFreeProvider blockstreamProvider)
+ public ThirdPartyFeeProvider(TimeSpan period, WasabiSynchronizer synchronizer, BlockstreamInfoFreeProvider blockstreamProvider)
+ : base(period)
{
Synchronizer = synchronizer;
BlockstreamProvider = blockstreamProvider;
@@ -30,7 +32,7 @@ namespace WalletWasabi.Blockchain.Analysis.FeesEstimation
public bool InError { get; private set; }
private AbandonedTasks ProcessingEvents { get; } = new();
- public Task StartAsync(CancellationToken cancellationToken)
+ public override async Task StartAsync(CancellationToken cancellationToken)
{
SetAllFeeEstimateIfLooksBetter(Synchronizer.LastResponse?.AllFeeEstimate);
SetAllFeeEstimateIfLooksBetter(BlockstreamProvider.LastAllFeeEstimate);
@@ -38,15 +40,16 @@ namespace WalletWasabi.Blockchain.Analysis.FeesEstimation
Synchronizer.ResponseArrived += Synchronizer_ResponseArrived;
BlockstreamProvider.AllFeeEstimateArrived += OnAllFeeEstimateArrived;
- return Task.CompletedTask;
+ await base.StartAsync(cancellationToken).ConfigureAwait(false);
}
- public async Task StopAsync(CancellationToken cancellationToken)
+ public override async Task StopAsync(CancellationToken cancellationToken)
{
Synchronizer.ResponseArrived -= Synchronizer_ResponseArrived;
BlockstreamProvider.AllFeeEstimateArrived -= OnAllFeeEstimateArrived;
await ProcessingEvents.WhenAllAsync().ConfigureAwait(false);
+ await base.StopAsync(cancellationToken).ConfigureAwait(false);
}
private void Synchronizer_ResponseArrived(object? sender, SynchronizeResponse e)
@@ -67,72 +70,19 @@ namespace WalletWasabi.Blockchain.Analysis.FeesEstimation
var notify = false;
lock (Lock)
{
- if (LastAllFeeEstimate is null)
- {
- // If it wasn't set before, then set it regardless everything.
- notify = SetAllFeeEstimate(fees);
- }
- else if (sender is BlockstreamInfoFreeProvider)
- {
- var syncronizer = Synchronizer;
-
- if (syncronizer.LastResponse?.AllFeeEstimate?.IsAccurate is true && syncronizer.BackendStatus == BackendStatus.Connected)
- {
- // If backend is properly serving data, then we don't care about this event.
- return;
- }
- else
- {
- if (fees.IsAccurate)
- {
- // If the backend is not properly serving accurate data then, this is the best we got.
notify = SetAllFeeEstimate(fees);
}
- else
- {
- // If third parties are not ready, then let's try our best effort figuring out which data looks better:
-
- notify = SetAllFeeEstimateIfLooksBetter(fees);
- }
- }
- }
- else if (sender is WasabiSynchronizer)
- {
- if (fees.IsAccurate)
- {
- // If the backend is properly serving data, we're done here.
- notify = SetAllFeeEstimate(fees);
- }
- else
- {
- if (ThirdPartyFeeProvider.InError)
- {
- // If the user's full node isn't ready, but the backend is, then let's leave it to the backend.
- return;
- }
- else
- {
- // If neither user's full node, nor backend is ready, then let's try our best effort figuring out which data looks better:
- notify = SetAllFeeEstimateIfLooksBetter(fees);
- }
- }
- }
- }
if (notify)
{
- var accuracy = fees.IsAccurate ? "Accurate" : "Inaccurate";
- var from = fees.Estimations.First();
- var to = fees.Estimations.Last();
- Logger.LogInfo($"{accuracy} fee rates are acquired from {sender?.GetType()?.Name} ranging from target {from.Key} at {from.Value} sat/b to target {to.Key} at {to.Value} sat/b.");
- AllFeeEstimateChanged?.Invoke(this, fees);
+ AllFeeEstimateArrived?.Invoke(this, fees);
}
}
}
private bool SetAllFeeEstimateIfLooksBetter(AllFeeEstimate? fees)
{
- var current = AllFeeEstimate;
+ var current = LastAllFeeEstimate;
if (fees is null
|| fees == current
|| (current is not null && ((!fees.IsAccurate && current.IsAccurate) || fees.Estimations.Count <= current.Estimations.Count)))
@@ -145,12 +95,28 @@ namespace WalletWasabi.Blockchain.Analysis.FeesEstimation
/// <returns>True if changed.</returns>
private bool SetAllFeeEstimate(AllFeeEstimate fees)
{
- if (AllFeeEstimate == fees)
+ if (LastAllFeeEstimate == fees)
{
return false;
}
- AllFeeEstimate = fees;
+ LastAllFeeEstimate = fees;
return true;
}
+
+ protected override Task ActionAsync(CancellationToken cancel)
+ {
+ if (Synchronizer.BackendStatus == BackendStatus.Connected)
+ {
+ BlockstreamProvider.IsPaused = true;
+ InError = false;
+ }
+ else
+ {
+ BlockstreamProvider.IsPaused = false;
+ InError = BlockstreamProvider.InError;
+ }
+
+ return Task.CompletedTask;
+ }
}
}

Implement ThirpartyFeeProvider