@@ -183,6 +183,15 @@ class ResearcherTally
183183 return iter->second ;
184184 }
185185
186+ // !
187+ // ! \brief Set the index of the first block that the tally tracks research
188+ // ! rewards for.
189+ // !
190+ void SetStartIndex (const CBlockIndex* pindex)
191+ {
192+ m_start_pindex = pindex;
193+ }
194+
186195 // !
187196 // ! \brief Record a block's research reward data in the tally.
188197 // !
@@ -376,6 +385,12 @@ class ResearcherTally
376385 // !
377386 AccrualSnapshotRepository m_snapshots;
378387
388+ // !
389+ // ! \brief Index of the first block that the tally tracks research rewards
390+ // ! for.
391+ // !
392+ const CBlockIndex* m_start_pindex = nullptr ;
393+
379394 // !
380395 // ! \brief Points to the index of the block when snapshot accrual activates
381396 // ! (the block just before protocol enforces snapshot accrual).
@@ -480,6 +495,38 @@ class ResearcherTally
480495 {
481496 assert (m_snapshot_baseline_pindex != nullptr );
482497
498+ // If we're not building the initial baseline at the threshold, reset
499+ // the research accounts and rescan these up to the threshold height.
500+ // We need to make sure that the accounts contain the blocks with the
501+ // last rewards before the baseline:
502+ //
503+ if (pindexBest != m_snapshot_baseline_pindex) {
504+ LogPrintf (" %s: Rebuilding research reward tally..." , __func__);
505+
506+ m_researchers.clear ();
507+
508+ const CBlockIndex* pindex = m_start_pindex;
509+
510+ for (;
511+ pindex && pindex != m_snapshot_baseline_pindex;
512+ pindex = pindex->pnext )
513+ {
514+ if (pindex->nResearchSubsidy <= 0 ) {
515+ continue ;
516+ }
517+
518+ if (const CpidOption cpid = pindex->GetMiningId ().TryCpid ()) {
519+ RecordRewardBlock (*cpid, pindex);
520+ }
521+ }
522+
523+ if (pindex && pindex->nResearchSubsidy > 0 ) {
524+ if (const CpidOption cpid = pindex->GetMiningId ().TryCpid ()) {
525+ RecordRewardBlock (*cpid, pindex);
526+ }
527+ }
528+ }
529+
483530 LogPrintf (" %s: rebuilding from %" PRId64 " to %" PRId64 " ..." ,
484531 __func__,
485532 m_snapshot_baseline_pindex->nHeight ,
@@ -497,22 +544,28 @@ class ResearcherTally
497544
498545 // Scan forward to the chain tip and reapply snapshot accrual for each
499546 // account while writing snapshot files for every superblock along the
500- // way:
547+ // way. Finish rescanning the research accounts :
501548 //
502549 for (const CBlockIndex* pindex = m_snapshot_baseline_pindex->pnext ;
503550 pindex;
504551 pindex = pindex->pnext )
505552 {
506- if (pindex->nIsSuperBlock != 1 ) {
553+ if (pindex->nResearchSubsidy <= 0 && pindex-> nIsSuperBlock != 1 ) {
507554 continue ;
508555 }
509556
510- if (!block.ReadFromDisk (pindex)) {
511- return false ;
557+ if (pindex->nIsSuperBlock == 1 ) {
558+ if (!block.ReadFromDisk (pindex)) {
559+ return false ;
560+ }
561+
562+ if (!ApplySuperblock (block.GetSuperblock (pindex))) {
563+ return false ;
564+ }
512565 }
513566
514- if (! ApplySuperblock (block. GetSuperblock (pindex) )) {
515- return false ;
567+ if (const CpidOption cpid = pindex-> GetMiningId (). TryCpid ( )) {
568+ RecordRewardBlock (*cpid, pindex) ;
516569 }
517570 }
518571
@@ -539,6 +592,7 @@ bool Tally::Initialize(CBlockIndex* pindex)
539592 LogPrintf (" Initializing research reward tally..." );
540593
541594 const int64_t start_time = GetTimeMillis ();
595+ g_researcher_tally.SetStartIndex (pindex);
542596
543597 for (; pindex; pindex = pindex->pnext ) {
544598 if (pindex->nHeight + 1 == GetV11Threshold ()) {
0 commit comments