|
| 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() |
0 commit comments