@@ -172,139 +172,6 @@ static const int64_t ADDRMAN_TEST_WINDOW = 40*60; // 40 minutes
172172 */
173173class CAddrMan
174174{
175- friend class CAddrManTest ;
176- private:
177- // ! critical section to protect the inner data structures
178- mutable RecursiveMutex cs;
179-
180- // ! Serialization versions.
181- enum Format : uint8_t {
182- V0_HISTORICAL = 0 , // !< historic format, before commit e6b343d88
183- V1_DETERMINISTIC = 1 , // !< for pre-asmap files
184- V2_ASMAP = 2 , // !< for files including asmap version
185- V3_BIP155 = 3 , // !< same as V2_ASMAP plus addresses are in BIP155 format
186- };
187-
188- // ! The maximum format this software knows it can unserialize. Also, we always serialize
189- // ! in this format.
190- // ! The format (first byte in the serialized stream) can be higher than this and
191- // ! still this software may be able to unserialize the file - if the second byte
192- // ! (see `lowest_compatible` in `Unserialize()`) is less or equal to this.
193- static constexpr Format FILE_FORMAT = Format::V3_BIP155;
194-
195- // ! The initial value of a field that is incremented every time an incompatible format
196- // ! change is made (such that old software versions would not be able to parse and
197- // ! understand the new file format). This is 32 because we overtook the "key size"
198- // ! field which was 32 historically.
199- // ! @note Don't increment this. Increment `lowest_compatible` in `Serialize()` instead.
200- static constexpr uint8_t INCOMPATIBILITY_BASE = 32 ;
201-
202- // ! last used nId
203- int nIdCount GUARDED_BY (cs);
204-
205- // ! table with information about all nIds
206- std::map<int , CAddrInfo> mapInfo GUARDED_BY (cs);
207-
208- // ! find an nId based on its network address
209- std::map<CNetAddr, int > mapAddr GUARDED_BY (cs);
210-
211- // ! randomly-ordered vector of all nIds
212- std::vector<int > vRandom GUARDED_BY (cs);
213-
214- // number of "tried" entries
215- int nTried GUARDED_BY (cs);
216-
217- // ! list of "tried" buckets
218- int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
219-
220- // ! number of (unique) "new" entries
221- int nNew GUARDED_BY (cs);
222-
223- // ! list of "new" buckets
224- int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
225-
226- // ! last time Good was called (memory only)
227- int64_t nLastGood GUARDED_BY (cs);
228-
229- // ! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
230- std::set<int > m_tried_collisions;
231-
232- protected:
233- // ! secret key to randomize bucket select with
234- uint256 nKey;
235-
236- // ! Source of random numbers for randomization in inner loops
237- FastRandomContext insecure_rand;
238-
239- private:
240- // ! Find an entry.
241- CAddrInfo* Find (const CNetAddr& addr, int *pnId = nullptr ) EXCLUSIVE_LOCKS_REQUIRED(cs);
242-
243- // ! find an entry, creating it if necessary.
244- // ! nTime and nServices of the found node are updated, if necessary.
245- CAddrInfo* Create (const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr ) EXCLUSIVE_LOCKS_REQUIRED(cs);
246-
247- // ! Swap two elements in vRandom.
248- void SwapRandom (unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
249-
250- // ! Move an entry from the "new" table(s) to the "tried" table
251- void MakeTried (CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
252-
253- // ! Delete an entry. It must not be in tried, and have refcount 0.
254- void Delete (int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
255-
256- // ! Clear a position in a "new" table. This is the only place where entries are actually deleted.
257- void ClearNew (int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
258-
259- // ! Mark an entry "good", possibly moving it from "new" to "tried".
260- void Good_ (const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
261-
262- // ! Add an entry to the "new" table.
263- bool Add_ (const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
264-
265- // ! Mark an entry as attempted to connect.
266- void Attempt_ (const CService &addr, bool fCountFailure , int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
267-
268- // ! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
269- CAddrInfo Select_ (bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs);
270-
271- // ! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
272- void ResolveCollisions_ () EXCLUSIVE_LOCKS_REQUIRED(cs);
273-
274- // ! Return a random to-be-evicted tried table address.
275- CAddrInfo SelectTriedCollision_ () EXCLUSIVE_LOCKS_REQUIRED(cs);
276-
277- #ifdef DEBUG_ADDRMAN
278- // ! Perform consistency check. Returns an error code or zero.
279- int Check_ () EXCLUSIVE_LOCKS_REQUIRED(cs);
280- #endif
281-
282- /* *
283- * Return all or many randomly selected addresses, optionally by network.
284- *
285- * @param[out] vAddr Vector of randomly selected addresses from vRandom.
286- * @param[in] max_addresses Maximum number of addresses to return (0 = all).
287- * @param[in] max_pct Maximum percentage of addresses to return (0 = all).
288- * @param[in] network Select only addresses of this network (nullopt = all).
289- */
290- void GetAddr_ (std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);
291-
292- /* * We have successfully connected to this peer. Calling this function
293- * updates the CAddress's nTime, which is used in our IsTerrible()
294- * decisions and gossiped to peers. Callers should be careful that updating
295- * this information doesn't leak topology information to network spies.
296- *
297- * net_processing calls this function when it *disconnects* from a peer to
298- * not leak information about currently connected peers.
299- *
300- * @param[in] addr The address of the peer we were connected to
301- * @param[in] nTime The time that we were last connected to this peer
302- */
303- void Connected_ (const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
304-
305- // ! Update an entry's service bits.
306- void SetServices_ (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
307-
308175public:
309176 // Compressed IP->ASN mapping, loaded from a file when a node starts.
310177 // Should be always empty if no file was provided.
@@ -325,7 +192,6 @@ friend class CAddrManTest;
325192 // Read asmap from provided binary file
326193 static std::vector<bool > DecodeAsmap (fs::path path);
327194
328-
329195 /* *
330196 * Serialized format.
331197 * * format version byte (@see `Format`)
@@ -759,6 +625,138 @@ friend class CAddrManTest;
759625 Check ();
760626 }
761627
628+ protected:
629+ // ! secret key to randomize bucket select with
630+ uint256 nKey;
631+
632+ // ! Source of random numbers for randomization in inner loops
633+ FastRandomContext insecure_rand;
634+
635+ private:
636+ // ! critical section to protect the inner data structures
637+ mutable RecursiveMutex cs;
638+
639+ // ! Serialization versions.
640+ enum Format : uint8_t {
641+ V0_HISTORICAL = 0 , // !< historic format, before commit e6b343d88
642+ V1_DETERMINISTIC = 1 , // !< for pre-asmap files
643+ V2_ASMAP = 2 , // !< for files including asmap version
644+ V3_BIP155 = 3 , // !< same as V2_ASMAP plus addresses are in BIP155 format
645+ };
646+
647+ // ! The maximum format this software knows it can unserialize. Also, we always serialize
648+ // ! in this format.
649+ // ! The format (first byte in the serialized stream) can be higher than this and
650+ // ! still this software may be able to unserialize the file - if the second byte
651+ // ! (see `lowest_compatible` in `Unserialize()`) is less or equal to this.
652+ static constexpr Format FILE_FORMAT = Format::V3_BIP155;
653+
654+ // ! The initial value of a field that is incremented every time an incompatible format
655+ // ! change is made (such that old software versions would not be able to parse and
656+ // ! understand the new file format). This is 32 because we overtook the "key size"
657+ // ! field which was 32 historically.
658+ // ! @note Don't increment this. Increment `lowest_compatible` in `Serialize()` instead.
659+ static constexpr uint8_t INCOMPATIBILITY_BASE = 32 ;
660+
661+ // ! last used nId
662+ int nIdCount GUARDED_BY (cs);
663+
664+ // ! table with information about all nIds
665+ std::map<int , CAddrInfo> mapInfo GUARDED_BY (cs);
666+
667+ // ! find an nId based on its network address
668+ std::map<CNetAddr, int > mapAddr GUARDED_BY (cs);
669+
670+ // ! randomly-ordered vector of all nIds
671+ std::vector<int > vRandom GUARDED_BY (cs);
672+
673+ // number of "tried" entries
674+ int nTried GUARDED_BY (cs);
675+
676+ // ! list of "tried" buckets
677+ int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
678+
679+ // ! number of (unique) "new" entries
680+ int nNew GUARDED_BY (cs);
681+
682+ // ! list of "new" buckets
683+ int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
684+
685+ // ! last time Good was called (memory only)
686+ int64_t nLastGood GUARDED_BY (cs);
687+
688+ // ! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
689+ std::set<int > m_tried_collisions;
690+
691+ // ! Find an entry.
692+ CAddrInfo* Find (const CNetAddr& addr, int *pnId = nullptr ) EXCLUSIVE_LOCKS_REQUIRED(cs);
693+
694+ // ! find an entry, creating it if necessary.
695+ // ! nTime and nServices of the found node are updated, if necessary.
696+ CAddrInfo* Create (const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr ) EXCLUSIVE_LOCKS_REQUIRED(cs);
697+
698+ // ! Swap two elements in vRandom.
699+ void SwapRandom (unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
700+
701+ // ! Move an entry from the "new" table(s) to the "tried" table
702+ void MakeTried (CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
703+
704+ // ! Delete an entry. It must not be in tried, and have refcount 0.
705+ void Delete (int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
706+
707+ // ! Clear a position in a "new" table. This is the only place where entries are actually deleted.
708+ void ClearNew (int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
709+
710+ // ! Mark an entry "good", possibly moving it from "new" to "tried".
711+ void Good_ (const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
712+
713+ // ! Add an entry to the "new" table.
714+ bool Add_ (const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
715+
716+ // ! Mark an entry as attempted to connect.
717+ void Attempt_ (const CService &addr, bool fCountFailure , int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
718+
719+ // ! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
720+ CAddrInfo Select_ (bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs);
721+
722+ // ! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
723+ void ResolveCollisions_ () EXCLUSIVE_LOCKS_REQUIRED(cs);
724+
725+ // ! Return a random to-be-evicted tried table address.
726+ CAddrInfo SelectTriedCollision_ () EXCLUSIVE_LOCKS_REQUIRED(cs);
727+
728+ #ifdef DEBUG_ADDRMAN
729+ // ! Perform consistency check. Returns an error code or zero.
730+ int Check_ () EXCLUSIVE_LOCKS_REQUIRED(cs);
731+ #endif
732+
733+ /* *
734+ * Return all or many randomly selected addresses, optionally by network.
735+ *
736+ * @param[out] vAddr Vector of randomly selected addresses from vRandom.
737+ * @param[in] max_addresses Maximum number of addresses to return (0 = all).
738+ * @param[in] max_pct Maximum percentage of addresses to return (0 = all).
739+ * @param[in] network Select only addresses of this network (nullopt = all).
740+ */
741+ void GetAddr_ (std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);
742+
743+ /* * We have successfully connected to this peer. Calling this function
744+ * updates the CAddress's nTime, which is used in our IsTerrible()
745+ * decisions and gossiped to peers. Callers should be careful that updating
746+ * this information doesn't leak topology information to network spies.
747+ *
748+ * net_processing calls this function when it *disconnects* from a peer to
749+ * not leak information about currently connected peers.
750+ *
751+ * @param[in] addr The address of the peer we were connected to
752+ * @param[in] nTime The time that we were last connected to this peer
753+ */
754+ void Connected_ (const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
755+
756+ // ! Update an entry's service bits.
757+ void SetServices_ (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
758+
759+ friend class CAddrManTest ;
762760};
763761
764762#endif // BITCOIN_ADDRMAN_H
0 commit comments