Skip to content

Commit

Permalink
[Refactor] Encapsulate ComputeTimeSmart in CWalletTx
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Apr 26, 2020
1 parent db99c41 commit b1dc5d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
44 changes: 22 additions & 22 deletions src/wallet/wallet.cpp
Expand Up @@ -846,11 +846,10 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletD
wtx.BindWallet(this);
bool fInsertedNew = ret.second;
if (fInsertedNew) {
if (!wtx.nTimeReceived)
wtx.nTimeReceived = GetAdjustedTime();
wtx.nTimeReceived = GetAdjustedTime();
wtx.nOrderPos = IncOrderPosNext(pwalletdb);
wtxOrdered.insert(std::make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0)));
wtx.nTimeSmart = ComputeTimeSmart(wtx);
wtx.UpdateTimeSmart();
AddToSpends(hash);
for (const CTxIn& txin : wtx.vin) {
if (mapWallet.count(txin.prevout.hash)) {
Expand All @@ -867,11 +866,13 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletD
// Merge
if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) {
wtx.hashBlock = wtxIn.hashBlock;
wtx.UpdateTimeSmart();
fUpdated = true;
}
// If no longer abandoned, update
if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) {
wtx.hashBlock = wtxIn.hashBlock;
if (!fUpdated) wtx.UpdateTimeSmart();
fUpdated = true;
}
if (wtxIn.nIndex != -1 && wtxIn.nIndex != wtx.nIndex) {
Expand Down Expand Up @@ -1177,6 +1178,24 @@ int64_t CWalletTx::GetTxTime() const
return n ? n : nTimeReceived;
}

/**
* 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
*/
void CWalletTx::UpdateTimeSmart()
{
nTimeSmart = nTimeReceived;
if (!hashBlock.IsNull()) {
if (mapBlockIndex.count(hashBlock)) {
nTimeSmart = mapBlockIndex.at(hashBlock)->GetBlockTime();
} else
LogPrintf("%s : found %s in block %s not in index\n", __func__, GetHash().ToString(), hashBlock.ToString());
}
}

int CWalletTx::GetRequestCount() const
{
// Returns -1 if it wasn't being tracked
Expand Down Expand Up @@ -3311,25 +3330,6 @@ 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)) {
nTimeSmart = mapBlockIndex[wtx.hashBlock]->GetBlockTime();
} else
LogPrintf("%s : found %s in block %s not in index\n", __func__, wtx.GetHash().ToString(), wtx.hashBlock.ToString());
}
return nTimeSmart;
}

bool CWallet::AddDestData(const CTxDestination& dest, const std::string& key, const std::string& value)
{
if (boost::get<CNoDestination>(&dest))
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Expand Up @@ -426,7 +426,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

std::vector<CKeyID> GetAffectedKeys(const CScript& spk);
void GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const;
unsigned int ComputeTimeSmart(const CWalletTx& wtx) const;

/**
* Increment the next transaction order id
Expand Down Expand Up @@ -910,6 +909,7 @@ class CWalletTx : public CMerkleTx
bool WriteToDisk(CWalletDB *pwalletdb);

int64_t GetTxTime() const;
void UpdateTimeSmart();
int GetRequestCount() const;
void RelayWalletTransaction(std::string strCommand = "tx");
std::set<uint256> GetConflicts() const;
Expand Down

0 comments on commit b1dc5d3

Please sign in to comment.