Handle "conflicted" transactions properly

Extend CMerkleTx::GetDepthInMainChain with the concept of
a "conflicted" transaction-- a transaction generated by the wallet
that is not in the main chain or in the mempool, and, therefore,
will likely never be confirmed.

GetDepthInMainChain() now returns -1 for conflicted transactions
(0 for unconfirmed-but-in-the-mempool, and >1 for confirmed).

This makes getbalance, getbalance '*', and listunspent all agree when there are
mutated transactions in the wallet.

Before:
 listunspent: one 49BTC output
 getbalance: 96 BTC (change counted twice)
 getbalance '*': 46 BTC (spends counted twice)

After: all agree, 49 BTC available to spend.
This commit is contained in:
Gavin Andresen
2014-02-12 13:43:07 -05:00
parent f582eda4ed
commit 2b72d46f42
8 changed files with 194 additions and 11 deletions

View File

@@ -700,8 +700,11 @@ public:
// Quick answer in most cases
if (!IsFinalTx(*this))
return false;
if (GetDepthInMainChain() >= 1)
int nDepth = GetDepthInMainChain();
if (nDepth >= 1)
return true;
if (nDepth < 0)
return false;
if (!bSpendZeroConfChange || !IsFromMe()) // using wtx's cached debit
return false;
@@ -717,8 +720,11 @@ public:
if (!IsFinalTx(*ptx))
return false;
if (ptx->GetDepthInMainChain() >= 1)
int nPDepth = ptx->GetDepthInMainChain();
if (nPDepth >= 1)
continue;
if (nPDepth < 0)
return false;
if (!pwallet->IsFromMe(*ptx))
return false;