Refactor WalletInput logic
MODIFY
WalletWasabi.Tests/Helpers/BitcoinFactory.cs
WalletWasabi.Tests/Helpers/BitcoinFactory.cs
@@ -60,11 +60,11 @@ public static class BitcoinFactory
foreach (var sc in walletInputs)
{
- stx.AddWalletInput(sc);
+ stx.TryAddWalletInput(sc);
}
foreach (var sc in walletOutputs)
{
- stx.AddWalletOutput(sc);
+ stx.TryAddWalletOutput(sc);
}
return stx;
}

MODIFY
WalletWasabi.Tests/UnitTests/BlockchainAnalysis/CoinJoinAnonscoreTests.cs
WalletWasabi.Tests/UnitTests/BlockchainAnalysis/CoinJoinAnonscoreTests.cs
@@ -41,9 +41,9 @@ public class CoinJoinAnonScoreTests
var analyser = ServiceFactory.CreateBlockchainAnalyzer();
var tx = BitcoinFactory.CreateSmartTransaction(9, Enumerable.Repeat(Money.Coins(1m), 8), new[] { (Money.Coins(1.1m), 1) }, new[] { (Money.Coins(1m), HdPubKey.DefaultHighAnonymitySet), (Money.Coins(1m), HdPubKey.DefaultHighAnonymitySet) });
var sc = tx.WalletOutputs.First();
- tx.RemoveWalletOutput(sc);
+ tx.TryRemoveWalletOutput(sc);
analyser.Analyze(tx);
- tx.AddWalletOutput(sc);
+ tx.TryAddWalletOutput(sc);
analyser.Analyze(tx);
Assert.Equal(1, tx.WalletInputs.First().HdPubKey.AnonymitySet);

MODIFY
WalletWasabi/Blockchain/TransactionOutputs/SmartCoin.cs
WalletWasabi/Blockchain/TransactionOutputs/SmartCoin.cs
@@ -48,7 +48,7 @@ public class SmartCoin : NotifyPropertyChangedBase, IEquatable<SmartCoin>, IDest
HdPubKey = pubKey;
- Transaction.AddWalletOutput(this);
+ Transaction.TryAddWalletOutput(this);
}
public SmartTransaction Transaction { get; }
@@ -79,10 +79,12 @@ public class SmartCoin : NotifyPropertyChangedBase, IEquatable<SmartCoin>, IDest
get => _spenderTransaction;
set
{
- value?.AddWalletInput(this);
+ if (value?.TryAddWalletInput(this) is true)
+ {
RaiseAndSetIfChanged(ref _spenderTransaction, value);
}
}
+ }
public bool RegisterToHdPubKey()
=> HdPubKey.Coins.Add(this);
@@ -161,7 +163,6 @@ public class SmartCoin : NotifyPropertyChangedBase, IEquatable<SmartCoin>, IDest
BannedUntilUtc = null;
return false;
-
}
[MemberNotNullWhen(returnValue: true, nameof(SpenderTransaction))]

MODIFY
WalletWasabi/Blockchain/Transactions/SmartTransaction.cs
WalletWasabi/Blockchain/Transactions/SmartTransaction.cs
@@ -32,44 +32,28 @@ public class SmartTransaction : IEquatable<SmartTransaction>
FirstSeen = firstSeen == default ? DateTimeOffset.UtcNow : firstSeen;
IsReplacement = isReplacement;
- _walletInputs = new HashSet<SmartCoin>(Transaction.Inputs.Count);
- _walletOutputs = new HashSet<SmartCoin>(Transaction.Outputs.Count);
+
+ WalletInputsInternal = new HashSet<SmartCoin>(Transaction.Inputs.Count);
+ WalletOutputsInternal = new HashSet<SmartCoin>(Transaction.Outputs.Count);
}
#endregion Constructors
#region Members
- public IReadOnlyCollection<SmartCoin> WalletInputs
- {
- get
- {
- return _walletInputs;
- }
- }
-
- public void AddWalletInput(SmartCoin input)
- {
- _walletInputs.Add(input);
- }
+ /// <summary>
+ /// Coins those are on the input side of the tx and belong to ANY loaded wallet. Later if more wallets are loaded this list can increase.
+ /// </summary>
+ private HashSet<SmartCoin> WalletInputsInternal { get; }
- public IReadOnlyCollection<SmartCoin> WalletOutputs
- {
- get
- {
- return _walletOutputs;
- }
- }
+ /// <summary>
+ /// Coins those are on the output side of the tx and belong to ANY loaded wallet. Later if more wallets are loaded this list can increase.
+ /// </summary>
+ private HashSet<SmartCoin> WalletOutputsInternal { get; }
- public void AddWalletOutput(SmartCoin output)
- {
- _walletOutputs.Add(output);
- }
+ public IReadOnlyCollection<SmartCoin> WalletInputs => WalletInputsInternal;
- public void RemoveWalletOutput(SmartCoin output)
- {
- _walletOutputs.Remove(output);
- }
+ public IReadOnlyCollection<SmartCoin> WalletOutputs => WalletOutputsInternal;
[JsonProperty]
[JsonConverter(typeof(TransactionJsonConverter))]
@@ -127,17 +111,15 @@ public class SmartTransaction : IEquatable<SmartTransaction>
/// </summary>
public bool IsRBF => !Confirmed && (Transaction.RBF || IsReplacement || WalletInputs.Any(x => x.IsReplaceable()));
- /// <summary>
- /// Coins those are on the input side of the tx and belong to ANY loaded wallet. Later if more wallets are loaded this list can increase.
- /// </summary>
- private HashSet<SmartCoin> _walletInputs;
+ #endregion Members
- /// <summary>
- /// Coins those are on the output side of the tx and belong to ANY loaded wallet. Later if more wallets are loaded this list can increase.
- /// </summary>
- private HashSet<SmartCoin> _walletOutputs;
+ public bool TryAddWalletInput(SmartCoin input) => WalletInputsInternal.Add(input);
- #endregion Members
+ public void TryAddWalletOutput(SmartCoin output) => WalletOutputsInternal.Add(output);
+
+ public bool TryRemoveWalletInput(SmartCoin input) => WalletInputsInternal.Remove(input);
+
+ public bool TryRemoveWalletOutput(SmartCoin output) => WalletOutputsInternal.Remove(output);
/// <summary>
/// Update the transaction with the data acquired from another transaction. (For example merge their labels.)

MODIFY
WalletWasabi/Blockchain/Transactions/TransactionFactory.cs
WalletWasabi/Blockchain/Transactions/TransactionFactory.cs
@@ -256,7 +256,7 @@ public class TransactionFactory
var smartTransaction = new SmartTransaction(tx, Height.Unknown, label: SmartLabel.Merge(payments.Requests.Select(x => x.Label)));
foreach (var coin in spentCoins)
{
- smartTransaction.AddWalletInput(coin);
+ smartTransaction.TryAddWalletInput(coin);
}
var label = SmartLabel.Merge(payments.Requests.Select(x => x.Label).Concat(smartTransaction.WalletInputs.Select(x => x.HdPubKey.Label)));
@@ -267,7 +267,7 @@ public class TransactionFactory
{
var smartCoin = new SmartCoin(smartTransaction, i, foundKey);
label = SmartLabel.Merge(label, smartCoin.HdPubKey.Label); // foundKey's label is already added to the coinlabel.
- smartTransaction.AddWalletOutput(smartCoin);
+ smartTransaction.TryAddWalletOutput(smartCoin);
}
}

Refactor WalletInput logic