2019-12-30 06:39:22 -03:00
|
|
|
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 12:20:43 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/transactionrecord.h>
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2018-10-23 16:02:20 -03:00
|
|
|
#include <chain.h>
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/wallet.h>
|
2017-09-19 22:12:25 -03:00
|
|
|
#include <key_io.h>
|
2019-06-06 03:53:16 -04:00
|
|
|
#include <wallet/ismine.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2018-10-23 16:02:20 -03:00
|
|
|
#include <QDateTime>
|
2015-02-04 21:21:11 -03:00
|
|
|
|
2011-05-27 13:48:42 -04:00
|
|
|
/* Return positive answer if transaction should be shown in list.
|
|
|
|
*/
|
2017-04-18 17:42:30 -03:00
|
|
|
bool TransactionRecord::showTransaction()
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2017-04-17 11:16:21 -03: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 13:48:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-18 05:53:25 -04:00
|
|
|
/*
|
|
|
|
* Decompose CWallet transaction to model transaction records.
|
2011-05-27 13:48:42 -04:00
|
|
|
*/
|
2018-04-07 04:42:02 -03:00
|
|
|
QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interfaces::WalletTx& wtx)
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
|
|
|
QList<TransactionRecord> parts;
|
2017-04-18 17:42:30 -03:00
|
|
|
int64_t nTime = wtx.time;
|
|
|
|
CAmount nCredit = wtx.credit;
|
|
|
|
CAmount nDebit = wtx.debit;
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount nNet = nCredit - nDebit;
|
2017-04-18 17:42:30 -03:00
|
|
|
uint256 hash = wtx.tx->GetHash();
|
|
|
|
std::map<std::string, std::string> mapValue = wtx.value_map;
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2017-04-18 17:42:30 -03:00
|
|
|
if (nNet > 0 || wtx.is_coinbase)
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2012-05-05 10:07:14 -04:00
|
|
|
//
|
|
|
|
// Credit
|
|
|
|
//
|
2017-01-26 09:21:11 -03:00
|
|
|
for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2017-01-26 09:21:11 -03:00
|
|
|
const CTxOut& txout = wtx.tx->vout[i];
|
2017-04-18 17:42:30 -03:00
|
|
|
isminetype mine = wtx.txout_is_mine[i];
|
2014-03-29 01:15:28 -03:00
|
|
|
if(mine)
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2012-05-05 10:07:14 -04:00
|
|
|
TransactionRecord sub(hash, nTime);
|
2017-01-26 09:21:11 -03:00
|
|
|
sub.idx = i; // vout index
|
2012-05-05 10:07:14 -04:00
|
|
|
sub.credit = txout.nValue;
|
2015-06-10 03:36:36 -03:00
|
|
|
sub.involvesWatchAddress = mine & ISMINE_WATCH_ONLY;
|
2017-04-18 17:42:30 -03:00
|
|
|
if (wtx.txout_address_is_mine[i])
|
2012-05-05 10:07:14 -04:00
|
|
|
{
|
|
|
|
// Received by Bitcoin Address
|
|
|
|
sub.type = TransactionRecord::RecvWithAddress;
|
2017-04-18 17:42:30 -03:00
|
|
|
sub.address = EncodeDestination(wtx.txout_address[i]);
|
2012-05-05 10:07:14 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
|
|
|
|
sub.type = TransactionRecord::RecvFromOther;
|
|
|
|
sub.address = mapValue["from"];
|
|
|
|
}
|
2017-04-18 17:42:30 -03:00
|
|
|
if (wtx.is_coinbase)
|
2012-06-01 22:33:28 -04:00
|
|
|
{
|
|
|
|
// Generated
|
|
|
|
sub.type = TransactionRecord::Generated;
|
|
|
|
}
|
2012-05-05 10:07:14 -04:00
|
|
|
|
|
|
|
parts.append(sub);
|
2011-05-27 13:48:42 -04:00
|
|
|
}
|
|
|
|
}
|
2012-05-05 10:07:14 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-05 16:36:48 -03:00
|
|
|
bool involvesWatchAddress = false;
|
2014-07-01 05:00:22 -04:00
|
|
|
isminetype fAllFromMe = ISMINE_SPENDABLE;
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const isminetype mine : wtx.txin_is_mine)
|
2014-03-29 01:15:28 -03:00
|
|
|
{
|
2015-06-10 03:36:36 -03:00
|
|
|
if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
|
2014-03-29 01:15:28 -03:00
|
|
|
if(fAllFromMe > mine) fAllFromMe = mine;
|
|
|
|
}
|
2012-05-05 10:07:14 -04:00
|
|
|
|
2014-07-01 05:00:22 -04:00
|
|
|
isminetype fAllToMe = ISMINE_SPENDABLE;
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const isminetype mine : wtx.txout_is_mine)
|
2014-03-29 01:15:28 -03:00
|
|
|
{
|
2015-06-10 03:36:36 -03:00
|
|
|
if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
|
2014-03-29 01:15:28 -03:00
|
|
|
if(fAllToMe > mine) fAllToMe = mine;
|
|
|
|
}
|
2012-05-05 10:07:14 -04:00
|
|
|
|
|
|
|
if (fAllFromMe && fAllToMe)
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2012-05-05 10:07:14 -04:00
|
|
|
// Payment to self
|
2019-01-04 04:09:06 -03:00
|
|
|
std::string address;
|
|
|
|
for (auto it = wtx.txout_address.begin(); it != wtx.txout_address.end(); ++it) {
|
|
|
|
if (it != wtx.txout_address.begin()) address += ", ";
|
|
|
|
address += EncodeDestination(*it);
|
|
|
|
}
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2019-01-04 04:09:06 -03:00
|
|
|
CAmount nChange = wtx.change;
|
|
|
|
parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, address, -(nDebit - nChange), nCredit - nChange));
|
2014-06-18 19:42:39 -04:00
|
|
|
parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
|
2012-05-05 10:07:14 -04:00
|
|
|
}
|
|
|
|
else if (fAllFromMe)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Debit
|
|
|
|
//
|
2016-11-11 21:54:51 -03:00
|
|
|
CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2016-11-11 21:54:51 -03:00
|
|
|
for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2016-11-11 21:54:51 -03:00
|
|
|
const CTxOut& txout = wtx.tx->vout[nOut];
|
2012-05-05 10:07:14 -04:00
|
|
|
TransactionRecord sub(hash, nTime);
|
2017-01-26 09:21:11 -03:00
|
|
|
sub.idx = nOut;
|
2014-06-18 19:42:39 -04:00
|
|
|
sub.involvesWatchAddress = involvesWatchAddress;
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2017-04-18 17:42:30 -03:00
|
|
|
if(wtx.txout_is_mine[nOut])
|
2012-05-05 10:07:14 -04:00
|
|
|
{
|
|
|
|
// Ignore parts sent to self, as this is usually the change
|
|
|
|
// from a transaction sent back to our own address.
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-27 13:48:42 -04:00
|
|
|
|
2017-04-18 17:42:30 -03:00
|
|
|
if (!boost::get<CNoDestination>(&wtx.txout_address[nOut]))
|
2011-05-27 13:48:42 -04:00
|
|
|
{
|
2012-05-05 10:07:14 -04:00
|
|
|
// Sent to Bitcoin Address
|
|
|
|
sub.type = TransactionRecord::SendToAddress;
|
2017-04-18 17:42:30 -03:00
|
|
|
sub.address = EncodeDestination(wtx.txout_address[nOut]);
|
2011-05-27 13:48:42 -04:00
|
|
|
}
|
2012-05-05 10:07:14 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Sent to IP, or other non-address transaction like OP_EVAL
|
|
|
|
sub.type = TransactionRecord::SendToOther;
|
|
|
|
sub.address = mapValue["to"];
|
|
|
|
}
|
|
|
|
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount nValue = txout.nValue;
|
2012-05-05 10:07:14 -04:00
|
|
|
/* Add fee to first output */
|
|
|
|
if (nTxFee > 0)
|
|
|
|
{
|
|
|
|
nValue += nTxFee;
|
|
|
|
nTxFee = 0;
|
|
|
|
}
|
|
|
|
sub.debit = -nValue;
|
|
|
|
|
|
|
|
parts.append(sub);
|
2011-06-07 12:59:01 -04:00
|
|
|
}
|
2012-05-05 10:07:14 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Mixed debit transaction, can't break down payees
|
|
|
|
//
|
|
|
|
parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
|
2014-04-05 16:36:48 -03:00
|
|
|
parts.last().involvesWatchAddress = involvesWatchAddress;
|
2011-05-27 13:48:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
2011-06-04 15:41:31 -04:00
|
|
|
|
2020-01-23 19:31:16 -03:00
|
|
|
void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, const uint256& block_hash, int numBlocks, int64_t block_time)
|
2011-06-04 15:41:31 -04:00
|
|
|
{
|
|
|
|
// Determine transaction status
|
|
|
|
|
|
|
|
// Sort order, unrecorded transactions sort to the top
|
|
|
|
status.sortKey = strprintf("%010d-%01d-%010u-%03d",
|
2017-04-18 17:42:30 -03:00
|
|
|
wtx.block_height,
|
|
|
|
wtx.is_coinbase ? 1 : 0,
|
|
|
|
wtx.time_received,
|
2011-06-04 15:41:31 -04:00
|
|
|
idx);
|
2017-04-18 17:42:30 -03:00
|
|
|
status.countsForBalance = wtx.is_trusted && !(wtx.blocks_to_maturity > 0);
|
|
|
|
status.depth = wtx.depth_in_main_chain;
|
2020-01-23 19:31:16 -03:00
|
|
|
status.m_cur_block_hash = block_hash;
|
2011-06-04 15:41:31 -04:00
|
|
|
|
2018-10-23 16:02:20 -03:00
|
|
|
const bool up_to_date = ((int64_t)QDateTime::currentMSecsSinceEpoch() / 1000 - block_time < MAX_BLOCK_TIME_GAP);
|
|
|
|
if (up_to_date && !wtx.is_final) {
|
|
|
|
if (wtx.lock_time < LOCKTIME_THRESHOLD) {
|
2011-06-04 15:41:31 -04:00
|
|
|
status.status = TransactionStatus::OpenUntilBlock;
|
2017-04-18 17:42:30 -03:00
|
|
|
status.open_for = wtx.lock_time - numBlocks;
|
2011-06-07 12:59:01 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-04 15:41:31 -04:00
|
|
|
status.status = TransactionStatus::OpenUntilDate;
|
2017-04-18 17:42:30 -03:00
|
|
|
status.open_for = wtx.lock_time;
|
2011-06-04 15:41:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// For generated transactions, determine maturity
|
2014-02-20 10:09:09 -03:00
|
|
|
else if(type == TransactionRecord::Generated)
|
2011-06-04 15:41:31 -04:00
|
|
|
{
|
2017-04-18 17:42:30 -03:00
|
|
|
if (wtx.blocks_to_maturity > 0)
|
2011-06-04 15:41:31 -04:00
|
|
|
{
|
2014-02-20 10:09:09 -03:00
|
|
|
status.status = TransactionStatus::Immature;
|
2011-06-04 15:41:31 -04:00
|
|
|
|
2017-04-18 17:42:30 -03:00
|
|
|
if (wtx.is_in_main_chain)
|
2011-06-04 15:41:31 -04:00
|
|
|
{
|
2017-04-18 17:42:30 -03:00
|
|
|
status.matures_in = wtx.blocks_to_maturity;
|
2011-06-07 12:59:01 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-20 10:09:09 -03:00
|
|
|
status.status = TransactionStatus::NotAccepted;
|
2011-06-04 15:41:31 -04:00
|
|
|
}
|
2011-06-07 12:59:01 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-20 10:09:09 -03:00
|
|
|
status.status = TransactionStatus::Confirmed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (status.depth < 0)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Conflicted;
|
|
|
|
}
|
|
|
|
else if (status.depth == 0)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Unconfirmed;
|
2017-04-18 17:42:30 -03:00
|
|
|
if (wtx.is_abandoned)
|
2016-03-17 13:54:54 -03:00
|
|
|
status.status = TransactionStatus::Abandoned;
|
2014-02-20 10:09:09 -03:00
|
|
|
}
|
|
|
|
else if (status.depth < RecommendedNumConfirmations)
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Confirming;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status.status = TransactionStatus::Confirmed;
|
2011-06-04 15:41:31 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 11:09:01 -04:00
|
|
|
status.needsUpdate = false;
|
2011-06-04 15:41:31 -04:00
|
|
|
}
|
|
|
|
|
2020-01-23 19:31:16 -03:00
|
|
|
bool TransactionRecord::statusUpdateNeeded(const uint256& block_hash) const
|
2011-06-04 15:41:31 -04:00
|
|
|
{
|
2020-06-22 16:35:48 -04:00
|
|
|
assert(!block_hash.IsNull());
|
2020-01-23 19:31:16 -03:00
|
|
|
return status.m_cur_block_hash != block_hash || status.needsUpdate;
|
2011-06-04 15:41:31 -04:00
|
|
|
}
|
2011-07-07 08:27:16 -04:00
|
|
|
|
2018-03-06 17:22:50 -03:00
|
|
|
QString TransactionRecord::getTxHash() const
|
2011-07-07 08:27:16 -04:00
|
|
|
{
|
2016-03-29 06:17:47 -03:00
|
|
|
return QString::fromStdString(hash.ToString());
|
2013-09-05 06:23:08 -04:00
|
|
|
}
|
|
|
|
|
2016-03-29 06:17:47 -03:00
|
|
|
int TransactionRecord::getOutputIndex() const
|
2013-09-05 06:23:08 -04:00
|
|
|
{
|
2016-03-29 06:17:47 -03:00
|
|
|
return idx;
|
2011-07-07 08:27:16 -04:00
|
|
|
}
|