Skip to content

Commit 522b940

Browse files
committed
[MultiSig] Clean federation wallet from CCTS (#515)
* Clean fed wallet from CCTS * Review * Add log
1 parent 064d035 commit 522b940

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/Stratis.Features.FederatedPeg/Interfaces/IFederationWalletManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ public interface IFederationWalletManager : ILockProtected
2828
/// <summary> The last processed block's height.</summary>
2929
int WalletTipHeight { get; set; }
3030

31+
/// <summary>
32+
/// Because the CCTS syncs from the federation wallet, it needs to be
33+
/// responsible for cleaning transactions past max reorg.
34+
/// <para>
35+
/// Doing this from the federation wallet manager could mean transactions
36+
/// are cleaned before they are processed by the CCTS (which means they will
37+
/// be wrongly added back.
38+
/// </para>
39+
/// </summary>
40+
/// <param name="crossChainTransferTip">The tip of the <see cref="ICrossChainTransferStore"/>.</param>
41+
/// <returns><c>true</c> if transactions were cleaned and the wallet should be saved.</returns>
42+
bool CleanTransactionsPastMaxReorg(int crossChainTransferTip);
43+
3144
/// <summary>
3245
/// Lists all spendable transactions from all accounts in the wallet.
3346
/// </summary>

src/Stratis.Features.FederatedPeg/TargetChain/CrossChainTransferStore.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,14 @@ private bool Synchronize()
10001000
dbreezeTransaction.Commit();
10011001
}
10021002

1003+
// As the CCTS syncs from the federation wallet, it needs to be
1004+
// responsible for cleaning transaciton past max reorg.
1005+
// Doing this from the federatrion wallet manager could mean transactions
1006+
// are cleaned before they are processed by the CCTS (which means they will
1007+
// be wrongly added back.
1008+
if (this.federationWalletManager.CleanTransactionsPastMaxReorg(this.TipHashAndHeight.Height))
1009+
this.federationWalletManager.SaveWallet();
1010+
10031011
return true;
10041012
}
10051013
}

src/Stratis.Features.FederatedPeg/Wallet/FederationWalletManager.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ public void ProcessBlock(Block block, ChainedHeader chainedHeader)
425425
walletUpdated = true;
426426
}
427427

428-
walletUpdated |= this.CleanTransactionsPastMaxReorg(chainedHeader.Height);
429-
430428
// Update the wallets with the last processed block height.
431429
// It's important that updating the height happens after the block processing is complete,
432430
// as if the node is stopped, on re-opening it will start updating from the previous height.
@@ -539,26 +537,27 @@ public bool ProcessTransaction(Transaction transaction, int? blockHeight = null,
539537
}
540538
}
541539

542-
private bool CleanTransactionsPastMaxReorg(int height)
540+
/// <inheritdoc />
541+
public bool CleanTransactionsPastMaxReorg(int crossChainTransferStoreTip)
543542
{
544543
bool walletUpdated = false;
545544

546545
if (this.network.Consensus.MaxReorgLength == 0 || this.Wallet.MultiSigAddress.Transactions.Count <= MinimumRetainedTransactions)
547546
return walletUpdated;
548547

549-
int finalisedHeight = height - (int)this.network.Consensus.MaxReorgLength;
550-
var pastMaxReorg = new List<TransactionData>();
548+
int heightToCleanFrom = crossChainTransferStoreTip - (int)this.network.Consensus.MaxReorgLength;
549+
var transactionsPastMaxReorg = new List<TransactionData>();
551550

552-
foreach ((_, List<TransactionData> txList) in this.Wallet.MultiSigAddress.Transactions.SpentTransactionsBeforeHeight(finalisedHeight))
551+
// Only want to remove transactions that are spent, and the spend must have passed max reorg too
552+
foreach ((_, List<TransactionData> txList) in this.Wallet.MultiSigAddress.Transactions.SpentTransactionsBeforeHeight(heightToCleanFrom))
553553
{
554554
foreach (TransactionData transactionData in txList)
555555
{
556-
// Only want to remove transactions that are spent, and the spend must have passed max reorg too
557-
pastMaxReorg.Add(transactionData);
556+
transactionsPastMaxReorg.Add(transactionData);
558557
}
559558
}
560559

561-
foreach (TransactionData transactionData in pastMaxReorg)
560+
foreach (TransactionData transactionData in transactionsPastMaxReorg)
562561
{
563562
this.Wallet.MultiSigAddress.Transactions.Remove(transactionData);
564563
walletUpdated = true;
@@ -567,6 +566,8 @@ private bool CleanTransactionsPastMaxReorg(int height)
567566
break;
568567
}
569568

569+
this.logger.Debug("Cleaned {0} transactions older than the CCTS tip less max reorg of {1}.", transactionsPastMaxReorg.Count, crossChainTransferStoreTip);
570+
570571
return walletUpdated;
571572
}
572573

0 commit comments

Comments
 (0)