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

[Wallet synchronization] Introduce BlockDownloadingService #12184

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
4a198c3
Fix some warnings
kiminuo Jan 4, 2024
455e3e4
Implement `ParallelBlockDownloadService`
kiminuo Jan 4, 2024
4ef8974
Merge commit '0b588849964ee1358aeec88070231ccba3f34e09' into feature/…
kiminuo Jan 6, 2024
876c26d
More tests
kiminuo Jan 6, 2024
65c9332
Merge branch 'master' into feature/2024-01-04-ParallelBlockDownloadin…
kiminuo Jan 9, 2024
4b51771
Merge branch 'master' into feature/2024-01-04-ParallelBlockDownloadin…
kiminuo Jan 11, 2024
c9dcfe7
Rename class
kiminuo Jan 11, 2024
1f22226
Re-design to work with priorities and refactor code
kiminuo Jan 11, 2024
721cec5
Merge branch 'master' into feature/2024-01-04-ParallelBlockDownloadin…
kiminuo Jan 11, 2024
5c042fc
Merge commit 'd724622604d14bc00e8db4e5849f99f7d04c61b2' into feature/…
kiminuo Jan 12, 2024
5a50ac1
Add results of operations
kiminuo Jan 12, 2024
37a7648
Improve
kiminuo Jan 12, 2024
7d81d7b
Remove `IRepository` and replace it with `IFileSystemBlockRepository`
kiminuo Jan 12, 2024
25e0f7f
Merge remote-tracking branch 'kiminuo/feature/2024-01-12-BlockReposit…
kiminuo Jan 12, 2024
373b516
Support for FileSystemBlockRepository
kiminuo Jan 12, 2024
96c9553
CF?
kiminuo Jan 12, 2024
f5237ea
Merge remote-tracking branch 'origin/master' into feature/2024-01-04-…
kiminuo Jan 12, 2024
1827f6a
Allow to set maximum number of requests
kiminuo Jan 13, 2024
467305a
Tests
kiminuo Jan 13, 2024
71fa3b2
WIP
kiminuo Jan 13, 2024
1396ba9
Integration test
kiminuo Jan 13, 2024
9dbc357
WIP
kiminuo Jan 13, 2024
0cc9959
Merge commit '7557f00e062693abd4adddb6c72d028c625d97f5' into feature/…
kiminuo Jan 15, 2024
390ab62
WIP
kiminuo Jan 16, 2024
e84724e
Merge commit 'a465c931cc9ef2e93a7a83e32d3cea0d3fc085e2' into feature/…
kiminuo Jan 16, 2024
e3b614d
Add sources and source data
kiminuo Jan 16, 2024
e600a50
rewire
kiminuo Jan 19, 2024
e102100
Remove `enqueue`
kiminuo Jan 19, 2024
c1d1c82
Add P2pSourceDataCode
kiminuo Jan 19, 2024
e1a7878
Use Clement's impl
kiminuo Jan 19, 2024
739f385
New P2p block provider overload
kiminuo Jan 19, 2024
aa525e4
No fallback. Maybe later.
kiminuo Jan 19, 2024
ee5599c
Merge commit '0afc02b697dadf149045f114775d5ee632da9404' into feature/…
kiminuo Jan 19, 2024
5ae2c8d
Add source request functionality
kiminuo Jan 21, 2024
7c48966
public -> internal
kiminuo Jan 22, 2024
1d95ba9
P2PNodesManager: Add DisconnectNode
kiminuo Jan 22, 2024
26e230b
Remove ^M
kiminuo Jan 22, 2024
47eb687
Fix nullability
kiminuo Jan 22, 2024
df095cc
P2PNodesManager: Use ConnectedNodesCount everywhere
kiminuo Jan 22, 2024
f7dd206
double l
kiminuo Jan 22, 2024
394c63c
better
kiminuo Jan 22, 2024
9c021b8
Remove try-catch
kiminuo Jan 22, 2024
43a960e
Clarify
kiminuo Jan 22, 2024
df64644
Update WalletWasabi/Wallets/FilterProcessor/BlockDownloadService.cs
kiminuo Jan 23, 2024
fbfb283
Remove block(s) from file cache when reorged
kiminuo Jan 23, 2024
3398ee5
`BlockFilterIterator`: Make `MaxNumberFiltersInMemory` public
kiminuo Jan 23, 2024
904270e
Simplify
kiminuo Jan 23, 2024
897b8d8
Remove max-attempts behavior, modify tests
kiminuo Jan 23, 2024
dca0ad7
Add NoSuchProviderResult
kiminuo Jan 24, 2024
e44d784
bigger timeout
kiminuo Jan 24, 2024
45be3e3
Improve parallel behavior
kiminuo Jan 25, 2024
9088c42
Merge commit '7137843fd202dc7ea95d6172a93b9184a22cf156' into feature/…
kiminuo Jan 25, 2024
9fc90f4
Merge commit '7207224510a653789ef9cba62c470235793386f8' into feature/…
kiminuo Feb 10, 2024
e835870
Revert "P2PNodesManager: Use ConnectedNodesCount everywhere"
Jan 30, 2024
3ec7c62
Code review suggestions.
Jan 30, 2024
26785d0
fix cf
Jan 30, 2024
f8a264a
Style suggestions
Jan 31, 2024
bc57424
Clarify P2P and Trusted
Feb 1, 2024
5d190d4
Add remarks for P2pSourceRequest.Automatic
Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion WalletWasabi.Daemon/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ public Global(string dataDir, string configFilePath, Config config)

// Block providers.
SpecificNodeBlockProvider = new SpecificNodeBlockProvider(Network, Config.ServiceConfiguration, HttpClientFactory.TorEndpoint);
P2PNodesManager = new P2PNodesManager(Network, HostedServices.Get<P2pNetwork>().Nodes, HttpClientFactory.IsTorEnabled);

var blockProvider = new SmartBlockProvider(
BitcoinStore.BlockRepository,
BitcoinCoreNode?.RpcClient is null ? null : new RpcBlockProvider(BitcoinCoreNode.RpcClient),
SpecificNodeBlockProvider,
new P2PBlockProvider(Network, HostedServices.Get<P2pNetwork>().Nodes, HttpClientFactory.IsTorEnabled),
new P2PBlockProvider(P2PNodesManager),
Cache);

WalletManager = new WalletManager(config.Network, DataDir, new WalletDirectories(Config.Network, DataDir), BitcoinStore, wasabiSynchronizer, HostedServices.Get<HybridFeeProvider>(), blockProvider, config.ServiceConfiguration);
Expand Down Expand Up @@ -153,6 +155,7 @@ public Global(string dataDir, string configFilePath, Config config)
public TransactionBroadcaster TransactionBroadcaster { get; set; }
public CoinJoinProcessor? CoinJoinProcessor { get; set; }
private SpecificNodeBlockProvider SpecificNodeBlockProvider { get; }
private P2PNodesManager P2PNodesManager { get; }
private TorProcessManager? TorManager { get; set; }
public CoreNode? BitcoinCoreNode { get; private set; }
public TorStatusChecker TorStatusChecker { get; set; }
Expand Down
110 changes: 110 additions & 0 deletions WalletWasabi.Tests/IntegrationTests/BlockDownloadTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Moq;
using NBitcoin;
using NBitcoin.Protocol;
using NBitcoin.Protocol.Behaviors;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Helpers;
using WalletWasabi.Logging;
using WalletWasabi.Tests.Helpers;
using WalletWasabi.Wallets;
using WalletWasabi.Wallets.BlockProvider;
using WalletWasabi.Wallets.FilterProcessor;
using Xunit;
using static WalletWasabi.Wallets.FilterProcessor.BlockDownloadService;

namespace WalletWasabi.Tests.IntegrationTests;

/// <summary>
/// Integration tests for <see cref="BlockDownloadService"/>.
/// </summary>
public class BlockDownloadTests
{
public static readonly SortedDictionary<uint, uint256> HeightToBlockHash = new()
{
{ 610_000, new uint256("0000000000000000000a6f607f74db48dae0a94022c10354536394c17672b7f7") },
{ 610_001, new uint256("0000000000000000000b9cfe321443cb2ced7a4182b8594f077ee4e23d6c2ae2") },
{ 610_002, new uint256("0000000000000000000aef4754dfc99fed93f40d8b90d06037075bc59af3ab4d") },
{ 610_003, new uint256("00000000000000000012afa0710628d1c504f1d40e1fc1b71c8cc66982547d0b") },
{ 610_004, new uint256("000000000000000000081bb924d567c05c39dabc7feda74b02a1327dc376634d") },
{ 610_005, new uint256("0000000000000000000f68159a871ea320691afcb3e0b7e8e3e03f53c0c0d58d") },
{ 610_006, new uint256("0000000000000000000571a7383c5679bf6967b6eb43efa807167ea3d1761e84") },
{ 610_007, new uint256("0000000000000000000ab3657ecbfa7e4e555d58f4fe6a0279bee8ea29ae2df7") },
{ 610_008, new uint256("000000000000000000157e58b222e0097afee38d6920f0750e177c0d02a5d593") },
{ 610_009, new uint256("00000000000000000003354e9f48af4b5850fd884f38886d00b92bfd4a015d54") },
};

[Fact]
public async Task BlockDownloadingTestAsync()
{
using CancellationTokenSource testCts = new(TimeSpan.FromMinutes(10));

Mock<IFileSystemBlockRepository> mockFileSystemBlockRepository = new(MockBehavior.Strict);
_ = mockFileSystemBlockRepository.Setup(c => c.TryGetAsync(It.IsAny<uint256>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((Block?)null);
_ = mockFileSystemBlockRepository.Setup(c => c.SaveAsync(It.IsAny<Block>(), It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);

RuntimeParams.SetDataDir(Path.Combine(Common.DataDir, "RegTests", "Backend"));
await RuntimeParams.LoadAsync();

string roamingDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

string addressManagerFilePath = Path.Combine(roamingDir, "WalletWasabi", "Client", "BitcoinP2pNetwork", $"AddressManager{Network.Main}.dat");
AddressManager addressManager = AddressManager.LoadPeerFile(addressManagerFilePath);
AddressManagerBehavior addressManagerBehavior = new(addressManager)
{
Mode = AddressManagerBehaviorMode.Discover
};

using NodesGroup nodes = new(Network.Main);
nodes.NodeConnectionParameters.TemplateBehaviors.Add(addressManagerBehavior);
nodes.Connect();

while (nodes.ConnectedNodes.Count == 0)
{
await Task.Delay(1000, testCts.Token);
}

P2PBlockProvider p2PBlockProvider = new(Network.Main, nodes, isTorEnabled: false);
using BlockDownloadService blockDownloadService = new(mockFileSystemBlockRepository.Object, trustedFullNodeBlockProviders: [], p2PBlockProvider);

try
{
await blockDownloadService.StartAsync(testCts.Token);

Stopwatch stopwatch = Stopwatch.StartNew();

List<Task<IResult>> tasks = [];

foreach ((uint height, uint256 blockHash) in HeightToBlockHash)
{
Task<IResult> taskCompletionSource = blockDownloadService.TryGetBlockAsync(P2pSourceRequest.Automatic, blockHash, new Priority(SyncType.Complete, height), testCts.Token);
tasks.Add(taskCompletionSource);
}

await Task.WhenAll(tasks);

uint blockHeight = HeightToBlockHash.First().Key;

foreach (Task<IResult> task in tasks)
{
IResult result = await task;
Logger.LogInfo($"Block #{blockHeight} finished with result: {result}");

blockHeight++;
}

stopwatch.Stop();
Logger.LogInfo($"Test finished in {stopwatch.ElapsedMilliseconds} ms");
}
finally
{
await blockDownloadService.StopAsync(testCts.Token);
}
}
}
Loading
Loading