Add Replace tx functionality
MODIFY
WalletWasabi/Coins/CoinsRegistry.cs
WalletWasabi/Coins/CoinsRegistry.cs
@@ -135,10 +135,11 @@ namespace WalletWasabi.Coins
}
}
- public void RemoveFromBlock(Height blockHeight)
+ public ICoinsView RemoveFromBlock(Height blockHeight)
{
lock (Lock)
{
+ var removedCoins = new List<SmartCoin>();
var allCoins = AsAllCoinsViewNoLock();
foreach (var toRemove in allCoins.AtBlockHeight(blockHeight).ToList())
{
@@ -156,28 +157,35 @@ namespace WalletWasabi.Coins
{
SpentCoins.Remove(toRemove);
}
+
+ removedCoins.Add(coin);
}
}
+ return new CoinsView(removedCoins);
}
}
- internal void Undo(uint256 txId)
+ internal (ICoinsView toRemove, ICoinsView toAdd) Undo(uint256 txId)
{
lock (Lock)
{
+ var toRemove = CreatedByNoLock(txId);
+ var toAdd = SpentByNoLock(txId);
+
// remove recursively the coins created by the transaction
- foreach (SmartCoin createdCoin in CreatedByNoLock(txId))
+ foreach (SmartCoin createdCoin in toRemove)
{
Coins.Remove(createdCoin);
}
// destroyed (spent) coins are now (unspent)
- foreach (SmartCoin destroyedCoin in SpentByNoLock(txId))
+ foreach (SmartCoin destroyedCoin in toAdd)
{
if (SpentCoins.Remove(destroyedCoin))
{
Coins.Add(destroyedCoin);
}
}
+ return (toRemove, toAdd);
}
}

MODIFY
WalletWasabi/Transactions/TransactionProcessor.cs
WalletWasabi/Transactions/TransactionProcessor.cs
@@ -9,6 +9,20 @@ using WalletWasabi.Models;
namespace WalletWasabi.Transactions
{
+ public class ReplaceTransactionArg
+ {
+ public ReplaceTransactionArg(SmartTransaction smartTransaction, IEnumerable<SmartCoin> added, IEnumerable<SmartCoin> remove)
+ {
+ SmartTransaction = smartTransaction;
+ Added = added;
+ Remove = remove;
+ }
+
+ public SmartTransaction SmartTransaction { get; }
+ public IEnumerable<SmartCoin> Added { get; }
+ public IEnumerable<SmartCoin> Remove { get; }
+ }
+
public class TransactionProcessor
{
public AllTransactionStore TransactionStore { get; }
@@ -24,6 +38,8 @@ namespace WalletWasabi.Transactions
public event EventHandler<SmartCoin> CoinReceived;
+ public event EventHandler<ReplaceTransactionArg> TransactionReplaced;
+
public TransactionProcessor(
AllTransactionStore transactionStore,
KeyManager keyManager,
@@ -101,7 +117,10 @@ namespace WalletWasabi.Transactions
// ones. After undoing the replaced transaction it will process the replacement
// transaction.
var replacedTxId = doubleSpends.First().TransactionId;
- Coins.Undo(replacedTxId);
+ var (toRemove, toAdd) = Coins.Undo(replacedTxId);
+
+ TransactionReplaced?.Invoke(this, new ReplaceTransactionArg(tx, toAdd, toRemove));
+
tx.SetReplacement();
walletRelevant = true;
}
@@ -117,6 +136,8 @@ namespace WalletWasabi.Transactions
{
Coins.Remove(doubleSpentCoin);
}
+
+ TransactionReplaced?.Invoke(this, new ReplaceTransactionArg(tx, Enumerable.Empty<SmartCoin>(), doubleSpends));
walletRelevant = true;
}
}
@@ -233,9 +254,9 @@ namespace WalletWasabi.Transactions
return walletRelevant;
}
- public void UndoBlock(Height blockHeight)
+ public ICoinsView UndoBlock(Height blockHeight)
{
- Coins.RemoveFromBlock(blockHeight);
+ return Coins.RemoveFromBlock(blockHeight);
}
}
}

Add Replace tx functionality