Skip to content

Commit

Permalink
[Bug] Fix nTimeSmart computation
Browse files Browse the repository at this point in the history
Given the strict timestamp checks enforced with POS TimeProtocol v2
(introduced with PIVX-Project#1002: a block cannot have a timestamp earlier than
previous blocks and cannot be more than 15 seconds in the future), we
don't need the complex (and error-prone) logic of ComputeTimeSmart. We
can, instead, simply rely on the blocktime, which already ensures a
proper ordering for the transactions.
  • Loading branch information
random-zebra committed Apr 25, 2020
1 parent 440c8f5 commit 3f31b4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
41 changes: 9 additions & 32 deletions src/wallet/wallet.cpp
Expand Up @@ -3323,44 +3323,21 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const
mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
}

/**
* Update smart timestamp for a transaction being added to the wallet.
*
* Logic:
* - If the transaction is not yet part of a block, assign its timestamp to the current time.
* - Else assign its timestamp to the block time
*/
unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
{
unsigned int nTimeSmart = wtx.nTimeReceived;
if (!wtx.hashBlock.IsNull()) {
if (mapBlockIndex.count(wtx.hashBlock)) {
int64_t latestNow = wtx.nTimeReceived;
int64_t latestEntry = 0;
{
// Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
int64_t latestTolerated = latestNow + 300;
TxItems txOrdered = wtxOrdered;
for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
CWalletTx* const pwtx = (*it).second.first;
if (pwtx == &wtx)
continue;
CAccountingEntry* const pacentry = (*it).second.second;
int64_t nSmartTime;
if (pwtx) {
nSmartTime = pwtx->nTimeSmart;
if (!nSmartTime)
nSmartTime = pwtx->nTimeReceived;
} else
nSmartTime = pacentry->nTime;
if (nSmartTime <= latestTolerated) {
latestEntry = nSmartTime;
if (nSmartTime > latestNow)
latestNow = nSmartTime;
break;
}
}
}

int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
nTimeSmart = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
} else
LogPrintf("AddToWallet() : found %s in block %s not in index\n",
wtx.GetHash().ToString(),
wtx.hashBlock.ToString());
LogPrintf("%s : found %s in block %s not in index\n", __func__, wtx.GetHash().ToString(), wtx.hashBlock.ToString());
}
return nTimeSmart;
}
Expand Down
4 changes: 4 additions & 0 deletions src/wallet/wallet.h
Expand Up @@ -769,6 +769,10 @@ class CWalletTx : public CMerkleTx
std::vector<std::pair<std::string, std::string> > vOrderForm;
unsigned int fTimeReceivedIsTxTime;
unsigned int nTimeReceived; //! time received by this node
/**
* Stable timestamp representing the block time, for a transaction included in a block,
* or else the time when the transaction was received if it isn't yet part of a block.
*/
unsigned int nTimeSmart;
char fFromMe;
std::string strFromAccount;
Expand Down

0 comments on commit 3f31b4f

Please sign in to comment.