@@ -308,8 +308,6 @@ AutoGreylist::AutoGreylist()
308
308
: m_greylist_ptr(std::make_shared<Greylist>())
309
309
, m_superblock_hash(uint256 {})
310
310
{
311
- // m_greylist_ptr = std::make_shared<Greylist>();
312
-
313
311
Refresh ();
314
312
}
315
313
@@ -328,6 +326,21 @@ AutoGreylist::Greylist::size_type AutoGreylist::size() const
328
326
return m_greylist_ptr->size ();
329
327
}
330
328
329
+ bool AutoGreylist::Contains (const std::string& name, const bool & only_auto_greylisted) const
330
+ {
331
+ auto iter = m_greylist_ptr->find (name);
332
+
333
+ if (iter != m_greylist_ptr->end ()) {
334
+ if (only_auto_greylisted) {
335
+ return (only_auto_greylisted && iter->second .m_meets_greylisting_crit );
336
+ } else {
337
+ return true ;
338
+ }
339
+ } else {
340
+ return false ;
341
+ }
342
+ }
343
+
331
344
void AutoGreylist::Refresh ()
332
345
{
333
346
LOCK (cs_main);
@@ -349,15 +362,23 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
349
362
LOCK (lock);
350
363
351
364
// We need the current whitelist, including all records except deleted. This will include greylisted projects,
352
- // whether currently marked as manually greylisted from protocol or overridden to auto greylisted by the auto greylist class.
353
- const WhitelistSnapshot whitelist = GetWhitelist ().Snapshot (GRC::ProjectEntry::ProjectFilterFlag::ALL_BUT_DELETED);
365
+ // whether currently marked as manually greylisted from protocol or overridden to auto greylisted by the auto greylist class,
366
+ // based on the current state of the autogreylist. NOTE that the refresh_greylist is set to false here and MUST be this
367
+ // when called in the AutoGreylist class itself, to avoid an infinite loop.
368
+ const WhitelistSnapshot whitelist = GetWhitelist ().Snapshot (GRC::ProjectEntry::ProjectFilterFlag::ALL_BUT_DELETED, false );
354
369
355
370
m_greylist_ptr->clear ();
356
371
372
+
373
+ // Notice the superblock_ptr_in m_projects_all_cpid_total_credits MUST ALEADY BE POPULATED to record the TC state into
374
+ // the auto greylist.
357
375
for (const auto & iter : whitelist) {
358
- if (auto project = superblock_ptr_in->m_projects .Try (iter.m_name )) {
376
+ auto project = superblock_ptr_in->m_projects_all_cpids_total_credits .m_projects_all_cpid_total_credits .find (iter.m_name );
377
+
378
+ if (project != superblock_ptr_in->m_projects_all_cpids_total_credits .m_projects_all_cpid_total_credits .end ()) {
359
379
// Record new greylist candidate entry baseline with the total credit for each project present in superblock.
360
- m_greylist_ptr->insert (std::make_pair (iter.m_name , GreylistCandidateEntry (iter.m_name , project->m_total_credit )));
380
+ m_greylist_ptr->insert (std::make_pair (iter.m_name ,
381
+ GreylistCandidateEntry (iter.m_name , std::nearbyint (project->second ))));
361
382
} else {
362
383
// Record new greylist candidate entry with nullopt total credit. This is for a project that is in the whitelist,
363
384
// but does not have a project entry in the superblock. This would be because the scrapers could not converge on the
@@ -373,7 +394,6 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
373
394
index_ptr = GRC::BlockFinder::FindByHeight (superblock_ptr_in.m_height - 1 );
374
395
}
375
396
376
- // SuperblockPtr superblock_ptr;
377
397
unsigned int superblock_count = 1 ; // The 0 (baseline) superblock was processed above. Here we start with 1 and go up to 40
378
398
379
399
while (index_ptr != nullptr && index_ptr->pprev != nullptr && superblock_count <= 40 ) {
@@ -400,9 +420,11 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
400
420
// This is guaranteed to succeed, because every whitelisted project was inserted as a new baseline entry above.
401
421
auto greylist_entry = m_greylist_ptr->find (iter.m_name );
402
422
403
- if (auto project = superblock_ptr->m_projects .Try (iter.m_name )) {
423
+ auto project = superblock_ptr->m_projects_all_cpids_total_credits .m_projects_all_cpid_total_credits .find (iter.m_name );
424
+
425
+ if (project != superblock_ptr->m_projects_all_cpids_total_credits .m_projects_all_cpid_total_credits .end ()) {
404
426
// Update greylist candidate entry with the total credit for each project present in superblock.
405
- greylist_entry->second .UpdateGreylistCandidateEntry (project->m_total_credit , superblock_count);
427
+ greylist_entry->second .UpdateGreylistCandidateEntry (project->second , superblock_count);
406
428
} else {
407
429
// Record updated greylist candidate entry with nullopt total credit. This is for a project that is in the whitelist,
408
430
// but does not have a project entry in this superblock. This would be because the scrapers could not converge on the
@@ -415,13 +437,38 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
415
437
index_ptr = index_ptr->pprev ;
416
438
}
417
439
418
- // Purge candidate elements that do not meet auto greylist criteria.
419
- for (auto iter = m_greylist_ptr->begin (); iter != m_greylist_ptr->end (); ) {
440
+ // Mark elements with whether they meet greylist criteria.
441
+ for (auto iter = m_greylist_ptr->begin (); iter != m_greylist_ptr->end (); ++iter ) {
420
442
if (iter->second .GetZCD () < 7 && iter->second .GetWAS () >= Fraction (1 , 10 )) {
421
- // Candidate greylist entry does not meet auto greylist criteria, so remove from greylist entry map.
422
- iter = m_greylist_ptr->erase (iter);
423
- } else {
424
- iter++;
443
+ iter->second .m_meets_greylisting_crit = true ;
444
+ }
445
+ }
446
+ }
447
+
448
+ void AutoGreylist::RefreshWithSuperblock (Superblock& superblock)
449
+ {
450
+ SuperblockPtr superblock_ptr;
451
+
452
+ // For the purposes of forming a superblock as the mining of a new block adding to the head of the chain, we will
453
+ // form a superblock ptr referencing the current head of the chain. The actual superblock will be the next block if it
454
+ // is added to the chain, for for the purposes here, this is what we want, because it is simply used to feed the
455
+ // overloaded version which takes the superblock_ptr and follows the chain backwards to do the greylist calculations.
456
+ superblock_ptr.Replace (superblock);
457
+
458
+ {
459
+ LOCK (cs_main);
460
+
461
+ superblock_ptr.Rebind (pindexBest);
462
+ }
463
+
464
+ RefreshWithSuperblock (superblock_ptr);
465
+
466
+ const WhitelistSnapshot whitelist = GetWhitelist ().Snapshot (GRC::ProjectEntry::ProjectFilterFlag::ALL_BUT_DELETED, false );
467
+
468
+ // Update the superblock object with the project greylist status.
469
+ for (const auto & project : whitelist) {
470
+ if (project.m_status == ProjectEntryStatus::AUTO_GREYLISTED || project.m_status == ProjectEntryStatus::MAN_GREYLISTED) {
471
+ superblock.m_project_status .m_project_status .insert (std::make_pair (project.m_name , project.m_status ));
425
472
}
426
473
}
427
474
}
@@ -438,15 +485,27 @@ std::shared_ptr<AutoGreylist> AutoGreylist::GetAutoGreylistCache()
438
485
// Class: Whitelist (Registry)
439
486
// -----------------------------------------------------------------------------
440
487
441
- WhitelistSnapshot Whitelist::Snapshot (const ProjectEntry::ProjectFilterFlag& filter) const
488
+ WhitelistSnapshot Whitelist::Snapshot (const ProjectEntry::ProjectFilterFlag& filter, const bool & refresh_greylist ) const
442
489
{
443
490
LOCK (cs_lock);
444
491
445
- // AutoGreylist::GetAutoGreylistCache()->Refresh();
492
+ std::shared_ptr<GRC::AutoGreylist> greylist_ptr = GRC::AutoGreylist::GetAutoGreylistCache ();
493
+
494
+ if (refresh_greylist) {
495
+ greylist_ptr->Refresh ();
496
+ }
446
497
447
498
ProjectList projects;
448
499
449
- for (const auto & iter : m_project_entries) {
500
+ // This is the override for automatic greylisting. If the AutoGreylist class refresh determines
501
+ // that the project meets greylisting criteria, it will be in the AutoGreylist object pointed to
502
+ // by the greylist_ptr. This will override the whitelist entries from the project whitelist registry.
503
+ for (auto iter : m_project_entries) {
504
+ if ((iter.second ->m_status == ProjectEntryStatus::ACTIVE || iter.second ->m_status == ProjectEntryStatus::MAN_GREYLISTED)
505
+ && greylist_ptr->Contains (iter.first )) {
506
+ iter.second ->m_status = ProjectEntryStatus::AUTO_GREYLISTED;
507
+ }
508
+
450
509
switch (filter) {
451
510
case ProjectEntry::ProjectFilterFlag::ACTIVE:
452
511
if (iter.second ->m_status == ProjectEntryStatus::ACTIVE) {
0 commit comments