| 104 | } |
| 105 | |
| 106 | bool TxIndex::DB::MigrateData(CBlockTreeDB& block_tree_db, const CBlockLocator& best_locator) |
| 107 | { |
| 108 | // The prior implementation of txindex was always in sync with block index |
| 109 | // and presence was indicated with a boolean DB flag. If the flag is set, |
| 110 | // this means the txindex from a previous version is valid and in sync with |
| 111 | // the chain tip. The first step of the migration is to unset the flag and |
| 112 | // write the chain hash to a separate key, DB_TXINDEX_BLOCK. After that, the |
| 113 | // index entries are copied over in batches to the new database. Finally, |
| 114 | // DB_TXINDEX_BLOCK is erased from the old database and the block hash is |
| 115 | // written to the new database. |
| 116 | // |
| 117 | // Unsetting the boolean flag ensures that if the node is downgraded to a |
| 118 | // previous version, it will not see a corrupted, partially migrated index |
| 119 | // -- it will see that the txindex is disabled. When the node is upgraded |
| 120 | // again, the migration will pick up where it left off and sync to the block |
| 121 | // with hash DB_TXINDEX_BLOCK. |
| 122 | bool f_legacy_flag = false; |
| 123 | block_tree_db.ReadFlag("txindex", f_legacy_flag); |
| 124 | if (f_legacy_flag) { |
| 125 | if (!block_tree_db.Write(DB_TXINDEX_BLOCK, best_locator)) { |
| 126 | return error("%s: cannot write block indicator", __func__); |
| 127 | } |
| 128 | if (!block_tree_db.WriteFlag("txindex", false)) { |
| 129 | return error("%s: cannot write block index db flag", __func__); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | CBlockLocator locator; |
| 134 | if (!block_tree_db.Read(DB_TXINDEX_BLOCK, locator)) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | int64_t count = 0; |
| 139 | LogPrintf("Upgrading txindex database... [0%%]\n"); |
| 140 | uiInterface.ShowProgress(_("Upgrading txindex database"), 0, true); |
| 141 | int report_done = 0; |
| 142 | const size_t batch_size = 1 << 24; // 16 MiB |
| 143 | |
| 144 | CDBBatch batch_newdb(*this); |
| 145 | CDBBatch batch_olddb(block_tree_db); |
| 146 | |
| 147 | std::pair<unsigned char, uint256> key; |
| 148 | std::pair<unsigned char, uint256> begin_key{DB_TXINDEX, uint256()}; |
| 149 | std::pair<unsigned char, uint256> prev_key = begin_key; |
| 150 | |
| 151 | bool interrupted = false; |
| 152 | std::unique_ptr<CDBIterator> cursor(block_tree_db.NewIterator()); |
| 153 | for (cursor->Seek(begin_key); cursor->Valid(); cursor->Next()) { |
| 154 | boost::this_thread::interruption_point(); |
| 155 | if (ShutdownRequested()) { |
| 156 | interrupted = true; |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | if (!cursor->GetKey(key)) { |
| 161 | return error("%s: cannot get key from valid cursor", __func__); |
| 162 | } |
| 163 | if (key.first != DB_TXINDEX) { |
no test coverage detected