From 2b72d46f4232b58835d44aa3abd711df399d4728 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Wed, 12 Feb 2014 13:43:07 -0500 Subject: 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. --- src/rpcwallet.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/rpcwallet.cpp') diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index bcbae06682..2b49762d4a 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -45,7 +45,7 @@ void WalletTxToJSON(const CWalletTx& wtx, Object& entry) entry.push_back(Pair("confirmations", confirms)); if (wtx.IsCoinBase()) entry.push_back(Pair("generated", true)); - if (confirms) + if (confirms > 0) { entry.push_back(Pair("blockhash", wtx.hashBlock.GetHex())); entry.push_back(Pair("blockindex", wtx.nIndex)); @@ -1109,7 +1109,10 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe Object entry; entry.push_back(Pair("account", strSentAccount)); MaybePushAddress(entry, s.first); - entry.push_back(Pair("category", "send")); + if (wtx.GetDepthInMainChain() < 0) + entry.push_back(Pair("category", "conflicted")); + else + entry.push_back(Pair("category", "send")); entry.push_back(Pair("amount", ValueFromAmount(-s.second))); entry.push_back(Pair("fee", ValueFromAmount(-nFee))); if (fLong) @@ -1141,7 +1144,12 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe entry.push_back(Pair("category", "generate")); } else - entry.push_back(Pair("category", "receive")); + { + if (wtx.GetDepthInMainChain() < 0) + entry.push_back(Pair("category", "conflicted")); + else + entry.push_back(Pair("category", "receive")); + } entry.push_back(Pair("amount", ValueFromAmount(r.second))); if (fLong) WalletTxToJSON(wtx, entry); -- cgit v1.2.3