Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work-package submission #290

Merged
merged 16 commits into from
Feb 20, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ public enum RuntimeEvents {
public let block: BlockRef
}

// New WorkPackagesGenerated by Guaranteeing Service
public struct WorkPackagesGenerated: Event {
public let items: [WorkPackageRef]
}

// When a work package is recived via CE133
public struct WorkPackagesReceived: Event {
public let item: WorkPackageRef
public init(item: WorkPackageRef) {
self.item = item
}
}

// When a work package bundle is ready to shared via CE134
Expand Down
8 changes: 7 additions & 1 deletion Blockchain/Sources/Blockchain/Types/RefinementContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Utils
// A refinement context, denoted by the set X, describes the context of the chain
// at the point that the report’s corresponding work-package was evaluated.
public struct RefinementContext: Comparable, Sendable, Equatable, Codable {
public struct Anchor: Comparable, Sendable, Equatable, Codable {
public struct Anchor: Comparable, Sendable, Equatable, Codable, Hashable {
// a
public var headerHash: Data32
// s
Expand Down Expand Up @@ -76,6 +76,12 @@ public struct RefinementContext: Comparable, Sendable, Equatable, Codable {
}
}

extension RefinementContext: Hashable32 {
public func hash() -> Data32 {
try! JamEncoder.encode(self).blake2b256hash()
}
}

extension RefinementContext: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(config _: Config) -> RefinementContext {
Expand Down
6 changes: 3 additions & 3 deletions Blockchain/Sources/Blockchain/Types/WorkItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Foundation
import Utils

// I
public struct WorkItem: Sendable, Equatable, Codable {
public struct ImportedDataSegment: Sendable, Equatable, Codable {
public enum DataSegmentRootKind: Sendable, Equatable {
public struct WorkItem: Sendable, Equatable, Codable, Hashable {
public struct ImportedDataSegment: Sendable, Equatable, Codable, Hashable {
public enum DataSegmentRootKind: Sendable, Equatable, Hashable {
case segmentRoot(Data32)
case workPackageHash(Data32)
}
Expand Down
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Types/WorkPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
import Utils

// P
public struct WorkPackage: Comparable, Sendable, Equatable, Codable {
public struct WorkPackage: Comparable, Sendable, Equatable, Codable, Hashable {
// j
public var authorizationToken: Data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
case blockRequest(BlockRequest)
case safroleTicket1(SafroleTicketMessage)
case safroleTicket2(SafroleTicketMessage)
case workPackageSubmission(WorkPackageMessage)
}

extension CERequest: RequestProtocol {
Expand All @@ -20,6 +21,8 @@
try JamEncoder.encode(message)
case let .safroleTicket2(message):
try JamEncoder.encode(message)
case let .workPackageSubmission(message):
try JamEncoder.encode(message)

Check warning on line 25 in Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift#L25

Added line #L25 was not covered by tests
}
}

Expand All @@ -31,6 +34,8 @@
.safroleTicket1
case .safroleTicket2:
.safroleTicket2
case .workPackageSubmission:
.workPackageSubmission

Check warning on line 38 in Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift#L38

Added line #L38 was not covered by tests
}
}

Expand All @@ -42,6 +47,8 @@
SafroleTicketMessage.self
case .safroleTicket2:
SafroleTicketMessage.self
case .workPackageSubmission:
WorkPackageMessage.self

Check warning on line 51 in Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift#L51

Added line #L51 was not covered by tests
default:
fatalError("unimplemented")
}
Expand All @@ -64,6 +71,9 @@
return nil
}
return .safroleTicket2(message)
case .workPackageSubmission:
guard let message = data as? WorkPackageMessage else { return nil }
return .workPackageSubmission(message)

Check warning on line 76 in Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NetworkingProtocol/CommonEphemeral/CERequest.swift#L75-L76

Added lines #L75 - L76 were not covered by tests
default:
fatalError("unimplemented")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Blockchain
import Codec
import Foundation

public struct WorkPackageMessage: Codable, Sendable, Equatable, Hashable {
/// The core index associated with the work-package.
public var coreIndex: CoreIndex

/// The work-package data.
public var workPackage: WorkPackage

/// The extrinsic data referenced by the work-package.
public var extrinsics: [Data]

public init(coreIndex: CoreIndex, workPackage: WorkPackage, extrinsics: [Data]) {
self.coreIndex = coreIndex
self.workPackage = workPackage
self.extrinsics = extrinsics
}
}
3 changes: 3 additions & 0 deletions Node/Sources/Node/NetworkingProtocol/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
]
))
return []
case let .workPackageSubmission(message):
blockchain.publish(event: RuntimeEvents.WorkPackagesReceived(item: message.workPackage.asRef()))
return []

Check warning on line 231 in Node/Sources/Node/NetworkingProtocol/NetworkManager.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NetworkingProtocol/NetworkManager.swift#L230-L231

Added lines #L230 - L231 were not covered by tests
}
}

Expand Down
28 changes: 28 additions & 0 deletions Node/Tests/NodeTests/NetworkManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ struct NetworkManagerTests {
)
}

@Test
func testWorkPackagesReceived() async throws {
// Create dummy work packages
let workPackage = WorkPackage.dummy(config: services.config).asRef()

// Publish WorkPackagesReceived event
await services.blockchain.publish(event: RuntimeEvents.WorkPackagesReceived(item: workPackage))

// Wait for event processing
await storeMiddleware.wait()

#expect(workPackage.value.hash() != nil)
#expect(workPackage.value.context.hash() != nil)
#expect(workPackage.hashValue != nil)

// Verify network calls
#expect(
network.contain(calls: [
.init(function: "connect", parameters: ["address": devPeers.first!, "role": PeerRole.validator]),
.init(function: "sendToPeer", parameters: [
"message": CERequest.workPackageSubmission(
WorkPackageMessage(coreIndex: 0, workPackage: workPackage.value, extrinsics: [])
),
]),
])
)
}

@Test
func testBlockBroadcast() async throws {
// Import a block
Expand Down
2 changes: 1 addition & 1 deletion Utils/Sources/Utils/SaturatingNumber.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Codec
import Numerics

public struct SaturatingNumber<T: FixedWidthInteger & Sendable>: Sendable {
public struct SaturatingNumber<T: FixedWidthInteger & Sendable>: Sendable, Hashable {
public private(set) var value: T

public static var max: SaturatingNumber {
Expand Down