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

Update consts and general merklization #254

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 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,
coreAccumulationGas: Gas(100_000),
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(341_000_000),
totalAccumulationGas: Gas(35_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand Down Expand Up @@ -52,10 +52,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 12,
auditBiasFactor: 2,
coreAccumulationGas: Gas(100_000),
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(341_000_000),
totalAccumulationGas: Gas(35_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand Down Expand Up @@ -92,10 +92,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 12,
auditBiasFactor: 2,
coreAccumulationGas: Gas(100_000),
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(341_000_000),
totalAccumulationGas: Gas(35_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand Down Expand Up @@ -132,10 +132,10 @@ extension Ref where T == ProtocolConfig {
preimagePurgePeriod: 28800,
epochLength: 600,
auditBiasFactor: 2,
coreAccumulationGas: Gas(100_000),
workReportAccumulationGas: Gas(100_000),
workPackageAuthorizerGas: Gas(1_000_000),
workPackageRefineGas: Gas(500_000_000),
totalAccumulationGas: Gas(341_000_000),
totalAccumulationGas: Gas(35_000_000),
recentHistorySize: 8,
maxWorkItems: 4,
maxDepsInWorkReport: 8,
Expand Down
100 changes: 50 additions & 50 deletions Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
@@ -1,119 +1,119 @@
import PolkaVM
import Utils

// constants defined in the graypaper
/// constants defined in the graypaper
public struct ProtocolConfig: Sendable, Codable, Equatable {
// A = 8: The period, in seconds, between audit tranches.
/// A = 8: The period, in seconds, between audit tranches.
public var auditTranchePeriod: Int

// BI = 10: The additional minimum balance required per item of elective service state.
/// BI = 10: The additional minimum balance required per item of elective service state.
public var additionalMinBalancePerStateItem: Int

// BL = 1: The additional minimum balance required per octet of elective service state.
/// BL = 1: The additional minimum balance required per octet of elective service state.
public var additionalMinBalancePerStateByte: Int

// BS = 100: The basic minimum balance which all services require.
/// BS = 100: The basic minimum balance which all services require.
public var serviceMinBalance: Int

// C = 341: The total number of cores.
/// C = 341: The total number of cores.
public var totalNumberOfCores: Int

// D = 28, 800: The period in timeslots after which an unreferenced preimage may be expunged.
/// D = 28, 800: The period in timeslots after which an unreferenced preimage may be expunged.
public var preimagePurgePeriod: Int

// E = 600: The length of an epoch in timeslots.
/// E = 600: The length of an epoch in timeslots.
public var epochLength: Int

// F = 2: The audit bias factor, the expected number of additional validators who will audit a work-report in the
// following tranche for each no-show in the previous.
/// F = 2: The audit bias factor, the expected number of additional validators who will audit a work-report in the
/// following tranche for each no-show in the previous.
public var auditBiasFactor: Int

// GA: The total gas allocated to a core for Accumulation.
public var coreAccumulationGas: Gas
/// GA: 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: The gas allocated to invoke a work-package’s Is-Authorized logic.
public var workPackageAuthorizerGas: Gas

// GR: The total gas allocated for a work-packages Refine logic.
/// GR: The gas allocated to invoke a work-package's Refine logic.
public var workPackageRefineGas: Gas

// GT: The total gas allocated across all cores for Accumulation.
/// GT: The total gas allocated across for all Accumulation.
public var totalAccumulationGas: Gas

// H = 8: The size of recent history, in blocks.
/// H = 8: The size of recent history, in blocks.
public var recentHistorySize: Int

// I = 4: The maximum amount of work items in a package.
/// I = 4: The maximum amount of work items in a package.
public var maxWorkItems: Int

// J = 8: The maximum sum of dependency items in a work-report.
/// J = 8: The maximum sum of dependency items in a work-report.
public var maxDepsInWorkReport: Int

// K = 16: The maximum number of tickets which may be submitted in a single extrinsic.
/// K = 16: The maximum number of tickets which may be submitted in a single extrinsic.
public var maxTicketsPerExtrinsic: Int

// L = 14, 400: The maximum age in timeslots of the lookup anchor.
/// L = 14, 400: The maximum age in timeslots of the lookup anchor.
public var maxLookupAnchorAge: Int

// N = 2: The number of ticket entries per validator.
/// N = 2: The number of ticket entries per validator.
public var ticketEntriesPerValidator: Int

// O = 8: The maximum number of items in the authorizations pool.
/// O = 8: The maximum number of items in the authorizations pool.
public var maxAuthorizationsPoolItems: Int

// P = 6: The slot period, in seconds.
/// P = 6: The slot period, in seconds.
public var slotPeriodSeconds: Int

// Q = 80: The number of items in the authorizations queue.
/// Q = 80: The number of items in the authorizations queue.
public var maxAuthorizationsQueueItems: Int

// R = 10: The rotation period of validator-core assignments, in timeslots.
/// R = 10: The rotation period of validator-core assignments, in timeslots.
public var coreAssignmentRotationPeriod: Int

// U = 5: The period in timeslots after which reported but unavailable work may be replaced.
/// U = 5: The period in timeslots after which reported but unavailable work may be replaced.
public var preimageReplacementPeriod: Int

// V = 1023: The total number of validators.
/// V = 1023: The total number of validators.
public var totalNumberOfValidators: Int

// WC = 4,000,000: The maximum size of service code in octets.
/// WC = 4,000,000: The maximum size of service code in octets.
public var maxServiceCodeSize: Int

// WE = 684: The basic size of our erasure-coded pieces.
/// WE = 684: The basic size of our erasure-coded pieces.
public var erasureCodedPieceSize: Int

// WM = 2^11: The maximum number of entries in a work-package manifest.
/// WM = 2^11: The maximum number of entries in a work-package manifest.
public var maxWorkPackageManifestEntries: Int

// WB = 12 * 2^20: The maximum size of an encoded work-package together with its extrinsic data and import impli-
// cations, in octets.
/// 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

// WG = WP*WE = 4104: The size of a segment in octets.
/// WG = WP*WE = 4104: The size of a segment in octets.
public var segmentSize: Int

// WR = 48 * 2^10: The maximum total size of all output blobs in a work-report, in octets.
/// 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.
/// 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.
/// WT = 128: The size of a transfer memo in octets.
public var transferMemoSize: Int

// Y = 500: The number of slots into an epoch at which ticket-submission ends.
/// Y = 500: The number of slots into an epoch at which ticket-submission ends.
public var ticketSubmissionEndSlot: Int

// ZA = 2: The pvm dynamic address alignment factor.
/// ZA = 2: The pvm dynamic address alignment factor.
public var pvmDynamicAddressAlignmentFactor: Int

// ZI = 2^24: The standard pvm program initialization input data size.
/// ZI = 2^24: The standard pvm program initialization input data size.
public var pvmProgramInitInputDataSize: Int

// ZZ = 2^16: The standard pvm program initialization zone size.
/// ZZ = 2^16: The standard pvm program initialization zone size.
public var pvmProgramInitZoneSize: Int

// ZP = 2^12: The pvm memory page size.
/// ZP = 2^12: The pvm memory page size.
public var pvmMemoryPageSize: Int

public init(
Expand All @@ -125,7 +125,7 @@
preimagePurgePeriod: Int,
epochLength: Int,
auditBiasFactor: Int,
coreAccumulationGas: Gas,
workReportAccumulationGas: Gas,
workPackageAuthorizerGas: Gas,
workPackageRefineGas: Gas,
totalAccumulationGas: Gas,
Expand Down Expand Up @@ -163,7 +163,7 @@
self.preimagePurgePeriod = preimagePurgePeriod
self.epochLength = epochLength
self.auditBiasFactor = auditBiasFactor
self.coreAccumulationGas = coreAccumulationGas
self.workReportAccumulationGas = workReportAccumulationGas
self.workPackageAuthorizerGas = workPackageAuthorizerGas
self.workPackageRefineGas = workPackageRefineGas
self.totalAccumulationGas = totalAccumulationGas
Expand Down Expand Up @@ -198,7 +198,7 @@
public typealias ProtocolConfigRef = Ref<ProtocolConfig>

extension ProtocolConfig: PvmConfig {}
// silence the warning about cross module conformances as we owns all the code
/// silence the warning about cross module conformances as we owns all the code
extension Ref: @retroactive PvmConfig where T == ProtocolConfig {
public var pvmDynamicAddressAlignmentFactor: Int { value.pvmDynamicAddressAlignmentFactor }
public var pvmProgramInitInputDataSize: Int { value.pvmProgramInitInputDataSize }
Expand All @@ -224,8 +224,8 @@
epochLength: other.epochLength != 0 ? other.epochLength : epochLength,
auditBiasFactor: other.auditBiasFactor != 0
? other.auditBiasFactor : auditBiasFactor,
coreAccumulationGas: other.coreAccumulationGas.value != 0
? other.coreAccumulationGas : coreAccumulationGas,
workReportAccumulationGas: other.workReportAccumulationGas.value != 0
? other.workReportAccumulationGas : workReportAccumulationGas,
workPackageAuthorizerGas: other.workPackageAuthorizerGas.value != 0
? other.workPackageAuthorizerGas : workPackageAuthorizerGas,
workPackageRefineGas: other.workPackageRefineGas.value != 0
Expand Down Expand Up @@ -306,8 +306,8 @@
preimagePurgePeriod = try decode(.preimagePurgePeriod, defaultValue: 0, required: required)
epochLength = try decode(.epochLength, defaultValue: 0, required: required)
auditBiasFactor = try decode(.auditBiasFactor, defaultValue: 0, required: required)
coreAccumulationGas = try decode(
.coreAccumulationGas, defaultValue: Gas(0), required: required
workReportAccumulationGas = try decode(
.workReportAccumulationGas, defaultValue: Gas(0), required: required
)
workPackageAuthorizerGas = try decode(
.workPackageAuthorizerGas, defaultValue: Gas(0), required: required
Expand Down Expand Up @@ -435,11 +435,11 @@
}
}

public enum CoreAccumulationGas: ReadGas {
public enum WorkReportAccumulationGas: ReadGas {
public typealias TConfig = ProtocolConfigRef
public typealias TOutput = Gas
public static func read(config: ProtocolConfigRef) -> Gas {
config.value.coreAccumulationGas
config.value.workReportAccumulationGas

Check warning on line 442 in Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift#L442

Added line #L442 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ extension Accumulation {
workReports: [WorkReport]
) async throws -> (numAccumulated: Int, state: AccumulateState, commitments: Set<Commitment>) {
let sumPrevilegedGas = privilegedServices.basicGas.values.reduce(Gas(0)) { $0 + $1.value }
let minTotalGas = config.value.coreAccumulationGas * Gas(config.value.totalNumberOfCores) + sumPrevilegedGas
let minTotalGas = config.value.workReportAccumulationGas * Gas(config.value.totalNumberOfCores) + sumPrevilegedGas
let gasLimit = max(config.value.totalAccumulationGas, minTotalGas)

let res = try await outerAccumulate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
}
}

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

Expand Down Expand Up @@ -197,7 +197,7 @@
guard context.anchor.stateRoot == history.stateRoot else {
throw .invalidContext
}
guard context.anchor.beefyRoot == history.mmr.hash() else {
guard context.anchor.beefyRoot == history.mmr.superPeak() else {

Check warning on line 200 in Blockchain/Sources/Blockchain/RuntimeProtocols/Guaranteeing.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/RuntimeProtocols/Guaranteeing.swift#L200

Added line #L200 was not covered by tests
throw .invalidContext
}
guard context.lookupAnchor.timeslot >= timeslot - UInt32(config.value.maxLookupAnchorAge) else {
Expand Down
4 changes: 2 additions & 2 deletions Node/Tests/NodeTests/chainfiles/devnet_allconfig_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"additionalMinBalancePerStateItem" : 10,
"auditBiasFactor" : 2,
"auditTranchePeriod" : 8,
"coreAccumulationGas" : 10000000,
"workReportAccumulationGas" : 10000000,
"coreAssignmentRotationPeriod" : 6,
"epochLength" : 6,
"erasureCodedPieceSize" : 684,
Expand Down Expand Up @@ -38,7 +38,7 @@
"transferMemoSize" : 128,
"workPackageAuthorizerGas" : 10000000,
"workPackageRefineGas" : 10000000,
"totalAccumulationGas": 341000000,
"totalAccumulationGas": 35000000,
"maxDepsInWorkReport": 8
},
"state" : {},
Expand Down
15 changes: 13 additions & 2 deletions Utils/Sources/Utils/Merklization/MMR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ public struct MMR: Sendable, Equatable, Codable {
}
}

public func hash() -> Data32 {
try! JamEncoder.encode(self).keccakHash()
public func superPeak() -> Data32 {
func helper(_ peaks: ArraySlice<Data32>) -> Data32 {
if peaks.count == 0 {
Data32()
} else if peaks.count == 1 {
peaks[0]
} else {
Keccak.hash("node", helper(peaks[0 ..< peaks.count - 1]), peaks.last!)
}
}

let nonNilPeaks = peaks.compactMap { $0 }
return helper(nonNilPeaks[...])
}
}
Loading