Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements regarding UpdateManager and LegalChecker #12121

Merged
merged 8 commits into from
Dec 21, 2023

Conversation

kiminuo
Copy link
Collaborator

@kiminuo kiminuo commented Dec 21, 2023

Extracted from #12094

Please review commit by commit.

@@ -29,8 +29,8 @@ public ClearnetHttpClient(HttpClient httpClient, Func<Uri>? baseUriGetter)
private HttpClient HttpClient { get; }

/// <inheritdoc cref="HttpClient.SendAsync(HttpRequestMessage, CancellationToken)"/>
public virtual Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default)
public virtual Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuation of the effort to remove defaults in the networking code.

Comment on lines -292 to -304
public event EventHandler<UpdateStatus>? UpdateAvailableToGet;

public string InstallerDir { get; }
public IHttpClient HttpClient { get; }

///<summary> Comes from config file. Decides Wasabi should download the new installer in the background or not.</summary>
public bool DownloadNewVersion { get; }

///<summary> Install new version on shutdown or not.</summary>
public bool DoUpdateOnClose { get; set; }

private UpdateChecker? UpdateChecker { get; set; }
private CancellationToken CancellationToken { get; set; }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved up. I'm not sure why it's here.

Comment on lines -350 to -355
public void Initialize(UpdateChecker updateChecker, CancellationToken cancelationToken)
{
UpdateChecker = updateChecker;
CancellationToken = cancelationToken;
updateChecker.UpdateStatusChanged += UpdateChecker_UpdateStatusChangedAsync;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved up as well.

@kiminuo kiminuo force-pushed the feature/2023-12-21-Minor-changes branch from ea1db52 to df4b31c Compare December 21, 2023 08:44
WasabiClient = Synchronizer.HttpClientFactory.SharedWasabiClient;
Synchronizer.PropertyChanged += Synchronizer_PropertyChanged;
}

public event EventHandler<UpdateStatus>? UpdateStatusChanged;

private WasabiSynchronizer Synchronizer { get; }
public UpdateStatus UpdateStatus { get; private set; }
private UpdateStatus UpdateStatus { get; set; }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be public.

@@ -12,15 +12,15 @@ public class UpdateChecker : PeriodicRunner
public UpdateChecker(TimeSpan period, WasabiSynchronizer synchronizer) : base(period)
{
Synchronizer = synchronizer;
UpdateStatus = new UpdateStatus(true, true, new Version(), 0, new Version());
UpdateStatus = new UpdateStatus(backendCompatible: true, clientUpToDate: true, new Version(), currentBackendMajorVersion: 0, new Version());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to show what is what.

public string LegalFolder { get; }
public string ProvisionalLegalFolder { get; }
public LegalDocuments? CurrentLegalDocument { get; private set; }
private LegalDocuments? ProvisionalLegalDocument { get; set; }
private TaskCompletionSource<LegalDocuments> LatestDocumentTaskCompletion { get; } = new();

public async Task InitializeAsync(UpdateChecker updateChecker)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed through ctor.

@@ -89,8 +95,7 @@ public static async Task VerifyInstallerFileHashesAsync(string[] finalFiles, str
public static async Task<byte[]> GetShaComputedBytesOfFileAsync(string filePath, CancellationToken cancellationToken = default)
{
byte[] bytes = await File.ReadAllBytesAsync(filePath, cancellationToken).ConfigureAwait(false);
using SHA256 sha = SHA256.Create();
byte[] computedHash = sha.ComputeHash(bytes);
byte[] computedHash = SHA256.HashData(bytes);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new API that does not require additional allocation.

@@ -30,12 +30,18 @@ public static async Task VerifySha256SumsFileAsync(string sha256SumsAscFilePath)
// Read the signature file
var wasabiSignatureFilePath = Path.ChangeExtension(sha256SumsAscFilePath, "wasabisig");
string signatureText = await File.ReadAllTextAsync(wasabiSignatureFilePath).ConfigureAwait(false);
byte[] sigBytes = Convert.FromBase64String(signatureText);
var wasabiSignature = ECDSASignature.FromDER(sigBytes);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted to VerifySha256Sum, that method can be more easily tested (not in this PR).

@kiminuo kiminuo changed the title Minor improvements regarding UpdateManager and `LegalChecker Minor improvements regarding UpdateManager and LegalChecker Dec 21, 2023
@kiminuo kiminuo mentioned this pull request Dec 21, 2023
@kiminuo kiminuo marked this pull request as ready for review December 21, 2023 09:04
Copy link
Collaborator

@adamPetho adamPetho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK, one question and one nit below

private UpdateChecker UpdateChecker { get; }
private CancellationToken CancellationToken { get; set; }

public void Initialize(CancellationToken cancelationToken)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to get rid of this function and failed miserably. (unless I make some questionable changes)

Do you know why we need THE application stopping cancel token here?

Wouldn't a local CTS be enough?
We could cancel and dispose the local CTS when the Dispose() method is called on stopping WW.
We could create that local CTS in ctor, then we don't need this method anymore.

WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a quick look and it seems to me that you are right, a local CTS should be sufficient here. Please open a PR for this, I think it would be a nice cleanup.

btw: What I don't like is

private async void UpdateChecker_UpdateStatusChangedAsync(object? sender, UpdateStatus updateStatus)

where we use async code with async void instead of async Task. This can crash the software if there is an unhandled exception IMO.

Comment on lines -41 to 42
UpdateChecker = updateChecker;
UpdateChecker.UpdateStatusChanged += UpdateChecker_UpdateStatusChangedAsync;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:
We could move up the event subscription to the ctor as well, so they are next to each other.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I don't think it's a trivial change because between ctor and initialize it would behave differently. It's likely OK but it needs to be explained why it's correct. If you like, you can open a PR for this.

@kiminuo kiminuo merged commit b4f16ae into WalletWasabi:master Dec 21, 2023
7 checks passed
@kiminuo kiminuo deleted the feature/2023-12-21-Minor-changes branch December 21, 2023 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants