Skip to content

Commit

Permalink
update WorkPackagePoolServiceTests
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Dec 18, 2024
1 parent 30cf6d7 commit 0ecf01b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
// }

//
}
Expand Down
26 changes: 2 additions & 24 deletions Blockchain/Sources/Blockchain/Validator/SafroleService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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 {
lhs.output < rhs.output
}

public static func == (lhs: WorkPackageAndOutput, rhs: WorkPackageAndOutput) -> Bool {
lhs.output == rhs.output && lhs.guarantee == rhs.guarantee
lhs.output == rhs.output && lhs.workPackage == rhs.workPackage
}
}

Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private actor WorkPackageStorage {
logger.warning("Invalid work package: \(package)")
continue
}
pendingWorkPackages.insert(package)
pendingWorkPackages.append(contentsOf: [package])
}
}

Expand All @@ -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<WorkPackageAndOutput> {
// return pendingWorkPackages.filter { $0.guarantee.workReport.coreIndex == core }
pendingWorkPackages
}
}
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
27 changes: 10 additions & 17 deletions Blockchain/Tests/BlockchainTests/WorkPackagePoolServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,23 @@ 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()
}

@Test
func testAddPendingWorkPackage() async throws {
let state = try await dataProvider.getBestState()

var allWorkPackages = SortedUniqueArray<WorkPackageAndOutput>()

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())
}
}

0 comments on commit 0ecf01b

Please sign in to comment.