Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Switch to new sync target if it exceeds the td threshold #1286

Merged
merged 8 commits into from
Apr 16, 2019

Conversation

ajsutton
Copy link
Contributor

PR description

When evaluating if the sync target should be changed, we would only switch if the new best peer's height was better by some amount. We should switch if the difficulty or height exceeds the "better" threshold (as long as the difficulty is at least equal).

Also splits this calculation into a separate class so it can be reused in the pipeline chain download and adds unit tests for it.

Copy link
Contributor

@mbaxter mbaxter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending a look into the default threshold values

.getBestBlock()
.getTotalDifficulty()
.minus(currentPeerChainState.getBestBlock().getTotalDifficulty());
if (tdDifference.compareTo(config.downloaderChangeTargetThresholdByTd()) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defaults we have for these values don't seem to make much sense. Looks like mainnet is changing by ~10^15 per block but the default threshold is 10^9. And td is tricky because the rate of change should depend on the particular chain. In any case, we should probably try to pick better defaults as part of this PR while we're here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a particularly astute yet inconvenient observation...

The situation we want to avoid is where our first peer has a very long but low difficulty chain, trying to draw us into a tar pit and spending a long time sync'ing their chain. Currently if their chain height was higher we'd continue syncing to them until we reached their head and only then switch to the actual best peer and begin syncing the right chain.

That said, it takes quite a lot of effort to generate a really long PoW chain because the difficulty will always keep blocks around 15s apart.

I'm inclined to set it to a high value like 10^18 which means the default is almost always switch on chain height before the TD threshold was reached. It would still differentiate between ETH and ETC though we have an explicit Dao block check for that as well now. Having the setting around is potentially quite useful for cases like when the Ropsten fork went wrong - you'd want to set it to something much smaller to ensure you switched to the best chain faster.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable. The height threshold is pretty low as well - should we bump that up?


import org.junit.Test;

public class BetterSyncTargetEvaluatorTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test coverage :D

@ajsutton ajsutton merged commit c17fb12 into PegaSysEng:master Apr 16, 2019
@ajsutton ajsutton deleted the switch-on-better-td branch April 16, 2019 22:59
notlesh pushed a commit to notlesh/pantheon that referenced this pull request Apr 24, 2019
…1286)

Increase total difficulty threshold required to switch sync targets.
Increase threshold for changing sync target based on height to 200.
notlesh pushed a commit to notlesh/pantheon that referenced this pull request May 4, 2019
…1286)

Increase total difficulty threshold required to switch sync targets.
Increase threshold for changing sync target based on height to 200.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants