|
|
@ -741,138 +741,140 @@ void CWallet::MarkDirty() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb) |
|
|
|
bool CWallet::AddToWallet(const CWalletTx& wtxIn, CWalletDB* pwalletdb) |
|
|
|
{ |
|
|
|
{ |
|
|
|
uint256 hash = wtxIn.GetHash(); |
|
|
|
uint256 hash = wtxIn.GetHash(); |
|
|
|
|
|
|
|
|
|
|
|
if (fFromLoadWallet) |
|
|
|
LOCK(cs_wallet); |
|
|
|
{ |
|
|
|
// Inserts only if not already there, returns tx inserted or tx found
|
|
|
|
mapWallet[hash] = wtxIn; |
|
|
|
pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn)); |
|
|
|
CWalletTx& wtx = mapWallet[hash]; |
|
|
|
CWalletTx& wtx = (*ret.first).second; |
|
|
|
wtx.BindWallet(this); |
|
|
|
wtx.BindWallet(this); |
|
|
|
|
|
|
|
bool fInsertedNew = ret.second; |
|
|
|
|
|
|
|
if (fInsertedNew) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.nTimeReceived = GetAdjustedTime(); |
|
|
|
|
|
|
|
wtx.nOrderPos = IncOrderPosNext(pwalletdb); |
|
|
|
wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); |
|
|
|
wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); |
|
|
|
AddToSpends(hash); |
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, wtx.vin) { |
|
|
|
wtx.nTimeSmart = wtx.nTimeReceived; |
|
|
|
if (mapWallet.count(txin.prevout.hash)) { |
|
|
|
if (!wtxIn.hashUnset()) |
|
|
|
CWalletTx& prevtx = mapWallet[txin.prevout.hash]; |
|
|
|
{ |
|
|
|
if (prevtx.nIndex == -1 && !prevtx.hashUnset()) { |
|
|
|
if (mapBlockIndex.count(wtxIn.hashBlock)) |
|
|
|
MarkConflicted(prevtx.hashBlock, wtx.GetHash()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LOCK(cs_wallet); |
|
|
|
|
|
|
|
// Inserts only if not already there, returns tx inserted or tx found
|
|
|
|
|
|
|
|
pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn)); |
|
|
|
|
|
|
|
CWalletTx& wtx = (*ret.first).second; |
|
|
|
|
|
|
|
wtx.BindWallet(this); |
|
|
|
|
|
|
|
bool fInsertedNew = ret.second; |
|
|
|
|
|
|
|
if (fInsertedNew) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.nTimeReceived = GetAdjustedTime(); |
|
|
|
|
|
|
|
wtx.nOrderPos = IncOrderPosNext(pwalletdb); |
|
|
|
|
|
|
|
wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wtx.nTimeSmart = wtx.nTimeReceived; |
|
|
|
|
|
|
|
if (!wtxIn.hashUnset()) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (mapBlockIndex.count(wtxIn.hashBlock)) |
|
|
|
int64_t latestNow = wtx.nTimeReceived; |
|
|
|
|
|
|
|
int64_t latestEntry = 0; |
|
|
|
{ |
|
|
|
{ |
|
|
|
int64_t latestNow = wtx.nTimeReceived; |
|
|
|
// Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
|
|
|
|
int64_t latestEntry = 0; |
|
|
|
int64_t latestTolerated = latestNow + 300; |
|
|
|
|
|
|
|
const TxItems & txOrdered = wtxOrdered; |
|
|
|
|
|
|
|
for (TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
|
|
|
|
CWalletTx *const pwtx = (*it).second.first; |
|
|
|
int64_t latestTolerated = latestNow + 300; |
|
|
|
if (pwtx == &wtx) |
|
|
|
const TxItems & txOrdered = wtxOrdered; |
|
|
|
continue; |
|
|
|
for (TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) |
|
|
|
CAccountingEntry *const pacentry = (*it).second.second; |
|
|
|
|
|
|
|
int64_t nSmartTime; |
|
|
|
|
|
|
|
if (pwtx) |
|
|
|
{ |
|
|
|
{ |
|
|
|
CWalletTx *const pwtx = (*it).second.first; |
|
|
|
nSmartTime = pwtx->nTimeSmart; |
|
|
|
if (pwtx == &wtx) |
|
|
|
if (!nSmartTime) |
|
|
|
continue; |
|
|
|
nSmartTime = pwtx->nTimeReceived; |
|
|
|
CAccountingEntry *const pacentry = (*it).second.second; |
|
|
|
} |
|
|
|
int64_t nSmartTime; |
|
|
|
else |
|
|
|
if (pwtx) |
|
|
|
nSmartTime = pacentry->nTime; |
|
|
|
{ |
|
|
|
if (nSmartTime <= latestTolerated) |
|
|
|
nSmartTime = pwtx->nTimeSmart; |
|
|
|
{ |
|
|
|
if (!nSmartTime) |
|
|
|
latestEntry = nSmartTime; |
|
|
|
nSmartTime = pwtx->nTimeReceived; |
|
|
|
if (nSmartTime > latestNow) |
|
|
|
} |
|
|
|
latestNow = nSmartTime; |
|
|
|
else |
|
|
|
break; |
|
|
|
nSmartTime = pacentry->nTime; |
|
|
|
|
|
|
|
if (nSmartTime <= latestTolerated) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
latestEntry = nSmartTime; |
|
|
|
|
|
|
|
if (nSmartTime > latestNow) |
|
|
|
|
|
|
|
latestNow = nSmartTime; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime(); |
|
|
|
|
|
|
|
wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
|
LogPrintf("AddToWallet(): found %s in block %s not in index\n", |
|
|
|
int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime(); |
|
|
|
wtxIn.GetHash().ToString(), |
|
|
|
wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); |
|
|
|
wtxIn.hashBlock.ToString()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
AddToSpends(hash); |
|
|
|
else |
|
|
|
|
|
|
|
LogPrintf("AddToWallet(): found %s in block %s not in index\n", |
|
|
|
|
|
|
|
wtxIn.GetHash().ToString(), |
|
|
|
|
|
|
|
wtxIn.hashBlock.ToString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
AddToSpends(hash); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool fUpdated = false; |
|
|
|
bool fUpdated = false; |
|
|
|
if (!fInsertedNew) |
|
|
|
if (!fInsertedNew) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Merge
|
|
|
|
|
|
|
|
if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Merge
|
|
|
|
wtx.hashBlock = wtxIn.hashBlock; |
|
|
|
if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) |
|
|
|
fUpdated = true; |
|
|
|
{ |
|
|
|
|
|
|
|
wtx.hashBlock = wtxIn.hashBlock; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// If no longer abandoned, update
|
|
|
|
|
|
|
|
if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.hashBlock = wtxIn.hashBlock; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.nIndex = wtxIn.nIndex; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.fFromMe = wtxIn.fFromMe; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// If no longer abandoned, update
|
|
|
|
|
|
|
|
if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.hashBlock = wtxIn.hashBlock; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.nIndex = wtxIn.nIndex; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
wtx.fFromMe = wtxIn.fFromMe; |
|
|
|
|
|
|
|
fUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//// debug print
|
|
|
|
//// debug print
|
|
|
|
LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : "")); |
|
|
|
LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : "")); |
|
|
|
|
|
|
|
|
|
|
|
// Write to disk
|
|
|
|
// Write to disk
|
|
|
|
if (fInsertedNew || fUpdated) |
|
|
|
if (fInsertedNew || fUpdated) |
|
|
|
if (!pwalletdb->WriteTx(wtx)) |
|
|
|
if (!pwalletdb->WriteTx(wtx)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
// Break debit/credit balance caches:
|
|
|
|
// Break debit/credit balance caches:
|
|
|
|
wtx.MarkDirty(); |
|
|
|
wtx.MarkDirty(); |
|
|
|
|
|
|
|
|
|
|
|
// Notify UI of new or updated transaction
|
|
|
|
// Notify UI of new or updated transaction
|
|
|
|
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED); |
|
|
|
NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED); |
|
|
|
|
|
|
|
|
|
|
|
// notify an external script when a wallet transaction comes in or is updated
|
|
|
|
// notify an external script when a wallet transaction comes in or is updated
|
|
|
|
std::string strCmd = GetArg("-walletnotify", ""); |
|
|
|
std::string strCmd = GetArg("-walletnotify", ""); |
|
|
|
|
|
|
|
|
|
|
|
if ( !strCmd.empty()) |
|
|
|
if ( !strCmd.empty()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex()); |
|
|
|
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex()); |
|
|
|
boost::thread t(runCommand, strCmd); // thread runs free
|
|
|
|
boost::thread t(runCommand, strCmd); // thread runs free
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CWallet::LoadToWallet(const CWalletTx& wtxIn) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint256 hash = wtxIn.GetHash(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mapWallet[hash] = wtxIn; |
|
|
|
|
|
|
|
CWalletTx& wtx = mapWallet[hash]; |
|
|
|
|
|
|
|
wtx.BindWallet(this); |
|
|
|
|
|
|
|
wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); |
|
|
|
|
|
|
|
AddToSpends(hash); |
|
|
|
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, wtx.vin) { |
|
|
|
|
|
|
|
if (mapWallet.count(txin.prevout.hash)) { |
|
|
|
|
|
|
|
CWalletTx& prevtx = mapWallet[txin.prevout.hash]; |
|
|
|
|
|
|
|
if (prevtx.nIndex == -1 && !prevtx.hashUnset()) { |
|
|
|
|
|
|
|
MarkConflicted(prevtx.hashBlock, wtx.GetHash()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -913,7 +915,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl |
|
|
|
// this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
|
|
|
|
// this is safe, as in case of a crash, we rescan the necessary blocks on startup through our SetBestChain-mechanism
|
|
|
|
CWalletDB walletdb(strWalletFile, "r+", false); |
|
|
|
CWalletDB walletdb(strWalletFile, "r+", false); |
|
|
|
|
|
|
|
|
|
|
|
return AddToWallet(wtx, false, &walletdb); |
|
|
|
return AddToWallet(wtx, &walletdb); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
@ -2456,7 +2458,7 @@ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey) |
|
|
|
|
|
|
|
|
|
|
|
// Add tx to wallet, because if it has change it's also ours,
|
|
|
|
// Add tx to wallet, because if it has change it's also ours,
|
|
|
|
// otherwise just for transaction history.
|
|
|
|
// otherwise just for transaction history.
|
|
|
|
AddToWallet(wtxNew, false, pwalletdb); |
|
|
|
AddToWallet(wtxNew, pwalletdb); |
|
|
|
|
|
|
|
|
|
|
|
// Notify that old coins are spent
|
|
|
|
// Notify that old coins are spent
|
|
|
|
set<CWalletTx*> setCoins; |
|
|
|
set<CWalletTx*> setCoins; |
|
|
|