Skip to content

Commit

Permalink
CompactBlockProcessor: Deduplicate sleep code
Browse files Browse the repository at this point in the history
Closes #1252.
  • Loading branch information
str4d committed Jan 26, 2024
1 parent 1bcd794 commit f7335ae
Showing 1 changed file with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,10 @@ class CompactBlockProcessor internal constructor(
BlockProcessingResult.Reconnecting -> {
setState(State.Disconnected)
downloader.reconnect()

// TODO [#1252]: Duplicated code in CompactBlockProcessor
// TODO [#1252]: https://github.com/zcash/zcash-android-wallet-sdk/issues/1252
val napTime = calculatePollInterval(true)
Twig.debug {
"Unable to process new blocks because we are disconnected! Attempting to " +
"reconnect in ${napTime}ms"
}
delay(napTime)
takeANap(
"Unable to process new blocks because we are disconnected! Attempting to reconnect.",
true
)
}
BlockProcessingResult.RestartSynchronization -> {
Twig.info { "Planned restarting of block synchronization..." }
Expand All @@ -323,13 +318,7 @@ class CompactBlockProcessor internal constructor(
}

resetErrorCounters()

val napTime = calculatePollInterval()
Twig.info {
"$summary Sleeping for ${napTime}ms " +
"(latest height: ${_processorInfo.value.networkBlockHeight})."
}
delay(napTime)
takeANap(summary, false)
}
is BlockProcessingResult.ContinuityError -> {
Twig.error {
Expand All @@ -346,11 +335,7 @@ class CompactBlockProcessor internal constructor(
}
val failed = checkErrorAndFail(result.failedAtHeight, result.error)
if (!failed) {
val napTime = calculatePollInterval(true)
Twig.info {
"Sleeping for ${napTime}ms (latest height: ${_processorInfo.value.networkBlockHeight})."
}
delay(napTime)
takeANap("", true)
}
}
is BlockProcessingResult.Success -> {
Expand Down Expand Up @@ -2099,6 +2084,18 @@ class CompactBlockProcessor internal constructor(
return deltaToNextInterval.toDuration(DurationUnit.MILLISECONDS)
}

private suspend fun takeANap(
summary: String,
fastIntervalDesired: Boolean
) {
val napTime = calculatePollInterval(fastIntervalDesired)
Twig.info {
"$summary Sleeping for ${napTime}ms " +
"(latest height: ${_processorInfo.value.networkBlockHeight})."
}
delay(napTime)
}

suspend fun calculateBirthdayHeight(): BlockHeight {
return repository.getOldestTransaction()?.minedHeight?.value?.let {
// To be safe adjust for reorgs (and generally a little cushion is good for privacy), so we round down to
Expand Down

0 comments on commit f7335ae

Please sign in to comment.