update transaction status as new blocks come in

This commit is contained in:
Wladimir J. van der Laan 2011-06-04 21:41:31 +02:00
parent 2547f1f7e5
commit 64bca50d54
3 changed files with 124 additions and 68 deletions

View File

@ -10,7 +10,7 @@ class TransactionStatus
public: public:
TransactionStatus(): TransactionStatus():
confirmed(false), sortKey(""), maturity(Mature), confirmed(false), sortKey(""), maturity(Mature),
matures_in(0), status(Offline), depth(0), open_for(0) matures_in(0), status(Offline), depth(0), open_for(0), cur_num_blocks(-1)
{ } { }
enum Maturity enum Maturity
@ -40,6 +40,9 @@ public:
Status status; Status status;
int64 depth; int64 depth;
int64 open_for; /* Timestamp if status==OpenUntilDate, otherwise number of blocks */ int64 open_for; /* Timestamp if status==OpenUntilDate, otherwise number of blocks */
/* Current number of blocks (to know whether cached status is still valid. */
int cur_num_blocks;
}; };
class TransactionRecord class TransactionRecord
@ -57,21 +60,21 @@ public:
}; };
TransactionRecord(): TransactionRecord():
hash(), time(0), type(Other), address(""), debit(0), credit(0) hash(), time(0), type(Other), address(""), debit(0), credit(0), idx(0)
{ {
} }
TransactionRecord(uint256 hash, int64 time, const TransactionStatus &status): TransactionRecord(uint256 hash, int64 time):
hash(hash), time(time), type(Other), address(""), debit(0), hash(hash), time(time), type(Other), address(""), debit(0),
credit(0), status(status) credit(0), idx(0)
{ {
} }
TransactionRecord(uint256 hash, int64 time, const TransactionStatus &status, TransactionRecord(uint256 hash, int64 time,
Type type, const std::string &address, Type type, const std::string &address,
int64 debit, int64 credit): int64 debit, int64 credit):
hash(hash), time(time), type(type), address(address), debit(debit), credit(credit), hash(hash), time(time), type(type), address(address), debit(debit), credit(credit),
status(status) idx(0)
{ {
} }
@ -88,8 +91,19 @@ public:
int64 debit; int64 debit;
int64 credit; int64 credit;
/* Subtransaction index, for sort key */
int idx;
/* Status: can change with block chain update */ /* Status: can change with block chain update */
TransactionStatus status; TransactionStatus status;
/* Update status from wallet tx.
*/
void updateStatus(const CWalletTx &wtx);
/* Is a status update needed?
*/
bool statusUpdateNeeded();
}; };
#endif // TRANSACTIONRECORD_H #endif // TRANSACTIONRECORD_H

View File

@ -38,47 +38,6 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
uint256 hash = wtx.GetHash(); uint256 hash = wtx.GetHash();
std::map<std::string, std::string> mapValue = wtx.mapValue; std::map<std::string, std::string> mapValue = wtx.mapValue;
// 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;
// Determine transaction status
TransactionStatus status;
// Sort order, unrecorded transactions sort to the top
status.sortKey = strprintf("%010d-%01d-%010u",
(pindex ? pindex->nHeight : INT_MAX),
(wtx.IsCoinBase() ? 1 : 0),
wtx.nTimeReceived);
status.confirmed = wtx.IsConfirmed();
status.depth = wtx.GetDepthInMainChain();
if (!wtx.IsFinal())
{
if (wtx.nLockTime < 500000000)
{
status.status = TransactionStatus::OpenUntilBlock;
status.open_for = nBestHeight - wtx.nLockTime;
} else {
status.status = TransactionStatus::OpenUntilDate;
status.open_for = wtx.nLockTime;
}
}
else
{
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
{
status.status = TransactionStatus::Offline;
} else if (status.depth < 6)
{
status.status = TransactionStatus::Unconfirmed;
} else
{
status.status = TransactionStatus::HaveConfirmations;
}
}
if (showTransaction(wtx)) if (showTransaction(wtx))
{ {
if (nNet > 0 || wtx.IsCoinBase()) if (nNet > 0 || wtx.IsCoinBase())
@ -86,7 +45,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
// //
// Credit // Credit
// //
TransactionRecord sub(hash, nTime, status); TransactionRecord sub(hash, nTime);
sub.credit = nNet; sub.credit = nNet;
@ -97,25 +56,10 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
if (nCredit == 0) if (nCredit == 0)
{ {
sub.status.maturity = TransactionStatus::Immature;
int64 nUnmatured = 0; int64 nUnmatured = 0;
BOOST_FOREACH(const CTxOut& txout, wtx.vout) BOOST_FOREACH(const CTxOut& txout, wtx.vout)
nUnmatured += txout.GetCredit(); nUnmatured += txout.GetCredit();
sub.credit = nUnmatured; sub.credit = nUnmatured;
if (wtx.IsInMainChain())
{
sub.status.matures_in = wtx.GetBlocksToMaturity();
// Check if the block was requested by anyone
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
sub.status.maturity = TransactionStatus::MaturesWarning;
}
else
{
sub.status.maturity = TransactionStatus::NotAccepted;
}
} }
} }
else if (!mapValue["from"].empty() || !mapValue["message"].empty()) else if (!mapValue["from"].empty() || !mapValue["message"].empty())
@ -159,7 +103,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
// Payment to self // Payment to self
int64 nChange = wtx.GetChange(); int64 nChange = wtx.GetChange();
parts.append(TransactionRecord(hash, nTime, status, TransactionRecord::SendToSelf, "", parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
-(nDebit - nChange), nCredit - nChange)); -(nDebit - nChange), nCredit - nChange));
} }
else if (fAllFromMe) else if (fAllFromMe)
@ -172,7 +116,8 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
for (int nOut = 0; nOut < wtx.vout.size(); nOut++) for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
{ {
const CTxOut& txout = wtx.vout[nOut]; const CTxOut& txout = wtx.vout[nOut];
TransactionRecord sub(hash, nTime, status); TransactionRecord sub(hash, nTime);
sub.idx = parts.size();
if (txout.IsMine()) if (txout.IsMine())
{ {
@ -200,7 +145,6 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
nTxFee = 0; nTxFee = 0;
} }
sub.debit = -nValue; sub.debit = -nValue;
sub.status.sortKey += strprintf("-%d", nOut);
parts.append(sub); parts.append(sub);
} }
@ -214,10 +158,84 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWalletTx
BOOST_FOREACH(const CTxIn& txin, wtx.vin) BOOST_FOREACH(const CTxIn& txin, wtx.vin)
fAllMine = fAllMine && txin.IsMine(); fAllMine = fAllMine && txin.IsMine();
parts.append(TransactionRecord(hash, nTime, status, TransactionRecord::Other, "", nNet, 0)); parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
} }
} }
} }
return parts; return parts;
} }
void TransactionRecord::updateStatus(const CWalletTx &wtx)
{
// 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",
(pindex ? pindex->nHeight : INT_MAX),
(wtx.IsCoinBase() ? 1 : 0),
wtx.nTimeReceived,
idx);
status.confirmed = wtx.IsConfirmed();
status.depth = wtx.GetDepthInMainChain();
status.cur_num_blocks = nBestHeight;
if (!wtx.IsFinal())
{
if (wtx.nLockTime < 500000000)
{
status.status = TransactionStatus::OpenUntilBlock;
status.open_for = nBestHeight - wtx.nLockTime;
} else {
status.status = TransactionStatus::OpenUntilDate;
status.open_for = wtx.nLockTime;
}
}
else
{
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
{
status.status = TransactionStatus::Offline;
} else if (status.depth < 6)
{
status.status = TransactionStatus::Unconfirmed;
} else
{
status.status = TransactionStatus::HaveConfirmations;
}
}
// For generated transactions, determine maturity
if(type == TransactionRecord::Generated)
{
int64 nCredit = wtx.GetCredit(true);
if (nCredit == 0)
{
status.maturity = TransactionStatus::Immature;
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)
status.maturity = TransactionStatus::MaturesWarning;
} else {
status.maturity = TransactionStatus::NotAccepted;
}
} else {
status.maturity = TransactionStatus::Mature;
}
}
}
bool TransactionRecord::statusUpdateNeeded()
{
return status.cur_num_blocks != nBestHeight;
}

View File

@ -148,7 +148,25 @@ struct TransactionTablePriv
{ {
if(idx >= 0 && idx < cachedWallet.size()) if(idx >= 0 && idx < cachedWallet.size())
{ {
return &cachedWallet[idx]; TransactionRecord *rec = &cachedWallet[idx];
/* If a status update is needed (blocks came in since last check),
update the status of this transaction from the wallet. Otherwise,
simply re-use the cached status.
*/
if(rec->statusUpdateNeeded())
{
CRITICAL_BLOCK(cs_mapWallet)
{
std::map<uint256, CWalletTx>::iterator mi = mapWallet.find(rec->hash);
if(mi != mapWallet.end())
{
rec->updateStatus(mi->second);
}
}
}
return rec;
} else { } else {
return 0; return 0;
} }
@ -204,6 +222,12 @@ void TransactionTableModel::update()
if(!updated.empty()) if(!updated.empty())
{ {
priv->updateWallet(updated); priv->updateWallet(updated);
/* Status (number of confirmations) and (possibly) description
columns changed for all rows.
*/
emit dataChanged(index(0, Status), index(priv->size()-1, Status));
emit dataChanged(index(0, Description), index(priv->size()-1, Description));
} }
} }