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