diff --git a/Blockchain/.swiftpm/xcode/xcshareddata/xcschemes/Blockchain.xcscheme b/Blockchain/.swiftpm/xcode/xcshareddata/xcschemes/Blockchain.xcscheme index e1d5352a..310f26cc 100644 --- a/Blockchain/.swiftpm/xcode/xcshareddata/xcschemes/Blockchain.xcscheme +++ b/Blockchain/.swiftpm/xcode/xcshareddata/xcschemes/Blockchain.xcscheme @@ -29,6 +29,18 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES" shouldAutocreateTestPlan = "YES"> + + + + + + [CoreIndex] { + public static func toCoreAssignment(_ source: [UInt32], n: UInt32, max: UInt32) -> [CoreIndex] { source.map { CoreIndex(($0 + n) % max) } } - private func getCoreAssignment(config: ProtocolConfigRef, randomness: Data32, timeslot: TimeslotIndex) -> [CoreIndex] { + public static func getCoreAssignment(config: ProtocolConfigRef, randomness: Data32, timeslot: TimeslotIndex) -> [CoreIndex] { var source = Array(repeating: UInt32(0), count: config.value.totalNumberOfValidators) for i in 0 ..< config.value.totalNumberOfValidators { source[i] = UInt32(config.value.totalNumberOfCores * i / config.value.totalNumberOfValidators) @@ -94,14 +94,14 @@ extension Guaranteeing { > { let coreAssignmentRotationPeriod = UInt32(config.value.coreAssignmentRotationPeriod) - let currentCoreAssignment = getCoreAssignment(config: config, randomness: entropyPool.t2, timeslot: timeslot) + let currentCoreAssignment = Self.getCoreAssignment(config: config, randomness: entropyPool.t2, timeslot: timeslot) let currentCoreKeys = withoutOffenders(keys: currentValidators.map(\.ed25519)) let isEpochChanging = (timeslot % UInt32(config.value.epochLength)) < coreAssignmentRotationPeriod let previousRandomness = isEpochChanging ? entropyPool.t3 : entropyPool.t2 let previousValidators = isEpochChanging ? previousValidators : currentValidators - let previousCoreAssignment = getCoreAssignment( + let previousCoreAssignment = Self.getCoreAssignment( config: config, randomness: previousRandomness, timeslot: timeslot - coreAssignmentRotationPeriod diff --git a/Blockchain/Tests/BlockchainTests/GuaranteeingServiceTests.swift b/Blockchain/Tests/BlockchainTests/GuaranteeingServiceTests.swift new file mode 100644 index 00000000..4bd24cbb --- /dev/null +++ b/Blockchain/Tests/BlockchainTests/GuaranteeingServiceTests.swift @@ -0,0 +1,55 @@ +import Foundation +import Testing +import TracingUtils +import Utils + +@testable import Blockchain + +struct GuaranteeingServiceTests { + func setup( + config: ProtocolConfigRef = .dev, + time: TimeInterval = 988, + keysCount: Int = 12 + ) async throws -> (BlockchainServices, GuaranteeingService) { + let services = await BlockchainServices( + config: config, + timeProvider: MockTimeProvider(time: time), + keysCount: keysCount + ) + + let extrinsicPoolService = await ExtrinsicPoolService( + config: config, + dataProvider: services.dataProvider, + eventBus: services.eventBus + ) + + let runtime = Runtime(config: config) + + let guaranteeingService = await GuaranteeingService( + config: config, + eventBus: services.eventBus, + scheduler: services.scheduler, + dataProvider: services.dataProvider, + keystore: services.keystore, + runtime: runtime, + extrinsicPool: extrinsicPoolService + ) + return (services, guaranteeingService) + } + + @Test func onGenesis() async throws { + let (services, validatorService) = try await setup() + let genesisState = services.genesisState + let storeMiddleware = services.storeMiddleware + let scheduler = services.scheduler + + await validatorService.on(genesis: genesisState) + + let events = await storeMiddleware.wait() + + // Check if xxx events were published + + // Check if block author tasks were scheduled + #expect(scheduler.taskCount == 0) + } +}