Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 869 Lines • ▼ Show 20 Lines | |||||
DBErrors CWallet::ReorderTransactions() { | DBErrors CWallet::ReorderTransactions() { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
WalletBatch batch(*database); | WalletBatch batch(*database); | ||||
// Old wallets didn't have any defined order for transactions. Probably a | // Old wallets didn't have any defined order for transactions. Probably a | ||||
// bad idea to change the output of this. | // bad idea to change the output of this. | ||||
// First: get all CWalletTx and CAccountingEntry into a sorted-by-time | // First: get all CWalletTx into a sorted-by-time | ||||
// multimap. | // multimap. | ||||
TxItems txByTime; | TxItems txByTime; | ||||
for (auto &entry : mapWallet) { | for (auto &entry : mapWallet) { | ||||
CWalletTx *wtx = &entry.second; | CWalletTx *wtx = &entry.second; | ||||
txByTime.insert( | txByTime.insert( | ||||
std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr))); | std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr))); | ||||
} | } | ||||
std::list<CAccountingEntry> acentries; | |||||
batch.ListAccountCreditDebit("", acentries); | |||||
for (CAccountingEntry &entry : acentries) { | |||||
txByTime.insert(std::make_pair(entry.nTime, TxPair(nullptr, &entry))); | |||||
} | |||||
nOrderPosNext = 0; | nOrderPosNext = 0; | ||||
std::vector<int64_t> nOrderPosOffsets; | std::vector<int64_t> nOrderPosOffsets; | ||||
for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) { | for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) { | ||||
CWalletTx *const pwtx = (*it).second.first; | CWalletTx *const pwtx = (*it).second.first; | ||||
CAccountingEntry *const pacentry = (*it).second.second; | CAccountingEntry *const pacentry = (*it).second.second; | ||||
int64_t &nOrderPos = | int64_t &nOrderPos = | ||||
(pwtx != nullptr) ? pwtx->nOrderPos : pacentry->nOrderPos; | (pwtx != nullptr) ? pwtx->nOrderPos : pacentry->nOrderPos; | ||||
if (nOrderPos == -1) { | if (nOrderPos == -1) { | ||||
nOrderPos = nOrderPosNext++; | nOrderPos = nOrderPosNext++; | ||||
nOrderPosOffsets.push_back(nOrderPos); | nOrderPosOffsets.push_back(nOrderPos); | ||||
if (pwtx) { | if (pwtx) { | ||||
if (!batch.WriteTx(*pwtx)) { | if (!batch.WriteTx(*pwtx)) { | ||||
return DBErrors::LOAD_FAIL; | return DBErrors::LOAD_FAIL; | ||||
} | } | ||||
} else if (!batch.WriteAccountingEntry(pacentry->nEntryNo, | |||||
*pacentry)) { | |||||
return DBErrors::LOAD_FAIL; | |||||
} | } | ||||
} else { | } else { | ||||
int64_t nOrderPosOff = 0; | int64_t nOrderPosOff = 0; | ||||
for (const int64_t &nOffsetStart : nOrderPosOffsets) { | for (const int64_t &nOffsetStart : nOrderPosOffsets) { | ||||
if (nOrderPos >= nOffsetStart) { | if (nOrderPos >= nOffsetStart) { | ||||
++nOrderPosOff; | ++nOrderPosOff; | ||||
} | } | ||||
} | } | ||||
nOrderPos += nOrderPosOff; | nOrderPos += nOrderPosOff; | ||||
nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1); | nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1); | ||||
if (!nOrderPosOff) { | if (!nOrderPosOff) { | ||||
continue; | continue; | ||||
} | } | ||||
// Since we're changing the order, write it back. | // Since we're changing the order, write it back. | ||||
if (pwtx) { | if (pwtx) { | ||||
if (!batch.WriteTx(*pwtx)) { | if (!batch.WriteTx(*pwtx)) { | ||||
return DBErrors::LOAD_FAIL; | return DBErrors::LOAD_FAIL; | ||||
} | } | ||||
} else if (!batch.WriteAccountingEntry(pacentry->nEntryNo, | |||||
*pacentry)) { | |||||
return DBErrors::LOAD_FAIL; | |||||
} | } | ||||
} | } | ||||
} | } | ||||
batch.WriteOrderPosNext(nOrderPosNext); | batch.WriteOrderPosNext(nOrderPosNext); | ||||
return DBErrors::LOAD_OK; | return DBErrors::LOAD_OK; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 4,060 Lines • Show Last 20 Lines |