Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
Added fix for syncingon xisting db
Browse files Browse the repository at this point in the history
  • Loading branch information
Brord van Wierst committed May 11, 2020
1 parent 2865e17 commit e731988
Showing 1 changed file with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public void start() {
setLatestMilestone(latestSnapshot.getHash(), latestSnapshot.getIndex());
logChange(snapshotProvider.getInitialSnapshot().getIndex());

syncProgressInfo.setSyncMilestoneStartIndex(snapshotProvider.getInitialSnapshot().getIndex());
milestoneSolidifier.start();

} catch (Exception e) {
Expand Down Expand Up @@ -222,6 +221,8 @@ private void processSolidifyQueue() throws Exception {
int lowest = oldestMilestoneInQueue == null ? -1 : oldestMilestoneInQueue.getValue();
scanMilestonesInQueue();
if (oldestMilestoneInQueue != null && lowest > oldestMilestoneInQueue.getValue()) {
// Going down or going up doesnt matter to the calculation
syncProgressInfo.addMilestoneApplicationTime();
logChange(-1);
}
}
Expand Down Expand Up @@ -477,21 +478,22 @@ private void logChange(int prevSolidMilestoneIndex) {
}

// only print more sophisticated progress if we are coming from a more unsynced state
if (oldestMilestoneInQueue == null ||
(prevSolidMilestoneIndex != -1 && getLatestMilestoneIndex() - nextLatestSolidMilestone < 1)) {
if (prevSolidMilestoneIndex != -1 && getLatestMilestoneIndex() - nextLatestSolidMilestone < 1) {
syncProgressInfo.setSyncMilestoneStartIndex(nextLatestSolidMilestone);
syncProgressInfo.resetMilestoneApplicationTimes();
return;
}

int estSecondsToBeSynced = syncProgressInfo.computeEstimatedTimeToSyncUpSeconds(getLatestMilestoneIndex(),
nextLatestSolidMilestone);
StringBuilder progressSB = new StringBuilder();

double percentPreferDown = 95;
double percentageSynced = ((100d - oldestMilestoneInQueue.getValue() / latestMilestoneIndex.doubleValue() / 0.01d) / 100d * percentPreferDown)
+ ((latestSolidMilestone.doubleValue() / latestMilestoneIndex.doubleValue() / 0.01d) / 100d * (100d - percentPreferDown));

// oldestMilestoneInQueue can be null if they are processed faster than coming in.
// Unlikely to happen on mainnet though.
double percentageSynced = syncProgressInfo.calculatePercentageSynced(getLatestMilestoneIndex(),
getLatestSolidMilestoneIndex(),
oldestMilestoneInQueue == null ? getLatestSolidMilestoneIndex() : oldestMilestoneInQueue.getValue());

StringBuilder progressSB = new StringBuilder();
// add progress bar
progressSB.append(ASCIIProgressBar.getProgressBarString(0, 100, (int)Math.round(percentageSynced)));
// add lsm to lm
Expand All @@ -509,6 +511,9 @@ private void logChange(int prevSolidMilestoneIndex) {
* Holds variables containing information needed for sync progress calculation.
*/
private static class SyncProgressInfo {

static final double PERCENT_WEIGHT_DOWN = 95;

/**
* The actual start milestone index from which the node started from when syncing up.
*/
Expand Down Expand Up @@ -579,6 +584,21 @@ int computeEstimatedTimeToSyncUpSeconds(int latestMilestoneIndex, int latestSoli

return (int) ((avgMilestoneApplyMillisec / 1000) * (latestMilestoneIndex - latestSolidMilestoneIndex));
}

/**
*
* @param latest
* @param latestSolid
* @return
*/
double calculatePercentageSynced(int latest, int latestSolid, int oldestInQueue) {
double currentD = oldestInQueue - latestSolid;
double targetD = latest - latestSolid;
double processPercentage = 100 - currentD / targetD / 0.01d / 100d * PERCENT_WEIGHT_DOWN;

double percentageSynced = processPercentage + ((latestSolid / latest / 0.01d) / 100d * (100d - PERCENT_WEIGHT_DOWN));
return percentageSynced;
}
}

}

0 comments on commit e731988

Please sign in to comment.