Skip to content

Commit

Permalink
update guaranteeing (#258)
Browse files Browse the repository at this point in the history
* update GuaranteeingService

* update refineInvocation

* update guaranteeing

* update test

* update  authorizationFunction

* update GuaranteeingService test

* update some tests

* fixed some issues

* update some issues

* update tests

* update tests
  • Loading branch information
MacOMNI authored Jan 7, 2025
1 parent eeb2ab4 commit 324a43f
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BlockchainTests"
BuildableName = "BlockchainTests"
BlueprintName = "BlockchainTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension Guaranteeing {
source.map { CoreIndex(($0 + n) % max) }
}

private func getCoreAssignment(config: ProtocolConfigRef, randomness: Data32, timeslot: TimeslotIndex) -> [CoreIndex] {
public 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)
Expand Down
20 changes: 20 additions & 0 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/RuntimeEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,24 @@ public enum RuntimeEvents {
public struct BlockAuthored: Event {
public let block: BlockRef
}

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

// WorkPackages Finalize by WorkPackages Service
public struct WorkPackagesFinalized: Event {
public let items: [WorkPackage]
}

// New WorkReportGenerated by Guaranteeing Service
public struct WorkReportGenerated: Event {
public let items: [WorkReport]
}

// New GuaranteeGenerated by Guaranteeing Service
public struct GuaranteeGenerated: Event {
public let items: [WorkPackage]
}
}
30 changes: 27 additions & 3 deletions Blockchain/Sources/Blockchain/Types/RefinementContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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: Sendable, Equatable, Codable {
public struct Anchor: Sendable, Equatable, Codable {
public struct RefinementContext: Comparable, Sendable, Equatable, Codable {
public struct Anchor: Comparable, Sendable, Equatable, Codable {
// a
public var headerHash: Data32
// s
Expand All @@ -21,9 +21,19 @@ public struct RefinementContext: Sendable, Equatable, Codable {
self.stateRoot = stateRoot
self.beefyRoot = beefyRoot
}

public static func < (lhs: Anchor, rhs: Anchor) -> Bool {
if lhs.headerHash != rhs.headerHash {
return lhs.headerHash < rhs.headerHash
}
if lhs.stateRoot != rhs.stateRoot {
return lhs.stateRoot < rhs.stateRoot
}
return lhs.beefyRoot < rhs.beefyRoot
}
}

public struct LookupAnchor: Sendable, Equatable, Codable, Hashable {
public struct LookupAnchor: Comparable, Sendable, Equatable, Codable, Hashable {
// l
public var headerHash: Data32
// t
Expand All @@ -36,6 +46,13 @@ public struct RefinementContext: Sendable, Equatable, Codable {
self.headerHash = headerHash
self.timeslot = timeslot
}

public static func < (lhs: LookupAnchor, rhs: LookupAnchor) -> Bool {
if lhs.headerHash != rhs.headerHash {
return lhs.headerHash < rhs.headerHash
}
return lhs.timeslot < rhs.timeslot
}
}

public var anchor: Anchor
Expand All @@ -50,6 +67,13 @@ public struct RefinementContext: Sendable, Equatable, Codable {
self.lookupAnchor = lookupAnchor
self.prerequisiteWorkPackages = prerequisiteWorkPackages
}

public static func < (lhs: RefinementContext, rhs: RefinementContext) -> Bool {
if lhs.anchor != rhs.anchor {
return lhs.anchor < rhs.anchor
}
return lhs.lookupAnchor < rhs.lookupAnchor
}
}

extension RefinementContext: Dummy {
Expand Down
22 changes: 21 additions & 1 deletion Blockchain/Sources/Blockchain/Types/WorkPackage.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Codec
import Foundation
import Utils

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

Expand Down Expand Up @@ -44,6 +45,25 @@ public struct WorkPackage: Sendable, Equatable, Codable {
self.context = context
self.workItems = workItems
}

public static func < (lhs: WorkPackage, rhs: WorkPackage) -> Bool {
if lhs.authorizationServiceIndex != rhs.authorizationServiceIndex {
return lhs.authorizationServiceIndex < rhs.authorizationServiceIndex
}
if lhs.authorizationCodeHash != rhs.authorizationCodeHash {
return lhs.authorizationCodeHash < rhs.authorizationCodeHash
}
if lhs.context != rhs.context {
return lhs.context < rhs.context
}
return lhs.workItems.count < rhs.workItems.count
}
}

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

extension WorkPackage: Dummy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import PolkaVM
public protocol IsAuthorizedFunction {
func invoke(
config: ProtocolConfigRef,
serviceAccounts: some ServiceAccounts,
package: WorkPackage,
coreIndex: CoreIndex
) throws -> Result<Data, WorkResultError>
) async throws -> Result<Data, WorkResultError>
}

extension IsAuthorizedFunction {
public func invoke(
config: ProtocolConfigRef,
serviceAccounts: some ServiceAccounts,
package: WorkPackage,
coreIndex: CoreIndex
) async throws -> Result<Data, WorkResultError> {
let args = try JamEncoder.encode(package, coreIndex)
let ctx = IsAuthorizedContext(config: config)

let (exitReason, _, output) = await invokePVM(
let (exitReason, _, output) = try await invokePVM(
config: config,
blob: package.authorizationCodeHash.data,
blob: package.authorizationCode(serviceAccounts: serviceAccounts),
pc: 0,
gas: config.value.workPackageAuthorizerGas,
argumentData: args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public protocol RefineInvocation {
func invoke(
config: ProtocolConfigRef,
serviceAccounts: some ServiceAccounts,
codeHash: Data,
codeHash: Data32,
gas: Gas,
service: ServiceIndex,
workPackageHash: Data32,
workPayload: Data,
refinementCtx: RefinementContext,
workPayload: Data, // y
refinementCtx: RefinementContext, // c
authorizerHash: Data32,
authorizationOutput: Data,
importSegments: [Data], // array of Data4104
importSegments: [Data],
extrinsicDataBlobs: [Data],
exportSegmentOffset: UInt64
) async throws -> (result: Result<Data, WorkResultError>, exports: [Data])
Expand Down
Loading

0 comments on commit 324a43f

Please sign in to comment.