make range function static

This commit is contained in:
Francisco Gindre 2021-06-17 18:03:42 -03:00
parent abf49abadd
commit 292d08af7a
2 changed files with 21 additions and 16 deletions

View File

@ -486,11 +486,14 @@ public class CompactBlockProcessor {
switch result { switch result {
case .success(let info): case .success(let info):
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
do { do {
try self?.validateServerInfo(info) try Self.validateServerInfo(info,
saplingActivation: self.config.saplingActivation,
rustBackend: self.rustBackend)
completionBlock() completionBlock()
} catch { } catch {
self?.severeFailure(error) self.severeFailure(error)
} }
} }
case .failure(let error): case .failure(let error):
@ -499,7 +502,9 @@ public class CompactBlockProcessor {
}) })
} }
func validateServerInfo(_ info: LightWalletdInfo) throws { static func validateServerInfo(_ info: LightWalletdInfo,
saplingActivation: BlockHeight,
rustBackend: ZcashRustBackendWelding.Type) throws {
do { do {
// check network types // check network types
@ -511,8 +516,8 @@ public class CompactBlockProcessor {
throw CompactBlockProcessorError.networkMismatch(expected: ZcashSDK.networkType, found: remoteNetworkType) throw CompactBlockProcessorError.networkMismatch(expected: ZcashSDK.networkType, found: remoteNetworkType)
} }
guard config.saplingActivation == info.saplingActivationHeight else { guard saplingActivation == info.saplingActivationHeight else {
throw CompactBlockProcessorError.saplingActivationMismatch(expected: config.saplingActivation, found: BlockHeight(info.saplingActivationHeight)) throw CompactBlockProcessorError.saplingActivationMismatch(expected: saplingActivation, found: BlockHeight(info.saplingActivationHeight))
} }
// check branch id // check branch id
@ -593,7 +598,7 @@ public class CompactBlockProcessor {
// get latest block height from lightwalletd // get latest block height from lightwalletd
if self.latestBlockHeight > latestDownloadedBlockHeight { if self.latestBlockHeight > latestDownloadedBlockHeight {
self.processNewBlocks(range: self.nextBatchBlockRange(latestHeight: self.latestBlockHeight, latestDownloadedHeight: latestDownloadedBlockHeight)) self.processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: self.latestBlockHeight, latestDownloadedHeight: latestDownloadedBlockHeight, walletBirthday: config.walletBirthday))
} else { } else {
self.downloader.latestBlockHeight { [weak self] (result) in self.downloader.latestBlockHeight { [weak self] (result) in
guard let self = self else { return } guard let self = self else { return }
@ -611,7 +616,7 @@ public class CompactBlockProcessor {
self.processingFinished(height: latestDownloadedBlockHeight) self.processingFinished(height: latestDownloadedBlockHeight)
} else { } else {
self.processNewBlocks(range: self.nextBatchBlockRange(latestHeight: self.latestBlockHeight, latestDownloadedHeight: latestDownloadedBlockHeight)) self.processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: self.latestBlockHeight, latestDownloadedHeight: latestDownloadedBlockHeight, walletBirthday: self.config.walletBirthday))
} }
} }
case .failure(let e): case .failure(let e):
@ -850,7 +855,7 @@ public class CompactBlockProcessor {
NotificationCenter.default.post(name: Notification.Name.blockProcessorHandledReOrg, object: self, userInfo: [CompactBlockProcessorNotificationKey.reorgHeight : height, CompactBlockProcessorNotificationKey.rewindHeight : rewindHeight]) NotificationCenter.default.post(name: Notification.Name.blockProcessorHandledReOrg, object: self, userInfo: [CompactBlockProcessorNotificationKey.reorgHeight : height, CompactBlockProcessorNotificationKey.rewindHeight : rewindHeight])
// process next batch // process next batch
processNewBlocks(range: self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight())) processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight(), walletBirthday: config.walletBirthday))
} catch { } catch {
self.fail(error) self.fail(error)
} }
@ -923,9 +928,9 @@ public class CompactBlockProcessor {
self.backoffTimer = timer self.backoffTimer = timer
} }
func nextBatchBlockRange(latestHeight: BlockHeight, latestDownloadedHeight: BlockHeight) -> CompactBlockRange { static func nextBatchBlockRange(latestHeight: BlockHeight, latestDownloadedHeight: BlockHeight, walletBirthday: BlockHeight) -> CompactBlockRange {
let lowerBound = latestDownloadedHeight <= config.walletBirthday ? config.walletBirthday : latestDownloadedHeight + 1 let lowerBound = latestDownloadedHeight <= walletBirthday ? walletBirthday : latestDownloadedHeight + 1
let upperBound = latestHeight let upperBound = latestHeight
return lowerBound ... upperBound return lowerBound ... upperBound
@ -947,7 +952,7 @@ public class CompactBlockProcessor {
try downloader.rewind(to: max(range.lowerBound, self.config.walletBirthday)) try downloader.rewind(to: max(range.lowerBound, self.config.walletBirthday))
// process next batch // process next batch
processNewBlocks(range: self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight())) processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight(), walletBirthday: config.walletBirthday))
} catch { } catch {
self.fail(error) self.fail(error)
} }

View File

@ -111,17 +111,17 @@ class CompactBlockProcessorTests: XCTestCase {
var latestDownloadedHeight = processorConfig.walletBirthday // this can be either this or Wallet Birthday. var latestDownloadedHeight = processorConfig.walletBirthday // this can be either this or Wallet Birthday.
var latestBlockchainHeight = BlockHeight(ZcashSDK.SAPLING_ACTIVATION_HEIGHT + 1000) var latestBlockchainHeight = BlockHeight(ZcashSDK.SAPLING_ACTIVATION_HEIGHT + 1000)
var expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight, upper:latestDownloadedHeight + processorConfig.downloadBatchSize - 1)) var expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight, upper:latestBlockchainHeight))
XCTAssertEqual(expectedBatchRange, processor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight)) XCTAssertEqual(expectedBatchRange, CompactBlockProcessor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight, walletBirthday: processorConfig.walletBirthday))
// Test mid-range // Test mid-range
latestDownloadedHeight = BlockHeight(ZcashSDK.SAPLING_ACTIVATION_HEIGHT + ZcashSDK.DEFAULT_BATCH_SIZE) latestDownloadedHeight = BlockHeight(ZcashSDK.SAPLING_ACTIVATION_HEIGHT + ZcashSDK.DEFAULT_BATCH_SIZE)
latestBlockchainHeight = BlockHeight(ZcashSDK.SAPLING_ACTIVATION_HEIGHT + 1000) latestBlockchainHeight = BlockHeight(ZcashSDK.SAPLING_ACTIVATION_HEIGHT + 1000)
expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight + 1, upper:latestDownloadedHeight + processorConfig.downloadBatchSize)) expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight + 1, upper: latestBlockchainHeight))
XCTAssertEqual(expectedBatchRange, processor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight)) XCTAssertEqual(expectedBatchRange, CompactBlockProcessor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight, walletBirthday: processorConfig.walletBirthday))
// Test last batch range // Test last batch range
@ -130,7 +130,7 @@ class CompactBlockProcessorTests: XCTestCase {
expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight + 1, upper: latestBlockchainHeight)) expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight + 1, upper: latestBlockchainHeight))
XCTAssertEqual(expectedBatchRange, processor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight)) XCTAssertEqual(expectedBatchRange, CompactBlockProcessor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight, walletBirthday: processorConfig.walletBirthday))
} }
func testDetermineLowerBoundPastBirthday() { func testDetermineLowerBoundPastBirthday() {