2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 16:20:43 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-05-27 19:48:42 +02:00
|
|
|
#include "transactionrecord.h"
|
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
#include "base58.h"
|
2015-01-24 15:29:29 +01:00
|
|
|
#include "consensus/consensus.h"
|
2015-02-05 01:21:11 +01:00
|
|
|
#include "main.h"
|
2014-06-19 15:08:37 +02:00
|
|
|
#include "timedata.h"
|
2015-02-03 21:09:47 +01:00
|
|
|
#include "wallet/wallet.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2015-02-05 01:21:11 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2011-05-27 19:48:42 +02:00
|
|
|
/* Return positive answer if transaction should be shown in list.
|
|
|
|
*/
|
|
|
|
bool TransactionRecord::showTransaction(const CWalletTx &wtx)
|
|
|
|
{
|
|
|
|
if (wtx.IsCoinBase())
|
|
|
|
{
|
2012-08-24 08:19:27 +02:00
|
|
|
// Ensures we show generated coins / mined transactions at depth 1
|
|
|
|
if (!wtx.IsInMainChain())
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-18 11:53:25 +02:00
|
|
|
/*
|
|
|
|
* Decompose CWallet transaction to model transaction records.
|
2011-05-27 19:48:42 +02:00
|
|
|
*/
|
2011-06-26 19:23:24 +02:00
|
|
|
QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
|
|
|
QList<TransactionRecord> parts;
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nTime = wtx.GetTxTime();
|
2014-07-26 21:05:11 +02:00
|
|
|
CAmount nCredit = wtx.GetCredit(ISMINE_ALL);
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nDebit = wtx.GetDebit(ISMINE_ALL);
|
|
|
|
CAmount nNet = nCredit - nDebit;
|
2011-05-27 19:48:42 +02:00
|
|
|
uint256 hash = wtx.GetHash();
|
|
|
|
std::map<std::string, std::string> mapValue = wtx.mapValue;
|
|
|
|
|
2012-05-05 16:07:14 +02:00
|
|
|
if (nNet > 0 || wtx.IsCoinBase())
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
//
|
|
|
|
// Credit
|
|
|
|
//
|
|
|
|
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2014-03-29 05:15:28 +01:00
|
|
|
isminetype mine = wallet->IsMine(txout);
|
|
|
|
if(mine)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
TransactionRecord sub(hash, nTime);
|
2012-05-14 23:44:52 +02:00
|
|
|
CTxDestination address;
|
2012-05-05 16:07:14 +02:00
|
|
|
sub.idx = parts.size(); // sequence number
|
|
|
|
sub.credit = txout.nValue;
|
2015-06-10 08:36:36 +02:00
|
|
|
sub.involvesWatchAddress = mine & ISMINE_WATCH_ONLY;
|
2012-06-02 04:33:28 +02:00
|
|
|
if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
|
2012-05-05 16:07:14 +02:00
|
|
|
{
|
|
|
|
// Received by Bitcoin Address
|
|
|
|
sub.type = TransactionRecord::RecvWithAddress;
|
2012-05-14 23:44:52 +02:00
|
|
|
sub.address = CBitcoinAddress(address).ToString();
|
2012-05-05 16:07:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
|
|
|
|
sub.type = TransactionRecord::RecvFromOther;
|
|
|
|
sub.address = mapValue["from"];
|
|
|
|
}
|
2012-06-02 04:33:28 +02:00
|
|
|
if (wtx.IsCoinBase())
|
|
|
|
{
|
|
|
|
// Generated
|
|
|
|
sub.type = TransactionRecord::Generated;
|
|
|
|
}
|
2012-05-05 16:07:14 +02:00
|
|
|
|
|
|
|
parts.append(sub);
|
2011-05-27 19:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-05 16:07:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-05 21:36:48 +02:00
|
|
|
bool involvesWatchAddress = false;
|
2014-07-01 11:00:22 +02:00
|
|
|
isminetype fAllFromMe = ISMINE_SPENDABLE;
|
2012-05-05 16:07:14 +02:00
|
|
|
BOOST_FOREACH(const CTxIn& txin, wtx.vin)
|
2014-03-29 05:15:28 +01:00
|
|
|
{
|
|
|
|
isminetype mine = wallet->IsMine(txin);
|
2015-06-10 08:36:36 +02:00
|
|
|
if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
|
2014-03-29 05:15:28 +01:00
|
|
|
if(fAllFromMe > mine) fAllFromMe = mine;
|
|
|
|
}
|
2012-05-05 16:07:14 +02:00
|
|
|
|
2014-07-01 11:00:22 +02:00
|
|
|
isminetype fAllToMe = ISMINE_SPENDABLE;
|
2012-05-05 16:07:14 +02:00
|
|
|
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
|
2014-03-29 05:15:28 +01:00
|
|
|
{
|
|
|
|
isminetype mine = wallet->IsMine(txout);
|
2015-06-10 08:36:36 +02:00
|
|
|
if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
|
2014-03-29 05:15:28 +01:00
|
|
|
if(fAllToMe > mine) fAllToMe = mine;
|
|
|
|
}
|
2012-05-05 16:07:14 +02:00
|
|
|
|
|
|
|
if (fAllFromMe && fAllToMe)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
// Payment to self
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nChange = wtx.GetChange();
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2012-05-05 16:07:14 +02:00
|
|
|
parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
|
|
|
|
-(nDebit - nChange), nCredit - nChange));
|
2014-06-19 01:42:39 +02:00
|
|
|
parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
|
2012-05-05 16:07:14 +02:00
|
|
|
}
|
|
|
|
else if (fAllFromMe)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Debit
|
|
|
|
//
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nTxFee = nDebit - wtx.GetValueOut();
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2012-05-05 16:07:14 +02:00
|
|
|
for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
const CTxOut& txout = wtx.vout[nOut];
|
|
|
|
TransactionRecord sub(hash, nTime);
|
|
|
|
sub.idx = parts.size();
|
2014-06-19 01:42:39 +02:00
|
|
|
sub.involvesWatchAddress = involvesWatchAddress;
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2012-05-05 16:07:14 +02:00
|
|
|
if(wallet->IsMine(txout))
|
|
|
|
{
|
|
|
|
// Ignore parts sent to self, as this is usually the change
|
|
|
|
// from a transaction sent back to our own address.
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
CTxDestination address;
|
|
|
|
if (ExtractDestination(txout.scriptPubKey, address))
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
// Sent to Bitcoin Address
|
|
|
|
sub.type = TransactionRecord::SendToAddress;
|
2012-05-14 23:44:52 +02:00
|
|
|
sub.address = CBitcoinAddress(address).ToString();
|
2011-05-27 19:48:42 +02:00
|
|
|
}
|
2012-05-05 16:07:14 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Sent to IP, or other non-address transaction like OP_EVAL
|
|
|
|
sub.type = TransactionRecord::SendToOther;
|
|
|
|
sub.address = mapValue["to"];
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nValue = txout.nValue;
|
2012-05-05 16:07:14 +02:00
|
|
|
/* Add fee to first output */
|
|
|
|
if (nTxFee > 0)
|
|
|
|
{
|
|
|
|
nValue += nTxFee;
|
|
|
|
nTxFee = 0;
|
|
|
|
}
|
|
|
|
sub.debit = -nValue;
|
|
|
|
|
|
|
|
parts.append(sub);
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
2012-05-05 16:07:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Mixed debit transaction, can't break down payees
|
|
|
|
//
|
|
|
|
parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
|
2014-04-05 21:36:48 +02:00
|
|
|
parts.last().involvesWatchAddress = involvesWatchAddress;
|
2011-05-27 19:48:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
2011-06-04 21:41:31 +02:00
|
|
|
|
|
|
|
void TransactionRecord::updateStatus(const CWalletTx &wtx)
|
|
|
|
{
|
2014-04-15 12:43:17 +02:00
|
|
|
AssertLockHeld(cs_main);
|
2011-06-04 21:41:31 +02:00
|
|
|
// Determine transaction status
|
|
|
|
|
|
|
|
// Find the block the tx is in
|
|
|
|
CBlockIndex* pindex = NULL;
|
2014-09-04 02:02:44 +02:00
|
|
|
BlockMap::iterator mi = mapBlockIndex.find(wtx.hashBlock);
|
2011-06-04 21:41:31 +02:00
|
|
|
if (mi != mapBlockIndex.end())
|
|
|
|
pindex = (*mi).second;
|
|
|
|
|
|
|
|
// Sort order, unrecorded transactions sort to the top
|
|
|
|
status.sortKey = strprintf("%010d-%01d-%010u-%03d",
|
2011-12-19 23:08:25 +01:00
|
|
|
(pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
|
2011-06-04 21:41:31 +02:00
|
|
|
(wtx.IsCoinBase() ? 1 : 0),
|
|
|
|
wtx.nTimeReceived,
|
|
|
|
idx);
|
2014-02-20 14:09:09 +01:00
|
|
|
status.countsForBalance = wtx.IsTrusted() && !(wtx.GetBlocksToMaturity() > 0);
|
2011-06-04 21:41:31 +02:00
|
|
|
status.depth = wtx.GetDepthInMainChain();
|
2013-10-10 23:07:44 +02:00
|
|
|
status.cur_num_blocks = chainActive.Height();
|
2011-06-04 21:41:31 +02:00
|
|
|
|
2015-05-25 06:48:33 +02:00
|
|
|
if (!CheckFinalTx(wtx))
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
2011-07-09 10:26:46 +02:00
|
|
|
if (wtx.nLockTime < LOCKTIME_THRESHOLD)
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
|
|
|
status.status = TransactionStatus::OpenUntilBlock;
|
2014-01-27 03:50:15 +01:00
|
|
|
status.open_for = wtx.nLockTime - chainActive.Height();
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-04 21:41:31 +02:00
|
|
|
status.status = TransactionStatus::OpenUntilDate;
|
|
|
|
status.open_for = wtx.nLockTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For generated transactions, determine maturity
|
2014-02-20 14:09:09 +01:00
|
|
|
else if(type == TransactionRecord::Generated)
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
2014-02-20 14:09:09 +01:00
|
|
|
if (wtx.GetBlocksToMaturity() > 0)
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
2014-02-20 14:09:09 +01:00
|
|
|
status.status = TransactionStatus::Immature;
|
2011-06-04 21:41:31 +02:00
|
|
|
|
|
|
|
if (wtx.IsInMainChain())
|
|
|
|
{
|
|
|
|
status.matures_in = wtx.GetBlocksToMaturity();
|
|
|
|
|
|
|
|
// Check if the block was requested by anyone
|
|
|
|
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
|
2014-02-20 14:09:09 +01:00
|
|
|
status.status = TransactionStatus::MaturesWarning;
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-20 14:09:09 +01:00
|
|
|
status.status = TransactionStatus::NotAccepted;
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-20 14:09:09 +01:00
|
|
|
status.status = TransactionStatus::Confirmed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (status.depth < 0)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Conflicted;
|
|
|
|
}
|
|
|
|
else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Offline;
|
|
|
|
}
|
|
|
|
else if (status.depth == 0)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Unconfirmed;
|
|
|
|
}
|
|
|
|
else if (status.depth < RecommendedNumConfirmations)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Confirming;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Confirmed;
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-17 14:09:46 +02:00
|
|
|
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:46 +02:00
|
|
|
bool TransactionRecord::statusUpdateNeeded()
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
2014-04-15 12:43:17 +02:00
|
|
|
AssertLockHeld(cs_main);
|
2014-07-17 14:09:46 +02:00
|
|
|
return status.cur_num_blocks != chainActive.Height();
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
2011-07-07 14:27:16 +02:00
|
|
|
|
2013-09-05 12:23:08 +02:00
|
|
|
QString TransactionRecord::getTxID() const
|
2011-07-07 14:27:16 +02:00
|
|
|
{
|
2013-09-05 12:23:08 +02:00
|
|
|
return formatSubTxId(hash, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TransactionRecord::formatSubTxId(const uint256 &hash, int vout)
|
|
|
|
{
|
|
|
|
return QString::fromStdString(hash.ToString() + strprintf("-%03d", vout));
|
2011-07-07 14:27:16 +02:00
|
|
|
}
|
|
|
|
|