Cache mempool hashes and implement mempool hash stripping to server side.
MODIFY
WalletWasabi.Backend/Controllers/BlockchainController.cs
WalletWasabi.Backend/Controllers/BlockchainController.cs
@@ -148,22 +148,50 @@ namespace WalletWasabi.Backend.Controllers
/// <summary>
/// Gets mempool hashes.
/// </summary>
+ /// <param name="compactness">Can strip the last x characters from the hashes.</param>
/// <returns>A collection of transaction hashes.</returns>
/// <response code="200">A collection of transaction hashes.</response>
/// <response code="404">Invalid model state.</response>
[HttpGet("mempool-hashes")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
- public async Task<IActionResult> GetMempoolHashesAsync()
+ [ResponseCache(Duration = 3, Location = ResponseCacheLocation.Client)]
+ public async Task<IActionResult> GetMempoolHashesAsync([FromQuery]int compactness = 64)
{
- if (!ModelState.IsValid)
+ if (compactness < 1 || compactness > 64 || !ModelState.IsValid)
+ {
+ return BadRequest("Invalid compactness parameter is provided.");
+ }
+
+ IEnumerable<string> fulls = await GetRawMempoolStringsAsync();
+
+ if (compactness == 64)
+ {
+ return Ok(fulls);
+ }
+ else
{
- return BadRequest("Invalid model state.");
+ IEnumerable<string> compacts = fulls.Select(x => x.Substring(0, compactness));
+ return Ok(compacts);
}
+ }
+
+ internal async Task<IEnumerable<string>> GetRawMempoolStringsAsync()
+ {
+ var cacheKey = $"{nameof(GetRawMempoolStringsAsync)}";
+
+ if (!Cache.TryGetValue(cacheKey, out IEnumerable<string> hashes))
+ {
+ uint256[] transactionHashes = await Global.RpcClient.GetRawMempoolAsync();
+
+ hashes = transactionHashes.Select(x => x.ToString());
- var transactionHashes = await Global.RpcClient.GetRawMempoolAsync();
+ var cacheEntryOptions = new MemoryCacheEntryOptions()
+ .SetAbsoluteExpiration(TimeSpan.FromSeconds(3));
- return Ok(transactionHashes.Select(x => x.ToString()));
+ Cache.Set(cacheKey, hashes, cacheEntryOptions);
+ }
+ return hashes;
}
/// <summary>

MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -292,7 +292,7 @@ namespace WalletWasabi.Services
ProcessTransaction(tx);
MemPool.TransactionHashes.TryAdd(tx.GetHash());
- Logger.LogInfo<WalletService>($"Transaction is tested against the backend's transaction hahses set: {tx.GetHash()}.");
+ Logger.LogInfo<WalletService>($"Transaction was successfully tested against the backend's mempool hahses: {tx.GetHash()}.");
}
}
}

MODIFY
WalletWasabi/WebClients/Wasabi/WasabiClient.cs
WalletWasabi/WebClients/Wasabi/WasabiClient.cs
@@ -151,6 +151,30 @@ namespace WalletWasabi.WebClients.Wasabi
}
}
+ /// <summary>
+ /// Gets mempool hashes, but strips the last x characters of each hash.
+ /// </summary>
+ /// <param name="compactness">1 to 64</param>
+ public async Task<IEnumerable<string>> GetMempoolHashesAsync(int compactness, CancellationToken cancel = default)
+ {
+ using (var response = await TorClient.SendAndRetryAsync(HttpMethod.Get,
+ HttpStatusCode.OK,
+ $"/api/v{Helpers.Constants.BackendMajorVersion}/btc/blockchain/mempool-hashes?compactness={compactness}",
+ cancel: cancel))
+ {
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ await response.ThrowRequestExceptionFromContentAsync();
+ }
+
+ using (HttpContent content = response.Content)
+ {
+ var strings = await content.ReadAsJsonAsync<IEnumerable<string>>();
+ return strings;
+ }
+ }
+ }
+
#endregion blockchain
#region offchain

Cache mempool hashes and implement mempool hash stripping to server side.