From 0ecf01b3c824aec151233f52491079a5b5088444 Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Wed, 18 Dec 2024 16:30:41 +0800 Subject: [PATCH] update WorkPackagePoolServiceTests --- .../Validator/GuaranteeingService.swift | 16 +++++------ .../Blockchain/Validator/SafroleService.swift | 26 ++---------------- .../Validator/WorkPackagePoolService.swift | 16 +++-------- .../WorkPackagePoolServiceTests.swift | 27 +++++++------------ 4 files changed, 24 insertions(+), 61 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift b/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift index 34306a61..b1943425 100644 --- a/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift +++ b/Blockchain/Sources/Blockchain/Validator/GuaranteeingService.swift @@ -67,14 +67,14 @@ public final class GuaranteeingService: ServiceBase2, @unchecked Sendable { let workPackage = await workPackagePool.getWorkPackage(for: core) // let workPackage = WorkPackage.dummy(config: config) // validateWorkPackage & mock wp - let validateWP = try validateWorkPackage(workPackage) - if validateWP { - let workReport = try await createWorkReport(for: workPackage, core: core) - // sign work report - // eventbus - } else { - logger.error("WorkPackage validation failed") - } +// let validateWP = try validateWorkPackage(workPackage) +// if validateWP { +// let workReport = try await createWorkReport(for: workPackage, core: core) +// // sign work report +// // eventbus +// } else { +// logger.error("WorkPackage validation failed") +// } // } diff --git a/Blockchain/Sources/Blockchain/Validator/SafroleService.swift b/Blockchain/Sources/Blockchain/Validator/SafroleService.swift index 3aa434ed..77927d25 100644 --- a/Blockchain/Sources/Blockchain/Validator/SafroleService.swift +++ b/Blockchain/Sources/Blockchain/Validator/SafroleService.swift @@ -16,7 +16,7 @@ public struct TicketItemAndOutput: Comparable, Sendable, Codable { } public struct WorkPackageAndOutput: Comparable, Sendable, Codable { - public let guarantee: ExtrinsicGuarantees.GuaranteeItem + public let workPackage: WorkPackage public let output: Data32 public static func < (lhs: WorkPackageAndOutput, rhs: WorkPackageAndOutput) -> Bool { @@ -24,7 +24,7 @@ public struct WorkPackageAndOutput: Comparable, Sendable, Codable { } public static func == (lhs: WorkPackageAndOutput, rhs: WorkPackageAndOutput) -> Bool { - lhs.output == rhs.output && lhs.guarantee == rhs.guarantee + lhs.output == rhs.output && lhs.workPackage == rhs.workPackage } } @@ -131,26 +131,4 @@ public final class SafroleService: ServiceBase, @unchecked Sendable { return tickets } - - public static func generateWorkPackages( - core _: CoreIndex, - validators: [ValidatorKey], - entropy: Data32, - ringContext: Bandersnatch.RingContext, - secret: Bandersnatch.SecretKey, - idx: UInt32 - ) throws -> [WorkPackageAndOutput] { - let pubkeys = try validators.map { - try Bandersnatch.PublicKey(data: $0.bandersnatch) - } - - let prover = Bandersnatch.Prover(sercret: secret, ring: pubkeys, proverIdx: UInt(idx), ctx: ringContext) - - var vrfInputData = SigningContext.entropyInputData(entropy: entropy) - - var wps: [WorkPackageAndOutput] = [] - // TODO: generateWorkPackages - - return wps - } } diff --git a/Blockchain/Sources/Blockchain/Validator/WorkPackagePoolService.swift b/Blockchain/Sources/Blockchain/Validator/WorkPackagePoolService.swift index 61d6a182..4faf42ca 100644 --- a/Blockchain/Sources/Blockchain/Validator/WorkPackagePoolService.swift +++ b/Blockchain/Sources/Blockchain/Validator/WorkPackagePoolService.swift @@ -39,7 +39,7 @@ private actor WorkPackageStorage { logger.warning("Invalid work package: \(package)") continue } - pendingWorkPackages.insert(package) + pendingWorkPackages.append(contentsOf: [package]) } } @@ -50,12 +50,11 @@ private actor WorkPackageStorage { func removeWorkPackages(workPackages: [WorkPackageAndOutput]) { pendingWorkPackages.remove { guarantee in - workPackages.contains { $0.guarantee == guarantee.guarantee } + workPackages.contains { $0 == guarantee } } } func getWorkPackage(for _: CoreIndex) -> SortedUniqueArray { - // return pendingWorkPackages.filter { $0.guarantee.workReport.coreIndex == core } pendingWorkPackages } } @@ -80,10 +79,8 @@ public final class WorkPackagePoolService: ServiceBase, @unchecked Sendable { await subscribe(RuntimeEvents.WorkPackagesGenerated.self, id: "WorkPackagePool.WorkPackagesGenerated") { [weak self] event in try await self?.on(workPackagesGenerated: event) } - - await subscribe(RuntimeEvents.BlockFinalized.self, id: "WorkPackagePool.BlockFinalized") { [weak self] event in - try await self?.on(blockFinalized: event) - } + // TODO: add remove subscribe + // TODO: add receive subscribe } private func on(workPackagesGenerated event: RuntimeEvents.WorkPackagesGenerated) async throws { @@ -92,11 +89,6 @@ public final class WorkPackagePoolService: ServiceBase, @unchecked Sendable { await storage.add(packages: event.items, config: config) } - private func on(blockFinalized event: RuntimeEvents.BlockFinalized) async throws { - let block = try await dataProvider.getBlock(hash: event.hash) -// await storage.removeWorkPackages(workPackages: block.extrinsic.reports.guarantees.) - } - public func update(state: StateRef, config: ProtocolConfigRef) async throws { try await storage.update(state: state, config: config) } diff --git a/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift b/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift index f8ddceac..3d15c0de 100644 --- a/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift +++ b/Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift @@ -31,7 +31,6 @@ struct WorkPackagePoolServiceTests { keystore = try await DevKeyStore(devKeysCount: config.value.totalNumberOfValidators) workPackagecPoolService = await WorkPackagePoolService(config: config, dataProvider: dataProvider, eventBus: eventBus) - try await workPackagecPoolService.addWorkPackages(packages: []) ringContext = try Bandersnatch.RingContext(size: UInt(config.value.totalNumberOfValidators)) // setupTestLogger() @@ -39,22 +38,16 @@ struct WorkPackagePoolServiceTests { @Test func testAddPendingWorkPackage() async throws { - let state = try await dataProvider.getBestState() - - var allWorkPackages = SortedUniqueArray() - - for (i, validatorKey) in state.value.nextValidators.enumerated() { - let secretKey = try await keystore.get(Bandersnatch.self, publicKey: Bandersnatch.PublicKey(data: validatorKey.bandersnatch))! - // generate work package - // eventBus.publish - // Wait for the event to be processed - await storeMiddleware.wait() + var allWorkPackages = [WorkPackageAndOutput]() + for _ in 0 ..< config.value.totalNumberOfCores { + let workpackage = WorkPackage.dummy(config: config) + let wpOut = WorkPackageAndOutput(workPackage: workpackage, output: Data32.random()) + allWorkPackages.append(wpOut) } - } - - @Test - func testAddAndInvalidWorkPackage() async throws { - let state = try await dataProvider.getBestState() - let validatorKey = state.value.currentValidators[0] + await eventBus.publish(RuntimeEvents.WorkPackagesGenerated(items: allWorkPackages)) + // Wait for the event to be processedBlockchain.RefinementContext + await storeMiddleware.wait() + let workPackages = await workPackagecPoolService.getWorkPackage(for: CoreIndex(0)) + #expect(workPackages.array == Array(allWorkPackages).sorted()) } }