-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:AcalaNetwork/boka
* 'master' of github.com:AcalaNetwork/boka: Block author (#131)
- Loading branch information
Showing
45 changed files
with
757 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,5 +42,5 @@ let package = Package( | |
] | ||
), | ||
], | ||
swiftLanguageVersions: [.version("6")] | ||
swiftLanguageModes: [.version("6")] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Blockchain/Sources/Blockchain/Scheduler/Date+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
|
||
extension Date { | ||
public static var jamCommonEraBeginning: UInt32 { | ||
// the Jam Common Era: 1200 UTC on January 1, 2024 | ||
// number of seconds since the Unix epoch | ||
1_704_110_400 | ||
} | ||
|
||
public var timeIntervalSinceJamCommonEra: UInt32 { | ||
let beginning = Double(Date.jamCommonEraBeginning) | ||
let now = timeIntervalSince1970 | ||
return UInt32(now - beginning) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Blockchain/Sources/Blockchain/Scheduler/DispatchQueueScheduler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@preconcurrency import Foundation | ||
import TracingUtils | ||
|
||
private let logger = Logger(label: "Scheduler") | ||
|
||
public final class DispatchQueueScheduler: Scheduler { | ||
public let timeProvider: TimeProvider | ||
private let queue: DispatchQueue | ||
|
||
public init(timeProvider: TimeProvider, queue: DispatchQueue = .global()) { | ||
self.timeProvider = timeProvider | ||
self.queue = queue | ||
} | ||
|
||
public func schedule( | ||
delay: TimeInterval, | ||
repeats: Bool, | ||
task: @escaping @Sendable () -> Void, | ||
onCancel: (@Sendable () -> Void)? | ||
) -> Cancellable { | ||
logger.trace("scheduling task in \(delay) seconds, repeats: \(repeats)") | ||
let timer = DispatchSource.makeTimerSource(queue: queue) | ||
timer.setEventHandler(handler: task) | ||
timer.setCancelHandler(handler: onCancel) | ||
timer.schedule(deadline: .now() + delay, repeating: repeats ? delay : .infinity) | ||
timer.activate() | ||
return Cancellable { | ||
timer.cancel() | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
Blockchain/Sources/Blockchain/Scheduler/MockScheduler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import Atomics | ||
import Foundation | ||
import Utils | ||
|
||
private final class SchedulerTask: Sendable { | ||
let id: Int | ||
let scheduleTime: UInt32 | ||
let repeats: TimeInterval? | ||
let task: @Sendable () -> Void | ||
let cancel: (@Sendable () -> Void)? | ||
|
||
init( | ||
id: Int, | ||
scheduleTime: UInt32, | ||
repeats: TimeInterval?, | ||
task: @escaping @Sendable () -> Void, | ||
cancel: (@Sendable () -> Void)? | ||
) { | ||
self.id = id | ||
self.scheduleTime = scheduleTime | ||
self.repeats = repeats | ||
self.task = task | ||
self.cancel = cancel | ||
} | ||
} | ||
|
||
private struct Storage: Sendable { | ||
var tasks: [SchedulerTask] = [] | ||
var prevTime: UInt32 = 0 | ||
} | ||
|
||
public final class MockScheduler: Scheduler, Sendable { | ||
static let idGenerator = ManagedAtomic<Int>(0) | ||
|
||
private let mockTimeProvider: MockTimeProvider | ||
public var timeProvider: TimeProvider { | ||
mockTimeProvider | ||
} | ||
|
||
private let storage: ThreadSafeContainer<Storage> = .init(.init()) | ||
|
||
public init(timeProvider: MockTimeProvider) { | ||
mockTimeProvider = timeProvider | ||
} | ||
|
||
public func schedule( | ||
delay: TimeInterval, | ||
repeats: Bool, | ||
task: @escaping @Sendable () -> Void, | ||
onCancel: (@Sendable () -> Void)? | ||
) -> Cancellable { | ||
let now = timeProvider.getTime() | ||
let scheduleTime = now + UInt32(delay) | ||
let id = Self.idGenerator.loadThenWrappingIncrement(ordering: .relaxed) | ||
let task = SchedulerTask(id: id, scheduleTime: scheduleTime, repeats: repeats ? delay : nil, task: task, cancel: onCancel) | ||
storage.write { storage in | ||
storage.tasks.append(task) | ||
} | ||
return Cancellable { | ||
self.storage.mutate { storage in | ||
if let index = storage.tasks.firstIndex(where: { $0.id == id }) { | ||
let task = storage.tasks.remove(at: index) | ||
task.cancel?() | ||
} | ||
} | ||
} | ||
} | ||
|
||
public func advance(by interval: UInt32) { | ||
mockTimeProvider.advance(by: interval) | ||
trigger() | ||
} | ||
|
||
public func trigger() { | ||
let now = timeProvider.getTime() | ||
let tasks = storage.mutate { storage in | ||
var tasksToDispatch: [SchedulerTask] = [] | ||
var remainingTasks: [SchedulerTask] = [] | ||
|
||
for task in storage.tasks { | ||
if task.scheduleTime <= now { | ||
tasksToDispatch.append(task) | ||
} else { | ||
remainingTasks.append(task) | ||
} | ||
} | ||
|
||
storage.tasks = remainingTasks | ||
storage.prevTime = now | ||
for task in tasksToDispatch { | ||
if let repeats = task.repeats { | ||
storage.tasks.append(SchedulerTask( | ||
id: task.id, | ||
scheduleTime: task.scheduleTime + UInt32(repeats), | ||
repeats: repeats, | ||
task: task.task, | ||
cancel: task.cancel | ||
)) | ||
} | ||
} | ||
return tasksToDispatch | ||
} | ||
|
||
for task in tasks { | ||
task.task() | ||
} | ||
} | ||
} |
Oops, something went wrong.