Skip to content

Commit adba609

Browse files
committed
addrman: allow for silent overwriting of inconsistent peers.dat
1 parent fbb2b51 commit adba609

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/addrdb.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ std::optional<bilingual_str> LoadAddrman(const std::vector<bool>& asmap, const A
196196
addrman = std::make_unique<AddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
197197
LogPrintf("Creating peers.dat because the file was not found (%s)\n", path_addr);
198198
DumpPeerAddresses(args, *addrman);
199+
} catch (const DbInconsistentError& e) {
200+
// Addrman has shown a tendency to corrupt itself even with graceful shutdowns on known-good
201+
// hardware. As the user would have to delete and recreate a new database regardless to cope
202+
// with frequent corruption, we are restoring old behaviour that does the same, silently.
203+
//
204+
// TODO: Evaluate cause and fix, revert this change at some point.
205+
addrman = std::make_unique<AddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
206+
LogPrintf("Creating peers.dat because of invalid or corrupt file (%s)\n", e.what());
207+
DumpPeerAddresses(args, *addrman);
199208
} catch (const std::exception& e) {
200209
addrman = nullptr;
201210
return strprintf(_("Invalid or corrupt peers.dat (%s). If you believe this is a bug, please report it to %s. As a workaround, you can move the file (%s) out of the way (rename, move, or delete) to have a new one created on the next start."),

src/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ void AddrManImpl::Unserialize(Stream& s_)
391391

392392
const int check_code{ForceCheckAddrman()};
393393
if (check_code != 0) {
394-
throw std::ios_base::failure(strprintf(
394+
throw DbInconsistentError(strprintf(
395395
"Corrupt data. Consistency check failed with code %s",
396396
check_code));
397397
}

src/addrman.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ class AddrManImpl;
2323
/** Default for -checkaddrman */
2424
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
2525

26+
class DbInconsistentError : public std::exception
27+
{
28+
using std::exception::exception;
29+
const std::string error;
30+
31+
public:
32+
explicit DbInconsistentError(const std::string _error) : error{_error} {}
33+
const char* what() const noexcept override { return error.c_str(); }
34+
};
35+
2636
/** Stochastic address manager
2737
*
2838
* Design goals:

test/functional/feature_addrman.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,8 @@ def run_test(self):
123123
self.log.info("Check that corrupt addrman cannot be read (failed check)")
124124
self.stop_node(0)
125125
write_addrman(peers_dat, bucket_key=0)
126-
self.nodes[0].assert_start_raises_init_error(
127-
expected_msg=init_error("Corrupt data. Consistency check failed with code -16: .*"),
128-
match=ErrorMatch.FULL_REGEX,
129-
)
126+
with self.nodes[0].assert_debug_log(['Creating peers.dat because of invalid or corrupt file']):
127+
self.start_node(0)
130128

131129
self.log.info("Check that missing addrman is recreated")
132130
self.stop_node(0)

0 commit comments

Comments
 (0)