Skip to content

Commit a4fd8df

Browse files
committed
Merge #8828: Move CWalletDB::ReorderTransactions to CWallet
86029e7 Move CWalletDB::ReorderTransactions to CWallet (Patrick Strateman)
2 parents bae81b8 + 86029e7 commit a4fd8df

File tree

3 files changed

+74
-80
lines changed

3 files changed

+74
-80
lines changed

src/wallet/wallet.cpp

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,79 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
658658

659659
DBErrors CWallet::ReorderTransactions()
660660
{
661+
LOCK(cs_wallet);
661662
CWalletDB walletdb(strWalletFile);
662-
return walletdb.ReorderTransactions(this);
663+
664+
// Old wallets didn't have any defined order for transactions
665+
// Probably a bad idea to change the output of this
666+
667+
// First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
668+
typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
669+
typedef multimap<int64_t, TxPair > TxItems;
670+
TxItems txByTime;
671+
672+
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
673+
{
674+
CWalletTx* wtx = &((*it).second);
675+
txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
676+
}
677+
list<CAccountingEntry> acentries;
678+
walletdb.ListAccountCreditDebit("", acentries);
679+
BOOST_FOREACH(CAccountingEntry& entry, acentries)
680+
{
681+
txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
682+
}
683+
684+
nOrderPosNext = 0;
685+
std::vector<int64_t> nOrderPosOffsets;
686+
for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
687+
{
688+
CWalletTx *const pwtx = (*it).second.first;
689+
CAccountingEntry *const pacentry = (*it).second.second;
690+
int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
691+
692+
if (nOrderPos == -1)
693+
{
694+
nOrderPos = nOrderPosNext++;
695+
nOrderPosOffsets.push_back(nOrderPos);
696+
697+
if (pwtx)
698+
{
699+
if (!walletdb.WriteTx(*pwtx))
700+
return DB_LOAD_FAIL;
701+
}
702+
else
703+
if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
704+
return DB_LOAD_FAIL;
705+
}
706+
else
707+
{
708+
int64_t nOrderPosOff = 0;
709+
BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
710+
{
711+
if (nOrderPos >= nOffsetStart)
712+
++nOrderPosOff;
713+
}
714+
nOrderPos += nOrderPosOff;
715+
nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
716+
717+
if (!nOrderPosOff)
718+
continue;
719+
720+
// Since we're changing the order, write it back
721+
if (pwtx)
722+
{
723+
if (!walletdb.WriteTx(*pwtx))
724+
return DB_LOAD_FAIL;
725+
}
726+
else
727+
if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
728+
return DB_LOAD_FAIL;
729+
}
730+
}
731+
walletdb.WriteOrderPosNext(nOrderPosNext);
732+
733+
return DB_LOAD_OK;
663734
}
664735

665736
int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)

src/wallet/walletdb.cpp

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -251,82 +251,6 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountin
251251
pcursor->close();
252252
}
253253

254-
DBErrors CWalletDB::ReorderTransactions(CWallet* pwallet)
255-
{
256-
LOCK(pwallet->cs_wallet);
257-
// Old wallets didn't have any defined order for transactions
258-
// Probably a bad idea to change the output of this
259-
260-
// First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
261-
typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
262-
typedef multimap<int64_t, TxPair > TxItems;
263-
TxItems txByTime;
264-
265-
for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
266-
{
267-
CWalletTx* wtx = &((*it).second);
268-
txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
269-
}
270-
list<CAccountingEntry> acentries;
271-
ListAccountCreditDebit("", acentries);
272-
BOOST_FOREACH(CAccountingEntry& entry, acentries)
273-
{
274-
txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
275-
}
276-
277-
int64_t& nOrderPosNext = pwallet->nOrderPosNext;
278-
nOrderPosNext = 0;
279-
std::vector<int64_t> nOrderPosOffsets;
280-
for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
281-
{
282-
CWalletTx *const pwtx = (*it).second.first;
283-
CAccountingEntry *const pacentry = (*it).second.second;
284-
int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
285-
286-
if (nOrderPos == -1)
287-
{
288-
nOrderPos = nOrderPosNext++;
289-
nOrderPosOffsets.push_back(nOrderPos);
290-
291-
if (pwtx)
292-
{
293-
if (!WriteTx(*pwtx))
294-
return DB_LOAD_FAIL;
295-
}
296-
else
297-
if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
298-
return DB_LOAD_FAIL;
299-
}
300-
else
301-
{
302-
int64_t nOrderPosOff = 0;
303-
BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
304-
{
305-
if (nOrderPos >= nOffsetStart)
306-
++nOrderPosOff;
307-
}
308-
nOrderPos += nOrderPosOff;
309-
nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
310-
311-
if (!nOrderPosOff)
312-
continue;
313-
314-
// Since we're changing the order, write it back
315-
if (pwtx)
316-
{
317-
if (!WriteTx(*pwtx))
318-
return DB_LOAD_FAIL;
319-
}
320-
else
321-
if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
322-
return DB_LOAD_FAIL;
323-
}
324-
}
325-
WriteOrderPosNext(nOrderPosNext);
326-
327-
return DB_LOAD_OK;
328-
}
329-
330254
class CWalletScanState {
331255
public:
332256
unsigned int nKeys;
@@ -711,7 +635,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
711635
WriteVersion(CLIENT_VERSION);
712636

713637
if (wss.fAnyUnordered)
714-
result = ReorderTransactions(pwallet);
638+
result = pwallet->ReorderTransactions();
715639

716640
pwallet->laccentries.clear();
717641
ListAccountCreditDebit("*", pwallet->laccentries);

src/wallet/walletdb.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class CWalletDB : public CDB
153153

154154
/// This writes directly to the database, and will not update the CWallet's cached accounting entries!
155155
/// Use wallet.AddAccountingEntry instead, to write *and* update its caches.
156+
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
156157
bool WriteAccountingEntry_Backend(const CAccountingEntry& acentry);
157158
bool ReadAccount(const std::string& strAccount, CAccount& account);
158159
bool WriteAccount(const std::string& strAccount, const CAccount& account);
@@ -165,7 +166,6 @@ class CWalletDB : public CDB
165166
CAmount GetAccountCreditDebit(const std::string& strAccount);
166167
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
167168

168-
DBErrors ReorderTransactions(CWallet* pwallet);
169169
DBErrors LoadWallet(CWallet* pwallet);
170170
DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
171171
DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
@@ -180,7 +180,6 @@ class CWalletDB : public CDB
180180
CWalletDB(const CWalletDB&);
181181
void operator=(const CWalletDB&);
182182

183-
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
184183
};
185184

186185
void ThreadFlushWalletDB(const std::string& strFile);

0 commit comments

Comments
 (0)