Merge pull request #978 from zcash/969_clear_cache_on_error

[#969] Clear blocks cache when error occurs
This commit is contained in:
Michal Fousek 2023-04-18 14:26:46 +02:00 committed by GitHub
commit 7b3b45adca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 6 deletions

View File

@ -843,16 +843,23 @@ actor CompactBlockProcessor {
logger.debug("Sync loop #\(i + 1) range: \(processingRange.lowerBound)...\(processingRange.upperBound)")
try await blockDownloader.downloadAndStoreBlocks(
using: downloadStream,
at: processingRange,
maxBlockBufferSize: config.downloadBufferSize,
totalProgressRange: totalProgressRange
)
do {
try await blockDownloader.downloadAndStoreBlocks(
using: downloadStream,
at: processingRange,
maxBlockBufferSize: config.downloadBufferSize,
totalProgressRange: totalProgressRange
)
} catch {
await ifTaskIsNotCanceledClearCompactBlockCache()
throw error
}
do {
try await blockValidator.validate()
} catch {
await ifTaskIsNotCanceledClearCompactBlockCache()
guard let validationError = error as? BlockValidatorError else {
logger.error("Block validation failed with generic error: \(error)")
throw error
@ -882,6 +889,7 @@ actor CompactBlockProcessor {
}
} catch {
logger.error("Scanning failed with error: \(error)")
await ifTaskIsNotCanceledClearCompactBlockCache()
throw error
}
@ -1078,6 +1086,15 @@ actor CompactBlockProcessor {
await setTimer()
}
private func ifTaskIsNotCanceledClearCompactBlockCache() async {
guard !Task.isCancelled else { return }
do {
try await clearCompactBlockCache()
} catch {
logger.error("`clearCompactBlockCache` failed after error: \(error.localizedDescription)")
}
}
private func clearCompactBlockCache() async throws {
try await storage.clear()
logger.info("Cache removed")