MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / Serialize

Method Serialize

src/addrman.cpp:114–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

112
113template <typename Stream>
114void AddrManImpl::Serialize(Stream& s_) const
115{
116 LOCK(cs);
117
118 /**
119 * Serialized format.
120 * * format version byte (@see `Format`)
121 * * lowest compatible format version byte. This is used to help old software decide
122 * whether to parse the file. For example:
123 * * Bitcoin Core version N knows how to parse up to format=3. If a new format=4 is
124 * introduced in version N+1 that is compatible with format=3 and it is known that
125 * version N will be able to parse it, then version N+1 will write
126 * (format=4, lowest_compatible=3) in the first two bytes of the file, and so
127 * version N will still try to parse it.
128 * * Bitcoin Core version N+2 introduces a new incompatible format=5. It will write
129 * (format=5, lowest_compatible=5) and so any versions that do not know how to parse
130 * format=5 will not try to read the file.
131 * * nKey
132 * * nNew
133 * * nTried
134 * * number of "new" buckets XOR 2**30
135 * * all new addresses (total count: nNew)
136 * * all tried addresses (total count: nTried)
137 * * for each new bucket:
138 * * number of elements
139 * * for each element: index in the serialized "all new addresses"
140 * * asmap version
141 *
142 * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
143 * as incompatible. This is necessary because it did not check the version number on
144 * deserialization.
145 *
146 * vvNew, vvTried, mapInfo, mapAddr and vRandom are never encoded explicitly;
147 * they are instead reconstructed from the other information.
148 *
149 * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
150 * changes to the ADDRMAN_ parameters without breaking the on-disk structure.
151 *
152 * We don't use SERIALIZE_METHODS since the serialization and deserialization code has
153 * very little in common.
154 */
155
156 // Always serialize in the latest version (FILE_FORMAT).
157 ParamsStream s{s_, CAddress::V2_DISK};
158
159 s << static_cast<uint8_t>(FILE_FORMAT);
160
161 // Increment `lowest_compatible` iff a newly introduced format is incompatible with
162 // the previous one.
163 static constexpr uint8_t lowest_compatible = Format::V4_MULTIPORT;
164 s << static_cast<uint8_t>(INCOMPATIBILITY_BASE + lowest_compatible);
165
166 s << nKey;
167 s << nNew;
168 s << nTried;
169
170 int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
171 s << nUBuckets;

Callers 1

SerializeBlockUndoFunction · 0.45

Calls 1

GetAsmapVersionMethod · 0.80

Tested by

no test coverage detected