[#1206] Frequent call of update chain tip
- The State Machine has been slightly updated so it measures time when it lastly updated chain tip. If it happened more than 10mins ago, it calls the .updateChainTip action once again before the download-scan-enhance loop continues - updated unit test [#1206] Frequent call of update chain tip (#1207) - whenever updateChainTip is called, it's followed by suggestScanRanges logic
This commit is contained in:
parent
62945ec17d
commit
c1ff5615dd
|
@ -18,6 +18,8 @@ actor ActionContext {
|
||||||
var totalProgressRange: CompactBlockRange = 0...0
|
var totalProgressRange: CompactBlockRange = 0...0
|
||||||
/// Amount of blocks that have been processed so far
|
/// Amount of blocks that have been processed so far
|
||||||
var processedHeight: BlockHeight = 0
|
var processedHeight: BlockHeight = 0
|
||||||
|
/// Update chain tip must be called repeatadly, this value stores the previous update and help to decide when to call it again
|
||||||
|
var lastChainTipUpdateTime: TimeInterval = 0.0
|
||||||
var lastScannedHeight: BlockHeight?
|
var lastScannedHeight: BlockHeight?
|
||||||
var lastDownloadedHeight: BlockHeight?
|
var lastDownloadedHeight: BlockHeight?
|
||||||
var lastEnhancedHeight: BlockHeight?
|
var lastEnhancedHeight: BlockHeight?
|
||||||
|
@ -38,6 +40,7 @@ actor ActionContext {
|
||||||
self.totalProgressRange = totalProgressRange
|
self.totalProgressRange = totalProgressRange
|
||||||
}
|
}
|
||||||
func update(processedHeight: BlockHeight) async { self.processedHeight = processedHeight }
|
func update(processedHeight: BlockHeight) async { self.processedHeight = processedHeight }
|
||||||
|
func update(lastChainTipUpdateTime: TimeInterval) async { self.lastChainTipUpdateTime = lastChainTipUpdateTime }
|
||||||
func update(lastScannedHeight: BlockHeight) async { self.lastScannedHeight = lastScannedHeight }
|
func update(lastScannedHeight: BlockHeight) async { self.lastScannedHeight = lastScannedHeight }
|
||||||
func update(lastDownloadedHeight: BlockHeight) async { self.lastDownloadedHeight = lastDownloadedHeight }
|
func update(lastDownloadedHeight: BlockHeight) async { self.lastDownloadedHeight = lastDownloadedHeight }
|
||||||
func update(lastEnhancedHeight: BlockHeight?) async { self.lastEnhancedHeight = lastEnhancedHeight }
|
func update(lastEnhancedHeight: BlockHeight?) async { self.lastEnhancedHeight = lastEnhancedHeight }
|
||||||
|
|
|
@ -30,7 +30,7 @@ final class EnhanceAction {
|
||||||
if lastScannedHeight >= latestBlockHeight {
|
if lastScannedHeight >= latestBlockHeight {
|
||||||
await context.update(state: .clearCache)
|
await context.update(state: .clearCache)
|
||||||
} else {
|
} else {
|
||||||
await context.update(state: .download)
|
await context.update(state: .updateChainTip)
|
||||||
}
|
}
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
|
@ -17,18 +17,30 @@ final class UpdateChainTipAction {
|
||||||
rustBackend = container.resolve(ZcashRustBackendWelding.self)
|
rustBackend = container.resolve(ZcashRustBackendWelding.self)
|
||||||
logger = container.resolve(Logger.self)
|
logger = container.resolve(Logger.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateChainTip(_ context: ActionContext, time: TimeInterval) async throws {
|
||||||
|
let latestBlockHeight = try await service.latestBlockHeight()
|
||||||
|
|
||||||
|
logger.info("Latest block height is \(latestBlockHeight)")
|
||||||
|
try await rustBackend.updateChainTip(height: Int32(latestBlockHeight))
|
||||||
|
await context.update(lastChainTipUpdateTime: time)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension UpdateChainTipAction: Action {
|
extension UpdateChainTipAction: Action {
|
||||||
var removeBlocksCacheWhenFailed: Bool { false }
|
var removeBlocksCacheWhenFailed: Bool { false }
|
||||||
|
|
||||||
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessor.Event) async -> Void) async throws -> ActionContext {
|
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessor.Event) async -> Void) async throws -> ActionContext {
|
||||||
let latestBlockHeight = try await service.latestBlockHeight()
|
let lastChainTipUpdateTime = await context.lastChainTipUpdateTime
|
||||||
|
let now = Date().timeIntervalSince1970
|
||||||
logger.info("Latest block height is \(latestBlockHeight)")
|
|
||||||
try await rustBackend.updateChainTip(height: Int32(latestBlockHeight))
|
|
||||||
|
|
||||||
|
// Update chain tip can be called from different contexts
|
||||||
|
if await context.prevState == .updateSubtreeRoots || now - lastChainTipUpdateTime > 600 {
|
||||||
|
try await updateChainTip(context, time: now)
|
||||||
await context.update(state: .processSuggestedScanRanges)
|
await context.update(state: .processSuggestedScanRanges)
|
||||||
|
} else if await context.prevState == .enhance {
|
||||||
|
await context.update(state: .download)
|
||||||
|
}
|
||||||
|
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ final class EnhanceActionTests: ZcashTestCase {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEnhanceAction_decideWhatToDoNext_DownloadExpected() async throws {
|
func testEnhanceAction_decideWhatToDoNext_UpdateChainTipExpected() async throws {
|
||||||
let enhanceAction = setupAction()
|
let enhanceAction = setupAction()
|
||||||
underlyingDownloadRange = CompactBlockRange(uncheckedBounds: (1000, 2000))
|
underlyingDownloadRange = CompactBlockRange(uncheckedBounds: (1000, 2000))
|
||||||
underlyingScanRange = CompactBlockRange(uncheckedBounds: (1000, 2000))
|
underlyingScanRange = CompactBlockRange(uncheckedBounds: (1000, 2000))
|
||||||
|
@ -60,8 +60,8 @@ final class EnhanceActionTests: ZcashTestCase {
|
||||||
let nextState = await nextContext.state
|
let nextState = await nextContext.state
|
||||||
|
|
||||||
XCTAssertTrue(
|
XCTAssertTrue(
|
||||||
nextState == .download,
|
nextState == .updateChainTip,
|
||||||
"testEnhanceAction_decideWhatToDoNext_DownloadExpected is expected to be .download but received \(nextState)"
|
"testEnhanceAction_decideWhatToDoNext_DownloadExpected is expected to be .updateChainTip but received \(nextState)"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue