Skip to content

Commit b720415

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#8828: Move CWalletDB::ReorderTransactions to CWallet
86029e7 Move CWalletDB::ReorderTransactions to CWallet (Patrick Strateman)
1 parent 0b93a58 commit b720415

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
@@ -868,8 +868,79 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
868868

869869
DBErrors CWallet::ReorderTransactions()
870870
{
871+
LOCK(cs_wallet);
871872
CWalletDB walletdb(strWalletFile);
872-
return walletdb.ReorderTransactions(this);
873+
874+
// Old wallets didn't have any defined order for transactions
875+
// Probably a bad idea to change the output of this
876+
877+
// First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
878+
typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
879+
typedef multimap<int64_t, TxPair > TxItems;
880+
TxItems txByTime;
881+
882+
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
883+
{
884+
CWalletTx* wtx = &((*it).second);
885+
txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
886+
}
887+
list<CAccountingEntry> acentries;
888+
walletdb.ListAccountCreditDebit("", acentries);
889+
BOOST_FOREACH(CAccountingEntry& entry, acentries)
890+
{
891+
txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
892+
}
893+
894+
nOrderPosNext = 0;
895+
std::vector<int64_t> nOrderPosOffsets;
896+
for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
897+
{
898+
CWalletTx *const pwtx = (*it).second.first;
899+
CAccountingEntry *const pacentry = (*it).second.second;
900+
int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
901+
902+
if (nOrderPos == -1)
903+
{
904+
nOrderPos = nOrderPosNext++;
905+
nOrderPosOffsets.push_back(nOrderPos);
906+
907+
if (pwtx)
908+
{
909+
if (!walletdb.WriteTx(*pwtx))
910+
return DB_LOAD_FAIL;
911+
}
912+
else
913+
if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
914+
return DB_LOAD_FAIL;
915+
}
916+
else
917+
{
918+
int64_t nOrderPosOff = 0;
919+
BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
920+
{
921+
if (nOrderPos >= nOffsetStart)
922+
++nOrderPosOff;
923+
}
924+
nOrderPos += nOrderPosOff;
925+
nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
926+
927+
if (!nOrderPosOff)
928+
continue;
929+
930+
// Since we're changing the order, write it back
931+
if (pwtx)
932+
{
933+
if (!walletdb.WriteTx(*pwtx))
934+
return DB_LOAD_FAIL;
935+
}
936+
else
937+
if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
938+
return DB_LOAD_FAIL;
939+
}
940+
}
941+
walletdb.WriteOrderPosNext(nOrderPosNext);
942+
943+
return DB_LOAD_OK;
873944
}
874945

875946
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;
@@ -744,7 +668,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
744668
WriteVersion(CLIENT_VERSION);
745669

746670
if (wss.fAnyUnordered)
747-
result = ReorderTransactions(pwallet);
671+
result = pwallet->ReorderTransactions();
748672

749673
pwallet->laccentries.clear();
750674
ListAccountCreditDebit("*", pwallet->laccentries);

src/wallet/walletdb.h

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

115115
/// This writes directly to the database, and will not update the CWallet's cached accounting entries!
116116
/// Use wallet.AddAccountingEntry instead, to write *and* update its caches.
117+
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
117118
bool WriteAccountingEntry_Backend(const CAccountingEntry& acentry);
118119
bool ReadAccount(const std::string& strAccount, CAccount& account);
119120
bool WriteAccount(const std::string& strAccount, const CAccount& account);
@@ -126,7 +127,6 @@ class CWalletDB : public CDB
126127
CAmount GetAccountCreditDebit(const std::string& strAccount);
127128
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
128129

129-
DBErrors ReorderTransactions(CWallet* pwallet);
130130
DBErrors LoadWallet(CWallet* pwallet);
131131
DBErrors FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
132132
DBErrors ZapWalletTx(CWallet* pwallet, std::vector<CWalletTx>& vWtx);
@@ -143,7 +143,6 @@ class CWalletDB : public CDB
143143
CWalletDB(const CWalletDB&);
144144
void operator=(const CWalletDB&);
145145

146-
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
147146
};
148147

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

0 commit comments

Comments
 (0)