Skip to content

Commit

Permalink
Update testvectors (#259)
Browse files Browse the repository at this point in the history
* reports tests

* add shuffle tests

* fixes

* fix

* enable full tests

* fix

* fix
  • Loading branch information
xlc authored Jan 3, 2025
1 parent 74b0ab5 commit 5749790
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 70 deletions.
42 changes: 21 additions & 21 deletions Blockchain/Sources/Blockchain/Config/ProtocolConfig+Preset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 6,
auditBiasFactor: 2,
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(35_000_000),
workReportAccumulationGas: Gas(10_000_000),
workPackageAuthorizerGas: Gas(50_000_000),
workPackageRefineGas: Gas(5_000_000_000),
totalAccumulationGas: Gas(3_500_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand All @@ -33,7 +33,7 @@ extension Ref where T == ProtocolConfig {
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
maxWorkReportOutputSize: 48 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 2,
pvmDynamicAddressAlignmentFactor: 2,
Expand All @@ -52,10 +52,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 12,
auditBiasFactor: 2,
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(35_000_000),
workReportAccumulationGas: Gas(10_000_000),
workPackageAuthorizerGas: Gas(50_000_000),
workPackageRefineGas: Gas(5_000_000_000),
totalAccumulationGas: Gas(3_500_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand All @@ -74,7 +74,7 @@ extension Ref where T == ProtocolConfig {
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
maxWorkReportOutputSize: 48 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 10,
pvmDynamicAddressAlignmentFactor: 2,
Expand All @@ -92,10 +92,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 12,
auditBiasFactor: 2,
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(35_000_000),
workReportAccumulationGas: Gas(10_000_000),
workPackageAuthorizerGas: Gas(50_000_000),
workPackageRefineGas: Gas(5_000_000_000),
totalAccumulationGas: Gas(3_500_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand All @@ -106,15 +106,15 @@ extension Ref where T == ProtocolConfig {
maxAuthorizationsPoolItems: 8,
slotPeriodSeconds: 6,
maxAuthorizationsQueueItems: 80,
coreAssignmentRotationPeriod: 10,
coreAssignmentRotationPeriod: 4,
maxServiceCodeSize: 4_000_000,
preimageReplacementPeriod: 5,
totalNumberOfValidators: 6,
erasureCodedPieceSize: 684,
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
maxWorkReportOutputSize: 48 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 10,
pvmDynamicAddressAlignmentFactor: 2,
Expand All @@ -132,10 +132,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 600,
auditBiasFactor: 2,
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(35_000_000),
workReportAccumulationGas: Gas(10_000_000),
workPackageAuthorizerGas: Gas(50_000_000),
workPackageRefineGas: Gas(5_000_000_000),
totalAccumulationGas: Gas(3_500_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand All @@ -154,7 +154,7 @@ extension Ref where T == ProtocolConfig {
maxWorkPackageManifestEntries: 1 << 11,
maxEncodedWorkPackageSize: 12 * 1 << 20,
segmentSize: 4104,
maxWorkReportOutputSize: 96 * 1 << 10,
maxWorkReportOutputSize: 48 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 500,
pvmDynamicAddressAlignmentFactor: 2,
Expand Down
6 changes: 3 additions & 3 deletions Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
/// following tranche for each no-show in the previous.
public var auditBiasFactor: Int

/// GA: The gas allocated to invoke a work-report's Accumulation logic.
/// GA = 10,000,000: The gas allocated to invoke a work-report's Accumulation logic.
public var workReportAccumulationGas: Gas

/// GI: The gas allocated to invoke a work-package’s Is-Authorized logic.
/// GI = 50,000,000: The gas allocated to invoke a work-package’s Is-Authorized logic.
public var workPackageAuthorizerGas: Gas

/// GR: The gas allocated to invoke a work-package's Refine logic.
/// GR = 5,000,000,000: The gas allocated to invoke a work-package's Refine logic.
public var workPackageRefineGas: Gas

/// GT: The total gas allocated across for all Accumulation.
Expand Down
57 changes: 37 additions & 20 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Guaranteeing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public enum GuaranteeingError: Error {
case invalidServiceGas
case invalidPublicKey
case invalidSegmentLookup
case futureReportSlot
}

public protocol Guaranteeing {
var entropyPool: EntropyPool { get }
var timeslot: TimeslotIndex { get }
var currentValidators: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
> { get }
Expand Down Expand Up @@ -87,11 +87,16 @@ extension Guaranteeing {

public func update(
config: ProtocolConfigRef,
timeslot: TimeslotIndex,
extrinsic: ExtrinsicGuarantees
) throws(GuaranteeingError) -> ConfigFixedSizeArray<
ReportItem?,
ProtocolConfig.TotalNumberOfCores
> {
) throws(GuaranteeingError) -> (
newReports: ConfigFixedSizeArray<
ReportItem?,
ProtocolConfig.TotalNumberOfCores
>,
reported: [WorkReport],
reporters: [Ed25519PublicKey]
) {
let coreAssignmentRotationPeriod = UInt32(config.value.coreAssignmentRotationPeriod)

let currentCoreAssignment = getCoreAssignment(config: config, randomness: entropyPool.t2, timeslot: timeslot)
Expand All @@ -108,23 +113,28 @@ extension Guaranteeing {
)
let pareviousCoreKeys = withoutOffenders(keys: previousValidators.map(\.ed25519))

var workReportHashes = Set<Data32>()

var totalMinGasRequirement = Gas(0)
var workPackageHashes = Set<Data32>()

var oldLookups = [Data32: Data32]()

var reporters = [Ed25519PublicKey]()

for guarantee in extrinsic.guarantees {
var totalGasUsage = Gas(0)
let report = guarantee.workReport

guard guarantee.timeslot <= timeslot else {
throw .futureReportSlot
}

oldLookups[report.packageSpecification.workPackageHash] = report.packageSpecification.segmentRoot

for credential in guarantee.credential {
let isCurrent = (guarantee.timeslot / coreAssignmentRotationPeriod) == (timeslot / coreAssignmentRotationPeriod)
let keys = isCurrent ? currentCoreKeys : pareviousCoreKeys
let key = keys[Int(credential.index)]
let reportHash = report.hash()
workReportHashes.insert(reportHash)
workPackageHashes.insert(report.packageSpecification.workPackageHash)
let payload = SigningContext.guarantee + reportHash.data
let pubkey = try Result { try Ed25519.PublicKey(from: key) }
.mapError { _ in GuaranteeingError.invalidPublicKey }
Expand All @@ -137,6 +147,8 @@ extension Guaranteeing {
guard coreAssignment[Int(credential.index)] == report.coreIndex else { // TODO: it should accepts the last core index?
throw .invalidGuaranteeCore
}

reporters.append(key)
}

let coreIndex = Int(report.coreIndex)
Expand Down Expand Up @@ -164,22 +176,22 @@ extension Guaranteeing {
throw .invalidServiceGas
}

totalMinGasRequirement += acc.minAccumlateGas
totalGasUsage += result.gasRatio
}
}

guard totalMinGasRequirement <= config.value.workReportAccumulationGas else {
throw .outOfGas
guard totalGasUsage <= config.value.workReportAccumulationGas else {
throw .outOfGas
}
}

let recentWorkReportHashes: Set<Data32> = Set(recentHistory.items.flatMap(\.lookup.keys))
let recentWorkPackageHashes: Set<Data32> = Set(recentHistory.items.flatMap(\.lookup.keys))
let accumulateHistoryReports = Set(accumulationHistory.array.flatMap { $0 })
let accumulateQueueReports = Set(accumulationQueue.array.flatMap { $0 }
.flatMap(\.workReport.refinementContext.prerequisiteWorkPackages))
let pendingWorkReportHashes = Set(reports.array.flatMap { $0?.workReport.refinementContext.prerequisiteWorkPackages ?? [] })
let pipelinedWorkReportHashes = recentWorkReportHashes.union(accumulateHistoryReports).union(accumulateQueueReports)
let pipelinedWorkReportHashes = recentWorkPackageHashes.union(accumulateHistoryReports).union(accumulateQueueReports)
.union(pendingWorkReportHashes)
guard pipelinedWorkReportHashes.isDisjoint(with: workReportHashes) else {
guard pipelinedWorkReportHashes.isDisjoint(with: workPackageHashes) else {
throw .duplicatedWorkPackage
}

Expand All @@ -200,13 +212,13 @@ extension Guaranteeing {
guard context.anchor.beefyRoot == history.mmr.superPeak() else {
throw .invalidContext
}
guard context.lookupAnchor.timeslot >= timeslot - UInt32(config.value.maxLookupAnchorAge) else {
guard context.lookupAnchor.timeslot >= Int64(timeslot) - Int64(config.value.maxLookupAnchorAge) else {
throw .invalidContext
}

for prerequisiteWorkPackage in context.prerequisiteWorkPackages.union(report.lookup.keys) {
guard recentWorkReportHashes.contains(prerequisiteWorkPackage) ||
workReportHashes.contains(prerequisiteWorkPackage)
guard recentWorkPackageHashes.contains(prerequisiteWorkPackage) ||
workPackageHashes.contains(prerequisiteWorkPackage)
else {
throw .prerequisiteNotFound
}
Expand All @@ -220,6 +232,7 @@ extension Guaranteeing {
}

var newReports = reports
var reported = [WorkReport]()

for guarantee in extrinsic.guarantees {
let report = guarantee.workReport
Expand All @@ -228,8 +241,12 @@ extension Guaranteeing {
workReport: report,
timeslot: timeslot
)
reported.append(report)
}

return newReports
reported.sort { $0.packageSpecification.workPackageHash < $1.packageSpecification.workPackageHash }
reporters.sort()

return (newReports, reported, reporters)
}
}
6 changes: 5 additions & 1 deletion Blockchain/Sources/Blockchain/RuntimeProtocols/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,11 @@ public final class Runtime {
)

newState.reports = newReports
newState.reports = try newState.update(config: config, extrinsic: block.extrinsic.reports)
let result = try newState.update(
config: config, timeslot: newState.timeslot, extrinsic: block.extrinsic.reports
)

newState.reports = result.newReports

return availableReports
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public class Solicit: HostCall {
if notRequestedYet {
x.serviceAccounts.set(serviceAccount: x.serviceIndex, preimageHash: Data32(hash!)!, length: length, value: [])
} else if isPreviouslyAvailable, var preimageInfo {
preimageInfo.append(timeslot)
try preimageInfo.append(timeslot)
x.serviceAccounts.set(serviceAccount: x.serviceIndex, preimageHash: Data32(hash!)!, length: length, value: preimageInfo)
}
}
Expand Down Expand Up @@ -602,7 +602,7 @@ public class Forget: HostCall {
x.serviceAccounts.set(serviceAccount: x.serviceIndex, preimageHash: Data32(hash!)!, length: length, value: nil)
x.serviceAccounts.set(serviceAccount: x.serviceIndex, preimageHash: Data32(hash!)!, value: nil)
} else if isAvailable1, var preimageInfo {
preimageInfo.append(timeslot)
try preimageInfo.append(timeslot)
x.serviceAccounts.set(serviceAccount: x.serviceIndex, preimageHash: Data32(hash!)!, length: length, value: preimageInfo)
} else if isAvailable3, var preimageInfo {
preimageInfo = [preimageInfo[2], timeslot]
Expand Down
11 changes: 10 additions & 1 deletion Boka/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Database/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5749790

Please sign in to comment.