Fix "spent" issue with confirmed tx spends
MODIFY
WalletWasabi.Gui/Global.cs
WalletWasabi.Gui/Global.cs
@@ -60,7 +60,7 @@ namespace WalletWasabi.Gui
public Network Network => Config.Network;
- Global()
+ private Global()
{
DataDir = EnvironmentHelpers.GetDataDir(Path.Combine("WalletWasabi", "Client"));
TorLogsFile = Path.Combine(DataDir, "TorLogs.txt");
@@ -409,6 +409,9 @@ namespace WalletWasabi.Gui
{
ChaumianClient = new CcjClient(Synchronizer, Network, keyManager, Config.GetFallbackBackendUri(), null);
}
+
+ keyManager.CorrectBlockHeights(BitcoinStore.HashChain); // Block heights are wrong sometimes. It's a hack. We have to retroactively fix existing wallets, but also we have to figure out where we ruin the block heights.
+
WalletService = new WalletService(BitcoinStore, keyManager, Synchronizer, ChaumianClient, MemPoolService, Nodes, DataDir, Config.ServiceConfiguration);
ChaumianClient.Start();

MODIFY
WalletWasabi/KeyManagement/KeyManager.cs
WalletWasabi/KeyManagement/KeyManager.cs
@@ -11,6 +11,7 @@ using WalletWasabi.Hwi.Models;
using WalletWasabi.JsonConverters;
using WalletWasabi.Logging;
using WalletWasabi.Models;
+using WalletWasabi.Stores;
namespace WalletWasabi.KeyManagement
{
@@ -655,6 +656,41 @@ namespace WalletWasabi.KeyManagement
#region BlockchainState
+ public void CorrectBlockHeights(HashChain hashChain)
+ {
+ lock (BlockchainStateLock)
+ {
+ // Block heights are wrong sometimes. It's a hack. We have to retroactively fix existing wallets, but also we have to figure out where we ruin the block heights.
+ // Assert the correct height.
+ var toRemove = new List<uint256>();
+ var toAdd = new List<BlockState>();
+ foreach (var state in BlockchainState.BlockStates)
+ {
+ int? foundHeight = hashChain.GetHeightOrDefault(state.BlockHash);
+ if (foundHeight.HasValue && foundHeight.Value != state.BlockHeight.Value)
+ {
+ toRemove.Add(state.BlockHash);
+ toAdd.Add(new BlockState(state.BlockHash, new Height(foundHeight.Value), state.TransactionIndices));
+ }
+ }
+
+ foreach (var rem in toRemove)
+ {
+ TryRemoveBlockState(rem);
+ }
+
+ foreach (var add in toAdd)
+ {
+ AddBlockState(add, setItsHeightToBest: false);
+ }
+
+ if (toRemove.Any())
+ {
+ ToFileNoBlockchainStateLock();
+ }
+ }
+ }
+
public Height GetBestHeight()
{
Height res;

MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -271,7 +271,6 @@ namespace WalletWasabi.Services
foreach (var blockstate in KeyManager.GetTransactionIndex())
{
var block = await GetOrDownloadBlockAsync(blockstate.BlockHash, cancel);
-
await ProcessBlockAsync(blockstate.BlockHeight, block, blockstate.TransactionIndices);
}

MODIFY
WalletWasabi/Stores/HashChain.cs
WalletWasabi/Stores/HashChain.cs
@@ -145,5 +145,13 @@ namespace WalletWasabi.Stores
return Chain.Select(x => (x.Key, x.Value)).ToArray();
}
}
+
+ public int? GetHeightOrDefault(uint256 hash)
+ {
+ lock (Lock)
+ {
+ return Chain.FirstOrDefault(x => x.Value == hash).Key;
+ }
+ }
}
}

Fix "spent" issue with confirmed tx spends