Skip to content

Commit

Permalink
Refine invocation (#237)
Browse files Browse the repository at this point in the history
* refine invocation

* latest changes

* fix lint
  • Loading branch information
qiweiii authored Dec 11, 2024
1 parent 80e6ed0 commit 6a3afb3
Show file tree
Hide file tree
Showing 18 changed files with 667 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ extension Ref where T == ProtocolConfig {
erasureCodedPieceSize: 684,
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
maxEncodedWorkReportSize: 96 * 1 << 10,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 2,
pvmDynamicAddressAlignmentFactor: 2,
Expand Down Expand Up @@ -72,7 +73,8 @@ extension Ref where T == ProtocolConfig {
erasureCodedPieceSize: 684,
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
maxEncodedWorkReportSize: 96 * 1 << 10,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 10,
pvmDynamicAddressAlignmentFactor: 2,
Expand Down Expand Up @@ -111,7 +113,8 @@ extension Ref where T == ProtocolConfig {
erasureCodedPieceSize: 684,
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
maxEncodedWorkReportSize: 96 * 1 << 10,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 500,
pvmDynamicAddressAlignmentFactor: 2,
Expand Down
38 changes: 26 additions & 12 deletions Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
// WM = 2^11: The maximum number of entries in a work-package manifest.
public var maxWorkPackageManifestEntries: Int

// WP = 12 * 2^20: The maximum size of an encoded work-package together with its extrinsic data and import impli-
// WB = 12 * 2^20: The maximum size of an encoded work-package together with its extrinsic data and import impli-
// cations, in octets.
public var maxEncodedWorkPackageSize: Int

// WR = 96 * 2^10: The maximum size of an encoded work-report in octets.
public var maxEncodedWorkReportSize: Int
// WG = WP*WE = 4104: The size of a segment in octets.
public var segmentSize: Int

// WS = 6: The size of an exported segment in erasure-coded pieces.
// WR = 48 * 2^10: The maximum total size of all output blobs in a work-report, in octets.
public var maxWorkReportOutputSize: Int

// WP = 6: The number of erasure-coded pieces in a segment.
public var erasureCodedSegmentSize: Int

// WT = 128: The size of a transfer memo in octets.
Expand Down Expand Up @@ -143,7 +146,8 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
erasureCodedPieceSize: Int,
maxWorkPackageManifestEntries: Int,
maxEncodedWorkPackageSize: Int,
maxEncodedWorkReportSize: Int,
segmentSize: Int,
maxWorkReportOutputSize: Int,
erasureCodedSegmentSize: Int,
ticketSubmissionEndSlot: Int,
pvmDynamicAddressAlignmentFactor: Int,
Expand Down Expand Up @@ -180,7 +184,8 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
self.erasureCodedPieceSize = erasureCodedPieceSize
self.maxWorkPackageManifestEntries = maxWorkPackageManifestEntries
self.maxEncodedWorkPackageSize = maxEncodedWorkPackageSize
self.maxEncodedWorkReportSize = maxEncodedWorkReportSize
self.segmentSize = segmentSize
self.maxWorkReportOutputSize = maxWorkReportOutputSize
self.erasureCodedSegmentSize = erasureCodedSegmentSize
self.ticketSubmissionEndSlot = ticketSubmissionEndSlot
self.pvmDynamicAddressAlignmentFactor = pvmDynamicAddressAlignmentFactor
Expand Down Expand Up @@ -260,8 +265,9 @@ extension ProtocolConfig {
? other.maxWorkPackageManifestEntries : maxWorkPackageManifestEntries,
maxEncodedWorkPackageSize: other.maxEncodedWorkPackageSize != 0
? other.maxEncodedWorkPackageSize : maxEncodedWorkPackageSize,
maxEncodedWorkReportSize: other.maxEncodedWorkReportSize != 0
? other.maxEncodedWorkReportSize : maxEncodedWorkReportSize,
segmentSize: other.segmentSize != 0 ? other.segmentSize : segmentSize,
maxWorkReportOutputSize: other.maxWorkReportOutputSize != 0
? other.maxWorkReportOutputSize : maxWorkReportOutputSize,
erasureCodedSegmentSize: other.erasureCodedSegmentSize != 0
? other.erasureCodedSegmentSize : erasureCodedSegmentSize,
ticketSubmissionEndSlot: other.ticketSubmissionEndSlot != 0
Expand Down Expand Up @@ -347,8 +353,9 @@ extension ProtocolConfig {
maxEncodedWorkPackageSize = try decode(
.maxEncodedWorkPackageSize, defaultValue: 0, required: required
)
maxEncodedWorkReportSize = try decode(
.maxEncodedWorkReportSize, defaultValue: 0, required: required
segmentSize = try decode(.segmentSize, defaultValue: 0, required: required)
maxWorkReportOutputSize = try decode(
.maxWorkReportOutputSize, defaultValue: 0, required: required
)
erasureCodedSegmentSize = try decode(
.erasureCodedSegmentSize, defaultValue: 0, required: required
Expand Down Expand Up @@ -579,10 +586,17 @@ extension ProtocolConfig {
}
}

public enum MaxEncodedWorkReportSize: ReadInt {
public enum SegmentSize: ReadInt {
public typealias TConfig = ProtocolConfigRef
public static func read(config: ProtocolConfigRef) -> Int {
config.value.segmentSize
}
}

public enum MaxWorkReportOutputSize: ReadInt {
public typealias TConfig = ProtocolConfigRef
public static func read(config: ProtocolConfigRef) -> Int {
config.value.maxEncodedWorkReportSize
config.value.maxWorkReportOutputSize
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public struct SingleAccumulationOutput {
}

public protocol Accumulation: ServiceAccounts {
var timeslot: TimeslotIndex { get }
var privilegedServices: PrivilegedServices { get }
var validatorQueue: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
Expand Down Expand Up @@ -359,6 +360,7 @@ extension Accumulation {
config: config,
service: service,
serviceAccounts: &self,
timeslot: timeslot,
transfers: transfers
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public protocol OnTransferFunction {
config: ProtocolConfigRef,
service: ServiceIndex,
serviceAccounts: inout some ServiceAccounts,
timeslot: TimeslotIndex,
transfers: [DeferredTransfers]
) async throws
}
2 changes: 2 additions & 0 deletions Blockchain/Sources/Blockchain/State/ServiceAccounts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public protocol ServiceAccounts {
serviceAccount index: ServiceIndex, preimageHash hash: Data32, length: UInt32
) async throws -> StateKeys.ServiceAccountPreimageInfoKey.Value?

func historicalLookup(serviceAccount index: ServiceIndex, timeslot: TimeslotIndex, preimageHash hash: Data32) async throws -> Data?

mutating func set(serviceAccount index: ServiceIndex, account: ServiceAccountDetails?)
mutating func set(serviceAccount index: ServiceIndex, storageKey key: Data32, value: Data?)
mutating func set(serviceAccount index: ServiceIndex, preimageHash hash: Data32, value: Data?)
Expand Down
24 changes: 24 additions & 0 deletions Blockchain/Sources/Blockchain/State/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,29 @@ extension State: ServiceAccounts {
return try await backend.read(StateKeys.ServiceAccountPreimageInfoKey(index: index, hash: hash, length: length))
}

public func historicalLookup(
serviceAccount index: ServiceIndex,
timeslot: TimeslotIndex,
preimageHash hash: Data32
) async throws -> Data? {
if let preimage = try await get(serviceAccount: index, preimageHash: hash),
let preimageInfo = try await get(serviceAccount: index, preimageHash: hash, length: UInt32(preimage.count))
{
var isAvailable = false
if preimageInfo.count == 1 {
isAvailable = preimageInfo[0] <= timeslot
} else if preimageInfo.count == 2 {
isAvailable = preimageInfo[0] <= timeslot && timeslot < preimageInfo[1]
} else if preimageInfo.count == 3 {
isAvailable = preimageInfo[0] <= timeslot && timeslot < preimageInfo[1] && preimageInfo[2] <= timeslot
}

return isAvailable ? preimage : nil
} else {
return nil
}
}

public mutating func set(serviceAccount index: ServiceIndex, account: ServiceAccountDetails?) {
layer[serviceAccount: index] = account
}
Expand Down Expand Up @@ -456,6 +479,7 @@ struct DummyFunction: AccumulateFunction, OnTransferFunction {
config _: ProtocolConfigRef,
service _: ServiceIndex,
serviceAccounts _: inout some ServiceAccounts,
timeslot _: TimeslotIndex,
transfers _: [DeferredTransfers]
) async throws {
fatalError("not implemented")
Expand Down
3 changes: 2 additions & 1 deletion Blockchain/Sources/Blockchain/Types/WorkReport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ extension WorkReport: Validate {
guard refinementContext.prerequisiteWorkPackages.count + lookup.count <= config.value.maxDepsInWorkReport else {
throw .tooManyDependencies
}
guard encodedSize <= config.value.maxEncodedWorkReportSize else {
let resultOutputSize = results.compactMap { result in try? result.output.result.get() }.reduce(0) { $0 + $1.count }
guard authorizationOutput.count + resultOutputSize <= config.value.maxWorkReportOutputSize else {
throw .tooBig
}
guard coreIndex < UInt32(config.value.totalNumberOfCores) else {
Expand Down
Loading

0 comments on commit 6a3afb3

Please sign in to comment.