2014-02-08 22:50:24 +01:00
|
|
|
// Copyright (c) 2011-2014 The Bitcoin developers
|
2013-11-04 16:20:43 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// 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"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "wallet.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
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();
|
|
|
|
int64_t nCredit = wtx.GetCredit(true);
|
|
|
|
int64_t nDebit = wtx.GetDebit();
|
|
|
|
int64_t 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
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
if(wallet->IsMine(txout))
|
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;
|
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
|
|
|
|
{
|
|
|
|
bool fAllFromMe = true;
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, wtx.vin)
|
|
|
|
fAllFromMe = fAllFromMe && wallet->IsMine(txin);
|
|
|
|
|
|
|
|
bool fAllToMe = true;
|
|
|
|
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
|
|
|
|
fAllToMe = fAllToMe && wallet->IsMine(txout);
|
|
|
|
|
|
|
|
if (fAllFromMe && fAllToMe)
|
2011-05-27 19:48:42 +02:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
// Payment to self
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t 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));
|
|
|
|
}
|
|
|
|
else if (fAllFromMe)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Debit
|
|
|
|
//
|
2013-11-11 07:03:51 +01:00
|
|
|
int64_t 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();
|
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"];
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t 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));
|
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;
|
|
|
|
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
|
|
|
|
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
|
|
|
|
2014-01-27 03:50:15 +01:00
|
|
|
if (!IsFinalTx(wtx, chainActive.Height() + 1))
|
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-02-20 14:09:09 +01:00
|
|
|
|
2011-06-04 21:41:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransactionRecord::statusUpdateNeeded()
|
|
|
|
{
|
2014-04-15 12:43:17 +02:00
|
|
|
AssertLockHeld(cs_main);
|
2013-10-10 23:07:44 +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
|
|
|
}
|
|
|
|
|