Skip to content

Commit

Permalink
Cleanup IndexBuilderService (WalletWasabi#13229)
Browse files Browse the repository at this point in the history
  • Loading branch information
lontivero authored Jul 8, 2024
1 parent 56fdd03 commit c9b268b
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 93 deletions.
2 changes: 1 addition & 1 deletion WalletWasabi.Backend/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockNotifier>(), indexFilePath);
IndexBuilderService = new(RpcClient, HostedServices.Get<BlockNotifier>(), indexFilePath);

MempoolMirror = new MempoolMirror(TimeSpan.FromSeconds(21), RpcClient, P2pNode);
CoinJoinMempoolManager = new CoinJoinMempoolManager(CoinJoinIdStore, MempoolMirror);
Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi.Daemon/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi.Fluent/Models/Wallets/WalletLoadWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion WalletWasabi.Tests/RegressionTests/BackendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockNotifier>(), indexFilePath);
IndexBuilderService indexBuilderService = new(rpc, global.HostedServices.Get<BlockNotifier>(), indexFilePath);
try
{
indexBuilderService.Synchronize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions WalletWasabi.Tests/UnitTests/SmartHeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
14 changes: 5 additions & 9 deletions WalletWasabi/Blockchain/BlockFilters/IndexBuilderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 0 additions & 7 deletions WalletWasabi/Blockchain/BlockFilters/IndexType.cs

This file was deleted.

14 changes: 0 additions & 14 deletions WalletWasabi/Blockchain/BlockFilters/IndexTypeConverter.cs

This file was deleted.

2 changes: 1 addition & 1 deletion WalletWasabi/Blockchain/BlockFilters/StartingFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
53 changes: 7 additions & 46 deletions WalletWasabi/Blockchain/Blocks/SmartHeader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using NBitcoin;
using System.Linq;
using WalletWasabi.BitcoinCore.Rpc.Models;
using WalletWasabi.Blockchain.BlockFilters;
using WalletWasabi.Exceptions;
using WalletWasabi.Helpers;

Expand Down Expand Up @@ -68,50 +65,14 @@ public SmartHeader(uint256 blockHash, uint256 prevHash, uint height, long epochB
0,
Network.RegTest.GetGenesis().Header.BlockTime);

/// <summary>
/// Where the first possible transaction can ever be found with the specified script types.
/// </summary>
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
}
2 changes: 1 addition & 1 deletion WalletWasabi/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public void UpdateUsedHdPubKeysLabels(Dictionary<HdPubKey, LabelsArray> 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);
Expand Down

0 comments on commit c9b268b

Please sign in to comment.