Fix coin duplication issue
MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -1537,11 +1537,10 @@ namespace WalletWasabi.Tests
Assert.True(wallet.Coins.All(x => x.Confirmed));
- // Test coin count
+ // Test coin basic count.
var coinCount = wallet.Coins.Count;
var to = wallet.GetReceiveKey("foo");
var res = await wallet.BuildTransactionAsync(password, new[] { new WalletService.Operation(to.GetP2wpkhScript(), Money.Coins(0.2345m), "bar") }, 2, allowUnconfirmed: true);
- Debug.WriteLine(res.Transaction.Transaction);
await wallet.SendTransactionAsync(res.Transaction);
Assert.Equal(coinCount + 2, wallet.Coins.Count);
Assert.Equal(2, wallet.Coins.Count(x => !x.Confirmed));

MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -506,11 +506,21 @@ namespace WalletWasabi.Services
{
mixin += spentOwnCoins.Min(x => x.Mixin);
}
- var coin = new SmartCoin(txId, i, output.ScriptPubKey, output.Value, tx.Transaction.Inputs.ToTxoRefs().ToArray(), tx.Height, tx.Transaction.RBF, mixin, foundKey.Label, spenderTransactionId: null, false); // Don't inherit locked status from key, that's different.
- ChaumianClient.State.UpdateCoin(coin);
+
+ SmartCoin coin = justUpdate.FirstOrDefault(x => x.TransactionId == txId && x.Index == i);
+ if (coin is null)
+ {
+ coin = new SmartCoin(txId, i, output.ScriptPubKey, output.Value, tx.Transaction.Inputs.ToTxoRefs().ToArray(), tx.Height, tx.Transaction.RBF, mixin, foundKey.Label, spenderTransactionId: null, false); // Don't inherit locked status from key, that's different.
+
Coins.TryAdd(coin);
TransactionCache.TryAdd(tx);
CoinReceived?.Invoke(this, coin);
+ }
+ else
+ {
+ coin.Height = tx.Height;
+ }
+ ChaumianClient.State.UpdateCoin(coin);
if (coin.Unspent && !(ChaumianClient.OnePiece is null) && coin.Label == "ZeroLink Change")
{
Task.Run(async () =>

Fix coin duplication issue