2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2011-2017 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.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/transactionrecord.h>
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <base58.h>
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
#include <validation.h>
|
|
|
|
#include <timedata.h>
|
|
|
|
#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
|
|
|
|
2011-05-27 19:48:42 +02:00
|
|
|
/* Return positive answer if transaction should be shown in list.
|
|
|
|
*/
|
|
|
|
bool TransactionRecord::showTransaction(const CWalletTx &wtx)
|
|
|
|
{
|
2017-04-17 16:16:21 +02:00
|
|
|
// There are currently no cases where we hide transactions, but
|
|
|
|
// we may want to use this in the future for things like RBF.
|
2011-05-27 19:48:42 +02:00
|
|
|
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
|
|
|
|
//
|
2017-01-26 13:21:11 +01:00
|
|
|
for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2017-01-26 13:21:11 +01:00
|
|
|
const CTxOut& txout = wtx.tx->vout[i];
|
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;
|
2017-01-26 13:21:11 +01:00
|
|
|
sub.idx = i; // vout index
|
2012-05-05 16:07:14 +02:00
|
|
|
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;
|
2017-08-23 03:02:33 +02:00
|
|
|
sub.address = EncodeDestination(address);
|
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;
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CTxIn& txin : wtx.tx->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;
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CTxOut& txout : wtx.tx->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
|
|
|
|
//
|
2016-11-12 01:54:51 +01:00
|
|
|
CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
|
2011-05-27 19:48:42 +02:00
|
|
|
|
2016-11-12 01:54:51 +01:00
|
|
|
for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2016-11-12 01:54:51 +01:00
|
|
|
const CTxOut& txout = wtx.tx->vout[nOut];
|
2012-05-05 16:07:14 +02:00
|
|
|
TransactionRecord sub(hash, nTime);
|
2017-01-26 13:21:11 +01:00
|
|
|
sub.idx = nOut;
|
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;
|
2017-08-23 03:02:33 +02:00
|
|
|
sub.address = EncodeDestination(address);
|
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
|
2017-08-07 07:36:37 +02:00
|
|
|
CBlockIndex* pindex = nullptr;
|
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
|
|
|
|
2017-05-09 08:46:26 +02:00
|
|
|
if (!CheckFinalTx(*wtx.tx))
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
2016-11-12 01:54:51 +01:00
|
|
|
if (wtx.tx->nLockTime < LOCKTIME_THRESHOLD)
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
|
|
|
status.status = TransactionStatus::OpenUntilBlock;
|
2016-11-12 01:54:51 +01:00
|
|
|
status.open_for = wtx.tx->nLockTime - chainActive.Height();
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-04 21:41:31 +02:00
|
|
|
status.status = TransactionStatus::OpenUntilDate;
|
2016-11-12 01:54:51 +01:00
|
|
|
status.open_for = wtx.tx->nLockTime;
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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;
|
2016-03-17 17:54:54 +01:00
|
|
|
if (wtx.isAbandoned())
|
|
|
|
status.status = TransactionStatus::Abandoned;
|
2014-02-20 14:09:09 +01:00
|
|
|
}
|
|
|
|
else if (status.depth < RecommendedNumConfirmations)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Confirming;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Confirmed;
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 17:09:01 +02:00
|
|
|
status.needsUpdate = false;
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:34:54 +01:00
|
|
|
bool TransactionRecord::statusUpdateNeeded() const
|
2011-06-04 21:41:31 +02:00
|
|
|
{
|
2014-04-15 12:43:17 +02:00
|
|
|
AssertLockHeld(cs_main);
|
2017-05-24 17:09:01 +02:00
|
|
|
return status.cur_num_blocks != chainActive.Height() || status.needsUpdate;
|
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
|
|
|
{
|
2016-03-29 11:17:47 +02:00
|
|
|
return QString::fromStdString(hash.ToString());
|
2013-09-05 12:23:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-29 11:17:47 +02:00
|
|
|
int TransactionRecord::getOutputIndex() const
|
2013-09-05 12:23:08 +02:00
|
|
|
{
|
2016-03-29 11:17:47 +02:00
|
|
|
return idx;
|
2011-07-07 14:27:16 +02:00
|
|
|
}
|