[#1227] Check continuation of UpdateChainTip repeated call
- My assumption was right, the way State Machine is done requires .celarCache to be called with every "restart" - I was able to reproduce errors when clearCache wasn't called so the updateChainTipAction needed to be updated to prepare clean conditions for the scan suggest ranges - tests updated
This commit is contained in:
parent
eca88735b4
commit
66c7c84f6f
|
@ -9,11 +9,13 @@ import Foundation
|
||||||
|
|
||||||
final class UpdateChainTipAction {
|
final class UpdateChainTipAction {
|
||||||
let rustBackend: ZcashRustBackendWelding
|
let rustBackend: ZcashRustBackendWelding
|
||||||
|
let downloader: BlockDownloader
|
||||||
let service: LightWalletService
|
let service: LightWalletService
|
||||||
let logger: Logger
|
let logger: Logger
|
||||||
|
|
||||||
init(container: DIContainer) {
|
init(container: DIContainer) {
|
||||||
service = container.resolve(LightWalletService.self)
|
service = container.resolve(LightWalletService.self)
|
||||||
|
downloader = container.resolve(BlockDownloader.self)
|
||||||
rustBackend = container.resolve(ZcashRustBackendWelding.self)
|
rustBackend = container.resolve(ZcashRustBackendWelding.self)
|
||||||
logger = container.resolve(Logger.self)
|
logger = container.resolve(Logger.self)
|
||||||
}
|
}
|
||||||
|
@ -36,8 +38,9 @@ extension UpdateChainTipAction: Action {
|
||||||
|
|
||||||
// Update chain tip can be called from different contexts
|
// Update chain tip can be called from different contexts
|
||||||
if await context.prevState == .updateSubtreeRoots || now - lastChainTipUpdateTime > 600 {
|
if await context.prevState == .updateSubtreeRoots || now - lastChainTipUpdateTime > 600 {
|
||||||
|
await downloader.stopDownload()
|
||||||
try await updateChainTip(context, time: now)
|
try await updateChainTip(context, time: now)
|
||||||
await context.update(state: .processSuggestedScanRanges)
|
await context.update(state: .clearCache)
|
||||||
} else if await context.prevState == .enhance {
|
} else if await context.prevState == .enhance {
|
||||||
await context.update(state: .download)
|
await context.update(state: .download)
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,12 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
|
|
||||||
func testUpdateChainTipAction_UpdateChainTipTimeTriggered() async throws {
|
func testUpdateChainTipAction_UpdateChainTipTimeTriggered() async throws {
|
||||||
let loggerMock = LoggerMock()
|
let loggerMock = LoggerMock()
|
||||||
|
let blockDownloaderMock = BlockDownloaderMock()
|
||||||
loggerMock.infoFileFunctionLineClosure = { _, _, _, _ in }
|
|
||||||
|
|
||||||
let updateChainTipAction = await setupAction(loggerMock)
|
loggerMock.infoFileFunctionLineClosure = { _, _, _, _ in }
|
||||||
|
blockDownloaderMock.stopDownloadClosure = { }
|
||||||
|
|
||||||
|
let updateChainTipAction = await setupAction(loggerMock, blockDownloaderMock)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let context = ActionContextMock.default()
|
let context = ActionContextMock.default()
|
||||||
|
@ -39,7 +41,9 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
|
|
||||||
let nextContext = try await updateChainTipAction.run(with: context) { _ in }
|
let nextContext = try await updateChainTipAction.run(with: context) { _ in }
|
||||||
|
|
||||||
let acResult = nextContext.checkStateIs(.processSuggestedScanRanges)
|
XCTAssertTrue(blockDownloaderMock.stopDownloadCallsCount == 1, "downloader.stopDownload() is expected to be called exactly once.")
|
||||||
|
|
||||||
|
let acResult = nextContext.checkStateIs(.clearCache)
|
||||||
XCTAssertTrue(acResult == .true, "Check of state failed with '\(acResult)'")
|
XCTAssertTrue(acResult == .true, "Check of state failed with '\(acResult)'")
|
||||||
} catch {
|
} catch {
|
||||||
XCTFail("testUpdateChainTipAction_UpdateChainTipTimeTriggered is not expected to fail. \(error)")
|
XCTFail("testUpdateChainTipAction_UpdateChainTipTimeTriggered is not expected to fail. \(error)")
|
||||||
|
@ -48,10 +52,12 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
|
|
||||||
func testUpdateChainTipAction_UpdateChainTipPrevActionTriggered() async throws {
|
func testUpdateChainTipAction_UpdateChainTipPrevActionTriggered() async throws {
|
||||||
let loggerMock = LoggerMock()
|
let loggerMock = LoggerMock()
|
||||||
|
let blockDownloaderMock = BlockDownloaderMock()
|
||||||
loggerMock.infoFileFunctionLineClosure = { _, _, _, _ in }
|
|
||||||
|
|
||||||
let updateChainTipAction = await setupAction(loggerMock)
|
loggerMock.infoFileFunctionLineClosure = { _, _, _, _ in }
|
||||||
|
blockDownloaderMock.stopDownloadClosure = { }
|
||||||
|
|
||||||
|
let updateChainTipAction = await setupAction(loggerMock, blockDownloaderMock)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let context = ActionContextMock.default()
|
let context = ActionContextMock.default()
|
||||||
|
@ -61,7 +67,9 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
|
|
||||||
let nextContext = try await updateChainTipAction.run(with: context) { _ in }
|
let nextContext = try await updateChainTipAction.run(with: context) { _ in }
|
||||||
|
|
||||||
let acResult = nextContext.checkStateIs(.processSuggestedScanRanges)
|
XCTAssertTrue(blockDownloaderMock.stopDownloadCallsCount == 1, "downloader.stopDownload() is expected to be called exactly once.")
|
||||||
|
|
||||||
|
let acResult = nextContext.checkStateIs(.clearCache)
|
||||||
XCTAssertTrue(acResult == .true, "Check of state failed with '\(acResult)'")
|
XCTAssertTrue(acResult == .true, "Check of state failed with '\(acResult)'")
|
||||||
} catch {
|
} catch {
|
||||||
XCTFail("testUpdateChainTipAction_UpdateChainTipPrevActionTriggered is not expected to fail. \(error)")
|
XCTFail("testUpdateChainTipAction_UpdateChainTipPrevActionTriggered is not expected to fail. \(error)")
|
||||||
|
@ -70,10 +78,12 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
|
|
||||||
func testUpdateChainTipAction_UpdateChainTipSkipped() async throws {
|
func testUpdateChainTipAction_UpdateChainTipSkipped() async throws {
|
||||||
let loggerMock = LoggerMock()
|
let loggerMock = LoggerMock()
|
||||||
|
let blockDownloaderMock = BlockDownloaderMock()
|
||||||
loggerMock.infoFileFunctionLineClosure = { _, _, _, _ in }
|
|
||||||
|
|
||||||
let updateChainTipAction = await setupAction(loggerMock)
|
loggerMock.infoFileFunctionLineClosure = { _, _, _, _ in }
|
||||||
|
blockDownloaderMock.stopDownloadClosure = { }
|
||||||
|
|
||||||
|
let updateChainTipAction = await setupAction(loggerMock, blockDownloaderMock)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let context = ActionContextMock.default()
|
let context = ActionContextMock.default()
|
||||||
|
@ -83,6 +93,8 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
|
|
||||||
let nextContext = try await updateChainTipAction.run(with: context) { _ in }
|
let nextContext = try await updateChainTipAction.run(with: context) { _ in }
|
||||||
|
|
||||||
|
XCTAssertFalse(blockDownloaderMock.stopDownloadCalled, "downloader.stopDownload() is not expected to be called.")
|
||||||
|
|
||||||
let acResult = nextContext.checkStateIs(.download)
|
let acResult = nextContext.checkStateIs(.download)
|
||||||
XCTAssertTrue(acResult == .true, "Check of state failed with '\(acResult)'")
|
XCTAssertTrue(acResult == .true, "Check of state failed with '\(acResult)'")
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -91,7 +103,8 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupAction(
|
private func setupAction(
|
||||||
_ loggerMock: LoggerMock = LoggerMock()
|
_ loggerMock: LoggerMock = LoggerMock(),
|
||||||
|
_ blockDownloaderMock: BlockDownloaderMock = BlockDownloaderMock()
|
||||||
) async -> UpdateChainTipAction {
|
) async -> UpdateChainTipAction {
|
||||||
let config: CompactBlockProcessor.Configuration = .standard(
|
let config: CompactBlockProcessor.Configuration = .standard(
|
||||||
for: ZcashNetworkBuilder.network(for: underlyingNetworkType), walletBirthday: 0
|
for: ZcashNetworkBuilder.network(for: underlyingNetworkType), walletBirthday: 0
|
||||||
|
@ -119,6 +132,7 @@ final class UpdateChainTipActionTests: ZcashTestCase {
|
||||||
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in rustBackendMock }
|
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in rustBackendMock }
|
||||||
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in serviceMock }
|
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in serviceMock }
|
||||||
mockContainer.mock(type: Logger.self, isSingleton: true) { _ in loggerMock }
|
mockContainer.mock(type: Logger.self, isSingleton: true) { _ in loggerMock }
|
||||||
|
mockContainer.mock(type: BlockDownloader.self, isSingleton: true) { _ in blockDownloaderMock }
|
||||||
|
|
||||||
return UpdateChainTipAction(container: mockContainer)
|
return UpdateChainTipAction(container: mockContainer)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue