private void ReprocessTxAfterReorg(IDictionary<Sha256Hash, Transaction> pool, Transaction tx)
{
_log.InfoFormat(" TX {0}", tx.HashAsString);
var numInputs = tx.Inputs.Count;
var noSuchTx = 0;
var success = 0;
var isDead = false;
foreach (var input in tx.Inputs)
{
if (input.IsCoinBase)
{
// Input is not in our wallet so there is "no such input tx", bit of an abuse.
noSuchTx++;
continue;
}
var result = input.Connect(pool, false);
if (result == TransactionInput.ConnectionResult.Success)
{
success++;
}
else if (result == TransactionInput.ConnectionResult.NoSuchTx)
{
noSuchTx++;
}
else if (result == TransactionInput.ConnectionResult.AlreadySpent)
{
isDead = true;
// This transaction was replaced by a double spend on the new chain. Did you just reverse
// your own transaction? I hope not!!
_log.Info(" ->dead, will not confirm now unless there's another re-org");
var doubleSpent = input.GetConnectedOutput(pool);
var replacement = doubleSpent.SpentBy.ParentTransaction;
_dead[tx.Hash] = tx;
Pending.Remove(tx.Hash);
// Inform the event listeners of the newly dead tx.
if (DeadTransaction != null)
{
lock (DeadTransaction)
{
DeadTransaction(this, new WalletDeadTransactionEventArgs(tx, replacement));
}
}
break;
}
}
if (isDead) return;
if (noSuchTx == numInputs)
{
_log.Info(" ->inactive");
_inactive[tx.Hash] = tx;
}
else if (success == numInputs - noSuchTx)
{
// All inputs are either valid for spending or don't come from us. Miners are trying to re-include it.
_log.Info(" ->pending");
Pending[tx.Hash] = tx;
_dead.Remove(tx.Hash);
}
}