| 781 | } |
| 782 | |
| 783 | DBErrors CWallet::ReorderTransactions() |
| 784 | { |
| 785 | LOCK(cs_wallet); |
| 786 | WalletBatch batch(GetDatabase()); |
| 787 | |
| 788 | // Old wallets didn't have any defined order for transactions |
| 789 | // Probably a bad idea to change the output of this |
| 790 | |
| 791 | // First: get all CWalletTx into a sorted-by-time multimap. |
| 792 | typedef std::multimap<int64_t, CWalletTx*> TxItems; |
| 793 | TxItems txByTime; |
| 794 | |
| 795 | for (auto& entry : mapWallet) |
| 796 | { |
| 797 | CWalletTx* wtx = &entry.second; |
| 798 | txByTime.insert(std::make_pair(wtx->nTimeReceived, wtx)); |
| 799 | } |
| 800 | |
| 801 | nOrderPosNext = 0; |
| 802 | std::vector<int64_t> nOrderPosOffsets; |
| 803 | for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) |
| 804 | { |
| 805 | CWalletTx *const pwtx = (*it).second; |
| 806 | int64_t& nOrderPos = pwtx->nOrderPos; |
| 807 | |
| 808 | if (nOrderPos == -1) |
| 809 | { |
| 810 | nOrderPos = nOrderPosNext++; |
| 811 | nOrderPosOffsets.push_back(nOrderPos); |
| 812 | |
| 813 | if (!batch.WriteTx(*pwtx)) |
| 814 | return DBErrors::LOAD_FAIL; |
| 815 | } |
| 816 | else |
| 817 | { |
| 818 | int64_t nOrderPosOff = 0; |
| 819 | for (const int64_t& nOffsetStart : nOrderPosOffsets) |
| 820 | { |
| 821 | if (nOrderPos >= nOffsetStart) |
| 822 | ++nOrderPosOff; |
| 823 | } |
| 824 | nOrderPos += nOrderPosOff; |
| 825 | nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1); |
| 826 | |
| 827 | if (!nOrderPosOff) |
| 828 | continue; |
| 829 | |
| 830 | // Since we're changing the order, write it back |
| 831 | if (!batch.WriteTx(*pwtx)) |
| 832 | return DBErrors::LOAD_FAIL; |
| 833 | } |
| 834 | } |
| 835 | batch.WriteOrderPosNext(nOrderPosNext); |
| 836 | |
| 837 | return DBErrors::LOAD_OK; |
| 838 | } |
| 839 | |
| 840 | int64_t CWallet::IncOrderPosNext(WalletBatch* batch) |
no test coverage detected