@@ -113,7 +113,6 @@ public void start() {
113113 setLatestMilestone (latestSnapshot .getHash (), latestSnapshot .getIndex ());
114114 logChange (snapshotProvider .getInitialSnapshot ().getIndex ());
115115
116- syncProgressInfo .setSyncMilestoneStartIndex (snapshotProvider .getInitialSnapshot ().getIndex ());
117116 milestoneSolidifier .start ();
118117
119118 } catch (Exception e ) {
@@ -222,6 +221,8 @@ private void processSolidifyQueue() throws Exception {
222221 int lowest = oldestMilestoneInQueue == null ? -1 : oldestMilestoneInQueue .getValue ();
223222 scanMilestonesInQueue ();
224223 if (oldestMilestoneInQueue != null && lowest > oldestMilestoneInQueue .getValue ()) {
224+ // Going down or going up doesnt matter to the calculation
225+ syncProgressInfo .addMilestoneApplicationTime ();
225226 logChange (-1 );
226227 }
227228 }
@@ -477,21 +478,22 @@ private void logChange(int prevSolidMilestoneIndex) {
477478 }
478479
479480 // only print more sophisticated progress if we are coming from a more unsynced state
480- if (oldestMilestoneInQueue == null ||
481- (prevSolidMilestoneIndex != -1 && getLatestMilestoneIndex () - nextLatestSolidMilestone < 1 )) {
481+ if (prevSolidMilestoneIndex != -1 && getLatestMilestoneIndex () - nextLatestSolidMilestone < 1 ) {
482482 syncProgressInfo .setSyncMilestoneStartIndex (nextLatestSolidMilestone );
483483 syncProgressInfo .resetMilestoneApplicationTimes ();
484484 return ;
485485 }
486486
487487 int estSecondsToBeSynced = syncProgressInfo .computeEstimatedTimeToSyncUpSeconds (getLatestMilestoneIndex (),
488488 nextLatestSolidMilestone );
489- StringBuilder progressSB = new StringBuilder ();
490-
491- double percentPreferDown = 95 ;
492- double percentageSynced = ((100d - oldestMilestoneInQueue .getValue () / latestMilestoneIndex .doubleValue () / 0.01d ) / 100d * percentPreferDown )
493- + ((latestSolidMilestone .doubleValue () / latestMilestoneIndex .doubleValue () / 0.01d ) / 100d * (100d - percentPreferDown ));
489+
490+ // oldestMilestoneInQueue can be null if they are processed faster than coming in.
491+ // Unlikely to happen on mainnet though.
492+ double percentageSynced = syncProgressInfo .calculatePercentageSynced (getLatestMilestoneIndex (),
493+ getLatestSolidMilestoneIndex (),
494+ oldestMilestoneInQueue == null ? getLatestSolidMilestoneIndex () : oldestMilestoneInQueue .getValue ());
494495
496+ StringBuilder progressSB = new StringBuilder ();
495497 // add progress bar
496498 progressSB .append (ASCIIProgressBar .getProgressBarString (0 , 100 , (int )Math .round (percentageSynced )));
497499 // add lsm to lm
@@ -509,6 +511,9 @@ private void logChange(int prevSolidMilestoneIndex) {
509511 * Holds variables containing information needed for sync progress calculation.
510512 */
511513 private static class SyncProgressInfo {
514+
515+ static final double PERCENT_WEIGHT_DOWN = 95 ;
516+
512517 /**
513518 * The actual start milestone index from which the node started from when syncing up.
514519 */
@@ -579,6 +584,21 @@ int computeEstimatedTimeToSyncUpSeconds(int latestMilestoneIndex, int latestSoli
579584
580585 return (int ) ((avgMilestoneApplyMillisec / 1000 ) * (latestMilestoneIndex - latestSolidMilestoneIndex ));
581586 }
587+
588+ /**
589+ *
590+ * @param latest
591+ * @param latestSolid
592+ * @return
593+ */
594+ double calculatePercentageSynced (int latest , int latestSolid , int oldestInQueue ) {
595+ double currentD = oldestInQueue - latestSolid ;
596+ double targetD = latest - latestSolid ;
597+ double processPercentage = 100 - currentD / targetD / 0.01d / 100d * PERCENT_WEIGHT_DOWN ;
598+
599+ double percentageSynced = processPercentage + ((latestSolid / latest / 0.01d ) / 100d * (100d - PERCENT_WEIGHT_DOWN ));
600+ return percentageSynced ;
601+ }
582602 }
583603
584604}
0 commit comments