[#1301] foundTransactions don't emit after rewind

- There was a reset missing in the rewind() call. This method is a direct call affecting the state of the compact block processor but the ActionContext has been handled only in the synchronization pipeline. Manual reset was needed to reset the last enhanced height.
- tests added
This commit is contained in:
Lukas Korba 2023-10-12 15:59:47 +02:00
parent bb99939415
commit e763e29dc1
2 changed files with 66 additions and 5 deletions

View File

@ -22,7 +22,7 @@ actor CompactBlockProcessor {
private var syncTask: Task<Void, Error>?
private let actions: [CBPState: Action]
private var context: ActionContext
var context: ActionContext
private(set) var config: Configuration
private let configProvider: ConfigProvider
@ -346,6 +346,8 @@ extension CompactBlockProcessor {
} catch {
return await context.completion(.failure(error))
}
await resetContext(restoreLastEnhancedHeight: false)
await context.completion(.success(rewindBlockHeight))
}
@ -615,10 +617,12 @@ extension CompactBlockProcessor {
}
}
private func resetContext() async {
let lastEnhancedheight = await context.lastEnhancedHeight
func resetContext(restoreLastEnhancedHeight: Bool = true) async {
let lastEnhancedHeight = await context.lastEnhancedHeight
context = ActionContextImpl(state: .idle)
await context.update(lastEnhancedHeight: lastEnhancedheight)
if restoreLastEnhancedHeight {
await context.update(lastEnhancedHeight: lastEnhancedHeight)
}
}
private func syncStarted() async {

View File

@ -9,7 +9,7 @@ import XCTest
@testable import TestUtils
@testable import ZcashLightClientKit
final class ActionContextStateTests: XCTestCase {
final class ActionContextStateTests: ZcashTestCase {
func testPreviousState() async throws {
let syncContext = ActionContextImpl(state: .idle)
@ -32,4 +32,61 @@ final class ActionContextStateTests: XCTestCase {
XCTFail("syncContext.prevState is not expected to be nil.")
}
}
func testActionContextReset_DefaultBehaviour() async throws {
let testCoordinator: TestCoordinator! = try await TestCoordinator(
alias: .default,
container: mockContainer,
walletBirthday: 10,
network: ZcashTestnet(),
callPrepareInConstructor: false
)
await testCoordinator.synchronizer.blockProcessor.context.update(lastEnhancedHeight: 1_500_000)
let contextLastEnhancedHeight = await testCoordinator.synchronizer.blockProcessor.context.lastEnhancedHeight
XCTAssertEqual(contextLastEnhancedHeight, 1_500_000)
await testCoordinator.synchronizer.blockProcessor.resetContext()
let contextLastEnhancedHeightAfterReset = await testCoordinator.synchronizer.blockProcessor.context.lastEnhancedHeight
XCTAssertEqual(
contextLastEnhancedHeightAfterReset,
1_500_000,
"""
testActionContextReset_DefaultBehaviour: The context after reset should restore the last enhanced height,
received \(String(describing: contextLastEnhancedHeightAfterReset)) instead
"""
)
}
func testActionContextReset_LastEnhancedHeightReset() async throws {
let testCoordinator: TestCoordinator! = try await TestCoordinator(
alias: .default,
container: mockContainer,
walletBirthday: 10,
network: ZcashTestnet(),
callPrepareInConstructor: false
)
await testCoordinator.synchronizer.blockProcessor.context.update(lastEnhancedHeight: 1_500_000)
let contextLastEnhancedHeight = await testCoordinator.synchronizer.blockProcessor.context.lastEnhancedHeight
XCTAssertEqual(contextLastEnhancedHeight, 1_500_000)
await testCoordinator.synchronizer.blockProcessor.resetContext(restoreLastEnhancedHeight: false)
let contextLastEnhancedHeightAfterReset = await testCoordinator.synchronizer.blockProcessor.context.lastEnhancedHeight
XCTAssertNil(
contextLastEnhancedHeightAfterReset,
"""
testActionContextReset_LastEnhancedHeightReset: The context after reset should be nil when forced,
received \(String(describing: contextLastEnhancedHeightAfterReset)) instead
"""
)
}
}