Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 1,194 Lines • ▼ Show 20 Lines | bool CWallet::AbandonTransaction(const TxId &txid) { | ||||
return true; | return true; | ||||
} | } | ||||
void CWallet::MarkConflicted(const uint256 &hashBlock, const TxId &txid) { | void CWallet::MarkConflicted(const uint256 &hashBlock, const TxId &txid) { | ||||
LOCK2(cs_main, cs_wallet); | LOCK2(cs_main, cs_wallet); | ||||
int conflictconfirms = 0; | int conflictconfirms = 0; | ||||
if (mapBlockIndex.count(hashBlock)) { | CBlockIndex *pindex = LookupBlockIndex(hashBlock); | ||||
CBlockIndex *pindex = mapBlockIndex[hashBlock]; | if (pindex && chainActive.Contains(pindex)) { | ||||
if (chainActive.Contains(pindex)) { | |||||
conflictconfirms = -(chainActive.Height() - pindex->nHeight + 1); | conflictconfirms = -(chainActive.Height() - pindex->nHeight + 1); | ||||
} | } | ||||
} | |||||
// If number of conflict confirms cannot be determined, this means that the | // If number of conflict confirms cannot be determined, this means that the | ||||
// block is still unknown or not yet part of the main chain, for example | // block is still unknown or not yet part of the main chain, for example | ||||
// when loading the wallet during a reindex. Do nothing in that case. | // when loading the wallet during a reindex. Do nothing in that case. | ||||
if (conflictconfirms >= 0) { | if (conflictconfirms >= 0) { | ||||
return; | return; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 2,783 Lines • ▼ Show 20 Lines | if (mapKeyFirstBlock.empty()) { | ||||
return; | return; | ||||
} | } | ||||
// Find first block that affects those keys, if there are any left. | // Find first block that affects those keys, if there are any left. | ||||
std::vector<CKeyID> vAffected; | std::vector<CKeyID> vAffected; | ||||
for (const auto &entry : mapWallet) { | for (const auto &entry : mapWallet) { | ||||
// iterate over all wallet transactions... | // iterate over all wallet transactions... | ||||
const CWalletTx &wtx = entry.second; | const CWalletTx &wtx = entry.second; | ||||
BlockMap::const_iterator blit = mapBlockIndex.find(wtx.hashBlock); | CBlockIndex *pindex = LookupBlockIndex(wtx.hashBlock); | ||||
if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) { | if (pindex && chainActive.Contains(pindex)) { | ||||
// ... which are already in a block. | // ... which are already in a block | ||||
int nHeight = blit->second->nHeight; | int nHeight = pindex->nHeight; | ||||
for (const CTxOut &txout : wtx.tx->vout) { | for (const CTxOut &txout : wtx.tx->vout) { | ||||
// Iterate over all their outputs... | // Iterate over all their outputs... | ||||
CAffectedKeysVisitor(*this, vAffected) | CAffectedKeysVisitor(*this, vAffected) | ||||
.Process(txout.scriptPubKey); | .Process(txout.scriptPubKey); | ||||
for (const CKeyID &keyid : vAffected) { | for (const CKeyID &keyid : vAffected) { | ||||
// ... and all their affected keys. | // ... and all their affected keys. | ||||
std::map<CKeyID, CBlockIndex *>::iterator rit = | std::map<CKeyID, CBlockIndex *>::iterator rit = | ||||
mapKeyFirstBlock.find(keyid); | mapKeyFirstBlock.find(keyid); | ||||
if (rit != mapKeyFirstBlock.end() && | if (rit != mapKeyFirstBlock.end() && | ||||
nHeight < rit->second->nHeight) { | nHeight < rit->second->nHeight) { | ||||
rit->second = blit->second; | rit->second = pindex; | ||||
} | } | ||||
} | } | ||||
vAffected.clear(); | vAffected.clear(); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Extract block timestamps for those keys. | // Extract block timestamps for those keys. | ||||
Show All 23 Lines | |||||
* | * | ||||
* For more information see CWalletTx::nTimeSmart, | * For more information see CWalletTx::nTimeSmart, | ||||
* https://bitcointalk.org/?topic=54527, or | * https://bitcointalk.org/?topic=54527, or | ||||
* https://github.com/bitcoin/bitcoin/pull/1393. | * https://github.com/bitcoin/bitcoin/pull/1393. | ||||
*/ | */ | ||||
unsigned int CWallet::ComputeTimeSmart(const CWalletTx &wtx) const { | unsigned int CWallet::ComputeTimeSmart(const CWalletTx &wtx) const { | ||||
unsigned int nTimeSmart = wtx.nTimeReceived; | unsigned int nTimeSmart = wtx.nTimeReceived; | ||||
if (!wtx.hashUnset()) { | if (!wtx.hashUnset()) { | ||||
if (mapBlockIndex.count(wtx.hashBlock)) { | const CBlockIndex *pindex = nullptr; | ||||
{ | |||||
LOCK(cs_main); | |||||
pindex = LookupBlockIndex(wtx.hashBlock); | |||||
} | |||||
if (pindex) { | |||||
int64_t latestNow = wtx.nTimeReceived; | int64_t latestNow = wtx.nTimeReceived; | ||||
int64_t latestEntry = 0; | int64_t latestEntry = 0; | ||||
// Tolerate times up to the last timestamp in the wallet not more | // Tolerate times up to the last timestamp in the wallet not more | ||||
// than 5 minutes into the future | // than 5 minutes into the future | ||||
int64_t latestTolerated = latestNow + 300; | int64_t latestTolerated = latestNow + 300; | ||||
const TxItems &txOrdered = wtxOrdered; | const TxItems &txOrdered = wtxOrdered; | ||||
for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) { | for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) { | ||||
Show All 15 Lines | if (!wtx.hashUnset()) { | ||||
latestEntry = nSmartTime; | latestEntry = nSmartTime; | ||||
if (nSmartTime > latestNow) { | if (nSmartTime > latestNow) { | ||||
latestNow = nSmartTime; | latestNow = nSmartTime; | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
int64_t blocktime = mapBlockIndex[wtx.hashBlock]->GetBlockTime(); | int64_t blocktime = pindex->GetBlockTime(); | ||||
nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); | ||||
} else { | } else { | ||||
LogPrintf("%s: found %s in block %s not in index\n", __func__, | LogPrintf("%s: found %s in block %s not in index\n", __func__, | ||||
wtx.GetId().ToString(), wtx.hashBlock.ToString()); | wtx.GetId().ToString(), wtx.hashBlock.ToString()); | ||||
} | } | ||||
} | } | ||||
return nTimeSmart; | return nTimeSmart; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 173 Lines • ▼ Show 20 Lines | if (fFirstRun) { | ||||
} | } | ||||
} | } | ||||
LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart); | LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart); | ||||
// Try to top up keypool. No-op if the wallet is locked. | // Try to top up keypool. No-op if the wallet is locked. | ||||
walletInstance->TopUpKeyPool(); | walletInstance->TopUpKeyPool(); | ||||
LOCK(cs_main); | |||||
CBlockIndex *pindexRescan = chainActive.Genesis(); | CBlockIndex *pindexRescan = chainActive.Genesis(); | ||||
if (!gArgs.GetBoolArg("-rescan", false)) { | if (!gArgs.GetBoolArg("-rescan", false)) { | ||||
CWalletDB walletdb(*walletInstance->dbw); | CWalletDB walletdb(*walletInstance->dbw); | ||||
CBlockLocator locator; | CBlockLocator locator; | ||||
if (walletdb.ReadBestBlock(locator)) { | if (walletdb.ReadBestBlock(locator)) { | ||||
pindexRescan = FindForkInGlobalIndex(chainActive, locator); | pindexRescan = FindForkInGlobalIndex(chainActive, locator); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | |||||
int CMerkleTx::GetDepthInMainChain(const CBlockIndex *&pindexRet) const { | int CMerkleTx::GetDepthInMainChain(const CBlockIndex *&pindexRet) const { | ||||
if (hashUnset()) { | if (hashUnset()) { | ||||
return 0; | return 0; | ||||
} | } | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
// Find the block it claims to be in. | // Find the block it claims to be in. | ||||
BlockMap::iterator mi = mapBlockIndex.find(hashBlock); | CBlockIndex *pindex = LookupBlockIndex(hashBlock); | ||||
if (mi == mapBlockIndex.end()) { | |||||
return 0; | |||||
} | |||||
CBlockIndex *pindex = (*mi).second; | |||||
if (!pindex || !chainActive.Contains(pindex)) { | if (!pindex || !chainActive.Contains(pindex)) { | ||||
return 0; | return 0; | ||||
} | } | ||||
pindexRet = pindex; | pindexRet = pindex; | ||||
return ((nIndex == -1) ? (-1) : 1) * | return ((nIndex == -1) ? (-1) : 1) * | ||||
(chainActive.Height() - pindex->nHeight + 1); | (chainActive.Height() - pindex->nHeight + 1); | ||||
} | } | ||||
Show All 28 Lines |