| 899 | } |
| 900 | |
| 901 | DBErrors CWallet::ReorderTransactions() |
| 902 | { |
| 903 | LOCK(cs_wallet); |
| 904 | WalletBatch batch(GetDatabase()); |
| 905 | |
| 906 | // Old wallets didn't have any defined order for transactions |
| 907 | // Probably a bad idea to change the output of this |
| 908 | |
| 909 | // First: get all CWalletTx into a sorted-by-time multimap. |
| 910 | typedef std::multimap<int64_t, CWalletTx*> TxItems; |
| 911 | TxItems txByTime; |
| 912 | |
| 913 | for (auto& entry : mapWallet) |
| 914 | { |
| 915 | CWalletTx* wtx = &entry.second; |
| 916 | txByTime.insert(std::make_pair(wtx->nTimeReceived, wtx)); |
| 917 | } |
| 918 | |
| 919 | nOrderPosNext = 0; |
| 920 | std::vector<int64_t> nOrderPosOffsets; |
| 921 | for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) |
| 922 | { |
| 923 | CWalletTx *const pwtx = (*it).second; |
| 924 | int64_t& nOrderPos = pwtx->nOrderPos; |
| 925 | |
| 926 | if (nOrderPos == -1) |
| 927 | { |
| 928 | nOrderPos = nOrderPosNext++; |
| 929 | nOrderPosOffsets.push_back(nOrderPos); |
| 930 | |
| 931 | if (!batch.WriteTx(*pwtx)) |
| 932 | return DBErrors::LOAD_FAIL; |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | int64_t nOrderPosOff = 0; |
| 937 | for (const int64_t& nOffsetStart : nOrderPosOffsets) |
| 938 | { |
| 939 | if (nOrderPos >= nOffsetStart) |
| 940 | ++nOrderPosOff; |
| 941 | } |
| 942 | nOrderPos += nOrderPosOff; |
| 943 | nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1); |
| 944 | |
| 945 | if (!nOrderPosOff) |
| 946 | continue; |
| 947 | |
| 948 | // Since we're changing the order, write it back |
| 949 | if (!batch.WriteTx(*pwtx)) |
| 950 | return DBErrors::LOAD_FAIL; |
| 951 | } |
| 952 | } |
| 953 | batch.WriteOrderPosNext(nOrderPosNext); |
| 954 | |
| 955 | return DBErrors::LOAD_OK; |
| 956 | } |
| 957 | |
| 958 | int64_t CWallet::IncOrderPosNext(WalletBatch* batch) |
no test coverage detected