From c9b268b338a3789b687c9a6dbb43c201df895dd0 Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Mon, 8 Jul 2024 14:17:05 -0300 Subject: [PATCH] Cleanup `IndexBuilderService` (#13229) --- WalletWasabi.Backend/Global.cs | 2 +- WalletWasabi.Daemon/Global.cs | 2 +- .../Models/Wallets/WalletLoadWorkflow.cs | 2 +- .../RegressionTests/BackendTests.cs | 2 +- .../BitcoinCore/IndexBuilderServiceTests.cs | 16 +++--- .../UnitTests/SmartHeaderTests.cs | 6 +-- .../BlockFilters/IndexBuilderService.cs | 14 ++--- .../Blockchain/BlockFilters/IndexType.cs | 7 --- .../BlockFilters/IndexTypeConverter.cs | 14 ----- .../BlockFilters/StartingFilters.cs | 2 +- WalletWasabi/Blockchain/Blocks/SmartHeader.cs | 53 +++---------------- WalletWasabi/Wallets/Wallet.cs | 2 +- 12 files changed, 29 insertions(+), 93 deletions(-) delete mode 100644 WalletWasabi/Blockchain/BlockFilters/IndexType.cs delete mode 100644 WalletWasabi/Blockchain/BlockFilters/IndexTypeConverter.cs diff --git a/WalletWasabi.Backend/Global.cs b/WalletWasabi.Backend/Global.cs index b635d84c9af..fb6e183aec8 100644 --- a/WalletWasabi.Backend/Global.cs +++ b/WalletWasabi.Backend/Global.cs @@ -48,7 +48,7 @@ public Global(string dataDir, IRPCClient rpcClient, Config config) // Initialize index building var indexBuilderServiceDir = Path.Combine(DataDir, "IndexBuilderService"); var indexFilePath = Path.Combine(indexBuilderServiceDir, $"Index{RpcClient.Network}.dat"); - IndexBuilderService = new(IndexType.SegwitTaproot, RpcClient, HostedServices.Get(), indexFilePath); + IndexBuilderService = new(RpcClient, HostedServices.Get(), indexFilePath); MempoolMirror = new MempoolMirror(TimeSpan.FromSeconds(21), RpcClient, P2pNode); CoinJoinMempoolManager = new CoinJoinMempoolManager(CoinJoinIdStore, MempoolMirror); diff --git a/WalletWasabi.Daemon/Global.cs b/WalletWasabi.Daemon/Global.cs index 0d7cf684acd..e34f66a08be 100644 --- a/WalletWasabi.Daemon/Global.cs +++ b/WalletWasabi.Daemon/Global.cs @@ -224,7 +224,7 @@ public async Task InitializeNoWalletAsync(bool initializeSleepInhibitor, Termina catch (Exception ex) when (ex is not OperationCanceledException) { // If our internal data structures in the Bitcoin Store gets corrupted, then it's better to rescan all the wallets. - WalletManager.SetMaxBestHeight(SmartHeader.GetStartingHeader(Network, IndexType.SegwitTaproot).Height); + WalletManager.SetMaxBestHeight(SmartHeader.GetStartingHeader(Network).Height); throw; } diff --git a/WalletWasabi.Fluent/Models/Wallets/WalletLoadWorkflow.cs b/WalletWasabi.Fluent/Models/Wallets/WalletLoadWorkflow.cs index f168e0b1d2d..25c17bde7a5 100644 --- a/WalletWasabi.Fluent/Models/Wallets/WalletLoadWorkflow.cs +++ b/WalletWasabi.Fluent/Models/Wallets/WalletLoadWorkflow.cs @@ -123,7 +123,7 @@ private async Task SetInitValuesAsync(bool isBackendAvailable) uint clientTipHeight = Services.SmartHeaderChain.TipHeight; var tipHeight = Math.Max(serverTipHeight, clientTipHeight); - var startingHeight = SmartHeader.GetStartingHeader(_wallet.Network, IndexType.SegwitTaproot).Height; + var startingHeight = SmartHeader.GetStartingHeader(_wallet.Network).Height; var bestHeight = (uint)_wallet.KeyManager.GetBestHeight(SyncType.Complete).Value; _filterProcessStartingHeight = bestHeight < startingHeight ? startingHeight : bestHeight; diff --git a/WalletWasabi.Tests/RegressionTests/BackendTests.cs b/WalletWasabi.Tests/RegressionTests/BackendTests.cs index 736aef70de1..ecf7e07621d 100644 --- a/WalletWasabi.Tests/RegressionTests/BackendTests.cs +++ b/WalletWasabi.Tests/RegressionTests/BackendTests.cs @@ -247,7 +247,7 @@ public async Task FilterBuilderTestAsync() var indexBuilderServiceDir = Helpers.Common.GetWorkDir(); var indexFilePath = Path.Combine(indexBuilderServiceDir, $"Index{rpc.Network}.dat"); - IndexBuilderService indexBuilderService = new(IndexType.SegwitTaproot, rpc, global.HostedServices.Get(), indexFilePath); + IndexBuilderService indexBuilderService = new(rpc, global.HostedServices.Get(), indexFilePath); try { indexBuilderService.Synchronize(); diff --git a/WalletWasabi.Tests/UnitTests/BitcoinCore/IndexBuilderServiceTests.cs b/WalletWasabi.Tests/UnitTests/BitcoinCore/IndexBuilderServiceTests.cs index 7f461ec29ab..e2f91b3a14c 100644 --- a/WalletWasabi.Tests/UnitTests/BitcoinCore/IndexBuilderServiceTests.cs +++ b/WalletWasabi.Tests/UnitTests/BitcoinCore/IndexBuilderServiceTests.cs @@ -28,7 +28,7 @@ public async Task SegwitTaprootUnsynchronizedBitcoinNodeAsync() }), }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.SegwitTaproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -55,7 +55,7 @@ public async Task SegwitTaprootStalledBitcoinNodeAsync() } }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.SegwitTaproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -86,7 +86,7 @@ public async Task SegwitTaprootSynchronizingBitcoinNodeAsync() OnGetVerboseBlockAsync = (hash) => Task.FromResult(blockchain.Single(x => x.Hash == hash)) }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.SegwitTaproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -156,7 +156,7 @@ public async Task SegwitTaprootSynchronizedBitcoinNodeAsync() OnGetVerboseBlockAsync = (hash) => Task.FromResult(blockchain.Single(x => x.Hash == hash)) }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.SegwitTaproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -182,7 +182,7 @@ public async Task TaprootUnsynchronizedBitcoinNodeAsync() }), }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.Taproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -209,7 +209,7 @@ public async Task TaprootStalledBitcoinNodeAsync() } }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.Taproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -240,7 +240,7 @@ public async Task TaprootSynchronizingBitcoinNodeAsync() OnGetVerboseBlockAsync = (hash) => Task.FromResult(blockchain.Single(x => x.Hash == hash)) }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.Taproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); @@ -268,7 +268,7 @@ public async Task TaprootSynchronizedBitcoinNodeAsync() OnGetVerboseBlockAsync = (hash) => Task.FromResult(blockchain.Single(x => x.Hash == hash)) }; using var blockNotifier = new BlockNotifier(TimeSpan.MaxValue, rpc); - var indexer = new IndexBuilderService(IndexType.Taproot, rpc, blockNotifier, "filters.txt"); + var indexer = new IndexBuilderService(rpc, blockNotifier, "filters.txt"); indexer.Synchronize(); diff --git a/WalletWasabi.Tests/UnitTests/SmartHeaderTests.cs b/WalletWasabi.Tests/UnitTests/SmartHeaderTests.cs index d35a7e506b0..1aad49ba23f 100644 --- a/WalletWasabi.Tests/UnitTests/SmartHeaderTests.cs +++ b/WalletWasabi.Tests/UnitTests/SmartHeaderTests.cs @@ -23,9 +23,9 @@ public void ConstructorTests() [Fact] public void StartingHeaderTests() { - var startingMain = SmartHeader.GetStartingHeader(Network.Main, IndexType.SegwitTaproot); - var startingTest = SmartHeader.GetStartingHeader(Network.TestNet, IndexType.SegwitTaproot); - var startingReg = SmartHeader.GetStartingHeader(Network.RegTest, IndexType.SegwitTaproot); + var startingMain = SmartHeader.GetStartingHeader(Network.Main); + var startingTest = SmartHeader.GetStartingHeader(Network.TestNet); + var startingReg = SmartHeader.GetStartingHeader(Network.RegTest); var expectedHashMain = new uint256("0000000000000000001c8018d9cb3b742ef25114f27563e3fc4a1902167f9893"); var expectedPrevHashMain = new uint256("000000000000000000cbeff0b533f8e1189cf09dfbebf57a8ebe349362811b80"); diff --git a/WalletWasabi/Blockchain/BlockFilters/IndexBuilderService.cs b/WalletWasabi/Blockchain/BlockFilters/IndexBuilderService.cs index b8217e66e24..77450b6d578 100644 --- a/WalletWasabi/Blockchain/BlockFilters/IndexBuilderService.cs +++ b/WalletWasabi/Blockchain/BlockFilters/IndexBuilderService.cs @@ -29,16 +29,13 @@ public class IndexBuilderService private long _workerCount; - public IndexBuilderService(IndexType indexType, IRPCClient rpc, BlockNotifier blockNotifier, string indexFilePath) + public IndexBuilderService(IRPCClient rpc, BlockNotifier blockNotifier, string indexFilePath) { - IndexType = indexType; RpcClient = Guard.NotNull(nameof(rpc), rpc); BlockNotifier = Guard.NotNull(nameof(blockNotifier), blockNotifier); IndexFilePath = Guard.NotNullOrEmptyOrWhitespace(nameof(indexFilePath), indexFilePath); - PubKeyTypes = IndexTypeConverter.ToRpcPubKeyTypes(IndexType); - - StartingHeight = SmartHeader.GetStartingHeader(RpcClient.Network, IndexType).Height; + StartingHeight = SmartHeader.GetStartingHeader(RpcClient.Network).Height; _serviceStatus = NotStarted; @@ -85,9 +82,8 @@ public IndexBuilderService(IndexType indexType, IRPCClient rpc, BlockNotifier bl public bool IsRunning => Interlocked.Read(ref _serviceStatus) == Running; private bool IsStopping => Interlocked.Read(ref _serviceStatus) >= Stopping; public DateTimeOffset LastFilterBuildTime { get; set; } - private IndexType IndexType { get; } - private RpcPubkeyType[] PubKeyTypes { get; } + private RpcPubkeyType[] PubKeyTypes { get; } = [RpcPubkeyType.TxWitnessV0Keyhash, RpcPubkeyType.TxWitnessV1Taproot]; public static GolombRiceFilter CreateDummyEmptyFilter(uint256 blockHash) { @@ -215,11 +211,11 @@ public void Synchronize() // If not close to the tip, just log debug. if (syncInfo.BlockCount - nextHeight <= 3 || nextHeight % 100 == 0) { - Logger.LogInfo($"Created {Enum.GetName(IndexType)} filter for block: {nextHeight}."); + Logger.LogInfo($"Created filter for block: {nextHeight}."); } else { - Logger.LogDebug($"Created {Enum.GetName(IndexType)} filter for block: {nextHeight}."); + Logger.LogDebug($"Created filter for block: {nextHeight}."); } LastFilterBuildTime = DateTimeOffset.UtcNow; } diff --git a/WalletWasabi/Blockchain/BlockFilters/IndexType.cs b/WalletWasabi/Blockchain/BlockFilters/IndexType.cs deleted file mode 100644 index 3a75979ec0d..00000000000 --- a/WalletWasabi/Blockchain/BlockFilters/IndexType.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace WalletWasabi.Blockchain.BlockFilters; - -public enum IndexType -{ - SegwitTaproot, - Taproot -} diff --git a/WalletWasabi/Blockchain/BlockFilters/IndexTypeConverter.cs b/WalletWasabi/Blockchain/BlockFilters/IndexTypeConverter.cs deleted file mode 100644 index ab967fa8ac7..00000000000 --- a/WalletWasabi/Blockchain/BlockFilters/IndexTypeConverter.cs +++ /dev/null @@ -1,14 +0,0 @@ -using WalletWasabi.BitcoinCore.Rpc.Models; - -namespace WalletWasabi.Blockchain.BlockFilters; - -public static class IndexTypeConverter -{ - public static RpcPubkeyType[] ToRpcPubKeyTypes(IndexType indexType) - => indexType switch - { - IndexType.SegwitTaproot => new[] { RpcPubkeyType.TxWitnessV0Keyhash, RpcPubkeyType.TxWitnessV1Taproot }, - IndexType.Taproot => new[] { RpcPubkeyType.TxWitnessV1Taproot }, - _ => throw new NotSupportedException("Index type not supported."), - }; -} diff --git a/WalletWasabi/Blockchain/BlockFilters/StartingFilters.cs b/WalletWasabi/Blockchain/BlockFilters/StartingFilters.cs index 0e9ffd27415..2d0c57663be 100644 --- a/WalletWasabi/Blockchain/BlockFilters/StartingFilters.cs +++ b/WalletWasabi/Blockchain/BlockFilters/StartingFilters.cs @@ -9,7 +9,7 @@ public static class StartingFilters { public static FilterModel GetStartingFilter(Network network) { - var startingHeader = SmartHeader.GetStartingHeader(network, IndexType.SegwitTaproot); + var startingHeader = SmartHeader.GetStartingHeader(network); if (network == Network.Main) { return FilterModel.FromLine($"{startingHeader.Height}:{startingHeader.BlockHash}:02832810ec08a0:{startingHeader.PrevHash}:{startingHeader.EpochBlockTime}"); diff --git a/WalletWasabi/Blockchain/Blocks/SmartHeader.cs b/WalletWasabi/Blockchain/Blocks/SmartHeader.cs index ee1cba66f1c..92526de8954 100644 --- a/WalletWasabi/Blockchain/Blocks/SmartHeader.cs +++ b/WalletWasabi/Blockchain/Blocks/SmartHeader.cs @@ -1,7 +1,4 @@ using NBitcoin; -using System.Linq; -using WalletWasabi.BitcoinCore.Rpc.Models; -using WalletWasabi.Blockchain.BlockFilters; using WalletWasabi.Exceptions; using WalletWasabi.Helpers; @@ -68,50 +65,14 @@ public SmartHeader(uint256 blockHash, uint256 prevHash, uint height, long epochB 0, Network.RegTest.GetGenesis().Header.BlockTime); - /// - /// Where the first possible transaction can ever be found with the specified script types. - /// - public static SmartHeader GetStartingHeader(Network network, IndexType indexType) - { - var scriptTypes = IndexTypeConverter.ToRpcPubKeyTypes(indexType); - - if (network == Network.Main) + public static SmartHeader GetStartingHeader(Network network) => + network.Name switch { - if (scriptTypes.Length == 1 && scriptTypes.Contains(RpcPubkeyType.TxWitnessV1Taproot)) - { - return StartingHeaderTaprootMain; - } - else if (scriptTypes.Length == 2 && scriptTypes.Contains(RpcPubkeyType.TxWitnessV1Taproot) && scriptTypes.Contains(RpcPubkeyType.TxWitnessV0Keyhash)) - { - return StartingHeaderSegwitMain; - } - else - { - throw new NotImplementedException("Script types not supported."); - } - } - else if (network == Network.TestNet) - { - if (scriptTypes.Length == 1 && scriptTypes.Contains(RpcPubkeyType.TxWitnessV1Taproot)) - { - return StartingHeaderTaprootTestNet; - } - else if (scriptTypes.Length == 2 && scriptTypes.Contains(RpcPubkeyType.TxWitnessV1Taproot) && scriptTypes.Contains(RpcPubkeyType.TxWitnessV0Keyhash)) - { - return StartingHeaderSegwitTestNet; - } - else - { - throw new NotImplementedException("Script types not supported."); - } - } - else if (network == Network.RegTest) - { - return StartingHeaderRegTest; - } - - throw new NotSupportedNetworkException(network); - } + "Main" => StartingHeaderSegwitMain, + "TestNet" => StartingHeaderSegwitTestNet, + "RegTest" => StartingHeaderRegTest, + _ => throw new NotSupportedNetworkException(network) + }; #endregion SpecialHeaders } diff --git a/WalletWasabi/Wallets/Wallet.cs b/WalletWasabi/Wallets/Wallet.cs index 3574acd0795..f2f928fb93e 100644 --- a/WalletWasabi/Wallets/Wallet.cs +++ b/WalletWasabi/Wallets/Wallet.cs @@ -586,7 +586,7 @@ public void UpdateUsedHdPubKeysLabels(Dictionary hdPubKey private void EnsureHeightsAreAtLeastSegWitActivation() { - var startingSegwitHeight = new Height(SmartHeader.GetStartingHeader(Network, IndexType.SegwitTaproot).Height); + var startingSegwitHeight = new Height(SmartHeader.GetStartingHeader(Network).Height); if (startingSegwitHeight > KeyManager.GetBestHeight(SyncType.Complete)) { KeyManager.SetBestHeight(startingSegwitHeight);