Skip to content

Commit 1f98abe

Browse files
committed
Factor out CWallet::nTimeSmart computation into a method.
No change in behavior, this change just pulls some code out of CWallet::AddToWallet that was making it very long into a separate method.
1 parent c6b82d1 commit 1f98abe

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
lines changed

src/wallet/wallet.cpp

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -896,51 +896,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
896896
wtx.nTimeReceived = GetAdjustedTime();
897897
wtx.nOrderPos = IncOrderPosNext(&walletdb);
898898
wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
899-
900-
wtx.nTimeSmart = wtx.nTimeReceived;
901-
if (!wtxIn.hashUnset())
902-
{
903-
if (mapBlockIndex.count(wtxIn.hashBlock))
904-
{
905-
int64_t latestNow = wtx.nTimeReceived;
906-
int64_t latestEntry = 0;
907-
{
908-
// Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
909-
int64_t latestTolerated = latestNow + 300;
910-
const TxItems & txOrdered = wtxOrdered;
911-
for (TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
912-
{
913-
CWalletTx *const pwtx = (*it).second.first;
914-
if (pwtx == &wtx)
915-
continue;
916-
CAccountingEntry *const pacentry = (*it).second.second;
917-
int64_t nSmartTime;
918-
if (pwtx)
919-
{
920-
nSmartTime = pwtx->nTimeSmart;
921-
if (!nSmartTime)
922-
nSmartTime = pwtx->nTimeReceived;
923-
}
924-
else
925-
nSmartTime = pacentry->nTime;
926-
if (nSmartTime <= latestTolerated)
927-
{
928-
latestEntry = nSmartTime;
929-
if (nSmartTime > latestNow)
930-
latestNow = nSmartTime;
931-
break;
932-
}
933-
}
934-
}
935-
936-
int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime();
937-
wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
938-
}
939-
else
940-
LogPrintf("AddToWallet(): found %s in block %s not in index\n",
941-
wtxIn.GetHash().ToString(),
942-
wtxIn.hashBlock.ToString());
943-
}
899+
wtx.nTimeSmart = ComputeTimeSmart(wtx);
944900
AddToSpends(hash);
945901
}
946902

@@ -3498,6 +3454,55 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
34983454
mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
34993455
}
35003456

3457+
unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
3458+
{
3459+
unsigned int nTimeSmart = wtx.nTimeReceived;
3460+
if (!wtx.hashUnset())
3461+
{
3462+
if (mapBlockIndex.count(wtx.hashBlock))
3463+
{
3464+
int64_t latestNow = wtx.nTimeReceived;
3465+
int64_t latestEntry = 0;
3466+
{
3467+
// Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
3468+
int64_t latestTolerated = latestNow + 300;
3469+
const TxItems & txOrdered = wtxOrdered;
3470+
for (TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
3471+
{
3472+
CWalletTx *const pwtx = (*it).second.first;
3473+
if (pwtx == &wtx)
3474+
continue;
3475+
CAccountingEntry *const pacentry = (*it).second.second;
3476+
int64_t nSmartTime;
3477+
if (pwtx)
3478+
{
3479+
nSmartTime = pwtx->nTimeSmart;
3480+
if (!nSmartTime)
3481+
nSmartTime = pwtx->nTimeReceived;
3482+
}
3483+
else
3484+
nSmartTime = pacentry->nTime;
3485+
if (nSmartTime <= latestTolerated)
3486+
{
3487+
latestEntry = nSmartTime;
3488+
if (nSmartTime > latestNow)
3489+
latestNow = nSmartTime;
3490+
break;
3491+
}
3492+
}
3493+
}
3494+
3495+
int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
3496+
nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
3497+
}
3498+
else
3499+
LogPrintf("AddToWallet(): found %s in block %s not in index\n",
3500+
wtx.GetHash().ToString(),
3501+
wtx.hashBlock.ToString());
3502+
}
3503+
return nTimeSmart;
3504+
}
3505+
35013506
bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
35023507
{
35033508
if (boost::get<CNoDestination>(&dest))

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
776776
bool EncryptWallet(const SecureString& strWalletPassphrase);
777777

778778
void GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) const;
779+
unsigned int ComputeTimeSmart(const CWalletTx& wtx) const;
779780

780781
/**
781782
* Increment the next transaction order id

0 commit comments

Comments
 (0)