| 460 | |
| 461 | using LoadFunc = std::function<DBErrors(CWallet* pwallet, DataStream& key, DataStream& value, std::string& err)>; |
| 462 | static LoadResult LoadRecords(CWallet* pwallet, DatabaseBatch& batch, const std::string& key, DataStream& prefix, LoadFunc load_func) |
| 463 | { |
| 464 | LoadResult result; |
| 465 | DataStream ssKey; |
| 466 | DataStream ssValue{}; |
| 467 | |
| 468 | Assume(!prefix.empty()); |
| 469 | std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix); |
| 470 | if (!cursor) { |
| 471 | pwallet->WalletLogPrintf("Error getting database cursor for '%s' records\n", key); |
| 472 | result.m_result = DBErrors::CORRUPT; |
| 473 | return result; |
| 474 | } |
| 475 | |
| 476 | while (true) { |
| 477 | DatabaseCursor::Status status = cursor->Next(ssKey, ssValue); |
| 478 | if (status == DatabaseCursor::Status::DONE) { |
| 479 | break; |
| 480 | } else if (status == DatabaseCursor::Status::FAIL) { |
| 481 | pwallet->WalletLogPrintf("Error reading next '%s' record for wallet database\n", key); |
| 482 | result.m_result = DBErrors::CORRUPT; |
| 483 | return result; |
| 484 | } |
| 485 | std::string type; |
| 486 | ssKey >> type; |
| 487 | assert(type == key); |
| 488 | std::string error; |
| 489 | DBErrors record_res = load_func(pwallet, ssKey, ssValue, error); |
| 490 | if (record_res != DBErrors::LOAD_OK) { |
| 491 | pwallet->WalletLogPrintf("%s\n", error); |
| 492 | } |
| 493 | result.m_result = std::max(result.m_result, record_res); |
| 494 | ++result.m_records; |
| 495 | } |
| 496 | return result; |
| 497 | } |
| 498 | |
| 499 | static LoadResult LoadRecords(CWallet* pwallet, DatabaseBatch& batch, const std::string& key, LoadFunc load_func) |
| 500 | { |
no test coverage detected