Use TryGet pattern instead of FirstOrDefault
MODIFY
WalletWasabi/KeyManagement/KeyManager.cs
WalletWasabi/KeyManagement/KeyManager.cs
@@ -666,11 +666,10 @@ namespace WalletWasabi.KeyManagement
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)
+ if (hashChain.TryGetHeight(state.BlockHash, out int foundHeight) && foundHeight != state.BlockHeight.Value)
{
toRemove.Add(state.BlockHash);
- toAdd.Add(new BlockState(state.BlockHash, new Height(foundHeight.Value), state.TransactionIndices));
+ toAdd.Add(new BlockState(state.BlockHash, new Height(foundHeight), state.TransactionIndices));
}
}

MODIFY
WalletWasabi/Stores/HashChain.cs
WalletWasabi/Stores/HashChain.cs
@@ -146,18 +146,21 @@ namespace WalletWasabi.Stores
}
}
- public int? GetHeightOrDefault(uint256 hash)
+ public bool TryGetHeight(uint256 hash, out int Height)
{
lock (Lock)
{
- int height = Chain.FirstOrDefault(x => x.Value == hash).Key;
- if (height == default // Default int will be 0. We don't know if this refers to the 0th hash or it just means the hash wasn't found.
- && !Chain.ContainsKey(height)) // So let's check if the height contains or not.
+ Height = Chain.FirstOrDefault(x => x.Value == hash).Key;
+ // Default int will be 0. We don't know if this refers to the 0th hash or it just means the hash wasn't found.
+ // So let's check if the height contains or not.
+ if (Height == default)
{
- return null;
+ if (Chain[Height] != hash)
+ {
+ return false;
}
-
- return height;
+ }
+ return true;
}
}
}

Use TryGet pattern instead of FirstOrDefault