Skip to content

Commit fab0b55

Browse files
author
MarcoFalke
committed
addrman: Fix format string in deserialize error
Also add a regression test.
1 parent facce4c commit fab0b55

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

src/addrman.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ void CAddrMan::Unserialize(Stream& s_)
242242
const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
243243
if (lowest_compatible > FILE_FORMAT) {
244244
throw std::ios_base::failure(strprintf(
245-
"Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
246-
"but the maximum supported by this version of %s is %u.",
247-
format, lowest_compatible, PACKAGE_NAME, static_cast<uint8_t>(FILE_FORMAT)));
245+
"Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
246+
"but the maximum supported by this version of %s is %u.",
247+
uint8_t{format}, uint8_t{lowest_compatible}, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
248248
}
249249

250250
s >> nKey;

test/functional/feature_addrman.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test addrman functionality"""
6+
7+
import os
8+
import struct
9+
10+
from test_framework.messages import ser_uint256, hash256
11+
from test_framework.p2p import MAGIC_BYTES
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import assert_equal
14+
15+
16+
def serialize_addrman(*, format=1, lowest_compatible=3):
17+
new = []
18+
tried = []
19+
INCOMPATIBILITY_BASE = 32
20+
r = MAGIC_BYTES["regtest"]
21+
r += struct.pack("B", format)
22+
r += struct.pack("B", INCOMPATIBILITY_BASE + lowest_compatible)
23+
r += ser_uint256(1)
24+
r += struct.pack("i", len(new))
25+
r += struct.pack("i", len(tried))
26+
ADDRMAN_NEW_BUCKET_COUNT = 1 << 10
27+
r += struct.pack("i", ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30))
28+
for _ in range(ADDRMAN_NEW_BUCKET_COUNT):
29+
r += struct.pack("i", 0)
30+
checksum = hash256(r)
31+
r += checksum
32+
return r
33+
34+
35+
def write_addrman(peers_dat, **kwargs):
36+
with open(peers_dat, "wb") as f:
37+
f.write(serialize_addrman(**kwargs))
38+
39+
40+
class AddrmanTest(BitcoinTestFramework):
41+
def set_test_params(self):
42+
self.num_nodes = 1
43+
44+
def run_test(self):
45+
peers_dat = os.path.join(self.nodes[0].datadir, self.chain, "peers.dat")
46+
47+
self.log.info("Check that mocked addrman is valid")
48+
self.stop_node(0)
49+
write_addrman(peers_dat)
50+
with self.nodes[0].assert_debug_log(["Loaded 0 addresses from peers.dat"]):
51+
self.start_node(0, extra_args=["-checkaddrman=1"])
52+
assert_equal(self.nodes[0].getnodeaddresses(), [])
53+
54+
self.log.info("Check that addrman from future cannot be read")
55+
self.stop_node(0)
56+
write_addrman(peers_dat, lowest_compatible=111)
57+
with self.nodes[0].assert_debug_log([
58+
f'ERROR: DeserializeDB: Deserialize or I/O error - Unsupported format of addrman database: 1. It is compatible with formats >=111, but the maximum supported by this version of {self.config["environment"]["PACKAGE_NAME"]} is 3.',
59+
"Recreating peers.dat",
60+
]):
61+
self.start_node(0)
62+
assert_equal(self.nodes[0].getnodeaddresses(), [])
63+
64+
self.log.info("Check that corrupt addrman cannot be read")
65+
self.stop_node(0)
66+
with open(peers_dat, "wb") as f:
67+
f.write(serialize_addrman()[:-1])
68+
with self.nodes[0].assert_debug_log([
69+
"ERROR: DeserializeDB: Deserialize or I/O error - CAutoFile::read: end of file",
70+
"Recreating peers.dat",
71+
]):
72+
self.start_node(0)
73+
assert_equal(self.nodes[0].getnodeaddresses(), [])
74+
75+
self.log.info("Check that missing addrman is recreated")
76+
self.stop_node(0)
77+
os.remove(peers_dat)
78+
with self.nodes[0].assert_debug_log([
79+
f"Missing or invalid file {peers_dat}",
80+
"Recreating peers.dat",
81+
]):
82+
self.start_node(0)
83+
assert_equal(self.nodes[0].getnodeaddresses(), [])
84+
85+
86+
if __name__ == "__main__":
87+
AddrmanTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
'p2p_blockfilters.py',
283283
'p2p_message_capture.py',
284284
'feature_includeconf.py',
285+
'feature_addrman.py',
285286
'feature_asmap.py',
286287
'mempool_unbroadcast.py',
287288
'mempool_compatibility.py',

0 commit comments

Comments
 (0)