@@ -44,6 +44,13 @@ fs::path pathScraper = {};
4444
4545extern bool fShutdown ;
4646extern CWallet* pwalletMain;
47+
48+ CCriticalSection cs_Scraper;
49+ CCriticalSection cs_StructScraperFileManifest;
50+ CCriticalSection cs_ConvergedScraperStatsCache;
51+ CCriticalSection cs_TeamIDMap;
52+ CCriticalSection cs_VerifiedBeacons;
53+
4754bool fScraperActive = false ;
4855std::vector<std::pair<std::string, std::string>> vuserpass;
4956std::vector<std::pair<std::string, int64_t >> vprojectteamids;
@@ -84,20 +91,20 @@ struct ScraperFileManifest
8491// Both TeamIDMap and ProjTeamETags are protected by cs_TeamIDMap.
8592// --------------- project -------------team name -- teamID
8693typedef std::map<std::string, std::map<std::string, int64_t >> mTeamIDs ;
87- mTeamIDs TeamIDMap;
94+ mTeamIDs TeamIDMap GUARDED_BY (cs_TeamIDMap) ;
8895
8996// ProjTeamETags is not persisted to disk. There would be little to be gained by doing so. The scrapers are restarted very
9097// rarely, and on restart, this would only save downloading team files for those projects that have one or TeamIDs missing AND
9198// an ETag had NOT changed since the last pull. Not worth the complexity.
9299// --------------- project ---- eTag
93100typedef std::map<std::string, std::string> mProjectTeamETags ;
94- mProjectTeamETags ProjTeamETags;
101+ mProjectTeamETags ProjTeamETags GUARDED_BY (cs_TeamIDMap) ;
95102
96103
97104std::vector<std::string> GetTeamWhiteList ();
98105
99106std::string urlsanity (const std::string& s, const std::string& type);
100- ScraperFileManifest StructScraperFileManifest = {};
107+ ScraperFileManifest StructScraperFileManifest GUARDED_BY (cs_StructScraperFileManifest) = {};
101108
102109// Although scraper_net.h declares these maps, we define them here instead of
103110// in scraper_net.cpp to ensure that the executable destroys these objects in
@@ -109,13 +116,7 @@ std::map<uint256, CSplitBlob::CPart> CSplitBlob::mapParts;
109116std::map<uint256, std::shared_ptr<CScraperManifest>> CScraperManifest::mapManifest;
110117
111118// Global cache for converged scraper stats. Access must be with the lock cs_ConvergedScraperStatsCache taken.
112- ConvergedScraperStats ConvergedScraperStatsCache = {};
113-
114- CCriticalSection cs_Scraper;
115- CCriticalSection cs_StructScraperFileManifest;
116- CCriticalSection cs_ConvergedScraperStatsCache;
117- CCriticalSection cs_TeamIDMap;
118- CCriticalSection cs_VerifiedBeacons;
119+ ConvergedScraperStats ConvergedScraperStatsCache GUARDED_BY (cs_ConvergedScraperStatsCache) = {};
119120
120121void _log (logattribute eType, const std::string& sCall , const std::string& sMessage );
121122
@@ -176,17 +177,17 @@ extern UniValue SuperblockToJson(const Superblock& superblock);
176177namespace {
177178// !
178179// ! \brief Get the block index for the height to consider beacons eligible for
179- // ! rewards.
180+ // ! rewards. A lock must be held on cs_main before calling this function.
180181// !
181182// ! \return Block index for the height of a block about 1 hour below the chain
182183// ! tip.
183184// !
184- const CBlockIndex* GetBeaconConsensusHeight ()
185+ const CBlockIndex* GetBeaconConsensusHeight () EXCLUSIVE_LOCKS_REQUIRED(cs_main)
185186{
186- static BlockFinder block_finder;
187-
188187 AssertLockHeld (cs_main);
189188
189+ static BlockFinder block_finder;
190+
190191 // Use 4 times the BLOCK_GRANULARITY which moves the consensus block every hour.
191192 // TODO: Make the mod a function of SCRAPER_CMANIFEST_RETENTION_TIME in scraper.h.
192193 return block_finder.FindByHeight (
@@ -280,7 +281,7 @@ BeaconConsensus GetConsensusBeaconList()
280281// the only one to survive, which is fine. We only need one.
281282
282283// This map has to be global because the scraper function is reentrant.
283- ScraperVerifiedBeacons g_verified_beacons;
284+ ScraperVerifiedBeacons g_verified_beacons GUARDED_BY (cs_VerifiedBeacons) ;
284285
285286// Use of this global should be protected by a lock on cs_VerifiedBeacons
286287ScraperVerifiedBeacons& GetVerifiedBeacons ()
@@ -1863,7 +1864,7 @@ bool DownloadProjectTeamFiles(const WhitelistSnapshot& projectWhitelist)
18631864
18641865// Note this should be called with a lock held on cs_TeamIDMap, which is intended to protect both
18651866// TeamIDMap and ProjTeamETags.
1866- bool ProcessProjectTeamFile (const std::string& project, const fs::path& file, const std::string& etag)
1867+ bool ProcessProjectTeamFile (const std::string& project, const fs::path& file, const std::string& etag) EXCLUSIVE_LOCKS_REQUIRED(cs_TeamIDMap)
18671868{
18681869 std::map<std::string, int64_t > mTeamIdsForProject ;
18691870
@@ -2448,7 +2449,7 @@ uint256 GetFileHash(const fs::path& inputfile)
24482449
24492450
24502451// Note that cs_StructScraperFileManifest needs to be taken before calling.
2451- uint256 GetmScraperFileManifestHash ()
2452+ uint256 GetmScraperFileManifestHash () EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
24522453{
24532454 uint256 nHash;
24542455 CDataStream ss (SER_NETWORK, 1 );
@@ -2752,7 +2753,7 @@ bool StoreTeamIDList(const fs::path& file)
27522753
27532754
27542755// Insert entry into Manifest. Note that cs_StructScraperFileManifest needs to be taken before calling.
2755- bool InsertScraperFileManifestEntry (ScraperFileManifestEntry& entry)
2756+ bool InsertScraperFileManifestEntry (ScraperFileManifestEntry& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
27562757{
27572758 // This less readable form is so we know whether the element already existed or not.
27582759 std::pair<ScraperFileManifestMap::iterator, bool > ret;
@@ -2773,7 +2774,7 @@ bool InsertScraperFileManifestEntry(ScraperFileManifestEntry& entry)
27732774}
27742775
27752776// Delete entry from Manifest and corresponding file if it exists. Note that cs_StructScraperFileManifest needs to be taken before calling.
2776- unsigned int DeleteScraperFileManifestEntry (ScraperFileManifestEntry& entry)
2777+ unsigned int DeleteScraperFileManifestEntry (ScraperFileManifestEntry& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
27772778{
27782779 unsigned int ret;
27792780
@@ -2800,7 +2801,7 @@ unsigned int DeleteScraperFileManifestEntry(ScraperFileManifestEntry& entry)
28002801// Mark manifest entry non-current. The reason this is encapsulated in a function is
28012802// to ensure the rehash is done. Note that cs_StructScraperFileManifest needs to be
28022803// taken before calling.
2803- bool MarkScraperFileManifestEntryNonCurrent (ScraperFileManifestEntry& entry)
2804+ bool MarkScraperFileManifestEntryNonCurrent (ScraperFileManifestEntry& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
28042805{
28052806 entry.current = false ;
28062807
@@ -4063,7 +4064,7 @@ unsigned int ScraperDeleteUnauthorizedCScraperManifests()
40634064
40644065// A lock needs to be taken on cs_StructScraperFileManifest for this function.
40654066// The sCManifestName is the public key of the scraper in address form.
4066- bool ScraperSendFileManifestContents (CBitcoinAddress& Address, CKey& Key)
4067+ bool ScraperSendFileManifestContents (CBitcoinAddress& Address, CKey& Key) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
40674068{
40684069 // This "broadcasts" the current ScraperFileManifest contents to the network.
40694070
@@ -5303,8 +5304,13 @@ UniValue sendscraperfilemanifest(const UniValue& params, bool fHelp)
53035304 bool ret;
53045305 if (IsScraperAuthorizedToBroadcastManifests (AddressOut, KeyOut))
53055306 {
5307+ LOCK (cs_StructScraperFileManifest);
5308+ _log (logattribute::INFO, " LOCK" , " cs_StructScraperFileManifest" );
5309+
53065310 ret = ScraperSendFileManifestContents (AddressOut, KeyOut);
53075311 uiInterface.NotifyScraperEvent (scrapereventtypes::Manifest, CT_NEW, {});
5312+
5313+ _log (logattribute::INFO, " ENDLOCK" , " cs_StructScraperFileManifest" );
53085314 }
53095315 else
53105316 ret = false ;
@@ -5525,6 +5531,8 @@ UniValue testnewsb(const UniValue& params, bool fHelp)
55255531
55265532 if (params.size () == 1 ) nReducedCacheBits = params[0 ].get_int ();
55275533
5534+ UniValue res (UniValue::VOBJ);
5535+
55285536 {
55295537 LOCK (cs_ConvergedScraperStatsCache);
55305538
@@ -5535,12 +5543,10 @@ UniValue testnewsb(const UniValue& params, bool fHelp)
55355543
55365544 return error;
55375545 }
5538- }
55395546
5540- UniValue res (UniValue::VOBJ);
5541-
5542- _log (logattribute::INFO, " testnewsb" , " Size of the PastConvergences map = " + ToString (ConvergedScraperStatsCache.PastConvergences .size ()));
5543- res.pushKV (" Size of the PastConvergences map" , ToString (ConvergedScraperStatsCache.PastConvergences .size ()));
5547+ _log (logattribute::INFO, " testnewsb" , " Size of the PastConvergences map = " + ToString (ConvergedScraperStatsCache.PastConvergences .size ()));
5548+ res.pushKV (" Size of the PastConvergences map" , ToString (ConvergedScraperStatsCache.PastConvergences .size ()));
5549+ }
55445550
55455551 // Contract binary pack/unpack check...
55465552 _log (logattribute::INFO, " testnewsb" , " Checking compatibility with binary SB pack/unpack by packing then unpacking, then comparing to the original" );
0 commit comments